Tim Murray | 3a38b74 | 2014-07-02 10:41:08 -0700 | [diff] [blame] | 1 | |
Zonr Chang | c383a50 | 2010-10-12 01:52:08 +0800 | [diff] [blame] | 2 | /* |
Jean-Luc Brouillet | f33e156 | 2014-06-03 17:55:57 -0700 | [diff] [blame] | 3 | * Copyright 2010-2014, The Android Open Source Project |
Zonr Chang | c383a50 | 2010-10-12 01:52:08 +0800 | [diff] [blame] | 4 | * |
| 5 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | * you may not use this file except in compliance with the License. |
| 7 | * You may obtain a copy of the License at |
| 8 | * |
| 9 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | * |
| 11 | * Unless required by applicable law or agreed to in writing, software |
| 12 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | * See the License for the specific language governing permissions and |
| 15 | * limitations under the License. |
| 16 | */ |
| 17 | |
zonr | 6315f76 | 2010-10-05 15:35:14 +0800 | [diff] [blame] | 18 | #include "slang_rs_reflection.h" |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 19 | |
Stephen Hines | e639eb5 | 2010-11-08 19:27:20 -0800 | [diff] [blame] | 20 | #include <sys/stat.h> |
| 21 | |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 22 | #include <cstdarg> |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 23 | #include <cctype> |
Stephen Hines | e639eb5 | 2010-11-08 19:27:20 -0800 | [diff] [blame] | 24 | |
| 25 | #include <algorithm> |
Stephen Hines | d369cda | 2012-02-13 12:00:03 -0800 | [diff] [blame] | 26 | #include <sstream> |
Stephen Hines | e639eb5 | 2010-11-08 19:27:20 -0800 | [diff] [blame] | 27 | #include <string> |
| 28 | #include <utility> |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 29 | |
zonr | 6315f76 | 2010-10-05 15:35:14 +0800 | [diff] [blame] | 30 | #include "llvm/ADT/APFloat.h" |
Zonr Chang | 89273bd | 2010-10-14 20:57:38 +0800 | [diff] [blame] | 31 | #include "llvm/ADT/StringExtras.h" |
zonr | 6315f76 | 2010-10-05 15:35:14 +0800 | [diff] [blame] | 32 | |
Raphael | 8d5a2f6 | 2011-02-08 00:15:05 -0800 | [diff] [blame] | 33 | #include "os_sep.h" |
zonr | 6315f76 | 2010-10-05 15:35:14 +0800 | [diff] [blame] | 34 | #include "slang_rs_context.h" |
| 35 | #include "slang_rs_export_var.h" |
Stephen Hines | 593a894 | 2011-05-10 15:29:50 -0700 | [diff] [blame] | 36 | #include "slang_rs_export_foreach.h" |
zonr | 6315f76 | 2010-10-05 15:35:14 +0800 | [diff] [blame] | 37 | #include "slang_rs_export_func.h" |
| 38 | #include "slang_rs_reflect_utils.h" |
Stephen Hines | 4cc499d | 2011-08-24 19:06:17 -0700 | [diff] [blame] | 39 | #include "slang_version.h" |
Stephen Hines | e639eb5 | 2010-11-08 19:27:20 -0800 | [diff] [blame] | 40 | #include "slang_utils.h" |
zonr | 6315f76 | 2010-10-05 15:35:14 +0800 | [diff] [blame] | 41 | |
Jean-Luc Brouillet | 2ce118e | 2014-05-27 17:41:22 -0700 | [diff] [blame] | 42 | #define RS_SCRIPT_CLASS_NAME_PREFIX "ScriptC_" |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 43 | #define RS_SCRIPT_CLASS_SUPER_CLASS_NAME "ScriptC" |
Shih-wei Liao | 8b1d0dd | 2010-07-15 18:56:55 -0700 | [diff] [blame] | 44 | |
Jean-Luc Brouillet | 2ce118e | 2014-05-27 17:41:22 -0700 | [diff] [blame] | 45 | #define RS_TYPE_CLASS_SUPER_CLASS_NAME ".Script.FieldBase" |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 46 | |
Jean-Luc Brouillet | 2ce118e | 2014-05-27 17:41:22 -0700 | [diff] [blame] | 47 | #define RS_TYPE_ITEM_CLASS_NAME "Item" |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 48 | |
Tim Murray | 3a38b74 | 2014-07-02 10:41:08 -0700 | [diff] [blame] | 49 | #define RS_TYPE_ITEM_SIZEOF_LEGACY "Item.sizeof" |
| 50 | #define RS_TYPE_ITEM_SIZEOF_CURRENT "mElement.getBytesSize()" |
| 51 | |
Jean-Luc Brouillet | 2ce118e | 2014-05-27 17:41:22 -0700 | [diff] [blame] | 52 | #define RS_TYPE_ITEM_BUFFER_NAME "mItemArray" |
| 53 | #define RS_TYPE_ITEM_BUFFER_PACKER_NAME "mIOBuffer" |
| 54 | #define RS_TYPE_ELEMENT_REF_NAME "mElementCache" |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 55 | |
Jean-Luc Brouillet | 2ce118e | 2014-05-27 17:41:22 -0700 | [diff] [blame] | 56 | #define RS_EXPORT_VAR_INDEX_PREFIX "mExportVarIdx_" |
| 57 | #define RS_EXPORT_VAR_PREFIX "mExportVar_" |
| 58 | #define RS_EXPORT_VAR_ELEM_PREFIX "mExportVarElem_" |
| 59 | #define RS_EXPORT_VAR_DIM_PREFIX "mExportVarDim_" |
| 60 | #define RS_EXPORT_VAR_CONST_PREFIX "const_" |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 61 | |
Jean-Luc Brouillet | 2ce118e | 2014-05-27 17:41:22 -0700 | [diff] [blame] | 62 | #define RS_ELEM_PREFIX "__" |
Stephen Hines | a6b5414 | 2012-04-09 18:25:08 -0700 | [diff] [blame] | 63 | |
Jean-Luc Brouillet | 2ce118e | 2014-05-27 17:41:22 -0700 | [diff] [blame] | 64 | #define RS_FP_PREFIX "__rs_fp_" |
Stephen Hines | 1f6c331 | 2012-07-03 17:23:33 -0700 | [diff] [blame] | 65 | |
Jean-Luc Brouillet | 2ce118e | 2014-05-27 17:41:22 -0700 | [diff] [blame] | 66 | #define RS_RESOURCE_NAME "__rs_resource_name" |
Stephen Hines | d293693 | 2012-09-12 20:44:32 -0700 | [diff] [blame] | 67 | |
Jean-Luc Brouillet | 2ce118e | 2014-05-27 17:41:22 -0700 | [diff] [blame] | 68 | #define RS_EXPORT_FUNC_INDEX_PREFIX "mExportFuncIdx_" |
| 69 | #define RS_EXPORT_FOREACH_INDEX_PREFIX "mExportForEachIdx_" |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 70 | |
Jean-Luc Brouillet | 2ce118e | 2014-05-27 17:41:22 -0700 | [diff] [blame] | 71 | #define RS_EXPORT_VAR_ALLOCATION_PREFIX "mAlloction_" |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 72 | #define RS_EXPORT_VAR_DATA_STORAGE_PREFIX "mData_" |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 73 | |
Stephen Hines | e639eb5 | 2010-11-08 19:27:20 -0800 | [diff] [blame] | 74 | namespace slang { |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 75 | |
Jean-Luc Brouillet | c643ceb | 2014-06-05 10:17:23 -0700 | [diff] [blame] | 76 | class RSReflectionJavaElementBuilder { |
| 77 | public: |
| 78 | RSReflectionJavaElementBuilder(const char *ElementBuilderName, |
| 79 | const RSExportRecordType *ERT, |
| 80 | const char *RenderScriptVar, |
| 81 | GeneratedFile *Out, const RSContext *RSContext, |
| 82 | RSReflectionJava *Reflection); |
| 83 | void generate(); |
| 84 | |
| 85 | private: |
| 86 | void genAddElement(const RSExportType *ET, const std::string &VarName, |
| 87 | unsigned ArraySize); |
| 88 | void genAddStatementStart(); |
| 89 | void genAddStatementEnd(const std::string &VarName, unsigned ArraySize); |
| 90 | void genAddPadding(int PaddingSize); |
| 91 | // TODO Will remove later due to field name information is not necessary for |
| 92 | // C-reflect-to-Java |
| 93 | std::string createPaddingField() { |
| 94 | return mPaddingPrefix + llvm::itostr(mPaddingFieldIndex++); |
| 95 | } |
| 96 | |
| 97 | const char *mElementBuilderName; |
| 98 | const RSExportRecordType *mERT; |
| 99 | const char *mRenderScriptVar; |
| 100 | GeneratedFile *mOut; |
| 101 | std::string mPaddingPrefix; |
| 102 | int mPaddingFieldIndex; |
| 103 | const RSContext *mRSContext; |
| 104 | RSReflectionJava *mReflection; |
| 105 | }; |
| 106 | |
Zonr Chang | 92b344a | 2010-10-05 20:39:03 +0800 | [diff] [blame] | 107 | static const char *GetMatrixTypeName(const RSExportMatrixType *EMT) { |
Jean-Luc Brouillet | 2ce118e | 2014-05-27 17:41:22 -0700 | [diff] [blame] | 108 | static const char *MatrixTypeJavaNameMap[] = {/* 2x2 */ "Matrix2f", |
| 109 | /* 3x3 */ "Matrix3f", |
| 110 | /* 4x4 */ "Matrix4f", |
Zonr Chang | 92b344a | 2010-10-05 20:39:03 +0800 | [diff] [blame] | 111 | }; |
| 112 | unsigned Dim = EMT->getDim(); |
| 113 | |
Jean-Luc Brouillet | 2ce118e | 2014-05-27 17:41:22 -0700 | [diff] [blame] | 114 | if ((Dim - 2) < (sizeof(MatrixTypeJavaNameMap) / sizeof(const char *))) |
| 115 | return MatrixTypeJavaNameMap[EMT->getDim() - 2]; |
Zonr Chang | 92b344a | 2010-10-05 20:39:03 +0800 | [diff] [blame] | 116 | |
Stephen Hines | 6e6578a | 2011-02-07 18:05:48 -0800 | [diff] [blame] | 117 | slangAssert(false && "GetMatrixTypeName : Unsupported matrix dimension"); |
Zonr Chang | 92b344a | 2010-10-05 20:39:03 +0800 | [diff] [blame] | 118 | return NULL; |
| 119 | } |
| 120 | |
Stephen Hines | 6e6578a | 2011-02-07 18:05:48 -0800 | [diff] [blame] | 121 | static const char *GetVectorAccessor(unsigned Index) { |
Jean-Luc Brouillet | 2ce118e | 2014-05-27 17:41:22 -0700 | [diff] [blame] | 122 | static const char *VectorAccessorMap[] = {/* 0 */ "x", |
| 123 | /* 1 */ "y", |
| 124 | /* 2 */ "z", |
| 125 | /* 3 */ "w", |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 126 | }; |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 127 | |
Jean-Luc Brouillet | 2ce118e | 2014-05-27 17:41:22 -0700 | [diff] [blame] | 128 | slangAssert((Index < (sizeof(VectorAccessorMap) / sizeof(const char *))) && |
Stephen Hines | 6e6578a | 2011-02-07 18:05:48 -0800 | [diff] [blame] | 129 | "Out-of-bound index to access vector member"); |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 130 | |
| 131 | return VectorAccessorMap[Index]; |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 132 | } |
| 133 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 134 | static const char *GetPackerAPIName(const RSExportPrimitiveType *EPT) { |
zonr | 6315f76 | 2010-10-05 15:35:14 +0800 | [diff] [blame] | 135 | static const char *PrimitiveTypePackerAPINameMap[] = { |
Jean-Luc Brouillet | 2ce118e | 2014-05-27 17:41:22 -0700 | [diff] [blame] | 136 | "", // DataTypeFloat16 |
| 137 | "addF32", // DataTypeFloat32 |
| 138 | "addF64", // DataTypeFloat64 |
| 139 | "addI8", // DataTypeSigned8 |
| 140 | "addI16", // DataTypeSigned16 |
| 141 | "addI32", // DataTypeSigned32 |
| 142 | "addI64", // DataTypeSigned64 |
| 143 | "addU8", // DataTypeUnsigned8 |
| 144 | "addU16", // DataTypeUnsigned16 |
| 145 | "addU32", // DataTypeUnsigned32 |
| 146 | "addU64", // DataTypeUnsigned64 |
| 147 | "addBoolean", // DataTypeBoolean |
| 148 | "addU16", // DataTypeUnsigned565 |
| 149 | "addU16", // DataTypeUnsigned5551 |
| 150 | "addU16", // DataTypeUnsigned4444 |
| 151 | "addMatrix", // DataTypeRSMatrix2x2 |
| 152 | "addMatrix", // DataTypeRSMatrix3x3 |
| 153 | "addMatrix", // DataTypeRSMatrix4x4 |
| 154 | "addObj", // DataTypeRSElement |
| 155 | "addObj", // DataTypeRSType |
| 156 | "addObj", // DataTypeRSAllocation |
| 157 | "addObj", // DataTypeRSSampler |
| 158 | "addObj", // DataTypeRSScript |
| 159 | "addObj", // DataTypeRSMesh |
| 160 | "addObj", // DataTypeRSPath |
| 161 | "addObj", // DataTypeRSProgramFragment |
| 162 | "addObj", // DataTypeRSProgramVertex |
| 163 | "addObj", // DataTypeRSProgramRaster |
| 164 | "addObj", // DataTypeRSProgramStore |
| 165 | "addObj", // DataTypeRSFont |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 166 | }; |
| 167 | unsigned TypeId = EPT->getType(); |
| 168 | |
Jean-Luc Brouillet | 2ce118e | 2014-05-27 17:41:22 -0700 | [diff] [blame] | 169 | if (TypeId < (sizeof(PrimitiveTypePackerAPINameMap) / sizeof(const char *))) |
| 170 | return PrimitiveTypePackerAPINameMap[EPT->getType()]; |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 171 | |
Stephen Hines | 6e6578a | 2011-02-07 18:05:48 -0800 | [diff] [blame] | 172 | slangAssert(false && "GetPackerAPIName : Unknown primitive data type"); |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 173 | return NULL; |
| 174 | } |
| 175 | |
Stephen Hines | d369cda | 2012-02-13 12:00:03 -0800 | [diff] [blame] | 176 | static std::string GetTypeName(const RSExportType *ET, bool Brackets = true) { |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 177 | switch (ET->getClass()) { |
Jean-Luc Brouillet | 2ce118e | 2014-05-27 17:41:22 -0700 | [diff] [blame] | 178 | case RSExportType::ExportClassPrimitive: { |
| 179 | return RSExportPrimitiveType::getRSReflectionType( |
| 180 | static_cast<const RSExportPrimitiveType *>(ET))->java_name; |
| 181 | } |
| 182 | case RSExportType::ExportClassPointer: { |
| 183 | const RSExportType *PointeeType = |
| 184 | static_cast<const RSExportPointerType *>(ET)->getPointeeType(); |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 185 | |
Jean-Luc Brouillet | 2ce118e | 2014-05-27 17:41:22 -0700 | [diff] [blame] | 186 | if (PointeeType->getClass() != RSExportType::ExportClassRecord) |
| 187 | return "Allocation"; |
| 188 | else |
| 189 | return PointeeType->getElementName(); |
| 190 | } |
| 191 | case RSExportType::ExportClassVector: { |
| 192 | const RSExportVectorType *EVT = static_cast<const RSExportVectorType *>(ET); |
| 193 | std::stringstream VecName; |
| 194 | VecName << EVT->getRSReflectionType(EVT)->rs_java_vector_prefix |
| 195 | << EVT->getNumElement(); |
| 196 | return VecName.str(); |
| 197 | } |
| 198 | case RSExportType::ExportClassMatrix: { |
| 199 | return GetMatrixTypeName(static_cast<const RSExportMatrixType *>(ET)); |
| 200 | } |
| 201 | case RSExportType::ExportClassConstantArray: { |
| 202 | const RSExportConstantArrayType *CAT = |
| 203 | static_cast<const RSExportConstantArrayType *>(ET); |
| 204 | std::string ElementTypeName = GetTypeName(CAT->getElementType()); |
| 205 | if (Brackets) { |
| 206 | ElementTypeName.append("[]"); |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 207 | } |
Jean-Luc Brouillet | 2ce118e | 2014-05-27 17:41:22 -0700 | [diff] [blame] | 208 | return ElementTypeName; |
| 209 | } |
| 210 | case RSExportType::ExportClassRecord: { |
| 211 | return ET->getElementName() + "." RS_TYPE_ITEM_CLASS_NAME; |
| 212 | } |
| 213 | default: { slangAssert(false && "Unknown class of type"); } |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 214 | } |
| 215 | |
| 216 | return ""; |
| 217 | } |
| 218 | |
Shih-wei Liao | cf950c4 | 2010-10-06 03:44:40 -0700 | [diff] [blame] | 219 | static const char *GetTypeNullValue(const RSExportType *ET) { |
| 220 | switch (ET->getClass()) { |
Jean-Luc Brouillet | 2ce118e | 2014-05-27 17:41:22 -0700 | [diff] [blame] | 221 | case RSExportType::ExportClassPrimitive: { |
| 222 | const RSExportPrimitiveType *EPT = |
| 223 | static_cast<const RSExportPrimitiveType *>(ET); |
| 224 | if (EPT->isRSObjectType()) |
Shih-wei Liao | cf950c4 | 2010-10-06 03:44:40 -0700 | [diff] [blame] | 225 | return "null"; |
Jean-Luc Brouillet | 2ce118e | 2014-05-27 17:41:22 -0700 | [diff] [blame] | 226 | else if (EPT->getType() == DataTypeBoolean) |
| 227 | return "false"; |
| 228 | else |
| 229 | return "0"; |
| 230 | break; |
| 231 | } |
| 232 | case RSExportType::ExportClassPointer: |
| 233 | case RSExportType::ExportClassVector: |
| 234 | case RSExportType::ExportClassMatrix: |
| 235 | case RSExportType::ExportClassConstantArray: |
| 236 | case RSExportType::ExportClassRecord: { |
| 237 | return "null"; |
| 238 | break; |
| 239 | } |
| 240 | default: { slangAssert(false && "Unknown class of type"); } |
Shih-wei Liao | cf950c4 | 2010-10-06 03:44:40 -0700 | [diff] [blame] | 241 | } |
| 242 | return ""; |
| 243 | } |
| 244 | |
Stephen Hines | 47aca4e | 2012-03-08 20:07:28 -0800 | [diff] [blame] | 245 | static std::string GetBuiltinElementConstruct(const RSExportType *ET) { |
Zonr Chang | 2e1dba6 | 2010-10-05 22:20:11 +0800 | [diff] [blame] | 246 | if (ET->getClass() == RSExportType::ExportClassPrimitive) { |
Stephen Hines | a6b5414 | 2012-04-09 18:25:08 -0700 | [diff] [blame] | 247 | return std::string("Element.") + ET->getElementName(); |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 248 | } else if (ET->getClass() == RSExportType::ExportClassVector) { |
Jean-Luc Brouillet | 2ce118e | 2014-05-27 17:41:22 -0700 | [diff] [blame] | 249 | const RSExportVectorType *EVT = static_cast<const RSExportVectorType *>(ET); |
Jean-Luc Brouillet | cec9b65 | 2014-05-14 19:33:57 -0700 | [diff] [blame] | 250 | if (EVT->getType() == DataTypeFloat32) { |
Jean-Luc Brouillet | c643ceb | 2014-06-05 10:17:23 -0700 | [diff] [blame] | 251 | if (EVT->getNumElement() == 2) { |
Stephen Hines | 2b8fb64 | 2012-03-09 00:12:47 -0800 | [diff] [blame] | 252 | return "Element.F32_2"; |
Jean-Luc Brouillet | c643ceb | 2014-06-05 10:17:23 -0700 | [diff] [blame] | 253 | } else if (EVT->getNumElement() == 3) { |
Stephen Hines | 2b8fb64 | 2012-03-09 00:12:47 -0800 | [diff] [blame] | 254 | return "Element.F32_3"; |
Jean-Luc Brouillet | c643ceb | 2014-06-05 10:17:23 -0700 | [diff] [blame] | 255 | } else if (EVT->getNumElement() == 4) { |
Stephen Hines | 2b8fb64 | 2012-03-09 00:12:47 -0800 | [diff] [blame] | 256 | return "Element.F32_4"; |
Jean-Luc Brouillet | c643ceb | 2014-06-05 10:17:23 -0700 | [diff] [blame] | 257 | } else { |
| 258 | slangAssert(false && "Vectors should be size 2, 3, 4"); |
| 259 | } |
Jean-Luc Brouillet | cec9b65 | 2014-05-14 19:33:57 -0700 | [diff] [blame] | 260 | } else if (EVT->getType() == DataTypeUnsigned8) { |
Stephen Hines | 2b8fb64 | 2012-03-09 00:12:47 -0800 | [diff] [blame] | 261 | if (EVT->getNumElement() == 4) |
| 262 | return "Element.U8_4"; |
Shih-wei Liao | 324c047 | 2010-06-21 13:15:11 -0700 | [diff] [blame] | 263 | } |
Zonr Chang | 92b344a | 2010-10-05 20:39:03 +0800 | [diff] [blame] | 264 | } else if (ET->getClass() == RSExportType::ExportClassMatrix) { |
| 265 | const RSExportMatrixType *EMT = static_cast<const RSExportMatrixType *>(ET); |
| 266 | switch (EMT->getDim()) { |
Jean-Luc Brouillet | 2ce118e | 2014-05-27 17:41:22 -0700 | [diff] [blame] | 267 | case 2: |
| 268 | return "Element.MATRIX_2X2"; |
| 269 | case 3: |
| 270 | return "Element.MATRIX_3X3"; |
| 271 | case 4: |
| 272 | return "Element.MATRIX_4X4"; |
| 273 | default: |
| 274 | slangAssert(false && "Unsupported dimension of matrix"); |
Zonr Chang | 92b344a | 2010-10-05 20:39:03 +0800 | [diff] [blame] | 275 | } |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 276 | } |
Stephen Hines | 47aca4e | 2012-03-08 20:07:28 -0800 | [diff] [blame] | 277 | // RSExportType::ExportClassPointer can't be generated in a struct. |
Shih-wei Liao | 324c047 | 2010-06-21 13:15:11 -0700 | [diff] [blame] | 278 | |
Stephen Hines | 47aca4e | 2012-03-08 20:07:28 -0800 | [diff] [blame] | 279 | return ""; |
Stephen Hines | 48b72bf | 2011-06-10 15:37:27 -0700 | [diff] [blame] | 280 | } |
| 281 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 282 | /********************** Methods to generate script class **********************/ |
Jean-Luc Brouillet | 59f22c3 | 2014-06-04 14:53:48 -0700 | [diff] [blame] | 283 | RSReflectionJava::RSReflectionJava(const RSContext *Context, |
| 284 | std::vector<std::string> *GeneratedFileNames, |
| 285 | const std::string &OutputBaseDirectory, |
| 286 | const std::string &RSSourceFileName, |
| 287 | const std::string &BitCodeFileName, |
| 288 | bool EmbedBitcodeInJava) |
| 289 | : mRSContext(Context), mPackageName(Context->getReflectJavaPackageName()), |
| 290 | mRSPackageName(Context->getRSPackageName()), |
| 291 | mOutputBaseDirectory(OutputBaseDirectory), |
| 292 | mRSSourceFileName(RSSourceFileName), mBitCodeFileName(BitCodeFileName), |
| 293 | mResourceId(RSSlangReflectUtils::JavaClassNameFromRSFileName( |
| 294 | mBitCodeFileName.c_str())), |
| 295 | mScriptClassName(RS_SCRIPT_CLASS_NAME_PREFIX + |
| 296 | RSSlangReflectUtils::JavaClassNameFromRSFileName( |
| 297 | mRSSourceFileName.c_str())), |
Jean-Luc Brouillet | c643ceb | 2014-06-05 10:17:23 -0700 | [diff] [blame] | 298 | mEmbedBitcodeInJava(EmbedBitcodeInJava), mNextExportVarSlot(0), |
| 299 | mNextExportFuncSlot(0), mNextExportForEachSlot(0), mLastError(""), |
| 300 | mGeneratedFileNames(GeneratedFileNames), mFieldIndex(0) { |
Jean-Luc Brouillet | 59f22c3 | 2014-06-04 14:53:48 -0700 | [diff] [blame] | 301 | slangAssert(mGeneratedFileNames && "Must supply GeneratedFileNames"); |
| 302 | slangAssert(!mPackageName.empty() && mPackageName != "-"); |
| 303 | |
| 304 | mOutputDirectory = RSSlangReflectUtils::ComputePackagedPath( |
| 305 | OutputBaseDirectory.c_str(), mPackageName.c_str()) + |
| 306 | OS_PATH_SEPARATOR_STR; |
Tim Murray | 3a38b74 | 2014-07-02 10:41:08 -0700 | [diff] [blame] | 307 | |
| 308 | // mElement.getBytesSize only exists on JB+ |
| 309 | if (mRSContext->getTargetAPI() >= SLANG_JB_TARGET_API) { |
| 310 | mItemSizeof = RS_TYPE_ITEM_SIZEOF_CURRENT; |
| 311 | } else { |
| 312 | mItemSizeof = RS_TYPE_ITEM_SIZEOF_LEGACY; |
| 313 | } |
Jean-Luc Brouillet | 59f22c3 | 2014-06-04 14:53:48 -0700 | [diff] [blame] | 314 | } |
| 315 | |
Jean-Luc Brouillet | 2e205d0 | 2014-06-02 21:06:52 -0700 | [diff] [blame] | 316 | bool RSReflectionJava::genScriptClass(const std::string &ClassName, |
Jean-Luc Brouillet | 602def7 | 2014-05-27 16:11:37 -0700 | [diff] [blame] | 317 | std::string &ErrorMsg) { |
Jean-Luc Brouillet | 2e205d0 | 2014-06-02 21:06:52 -0700 | [diff] [blame] | 318 | if (!startClass(AM_Public, false, ClassName, RS_SCRIPT_CLASS_SUPER_CLASS_NAME, |
| 319 | ErrorMsg)) |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 320 | return false; |
| 321 | |
Jean-Luc Brouillet | 2e205d0 | 2014-06-02 21:06:52 -0700 | [diff] [blame] | 322 | genScriptClassConstructor(); |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 323 | |
| 324 | // Reflect export variable |
| 325 | for (RSContext::const_export_var_iterator I = mRSContext->export_vars_begin(), |
Jean-Luc Brouillet | 2ce118e | 2014-05-27 17:41:22 -0700 | [diff] [blame] | 326 | E = mRSContext->export_vars_end(); |
| 327 | I != E; I++) |
Jean-Luc Brouillet | 2e205d0 | 2014-06-02 21:06:52 -0700 | [diff] [blame] | 328 | genExportVariable(*I); |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 329 | |
Stephen Hines | 4a4bf92 | 2011-08-18 17:20:33 -0700 | [diff] [blame] | 330 | // Reflect export for each functions (only available on ICS+) |
Stephen Hines | 4cc499d | 2011-08-24 19:06:17 -0700 | [diff] [blame] | 331 | if (mRSContext->getTargetAPI() >= SLANG_ICS_TARGET_API) { |
Stephen Hines | 4a4bf92 | 2011-08-18 17:20:33 -0700 | [diff] [blame] | 332 | for (RSContext::const_export_foreach_iterator |
| 333 | I = mRSContext->export_foreach_begin(), |
| 334 | E = mRSContext->export_foreach_end(); |
| 335 | I != E; I++) |
Jean-Luc Brouillet | 2e205d0 | 2014-06-02 21:06:52 -0700 | [diff] [blame] | 336 | genExportForEach(*I); |
Stephen Hines | 4a4bf92 | 2011-08-18 17:20:33 -0700 | [diff] [blame] | 337 | } |
Stephen Hines | 593a894 | 2011-05-10 15:29:50 -0700 | [diff] [blame] | 338 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 339 | // Reflect export function |
| 340 | for (RSContext::const_export_func_iterator |
| 341 | I = mRSContext->export_funcs_begin(), |
| 342 | E = mRSContext->export_funcs_end(); |
| 343 | I != E; I++) |
Jean-Luc Brouillet | 2e205d0 | 2014-06-02 21:06:52 -0700 | [diff] [blame] | 344 | genExportFunction(*I); |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 345 | |
Jean-Luc Brouillet | 2e205d0 | 2014-06-02 21:06:52 -0700 | [diff] [blame] | 346 | endClass(); |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 347 | |
| 348 | return true; |
| 349 | } |
| 350 | |
Jean-Luc Brouillet | 2e205d0 | 2014-06-02 21:06:52 -0700 | [diff] [blame] | 351 | void RSReflectionJava::genScriptClassConstructor() { |
Stephen Hines | 4c8b659 | 2014-05-22 20:18:45 -0700 | [diff] [blame] | 352 | std::string className(RSSlangReflectUtils::JavaBitcodeClassNameFromRSFileName( |
Jean-Luc Brouillet | 59f22c3 | 2014-06-04 14:53:48 -0700 | [diff] [blame] | 353 | mRSSourceFileName.c_str())); |
Stephen Hines | d293693 | 2012-09-12 20:44:32 -0700 | [diff] [blame] | 354 | // Provide a simple way to reference this object. |
Jean-Luc Brouillet | f33e156 | 2014-06-03 17:55:57 -0700 | [diff] [blame] | 355 | mOut.indent() << "private static final String " RS_RESOURCE_NAME " = \"" |
| 356 | << getResourceId() << "\";\n"; |
Stephen Hines | d293693 | 2012-09-12 20:44:32 -0700 | [diff] [blame] | 357 | |
| 358 | // Generate a simple constructor with only a single parameter (the rest |
| 359 | // can be inferred from information we already have). |
Jean-Luc Brouillet | f33e156 | 2014-06-03 17:55:57 -0700 | [diff] [blame] | 360 | mOut.indent() << "// Constructor\n"; |
Jean-Luc Brouillet | 2e205d0 | 2014-06-02 21:06:52 -0700 | [diff] [blame] | 361 | startFunction(AM_Public, false, NULL, getClassName(), 1, "RenderScript", |
| 362 | "rs"); |
Stephen Hines | 44d495d | 2014-05-22 19:42:55 -0700 | [diff] [blame] | 363 | |
Jean-Luc Brouillet | 2e205d0 | 2014-06-02 21:06:52 -0700 | [diff] [blame] | 364 | if (getEmbedBitcodeInJava()) { |
Stephen Hines | 4c8b659 | 2014-05-22 20:18:45 -0700 | [diff] [blame] | 365 | // Call new single argument Java-only constructor |
Jean-Luc Brouillet | f33e156 | 2014-06-03 17:55:57 -0700 | [diff] [blame] | 366 | mOut.indent() << "super(rs,\n"; |
| 367 | mOut.indent() << " " << RS_RESOURCE_NAME ",\n"; |
| 368 | mOut.indent() << " " << className << ".getBitCode32(),\n"; |
Stephen Hines | 9ae18b2 | 2014-06-10 23:53:00 -0700 | [diff] [blame] | 369 | mOut.indent() << " " << className << ".getBitCode64());\n"; |
Stephen Hines | 4c8b659 | 2014-05-22 20:18:45 -0700 | [diff] [blame] | 370 | } else { |
| 371 | // Call alternate constructor with required parameters. |
| 372 | // Look up the proper raw bitcode resource id via the context. |
Jean-Luc Brouillet | f33e156 | 2014-06-03 17:55:57 -0700 | [diff] [blame] | 373 | mOut.indent() << "this(rs,\n"; |
| 374 | mOut.indent() << " rs.getApplicationContext().getResources(),\n"; |
| 375 | mOut.indent() << " rs.getApplicationContext().getResources()." |
| 376 | "getIdentifier(\n"; |
| 377 | mOut.indent() << " " RS_RESOURCE_NAME ", \"raw\",\n"; |
| 378 | mOut.indent() |
| 379 | << " rs.getApplicationContext().getPackageName()));\n"; |
Jean-Luc Brouillet | 2e205d0 | 2014-06-02 21:06:52 -0700 | [diff] [blame] | 380 | endFunction(); |
Stephen Hines | d293693 | 2012-09-12 20:44:32 -0700 | [diff] [blame] | 381 | |
Stephen Hines | 4c8b659 | 2014-05-22 20:18:45 -0700 | [diff] [blame] | 382 | // Alternate constructor (legacy) with 3 original parameters. |
Jean-Luc Brouillet | 2e205d0 | 2014-06-02 21:06:52 -0700 | [diff] [blame] | 383 | startFunction(AM_Public, false, NULL, getClassName(), 3, "RenderScript", |
| 384 | "rs", "Resources", "resources", "int", "id"); |
Stephen Hines | 4c8b659 | 2014-05-22 20:18:45 -0700 | [diff] [blame] | 385 | // Call constructor of super class |
Jean-Luc Brouillet | f33e156 | 2014-06-03 17:55:57 -0700 | [diff] [blame] | 386 | mOut.indent() << "super(rs, resources, id);\n"; |
Stephen Hines | 4c8b659 | 2014-05-22 20:18:45 -0700 | [diff] [blame] | 387 | } |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 388 | |
| 389 | // If an exported variable has initial value, reflect it |
| 390 | |
| 391 | for (RSContext::const_export_var_iterator I = mRSContext->export_vars_begin(), |
Jean-Luc Brouillet | 2ce118e | 2014-05-27 17:41:22 -0700 | [diff] [blame] | 392 | E = mRSContext->export_vars_end(); |
| 393 | I != E; I++) { |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 394 | const RSExportVar *EV = *I; |
Stephen Hines | d369cda | 2012-02-13 12:00:03 -0800 | [diff] [blame] | 395 | if (!EV->getInit().isUninit()) { |
Jean-Luc Brouillet | 2e205d0 | 2014-06-02 21:06:52 -0700 | [diff] [blame] | 396 | genInitExportVariable(EV->getType(), EV->getName(), EV->getInit()); |
Stephen Hines | d369cda | 2012-02-13 12:00:03 -0800 | [diff] [blame] | 397 | } else if (EV->getArraySize()) { |
| 398 | // Always create an initial zero-init array object. |
Jean-Luc Brouillet | f33e156 | 2014-06-03 17:55:57 -0700 | [diff] [blame] | 399 | mOut.indent() << RS_EXPORT_VAR_PREFIX << EV->getName() << " = new " |
| 400 | << GetTypeName(EV->getType(), false) << "[" |
| 401 | << EV->getArraySize() << "];\n"; |
Stephen Hines | d369cda | 2012-02-13 12:00:03 -0800 | [diff] [blame] | 402 | size_t NumInits = EV->getNumInits(); |
| 403 | const RSExportConstantArrayType *ECAT = |
Jean-Luc Brouillet | 2ce118e | 2014-05-27 17:41:22 -0700 | [diff] [blame] | 404 | static_cast<const RSExportConstantArrayType *>(EV->getType()); |
Stephen Hines | d369cda | 2012-02-13 12:00:03 -0800 | [diff] [blame] | 405 | const RSExportType *ET = ECAT->getElementType(); |
| 406 | for (size_t i = 0; i < NumInits; i++) { |
| 407 | std::stringstream Name; |
| 408 | Name << EV->getName() << "[" << i << "]"; |
Jean-Luc Brouillet | 2e205d0 | 2014-06-02 21:06:52 -0700 | [diff] [blame] | 409 | genInitExportVariable(ET, Name.str(), EV->getInitArray(i)); |
Stephen Hines | d369cda | 2012-02-13 12:00:03 -0800 | [diff] [blame] | 410 | } |
| 411 | } |
Stephen Hines | a6b5414 | 2012-04-09 18:25:08 -0700 | [diff] [blame] | 412 | if (mRSContext->getTargetAPI() >= SLANG_JB_TARGET_API) { |
Jean-Luc Brouillet | 2e205d0 | 2014-06-02 21:06:52 -0700 | [diff] [blame] | 413 | genTypeInstance(EV->getType()); |
Stephen Hines | a6b5414 | 2012-04-09 18:25:08 -0700 | [diff] [blame] | 414 | } |
Jean-Luc Brouillet | 2e205d0 | 2014-06-02 21:06:52 -0700 | [diff] [blame] | 415 | genFieldPackerInstance(EV->getType()); |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 416 | } |
| 417 | |
Stephen Hines | 48b72bf | 2011-06-10 15:37:27 -0700 | [diff] [blame] | 418 | for (RSContext::const_export_foreach_iterator |
| 419 | I = mRSContext->export_foreach_begin(), |
| 420 | E = mRSContext->export_foreach_end(); |
Jean-Luc Brouillet | 2ce118e | 2014-05-27 17:41:22 -0700 | [diff] [blame] | 421 | I != E; I++) { |
Stephen Hines | 48b72bf | 2011-06-10 15:37:27 -0700 | [diff] [blame] | 422 | const RSExportForEach *EF = *I; |
| 423 | |
Chris Wailes | c9454af | 2014-06-13 17:25:40 -0700 | [diff] [blame] | 424 | const RSExportForEach::InTypeVec &InTypes = EF->getInTypes(); |
| 425 | for (RSExportForEach::InTypeIter BI = InTypes.begin(), EI = InTypes.end(); |
| 426 | BI != EI; BI++) { |
| 427 | |
| 428 | if (*BI != NULL) { |
| 429 | genTypeInstanceFromPointer(*BI); |
| 430 | } |
Stephen Hines | 48b72bf | 2011-06-10 15:37:27 -0700 | [diff] [blame] | 431 | } |
Chris Wailes | c9454af | 2014-06-13 17:25:40 -0700 | [diff] [blame] | 432 | |
Stephen Hines | 48b72bf | 2011-06-10 15:37:27 -0700 | [diff] [blame] | 433 | const RSExportType *OET = EF->getOutType(); |
| 434 | if (OET) { |
Jean-Luc Brouillet | 2e205d0 | 2014-06-02 21:06:52 -0700 | [diff] [blame] | 435 | genTypeInstanceFromPointer(OET); |
Stephen Hines | 48b72bf | 2011-06-10 15:37:27 -0700 | [diff] [blame] | 436 | } |
| 437 | } |
| 438 | |
Jean-Luc Brouillet | 2e205d0 | 2014-06-02 21:06:52 -0700 | [diff] [blame] | 439 | endFunction(); |
Jason Sams | b6902e2 | 2010-11-03 17:04:06 -0700 | [diff] [blame] | 440 | |
Jean-Luc Brouillet | 2e205d0 | 2014-06-02 21:06:52 -0700 | [diff] [blame] | 441 | for (std::set<std::string>::iterator I = mTypesToCheck.begin(), |
| 442 | E = mTypesToCheck.end(); |
Jean-Luc Brouillet | 2ce118e | 2014-05-27 17:41:22 -0700 | [diff] [blame] | 443 | I != E; I++) { |
Jean-Luc Brouillet | f33e156 | 2014-06-03 17:55:57 -0700 | [diff] [blame] | 444 | mOut.indent() << "private Element " RS_ELEM_PREFIX << *I << ";\n"; |
Stephen Hines | 48b72bf | 2011-06-10 15:37:27 -0700 | [diff] [blame] | 445 | } |
| 446 | |
Jean-Luc Brouillet | 2e205d0 | 2014-06-02 21:06:52 -0700 | [diff] [blame] | 447 | for (std::set<std::string>::iterator I = mFieldPackerTypes.begin(), |
| 448 | E = mFieldPackerTypes.end(); |
Jean-Luc Brouillet | 2ce118e | 2014-05-27 17:41:22 -0700 | [diff] [blame] | 449 | I != E; I++) { |
Jean-Luc Brouillet | f33e156 | 2014-06-03 17:55:57 -0700 | [diff] [blame] | 450 | mOut.indent() << "private FieldPacker " RS_FP_PREFIX << *I << ";\n"; |
Stephen Hines | 1f6c331 | 2012-07-03 17:23:33 -0700 | [diff] [blame] | 451 | } |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 452 | } |
| 453 | |
Jean-Luc Brouillet | 2e205d0 | 2014-06-02 21:06:52 -0700 | [diff] [blame] | 454 | void RSReflectionJava::genInitBoolExportVariable(const std::string &VarName, |
Jean-Luc Brouillet | 602def7 | 2014-05-27 16:11:37 -0700 | [diff] [blame] | 455 | const clang::APValue &Val) { |
Stephen Hines | 6e6578a | 2011-02-07 18:05:48 -0800 | [diff] [blame] | 456 | slangAssert(!Val.isUninit() && "Not a valid initializer"); |
Jean-Luc Brouillet | 2ce118e | 2014-05-27 17:41:22 -0700 | [diff] [blame] | 457 | slangAssert((Val.getKind() == clang::APValue::Int) && |
| 458 | "Bool type has wrong initial APValue"); |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 459 | |
Jean-Luc Brouillet | f33e156 | 2014-06-03 17:55:57 -0700 | [diff] [blame] | 460 | mOut.indent() << RS_EXPORT_VAR_PREFIX << VarName << " = "; |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 461 | |
Jean-Luc Brouillet | f33e156 | 2014-06-03 17:55:57 -0700 | [diff] [blame] | 462 | mOut << ((Val.getInt().getSExtValue() == 0) ? "false" : "true") << ";\n"; |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 463 | } |
| 464 | |
Jean-Luc Brouillet | 2e205d0 | 2014-06-02 21:06:52 -0700 | [diff] [blame] | 465 | void |
| 466 | RSReflectionJava::genInitPrimitiveExportVariable(const std::string &VarName, |
| 467 | const clang::APValue &Val) { |
Stephen Hines | 5d67178 | 2012-01-31 19:32:04 -0800 | [diff] [blame] | 468 | slangAssert(!Val.isUninit() && "Not a valid initializer"); |
| 469 | |
Jean-Luc Brouillet | f33e156 | 2014-06-03 17:55:57 -0700 | [diff] [blame] | 470 | mOut.indent() << RS_EXPORT_VAR_PREFIX << VarName << " = "; |
Jean-Luc Brouillet | efcff10 | 2014-06-03 16:13:51 -0700 | [diff] [blame] | 471 | genInitValue(Val, false); |
Jean-Luc Brouillet | f33e156 | 2014-06-03 17:55:57 -0700 | [diff] [blame] | 472 | mOut << ";\n"; |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 473 | } |
| 474 | |
Jean-Luc Brouillet | 2e205d0 | 2014-06-02 21:06:52 -0700 | [diff] [blame] | 475 | void RSReflectionJava::genInitExportVariable(const RSExportType *ET, |
Jean-Luc Brouillet | 602def7 | 2014-05-27 16:11:37 -0700 | [diff] [blame] | 476 | const std::string &VarName, |
| 477 | const clang::APValue &Val) { |
Stephen Hines | 6e6578a | 2011-02-07 18:05:48 -0800 | [diff] [blame] | 478 | slangAssert(!Val.isUninit() && "Not a valid initializer"); |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 479 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 480 | switch (ET->getClass()) { |
Jean-Luc Brouillet | 2ce118e | 2014-05-27 17:41:22 -0700 | [diff] [blame] | 481 | case RSExportType::ExportClassPrimitive: { |
| 482 | const RSExportPrimitiveType *EPT = |
| 483 | static_cast<const RSExportPrimitiveType *>(ET); |
| 484 | if (EPT->getType() == DataTypeBoolean) { |
Jean-Luc Brouillet | 2e205d0 | 2014-06-02 21:06:52 -0700 | [diff] [blame] | 485 | genInitBoolExportVariable(VarName, Val); |
Jean-Luc Brouillet | 2ce118e | 2014-05-27 17:41:22 -0700 | [diff] [blame] | 486 | } else { |
Jean-Luc Brouillet | 2e205d0 | 2014-06-02 21:06:52 -0700 | [diff] [blame] | 487 | genInitPrimitiveExportVariable(VarName, Val); |
Jean-Luc Brouillet | 2ce118e | 2014-05-27 17:41:22 -0700 | [diff] [blame] | 488 | } |
| 489 | break; |
| 490 | } |
| 491 | case RSExportType::ExportClassPointer: { |
| 492 | if (!Val.isInt() || Val.getInt().getSExtValue() != 0) |
| 493 | std::cout << "Initializer which is non-NULL to pointer type variable " |
| 494 | "will be ignored\n"; |
| 495 | break; |
| 496 | } |
| 497 | case RSExportType::ExportClassVector: { |
| 498 | const RSExportVectorType *EVT = static_cast<const RSExportVectorType *>(ET); |
| 499 | switch (Val.getKind()) { |
| 500 | case clang::APValue::Int: |
| 501 | case clang::APValue::Float: { |
| 502 | for (unsigned i = 0; i < EVT->getNumElement(); i++) { |
| 503 | std::string Name = VarName + "." + GetVectorAccessor(i); |
Jean-Luc Brouillet | 2e205d0 | 2014-06-02 21:06:52 -0700 | [diff] [blame] | 504 | genInitPrimitiveExportVariable(Name, Val); |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 505 | } |
| 506 | break; |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 507 | } |
Jean-Luc Brouillet | 2ce118e | 2014-05-27 17:41:22 -0700 | [diff] [blame] | 508 | case clang::APValue::Vector: { |
| 509 | std::stringstream VecName; |
| 510 | VecName << EVT->getRSReflectionType(EVT)->rs_java_vector_prefix |
| 511 | << EVT->getNumElement(); |
Jean-Luc Brouillet | f33e156 | 2014-06-03 17:55:57 -0700 | [diff] [blame] | 512 | mOut.indent() << RS_EXPORT_VAR_PREFIX << VarName << " = new " |
| 513 | << VecName.str() << "();\n"; |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 514 | |
Jean-Luc Brouillet | 2ce118e | 2014-05-27 17:41:22 -0700 | [diff] [blame] | 515 | unsigned NumElements = std::min( |
| 516 | static_cast<unsigned>(EVT->getNumElement()), Val.getVectorLength()); |
| 517 | for (unsigned i = 0; i < NumElements; i++) { |
| 518 | const clang::APValue &ElementVal = Val.getVectorElt(i); |
| 519 | std::string Name = VarName + "." + GetVectorAccessor(i); |
Jean-Luc Brouillet | 2e205d0 | 2014-06-02 21:06:52 -0700 | [diff] [blame] | 520 | genInitPrimitiveExportVariable(Name, ElementVal); |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 521 | } |
| 522 | break; |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 523 | } |
Jean-Luc Brouillet | 2ce118e | 2014-05-27 17:41:22 -0700 | [diff] [blame] | 524 | case clang::APValue::MemberPointer: |
| 525 | case clang::APValue::Uninitialized: |
| 526 | case clang::APValue::ComplexInt: |
| 527 | case clang::APValue::ComplexFloat: |
| 528 | case clang::APValue::LValue: |
| 529 | case clang::APValue::Array: |
| 530 | case clang::APValue::Struct: |
| 531 | case clang::APValue::Union: |
| 532 | case clang::APValue::AddrLabelDiff: { |
| 533 | slangAssert(false && "Unexpected type of value of initializer."); |
| 534 | } |
| 535 | } |
| 536 | break; |
| 537 | } |
| 538 | // TODO(zonr): Resolving initializer of a record (and matrix) type variable |
| 539 | // is complex. It cannot obtain by just simply evaluating the initializer |
| 540 | // expression. |
| 541 | case RSExportType::ExportClassMatrix: |
| 542 | case RSExportType::ExportClassConstantArray: |
| 543 | case RSExportType::ExportClassRecord: { |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 544 | #if 0 |
| 545 | unsigned InitIndex = 0; |
zonr | 6315f76 | 2010-10-05 15:35:14 +0800 | [diff] [blame] | 546 | const RSExportRecordType *ERT = |
| 547 | static_cast<const RSExportRecordType*>(ET); |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 548 | |
Stephen Hines | 6e6578a | 2011-02-07 18:05:48 -0800 | [diff] [blame] | 549 | slangAssert((Val.getKind() == clang::APValue::Vector) && |
| 550 | "Unexpected type of initializer for record type variable"); |
Shih-wei Liao | 9c631ff | 2010-09-17 11:57:29 -0700 | [diff] [blame] | 551 | |
Jean-Luc Brouillet | f33e156 | 2014-06-03 17:55:57 -0700 | [diff] [blame] | 552 | mOut.indent() << RS_EXPORT_VAR_PREFIX << VarName |
Stephen Hines | a6b5414 | 2012-04-09 18:25:08 -0700 | [diff] [blame] | 553 | << " = new " << ERT->getElementName() |
Jean-Luc Brouillet | 2968921 | 2014-05-27 13:25:31 -0700 | [diff] [blame] | 554 | << "." RS_TYPE_ITEM_CLASS_NAME"();\n"; |
Shih-wei Liao | 9c631ff | 2010-09-17 11:57:29 -0700 | [diff] [blame] | 555 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 556 | for (RSExportRecordType::const_field_iterator I = ERT->fields_begin(), |
| 557 | E = ERT->fields_end(); |
| 558 | I != E; |
| 559 | I++) { |
| 560 | const RSExportRecordType::Field *F = *I; |
| 561 | std::string FieldName = VarName + "." + F->getName(); |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 562 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 563 | if (InitIndex > Val.getVectorLength()) |
| 564 | break; |
| 565 | |
Jean-Luc Brouillet | f33e156 | 2014-06-03 17:55:57 -0700 | [diff] [blame] | 566 | genInitPrimitiveExportVariable(FieldName, |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 567 | Val.getVectorElt(InitIndex++)); |
| 568 | } |
| 569 | #endif |
Jean-Luc Brouillet | 2ce118e | 2014-05-27 17:41:22 -0700 | [diff] [blame] | 570 | slangAssert(false && "Unsupported initializer for record/matrix/constant " |
| 571 | "array type variable currently"); |
| 572 | break; |
| 573 | } |
| 574 | default: { slangAssert(false && "Unknown class of type"); } |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 575 | } |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 576 | } |
| 577 | |
Jean-Luc Brouillet | 2e205d0 | 2014-06-02 21:06:52 -0700 | [diff] [blame] | 578 | void RSReflectionJava::genExportVariable(const RSExportVar *EV) { |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 579 | const RSExportType *ET = EV->getType(); |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 580 | |
Jean-Luc Brouillet | f33e156 | 2014-06-03 17:55:57 -0700 | [diff] [blame] | 581 | mOut.indent() << "private final static int " << RS_EXPORT_VAR_INDEX_PREFIX |
| 582 | << EV->getName() << " = " << getNextExportVarSlot() << ";\n"; |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 583 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 584 | switch (ET->getClass()) { |
Jean-Luc Brouillet | 2ce118e | 2014-05-27 17:41:22 -0700 | [diff] [blame] | 585 | case RSExportType::ExportClassPrimitive: { |
Jean-Luc Brouillet | 2e205d0 | 2014-06-02 21:06:52 -0700 | [diff] [blame] | 586 | genPrimitiveTypeExportVariable(EV); |
Jean-Luc Brouillet | 2ce118e | 2014-05-27 17:41:22 -0700 | [diff] [blame] | 587 | break; |
| 588 | } |
| 589 | case RSExportType::ExportClassPointer: { |
Jean-Luc Brouillet | 2e205d0 | 2014-06-02 21:06:52 -0700 | [diff] [blame] | 590 | genPointerTypeExportVariable(EV); |
Jean-Luc Brouillet | 2ce118e | 2014-05-27 17:41:22 -0700 | [diff] [blame] | 591 | break; |
| 592 | } |
| 593 | case RSExportType::ExportClassVector: { |
Jean-Luc Brouillet | 2e205d0 | 2014-06-02 21:06:52 -0700 | [diff] [blame] | 594 | genVectorTypeExportVariable(EV); |
Jean-Luc Brouillet | 2ce118e | 2014-05-27 17:41:22 -0700 | [diff] [blame] | 595 | break; |
| 596 | } |
| 597 | case RSExportType::ExportClassMatrix: { |
Jean-Luc Brouillet | 2e205d0 | 2014-06-02 21:06:52 -0700 | [diff] [blame] | 598 | genMatrixTypeExportVariable(EV); |
Jean-Luc Brouillet | 2ce118e | 2014-05-27 17:41:22 -0700 | [diff] [blame] | 599 | break; |
| 600 | } |
| 601 | case RSExportType::ExportClassConstantArray: { |
Jean-Luc Brouillet | 2e205d0 | 2014-06-02 21:06:52 -0700 | [diff] [blame] | 602 | genConstantArrayTypeExportVariable(EV); |
Jean-Luc Brouillet | 2ce118e | 2014-05-27 17:41:22 -0700 | [diff] [blame] | 603 | break; |
| 604 | } |
| 605 | case RSExportType::ExportClassRecord: { |
Jean-Luc Brouillet | 2e205d0 | 2014-06-02 21:06:52 -0700 | [diff] [blame] | 606 | genRecordTypeExportVariable(EV); |
Jean-Luc Brouillet | 2ce118e | 2014-05-27 17:41:22 -0700 | [diff] [blame] | 607 | break; |
| 608 | } |
| 609 | default: { slangAssert(false && "Unknown class of type"); } |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 610 | } |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 611 | } |
| 612 | |
Jean-Luc Brouillet | 2e205d0 | 2014-06-02 21:06:52 -0700 | [diff] [blame] | 613 | void RSReflectionJava::genExportFunction(const RSExportFunc *EF) { |
Jean-Luc Brouillet | f33e156 | 2014-06-03 17:55:57 -0700 | [diff] [blame] | 614 | mOut.indent() << "private final static int " << RS_EXPORT_FUNC_INDEX_PREFIX |
| 615 | << EF->getName() << " = " << getNextExportFuncSlot() << ";\n"; |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 616 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 617 | // invoke_*() |
Jean-Luc Brouillet | 2e205d0 | 2014-06-02 21:06:52 -0700 | [diff] [blame] | 618 | ArgTy Args; |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 619 | |
Zonr Chang | 0da0a7d | 2010-10-05 21:26:37 +0800 | [diff] [blame] | 620 | if (EF->hasParam()) { |
| 621 | for (RSExportFunc::const_param_iterator I = EF->params_begin(), |
Jean-Luc Brouillet | 2ce118e | 2014-05-27 17:41:22 -0700 | [diff] [blame] | 622 | E = EF->params_end(); |
| 623 | I != E; I++) { |
| 624 | Args.push_back( |
| 625 | std::make_pair(GetTypeName((*I)->getType()), (*I)->getName())); |
Zonr Chang | 0da0a7d | 2010-10-05 21:26:37 +0800 | [diff] [blame] | 626 | } |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 627 | } |
| 628 | |
Jean-Luc Brouillet | 2e205d0 | 2014-06-02 21:06:52 -0700 | [diff] [blame] | 629 | startFunction(AM_Public, false, "void", |
| 630 | "invoke_" + EF->getName(/*Mangle=*/false), |
| 631 | // We are using un-mangled name since Java |
| 632 | // supports method overloading. |
| 633 | Args); |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 634 | |
| 635 | if (!EF->hasParam()) { |
Jean-Luc Brouillet | f33e156 | 2014-06-03 17:55:57 -0700 | [diff] [blame] | 636 | mOut.indent() << "invoke(" << RS_EXPORT_FUNC_INDEX_PREFIX << EF->getName() |
| 637 | << ");\n"; |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 638 | } else { |
| 639 | const RSExportRecordType *ERT = EF->getParamPacketType(); |
| 640 | std::string FieldPackerName = EF->getName() + "_fp"; |
| 641 | |
Jean-Luc Brouillet | 2e205d0 | 2014-06-02 21:06:52 -0700 | [diff] [blame] | 642 | if (genCreateFieldPacker(ERT, FieldPackerName.c_str())) |
| 643 | genPackVarOfType(ERT, NULL, FieldPackerName.c_str()); |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 644 | |
Jean-Luc Brouillet | f33e156 | 2014-06-03 17:55:57 -0700 | [diff] [blame] | 645 | mOut.indent() << "invoke(" << RS_EXPORT_FUNC_INDEX_PREFIX << EF->getName() |
| 646 | << ", " << FieldPackerName << ");\n"; |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 647 | } |
| 648 | |
Jean-Luc Brouillet | 2e205d0 | 2014-06-02 21:06:52 -0700 | [diff] [blame] | 649 | endFunction(); |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 650 | } |
| 651 | |
Chris Wailes | c9454af | 2014-06-13 17:25:40 -0700 | [diff] [blame] | 652 | void RSReflectionJava::genPairwiseDimCheck(std::string name0, |
| 653 | std::string name1) { |
| 654 | |
| 655 | mOut.indent() << "// Verify dimensions\n"; |
| 656 | mOut.indent() << "t0 = " << name0 << ".getType();\n"; |
| 657 | mOut.indent() << "t1 = " << name1 << ".getType();\n"; |
| 658 | mOut.indent() << "if ((t0.getCount() != t1.getCount()) ||\n"; |
| 659 | mOut.indent() << " (t0.getX() != t1.getX()) ||\n"; |
| 660 | mOut.indent() << " (t0.getY() != t1.getY()) ||\n"; |
| 661 | mOut.indent() << " (t0.getZ() != t1.getZ()) ||\n"; |
| 662 | mOut.indent() << " (t0.hasFaces() != t1.hasFaces()) ||\n"; |
| 663 | mOut.indent() << " (t0.hasMipmaps() != t1.hasMipmaps())) {\n"; |
| 664 | mOut.indent() << " throw new RSRuntimeException(\"Dimension mismatch " |
| 665 | << "between parameters " << name0 << " and " << name1 |
| 666 | << "!\");\n"; |
| 667 | mOut.indent() << "}\n\n"; |
| 668 | } |
| 669 | |
Jean-Luc Brouillet | 2e205d0 | 2014-06-02 21:06:52 -0700 | [diff] [blame] | 670 | void RSReflectionJava::genExportForEach(const RSExportForEach *EF) { |
Stephen Hines | c17e198 | 2012-02-22 12:30:45 -0800 | [diff] [blame] | 671 | if (EF->isDummyRoot()) { |
| 672 | // Skip reflection for dummy root() kernels. Note that we have to |
| 673 | // advance the next slot number for ForEach, however. |
Jean-Luc Brouillet | f33e156 | 2014-06-03 17:55:57 -0700 | [diff] [blame] | 674 | mOut.indent() << "//private final static int " |
| 675 | << RS_EXPORT_FOREACH_INDEX_PREFIX << EF->getName() << " = " |
| 676 | << getNextExportForEachSlot() << ";\n"; |
Stephen Hines | c17e198 | 2012-02-22 12:30:45 -0800 | [diff] [blame] | 677 | return; |
| 678 | } |
| 679 | |
Jean-Luc Brouillet | f33e156 | 2014-06-03 17:55:57 -0700 | [diff] [blame] | 680 | mOut.indent() << "private final static int " << RS_EXPORT_FOREACH_INDEX_PREFIX |
| 681 | << EF->getName() << " = " << getNextExportForEachSlot() |
| 682 | << ";\n"; |
Stephen Hines | 593a894 | 2011-05-10 15:29:50 -0700 | [diff] [blame] | 683 | |
Stephen Hines | b5a89fb | 2011-05-17 14:48:02 -0700 | [diff] [blame] | 684 | // forEach_*() |
Jean-Luc Brouillet | 2e205d0 | 2014-06-02 21:06:52 -0700 | [diff] [blame] | 685 | ArgTy Args; |
Stephen Hines | 593a894 | 2011-05-10 15:29:50 -0700 | [diff] [blame] | 686 | |
Stephen Hines | 9ca96e7 | 2012-09-13 16:57:06 -0700 | [diff] [blame] | 687 | slangAssert(EF->getNumParameters() > 0 || EF->hasReturn()); |
Stephen Hines | 593a894 | 2011-05-10 15:29:50 -0700 | [diff] [blame] | 688 | |
Chris Wailes | c9454af | 2014-06-13 17:25:40 -0700 | [diff] [blame] | 689 | const RSExportForEach::InVec &Ins = EF->getIns(); |
| 690 | const RSExportForEach::InTypeVec &InTypes = EF->getInTypes(); |
| 691 | const RSExportType *OET = EF->getOutType(); |
| 692 | |
| 693 | if (Ins.size() == 1) { |
Stephen Hines | b5a89fb | 2011-05-17 14:48:02 -0700 | [diff] [blame] | 694 | Args.push_back(std::make_pair("Allocation", "ain")); |
Chris Wailes | c9454af | 2014-06-13 17:25:40 -0700 | [diff] [blame] | 695 | |
| 696 | } else if (Ins.size() > 1) { |
| 697 | for (RSExportForEach::InIter BI = Ins.begin(), EI = Ins.end(); BI != EI; |
| 698 | BI++) { |
| 699 | |
| 700 | Args.push_back(std::make_pair("Allocation", |
| 701 | "ain_" + (*BI)->getName().str())); |
| 702 | } |
| 703 | } |
| 704 | |
Stephen Hines | 9ca96e7 | 2012-09-13 16:57:06 -0700 | [diff] [blame] | 705 | if (EF->hasOut() || EF->hasReturn()) |
Stephen Hines | 593a894 | 2011-05-10 15:29:50 -0700 | [diff] [blame] | 706 | Args.push_back(std::make_pair("Allocation", "aout")); |
Stephen Hines | b5a89fb | 2011-05-17 14:48:02 -0700 | [diff] [blame] | 707 | |
| 708 | const RSExportRecordType *ERT = EF->getParamPacketType(); |
| 709 | if (ERT) { |
| 710 | for (RSExportForEach::const_param_iterator I = EF->params_begin(), |
Jean-Luc Brouillet | 2ce118e | 2014-05-27 17:41:22 -0700 | [diff] [blame] | 711 | E = EF->params_end(); |
| 712 | I != E; I++) { |
| 713 | Args.push_back( |
| 714 | std::make_pair(GetTypeName((*I)->getType()), (*I)->getName())); |
Stephen Hines | 593a894 | 2011-05-10 15:29:50 -0700 | [diff] [blame] | 715 | } |
| 716 | } |
| 717 | |
Tim Murray | b81a993 | 2012-10-10 15:56:02 -0700 | [diff] [blame] | 718 | if (mRSContext->getTargetAPI() >= SLANG_JB_MR1_TARGET_API) { |
Jean-Luc Brouillet | 2e205d0 | 2014-06-02 21:06:52 -0700 | [diff] [blame] | 719 | startFunction(AM_Public, false, "Script.KernelID", |
| 720 | "getKernelID_" + EF->getName(), 0); |
Tim Murray | b81a993 | 2012-10-10 15:56:02 -0700 | [diff] [blame] | 721 | |
Jean-Luc Brouillet | 2ce118e | 2014-05-27 17:41:22 -0700 | [diff] [blame] | 722 | // TODO: add element checking |
Jean-Luc Brouillet | f33e156 | 2014-06-03 17:55:57 -0700 | [diff] [blame] | 723 | mOut.indent() << "return createKernelID(" << RS_EXPORT_FOREACH_INDEX_PREFIX |
Chris Wailes | c9454af | 2014-06-13 17:25:40 -0700 | [diff] [blame] | 724 | << EF->getName() << ", " << EF->getSignatureMetadata() |
| 725 | << ", null, null);\n"; |
Tim Murray | b81a993 | 2012-10-10 15:56:02 -0700 | [diff] [blame] | 726 | |
Jean-Luc Brouillet | 2e205d0 | 2014-06-02 21:06:52 -0700 | [diff] [blame] | 727 | endFunction(); |
Tim Murray | b81a993 | 2012-10-10 15:56:02 -0700 | [diff] [blame] | 728 | } |
| 729 | |
Stephen Hines | 5097474 | 2013-02-12 22:11:08 -0800 | [diff] [blame] | 730 | if (mRSContext->getTargetAPI() >= SLANG_JB_MR2_TARGET_API) { |
Jean-Luc Brouillet | 2e205d0 | 2014-06-02 21:06:52 -0700 | [diff] [blame] | 731 | startFunction(AM_Public, false, "void", "forEach_" + EF->getName(), Args); |
Stephen Hines | 5097474 | 2013-02-12 22:11:08 -0800 | [diff] [blame] | 732 | |
Jean-Luc Brouillet | f33e156 | 2014-06-03 17:55:57 -0700 | [diff] [blame] | 733 | mOut.indent() << "forEach_" << EF->getName(); |
| 734 | mOut << "("; |
Stephen Hines | 5097474 | 2013-02-12 22:11:08 -0800 | [diff] [blame] | 735 | |
Chris Wailes | c9454af | 2014-06-13 17:25:40 -0700 | [diff] [blame] | 736 | if (Ins.size() == 1) { |
Jean-Luc Brouillet | f33e156 | 2014-06-03 17:55:57 -0700 | [diff] [blame] | 737 | mOut << "ain, "; |
Chris Wailes | c9454af | 2014-06-13 17:25:40 -0700 | [diff] [blame] | 738 | |
| 739 | } else if (Ins.size() > 1) { |
| 740 | for (RSExportForEach::InIter BI = Ins.begin(), EI = Ins.end(); BI != EI; |
| 741 | BI++) { |
| 742 | |
| 743 | mOut << "ain_" << (*BI)->getName().str() << ", "; |
| 744 | } |
Stephen Hines | 5097474 | 2013-02-12 22:11:08 -0800 | [diff] [blame] | 745 | } |
| 746 | |
| 747 | if (EF->hasOut() || EF->hasReturn()) { |
Jean-Luc Brouillet | f33e156 | 2014-06-03 17:55:57 -0700 | [diff] [blame] | 748 | mOut << "aout, "; |
Stephen Hines | 5097474 | 2013-02-12 22:11:08 -0800 | [diff] [blame] | 749 | } |
| 750 | |
| 751 | if (EF->hasUsrData()) { |
Jean-Luc Brouillet | f33e156 | 2014-06-03 17:55:57 -0700 | [diff] [blame] | 752 | mOut << Args.back().second << ", "; |
Stephen Hines | 5097474 | 2013-02-12 22:11:08 -0800 | [diff] [blame] | 753 | } |
| 754 | |
| 755 | // No clipped bounds to pass in. |
Jean-Luc Brouillet | f33e156 | 2014-06-03 17:55:57 -0700 | [diff] [blame] | 756 | mOut << "null);\n"; |
Stephen Hines | 5097474 | 2013-02-12 22:11:08 -0800 | [diff] [blame] | 757 | |
Jean-Luc Brouillet | 2e205d0 | 2014-06-02 21:06:52 -0700 | [diff] [blame] | 758 | endFunction(); |
Stephen Hines | 5097474 | 2013-02-12 22:11:08 -0800 | [diff] [blame] | 759 | |
| 760 | // Add the clipped kernel parameters to the Args list. |
| 761 | Args.push_back(std::make_pair("Script.LaunchOptions", "sc")); |
| 762 | } |
| 763 | |
Jean-Luc Brouillet | 2e205d0 | 2014-06-02 21:06:52 -0700 | [diff] [blame] | 764 | startFunction(AM_Public, false, "void", "forEach_" + EF->getName(), Args); |
Stephen Hines | 593a894 | 2011-05-10 15:29:50 -0700 | [diff] [blame] | 765 | |
Chris Wailes | c9454af | 2014-06-13 17:25:40 -0700 | [diff] [blame] | 766 | if (InTypes.size() == 1) { |
| 767 | if (InTypes.front() != NULL) { |
| 768 | genTypeCheck(InTypes.front(), "ain"); |
| 769 | } |
| 770 | |
| 771 | } else if (InTypes.size() > 1) { |
| 772 | size_t Index = 0; |
| 773 | for (RSExportForEach::InTypeIter BI = InTypes.begin(), EI = InTypes.end(); |
| 774 | BI != EI; BI++, ++Index) { |
| 775 | |
| 776 | if (*BI != NULL) { |
| 777 | genTypeCheck(*BI, ("ain_" + Ins[Index]->getName()).str().c_str()); |
| 778 | } |
| 779 | } |
Stephen Hines | b5a89fb | 2011-05-17 14:48:02 -0700 | [diff] [blame] | 780 | } |
Chris Wailes | c9454af | 2014-06-13 17:25:40 -0700 | [diff] [blame] | 781 | |
Stephen Hines | b5a89fb | 2011-05-17 14:48:02 -0700 | [diff] [blame] | 782 | if (OET) { |
Jean-Luc Brouillet | 2e205d0 | 2014-06-02 21:06:52 -0700 | [diff] [blame] | 783 | genTypeCheck(OET, "aout"); |
Stephen Hines | b5a89fb | 2011-05-17 14:48:02 -0700 | [diff] [blame] | 784 | } |
| 785 | |
Chris Wailes | c9454af | 2014-06-13 17:25:40 -0700 | [diff] [blame] | 786 | if (Ins.size() == 1 && (EF->hasOut() || EF->hasReturn())) { |
| 787 | mOut.indent() << "Type t0, t1;"; |
| 788 | genPairwiseDimCheck("ain", "aout"); |
| 789 | |
| 790 | } else if (Ins.size() > 1) { |
| 791 | mOut.indent() << "Type t0, t1;"; |
| 792 | |
| 793 | std::string In0Name = "ain_" + Ins[0]->getName().str(); |
| 794 | |
| 795 | for (size_t index = 1; index < Ins.size(); ++index) { |
| 796 | genPairwiseDimCheck(In0Name, "ain_" + Ins[index]->getName().str()); |
| 797 | } |
| 798 | |
| 799 | if (EF->hasOut() || EF->hasReturn()) { |
| 800 | genPairwiseDimCheck(In0Name, "aout"); |
| 801 | } |
Stephen Hines | b5a89fb | 2011-05-17 14:48:02 -0700 | [diff] [blame] | 802 | } |
| 803 | |
| 804 | std::string FieldPackerName = EF->getName() + "_fp"; |
| 805 | if (ERT) { |
Jean-Luc Brouillet | 2e205d0 | 2014-06-02 21:06:52 -0700 | [diff] [blame] | 806 | if (genCreateFieldPacker(ERT, FieldPackerName.c_str())) { |
| 807 | genPackVarOfType(ERT, NULL, FieldPackerName.c_str()); |
Stephen Hines | b5a89fb | 2011-05-17 14:48:02 -0700 | [diff] [blame] | 808 | } |
Stephen Hines | 593a894 | 2011-05-10 15:29:50 -0700 | [diff] [blame] | 809 | } |
Jean-Luc Brouillet | f33e156 | 2014-06-03 17:55:57 -0700 | [diff] [blame] | 810 | mOut.indent() << "forEach(" << RS_EXPORT_FOREACH_INDEX_PREFIX |
| 811 | << EF->getName(); |
Stephen Hines | 593a894 | 2011-05-10 15:29:50 -0700 | [diff] [blame] | 812 | |
Chris Wailes | c9454af | 2014-06-13 17:25:40 -0700 | [diff] [blame] | 813 | if (Ins.size() == 1) { |
Jean-Luc Brouillet | f33e156 | 2014-06-03 17:55:57 -0700 | [diff] [blame] | 814 | mOut << ", ain"; |
Chris Wailes | c9454af | 2014-06-13 17:25:40 -0700 | [diff] [blame] | 815 | } else if (Ins.size() > 1) { |
| 816 | mOut << ", new Allocation[]{ain_" << Ins[0]->getName().str(); |
| 817 | |
| 818 | for (size_t index = 1; index < Ins.size(); ++index) { |
| 819 | mOut << ", ain_" << Ins[index]->getName().str(); |
| 820 | } |
| 821 | |
| 822 | mOut << "}"; |
| 823 | |
| 824 | } else { |
| 825 | mOut << ", (Allocation) null"; |
| 826 | } |
Stephen Hines | b5a89fb | 2011-05-17 14:48:02 -0700 | [diff] [blame] | 827 | |
Stephen Hines | 9ca96e7 | 2012-09-13 16:57:06 -0700 | [diff] [blame] | 828 | if (EF->hasOut() || EF->hasReturn()) |
Jean-Luc Brouillet | f33e156 | 2014-06-03 17:55:57 -0700 | [diff] [blame] | 829 | mOut << ", aout"; |
Stephen Hines | b5a89fb | 2011-05-17 14:48:02 -0700 | [diff] [blame] | 830 | else |
Jean-Luc Brouillet | f33e156 | 2014-06-03 17:55:57 -0700 | [diff] [blame] | 831 | mOut << ", null"; |
Stephen Hines | b5a89fb | 2011-05-17 14:48:02 -0700 | [diff] [blame] | 832 | |
| 833 | if (EF->hasUsrData()) |
Jean-Luc Brouillet | f33e156 | 2014-06-03 17:55:57 -0700 | [diff] [blame] | 834 | mOut << ", " << FieldPackerName; |
Stephen Hines | b5a89fb | 2011-05-17 14:48:02 -0700 | [diff] [blame] | 835 | else |
Jean-Luc Brouillet | f33e156 | 2014-06-03 17:55:57 -0700 | [diff] [blame] | 836 | mOut << ", null"; |
Stephen Hines | b5a89fb | 2011-05-17 14:48:02 -0700 | [diff] [blame] | 837 | |
Stephen Hines | 5097474 | 2013-02-12 22:11:08 -0800 | [diff] [blame] | 838 | if (mRSContext->getTargetAPI() >= SLANG_JB_MR2_TARGET_API) { |
Jean-Luc Brouillet | f33e156 | 2014-06-03 17:55:57 -0700 | [diff] [blame] | 839 | mOut << ", sc);\n"; |
Stephen Hines | 5097474 | 2013-02-12 22:11:08 -0800 | [diff] [blame] | 840 | } else { |
Jean-Luc Brouillet | f33e156 | 2014-06-03 17:55:57 -0700 | [diff] [blame] | 841 | mOut << ");\n"; |
Stephen Hines | 5097474 | 2013-02-12 22:11:08 -0800 | [diff] [blame] | 842 | } |
Stephen Hines | 593a894 | 2011-05-10 15:29:50 -0700 | [diff] [blame] | 843 | |
Jean-Luc Brouillet | 2e205d0 | 2014-06-02 21:06:52 -0700 | [diff] [blame] | 844 | endFunction(); |
Stephen Hines | 593a894 | 2011-05-10 15:29:50 -0700 | [diff] [blame] | 845 | } |
| 846 | |
Jean-Luc Brouillet | 2e205d0 | 2014-06-02 21:06:52 -0700 | [diff] [blame] | 847 | void RSReflectionJava::genTypeInstanceFromPointer(const RSExportType *ET) { |
Stephen Hines | 48b72bf | 2011-06-10 15:37:27 -0700 | [diff] [blame] | 848 | if (ET->getClass() == RSExportType::ExportClassPointer) { |
Stephen Hines | 9ca96e7 | 2012-09-13 16:57:06 -0700 | [diff] [blame] | 849 | // For pointer parameters to original forEach kernels. |
Stephen Hines | 48b72bf | 2011-06-10 15:37:27 -0700 | [diff] [blame] | 850 | const RSExportPointerType *EPT = |
Jean-Luc Brouillet | 2ce118e | 2014-05-27 17:41:22 -0700 | [diff] [blame] | 851 | static_cast<const RSExportPointerType *>(ET); |
Jean-Luc Brouillet | 2e205d0 | 2014-06-02 21:06:52 -0700 | [diff] [blame] | 852 | genTypeInstance(EPT->getPointeeType()); |
Stephen Hines | 9ca96e7 | 2012-09-13 16:57:06 -0700 | [diff] [blame] | 853 | } else { |
| 854 | // For handling pass-by-value kernel parameters. |
Jean-Luc Brouillet | 2e205d0 | 2014-06-02 21:06:52 -0700 | [diff] [blame] | 855 | genTypeInstance(ET); |
Stephen Hines | a6b5414 | 2012-04-09 18:25:08 -0700 | [diff] [blame] | 856 | } |
| 857 | } |
Stephen Hines | 48b72bf | 2011-06-10 15:37:27 -0700 | [diff] [blame] | 858 | |
Jean-Luc Brouillet | 2e205d0 | 2014-06-02 21:06:52 -0700 | [diff] [blame] | 859 | void RSReflectionJava::genTypeInstance(const RSExportType *ET) { |
Stephen Hines | a6b5414 | 2012-04-09 18:25:08 -0700 | [diff] [blame] | 860 | switch (ET->getClass()) { |
Jean-Luc Brouillet | 2ce118e | 2014-05-27 17:41:22 -0700 | [diff] [blame] | 861 | case RSExportType::ExportClassPrimitive: |
| 862 | case RSExportType::ExportClassVector: |
| 863 | case RSExportType::ExportClassConstantArray: { |
| 864 | std::string TypeName = ET->getElementName(); |
Jean-Luc Brouillet | 2e205d0 | 2014-06-02 21:06:52 -0700 | [diff] [blame] | 865 | if (addTypeNameForElement(TypeName)) { |
Jean-Luc Brouillet | f33e156 | 2014-06-03 17:55:57 -0700 | [diff] [blame] | 866 | mOut.indent() << RS_ELEM_PREFIX << TypeName << " = Element." << TypeName |
| 867 | << "(rs);\n"; |
Stephen Hines | 48b72bf | 2011-06-10 15:37:27 -0700 | [diff] [blame] | 868 | } |
Jean-Luc Brouillet | 2ce118e | 2014-05-27 17:41:22 -0700 | [diff] [blame] | 869 | break; |
| 870 | } |
Stephen Hines | a6b5414 | 2012-04-09 18:25:08 -0700 | [diff] [blame] | 871 | |
Jean-Luc Brouillet | 2ce118e | 2014-05-27 17:41:22 -0700 | [diff] [blame] | 872 | case RSExportType::ExportClassRecord: { |
| 873 | std::string ClassName = ET->getElementName(); |
Jean-Luc Brouillet | 2e205d0 | 2014-06-02 21:06:52 -0700 | [diff] [blame] | 874 | if (addTypeNameForElement(ClassName)) { |
Jean-Luc Brouillet | f33e156 | 2014-06-03 17:55:57 -0700 | [diff] [blame] | 875 | mOut.indent() << RS_ELEM_PREFIX << ClassName << " = " << ClassName |
| 876 | << ".createElement(rs);\n"; |
Stephen Hines | a6b5414 | 2012-04-09 18:25:08 -0700 | [diff] [blame] | 877 | } |
Jean-Luc Brouillet | 2ce118e | 2014-05-27 17:41:22 -0700 | [diff] [blame] | 878 | break; |
| 879 | } |
Stephen Hines | a6b5414 | 2012-04-09 18:25:08 -0700 | [diff] [blame] | 880 | |
Jean-Luc Brouillet | 2ce118e | 2014-05-27 17:41:22 -0700 | [diff] [blame] | 881 | default: |
| 882 | break; |
Stephen Hines | 48b72bf | 2011-06-10 15:37:27 -0700 | [diff] [blame] | 883 | } |
| 884 | } |
| 885 | |
Jean-Luc Brouillet | 2e205d0 | 2014-06-02 21:06:52 -0700 | [diff] [blame] | 886 | void RSReflectionJava::genFieldPackerInstance(const RSExportType *ET) { |
Stephen Hines | 1f6c331 | 2012-07-03 17:23:33 -0700 | [diff] [blame] | 887 | switch (ET->getClass()) { |
Jean-Luc Brouillet | 2ce118e | 2014-05-27 17:41:22 -0700 | [diff] [blame] | 888 | case RSExportType::ExportClassPrimitive: |
| 889 | case RSExportType::ExportClassVector: |
| 890 | case RSExportType::ExportClassConstantArray: |
| 891 | case RSExportType::ExportClassRecord: { |
| 892 | std::string TypeName = ET->getElementName(); |
Jean-Luc Brouillet | 2e205d0 | 2014-06-02 21:06:52 -0700 | [diff] [blame] | 893 | addTypeNameForFieldPacker(TypeName); |
Jean-Luc Brouillet | 2ce118e | 2014-05-27 17:41:22 -0700 | [diff] [blame] | 894 | break; |
| 895 | } |
Stephen Hines | 1f6c331 | 2012-07-03 17:23:33 -0700 | [diff] [blame] | 896 | |
Jean-Luc Brouillet | 2ce118e | 2014-05-27 17:41:22 -0700 | [diff] [blame] | 897 | default: |
| 898 | break; |
Stephen Hines | 1f6c331 | 2012-07-03 17:23:33 -0700 | [diff] [blame] | 899 | } |
| 900 | } |
| 901 | |
Jean-Luc Brouillet | 2e205d0 | 2014-06-02 21:06:52 -0700 | [diff] [blame] | 902 | void RSReflectionJava::genTypeCheck(const RSExportType *ET, |
Jean-Luc Brouillet | 602def7 | 2014-05-27 16:11:37 -0700 | [diff] [blame] | 903 | const char *VarName) { |
Jean-Luc Brouillet | f33e156 | 2014-06-03 17:55:57 -0700 | [diff] [blame] | 904 | mOut.indent() << "// check " << VarName << "\n"; |
Stephen Hines | 48b72bf | 2011-06-10 15:37:27 -0700 | [diff] [blame] | 905 | |
| 906 | if (ET->getClass() == RSExportType::ExportClassPointer) { |
| 907 | const RSExportPointerType *EPT = |
Jean-Luc Brouillet | 2ce118e | 2014-05-27 17:41:22 -0700 | [diff] [blame] | 908 | static_cast<const RSExportPointerType *>(ET); |
Stephen Hines | 48b72bf | 2011-06-10 15:37:27 -0700 | [diff] [blame] | 909 | ET = EPT->getPointeeType(); |
| 910 | } |
| 911 | |
| 912 | std::string TypeName; |
| 913 | |
| 914 | switch (ET->getClass()) { |
Jean-Luc Brouillet | 2ce118e | 2014-05-27 17:41:22 -0700 | [diff] [blame] | 915 | case RSExportType::ExportClassPrimitive: |
| 916 | case RSExportType::ExportClassVector: |
| 917 | case RSExportType::ExportClassRecord: { |
| 918 | TypeName = ET->getElementName(); |
| 919 | break; |
| 920 | } |
Stephen Hines | 48b72bf | 2011-06-10 15:37:27 -0700 | [diff] [blame] | 921 | |
Jean-Luc Brouillet | 2ce118e | 2014-05-27 17:41:22 -0700 | [diff] [blame] | 922 | default: |
| 923 | break; |
Stephen Hines | 48b72bf | 2011-06-10 15:37:27 -0700 | [diff] [blame] | 924 | } |
| 925 | |
| 926 | if (!TypeName.empty()) { |
Jean-Luc Brouillet | f33e156 | 2014-06-03 17:55:57 -0700 | [diff] [blame] | 927 | mOut.indent() << "if (!" << VarName |
| 928 | << ".getType().getElement().isCompatible(" RS_ELEM_PREFIX |
| 929 | << TypeName << ")) {\n"; |
| 930 | mOut.indent() << " throw new RSRuntimeException(\"Type mismatch with " |
| 931 | << TypeName << "!\");\n"; |
| 932 | mOut.indent() << "}\n"; |
Stephen Hines | 48b72bf | 2011-06-10 15:37:27 -0700 | [diff] [blame] | 933 | } |
Stephen Hines | b5a89fb | 2011-05-17 14:48:02 -0700 | [diff] [blame] | 934 | } |
| 935 | |
Jean-Luc Brouillet | 2e205d0 | 2014-06-02 21:06:52 -0700 | [diff] [blame] | 936 | void RSReflectionJava::genPrimitiveTypeExportVariable(const RSExportVar *EV) { |
Jean-Luc Brouillet | 602def7 | 2014-05-27 16:11:37 -0700 | [diff] [blame] | 937 | slangAssert( |
| 938 | (EV->getType()->getClass() == RSExportType::ExportClassPrimitive) && |
| 939 | "Variable should be type of primitive here"); |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 940 | |
| 941 | const RSExportPrimitiveType *EPT = |
Jean-Luc Brouillet | 2ce118e | 2014-05-27 17:41:22 -0700 | [diff] [blame] | 942 | static_cast<const RSExportPrimitiveType *>(EV->getType()); |
Stephen Hines | 0d26cef | 2012-05-01 19:23:01 -0700 | [diff] [blame] | 943 | std::string TypeName = GetTypeName(EPT); |
| 944 | std::string VarName = EV->getName(); |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 945 | |
Jean-Luc Brouillet | 2e205d0 | 2014-06-02 21:06:52 -0700 | [diff] [blame] | 946 | genPrivateExportVariable(TypeName, EV->getName()); |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 947 | |
Stephen Hines | 5d67178 | 2012-01-31 19:32:04 -0800 | [diff] [blame] | 948 | if (EV->isConst()) { |
Jean-Luc Brouillet | f33e156 | 2014-06-03 17:55:57 -0700 | [diff] [blame] | 949 | mOut.indent() << "public final static " << TypeName |
| 950 | << " " RS_EXPORT_VAR_CONST_PREFIX << VarName << " = "; |
Stephen Hines | 5d67178 | 2012-01-31 19:32:04 -0800 | [diff] [blame] | 951 | const clang::APValue &Val = EV->getInit(); |
Jean-Luc Brouillet | efcff10 | 2014-06-03 16:13:51 -0700 | [diff] [blame] | 952 | genInitValue(Val, EPT->getType() == DataTypeBoolean); |
Jean-Luc Brouillet | f33e156 | 2014-06-03 17:55:57 -0700 | [diff] [blame] | 953 | mOut << ";\n"; |
Stephen Hines | 5d67178 | 2012-01-31 19:32:04 -0800 | [diff] [blame] | 954 | } else { |
| 955 | // set_*() |
Stephen Hines | 1f6c331 | 2012-07-03 17:23:33 -0700 | [diff] [blame] | 956 | // This must remain synchronized, since multiple Dalvik threads may |
| 957 | // be calling setters. |
Jean-Luc Brouillet | 2e205d0 | 2014-06-02 21:06:52 -0700 | [diff] [blame] | 958 | startFunction(AM_PublicSynchronized, false, "void", "set_" + VarName, 1, |
| 959 | TypeName.c_str(), "v"); |
Stephen Hines | bcae1fe | 2012-09-25 19:17:33 -0700 | [diff] [blame] | 960 | if ((EPT->getSize() < 4) || EV->isUnsigned()) { |
Stephen Hines | 1f6c331 | 2012-07-03 17:23:33 -0700 | [diff] [blame] | 961 | // We create/cache a per-type FieldPacker. This allows us to reuse the |
| 962 | // validation logic (for catching negative inputs from Dalvik, as well |
| 963 | // as inputs that are too large to be represented in the unsigned type). |
Stephen Hines | bcae1fe | 2012-09-25 19:17:33 -0700 | [diff] [blame] | 964 | // Sub-integer types are also handled specially here, so that we don't |
| 965 | // overwrite bytes accidentally. |
Stephen Hines | 1f6c331 | 2012-07-03 17:23:33 -0700 | [diff] [blame] | 966 | std::string ElemName = EPT->getElementName(); |
| 967 | std::string FPName; |
| 968 | FPName = RS_FP_PREFIX + ElemName; |
Jean-Luc Brouillet | f33e156 | 2014-06-03 17:55:57 -0700 | [diff] [blame] | 969 | mOut.indent() << "if (" << FPName << "!= null) {\n"; |
| 970 | mOut.increaseIndent(); |
| 971 | mOut.indent() << FPName << ".reset();\n"; |
| 972 | mOut.decreaseIndent(); |
| 973 | mOut.indent() << "} else {\n"; |
| 974 | mOut.increaseIndent(); |
| 975 | mOut.indent() << FPName << " = new FieldPacker(" << EPT->getSize() |
| 976 | << ");\n"; |
| 977 | mOut.decreaseIndent(); |
| 978 | mOut.indent() << "}\n"; |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 979 | |
Jean-Luc Brouillet | 2e205d0 | 2014-06-02 21:06:52 -0700 | [diff] [blame] | 980 | genPackVarOfType(EPT, "v", FPName.c_str()); |
Jean-Luc Brouillet | f33e156 | 2014-06-03 17:55:57 -0700 | [diff] [blame] | 981 | mOut.indent() << "setVar(" << RS_EXPORT_VAR_INDEX_PREFIX << VarName |
| 982 | << ", " << FPName << ");\n"; |
Stephen Hines | 1f6c331 | 2012-07-03 17:23:33 -0700 | [diff] [blame] | 983 | } else { |
Jean-Luc Brouillet | f33e156 | 2014-06-03 17:55:57 -0700 | [diff] [blame] | 984 | mOut.indent() << "setVar(" << RS_EXPORT_VAR_INDEX_PREFIX << VarName |
| 985 | << ", v);\n"; |
Stephen Hines | 1f6c331 | 2012-07-03 17:23:33 -0700 | [diff] [blame] | 986 | } |
| 987 | |
| 988 | // Dalvik update comes last, since the input may be invalid (and hence |
| 989 | // throw an exception). |
Jean-Luc Brouillet | f33e156 | 2014-06-03 17:55:57 -0700 | [diff] [blame] | 990 | mOut.indent() << RS_EXPORT_VAR_PREFIX << VarName << " = v;\n"; |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 991 | |
Jean-Luc Brouillet | 2e205d0 | 2014-06-02 21:06:52 -0700 | [diff] [blame] | 992 | endFunction(); |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 993 | } |
Shih-wei Liao | 9e86e19 | 2010-06-18 20:42:28 -0700 | [diff] [blame] | 994 | |
Jean-Luc Brouillet | 2e205d0 | 2014-06-02 21:06:52 -0700 | [diff] [blame] | 995 | genGetExportVariable(TypeName, VarName); |
| 996 | genGetFieldID(VarName); |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 997 | } |
| 998 | |
Jean-Luc Brouillet | efcff10 | 2014-06-03 16:13:51 -0700 | [diff] [blame] | 999 | void RSReflectionJava::genInitValue(const clang::APValue &Val, bool asBool) { |
| 1000 | switch (Val.getKind()) { |
| 1001 | case clang::APValue::Int: { |
| 1002 | llvm::APInt api = Val.getInt(); |
| 1003 | if (asBool) { |
Jean-Luc Brouillet | f33e156 | 2014-06-03 17:55:57 -0700 | [diff] [blame] | 1004 | mOut << ((api.getSExtValue() == 0) ? "false" : "true"); |
Jean-Luc Brouillet | efcff10 | 2014-06-03 16:13:51 -0700 | [diff] [blame] | 1005 | } else { |
| 1006 | // TODO: Handle unsigned correctly |
Jean-Luc Brouillet | f33e156 | 2014-06-03 17:55:57 -0700 | [diff] [blame] | 1007 | mOut << api.getSExtValue(); |
Jean-Luc Brouillet | efcff10 | 2014-06-03 16:13:51 -0700 | [diff] [blame] | 1008 | if (api.getBitWidth() > 32) { |
Jean-Luc Brouillet | f33e156 | 2014-06-03 17:55:57 -0700 | [diff] [blame] | 1009 | mOut << "L"; |
Jean-Luc Brouillet | efcff10 | 2014-06-03 16:13:51 -0700 | [diff] [blame] | 1010 | } |
| 1011 | } |
| 1012 | break; |
| 1013 | } |
| 1014 | |
| 1015 | case clang::APValue::Float: { |
| 1016 | llvm::APFloat apf = Val.getFloat(); |
| 1017 | llvm::SmallString<30> s; |
| 1018 | apf.toString(s); |
Jean-Luc Brouillet | f33e156 | 2014-06-03 17:55:57 -0700 | [diff] [blame] | 1019 | mOut << s.c_str(); |
Jean-Luc Brouillet | efcff10 | 2014-06-03 16:13:51 -0700 | [diff] [blame] | 1020 | if (&apf.getSemantics() == &llvm::APFloat::IEEEsingle) { |
| 1021 | if (s.count('.') == 0) { |
Jean-Luc Brouillet | f33e156 | 2014-06-03 17:55:57 -0700 | [diff] [blame] | 1022 | mOut << ".f"; |
Jean-Luc Brouillet | efcff10 | 2014-06-03 16:13:51 -0700 | [diff] [blame] | 1023 | } else { |
Jean-Luc Brouillet | f33e156 | 2014-06-03 17:55:57 -0700 | [diff] [blame] | 1024 | mOut << "f"; |
Jean-Luc Brouillet | efcff10 | 2014-06-03 16:13:51 -0700 | [diff] [blame] | 1025 | } |
| 1026 | } |
| 1027 | break; |
| 1028 | } |
| 1029 | |
| 1030 | case clang::APValue::ComplexInt: |
| 1031 | case clang::APValue::ComplexFloat: |
| 1032 | case clang::APValue::LValue: |
| 1033 | case clang::APValue::Vector: { |
| 1034 | slangAssert(false && "Primitive type cannot have such kind of initializer"); |
| 1035 | break; |
| 1036 | } |
| 1037 | |
| 1038 | default: { slangAssert(false && "Unknown kind of initializer"); } |
| 1039 | } |
| 1040 | } |
| 1041 | |
Jean-Luc Brouillet | 2e205d0 | 2014-06-02 21:06:52 -0700 | [diff] [blame] | 1042 | void RSReflectionJava::genPointerTypeExportVariable(const RSExportVar *EV) { |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 1043 | const RSExportType *ET = EV->getType(); |
| 1044 | const RSExportType *PointeeType; |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 1045 | |
Stephen Hines | 6e6578a | 2011-02-07 18:05:48 -0800 | [diff] [blame] | 1046 | slangAssert((ET->getClass() == RSExportType::ExportClassPointer) && |
| 1047 | "Variable should be type of pointer here"); |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 1048 | |
Jean-Luc Brouillet | 2ce118e | 2014-05-27 17:41:22 -0700 | [diff] [blame] | 1049 | PointeeType = static_cast<const RSExportPointerType *>(ET)->getPointeeType(); |
Stephen Hines | 0d26cef | 2012-05-01 19:23:01 -0700 | [diff] [blame] | 1050 | std::string TypeName = GetTypeName(ET); |
| 1051 | std::string VarName = EV->getName(); |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 1052 | |
Jean-Luc Brouillet | 2e205d0 | 2014-06-02 21:06:52 -0700 | [diff] [blame] | 1053 | genPrivateExportVariable(TypeName, VarName); |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 1054 | |
Zonr Chang | 89273bd | 2010-10-14 20:57:38 +0800 | [diff] [blame] | 1055 | // bind_*() |
Jean-Luc Brouillet | 2e205d0 | 2014-06-02 21:06:52 -0700 | [diff] [blame] | 1056 | startFunction(AM_Public, false, "void", "bind_" + VarName, 1, |
| 1057 | TypeName.c_str(), "v"); |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 1058 | |
Jean-Luc Brouillet | f33e156 | 2014-06-03 17:55:57 -0700 | [diff] [blame] | 1059 | mOut.indent() << RS_EXPORT_VAR_PREFIX << VarName << " = v;\n"; |
| 1060 | mOut.indent() << "if (v == null) bindAllocation(null, " |
| 1061 | << RS_EXPORT_VAR_INDEX_PREFIX << VarName << ");\n"; |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 1062 | |
Jean-Luc Brouillet | f33e156 | 2014-06-03 17:55:57 -0700 | [diff] [blame] | 1063 | if (PointeeType->getClass() == RSExportType::ExportClassRecord) { |
| 1064 | mOut.indent() << "else bindAllocation(v.getAllocation(), " |
| 1065 | << RS_EXPORT_VAR_INDEX_PREFIX << VarName << ");\n"; |
| 1066 | } else { |
| 1067 | mOut.indent() << "else bindAllocation(v, " << RS_EXPORT_VAR_INDEX_PREFIX |
| 1068 | << VarName << ");\n"; |
| 1069 | } |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 1070 | |
Jean-Luc Brouillet | 2e205d0 | 2014-06-02 21:06:52 -0700 | [diff] [blame] | 1071 | endFunction(); |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 1072 | |
Jean-Luc Brouillet | 2e205d0 | 2014-06-02 21:06:52 -0700 | [diff] [blame] | 1073 | genGetExportVariable(TypeName, VarName); |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 1074 | } |
| 1075 | |
Jean-Luc Brouillet | 2e205d0 | 2014-06-02 21:06:52 -0700 | [diff] [blame] | 1076 | void RSReflectionJava::genVectorTypeExportVariable(const RSExportVar *EV) { |
Stephen Hines | 6e6578a | 2011-02-07 18:05:48 -0800 | [diff] [blame] | 1077 | slangAssert((EV->getType()->getClass() == RSExportType::ExportClassVector) && |
| 1078 | "Variable should be type of vector here"); |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 1079 | |
Stephen Hines | 0d26cef | 2012-05-01 19:23:01 -0700 | [diff] [blame] | 1080 | std::string TypeName = GetTypeName(EV->getType()); |
| 1081 | std::string VarName = EV->getName(); |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 1082 | |
Jean-Luc Brouillet | 2e205d0 | 2014-06-02 21:06:52 -0700 | [diff] [blame] | 1083 | genPrivateExportVariable(TypeName, VarName); |
| 1084 | genSetExportVariable(TypeName, EV); |
| 1085 | genGetExportVariable(TypeName, VarName); |
| 1086 | genGetFieldID(VarName); |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 1087 | } |
| 1088 | |
Jean-Luc Brouillet | 2e205d0 | 2014-06-02 21:06:52 -0700 | [diff] [blame] | 1089 | void RSReflectionJava::genMatrixTypeExportVariable(const RSExportVar *EV) { |
Stephen Hines | 6e6578a | 2011-02-07 18:05:48 -0800 | [diff] [blame] | 1090 | slangAssert((EV->getType()->getClass() == RSExportType::ExportClassMatrix) && |
| 1091 | "Variable should be type of matrix here"); |
Zonr Chang | 92b344a | 2010-10-05 20:39:03 +0800 | [diff] [blame] | 1092 | |
Jean-Luc Brouillet | 2ce118e | 2014-05-27 17:41:22 -0700 | [diff] [blame] | 1093 | const RSExportType *ET = EV->getType(); |
Stephen Hines | 0d26cef | 2012-05-01 19:23:01 -0700 | [diff] [blame] | 1094 | std::string TypeName = GetTypeName(ET); |
| 1095 | std::string VarName = EV->getName(); |
Zonr Chang | 92b344a | 2010-10-05 20:39:03 +0800 | [diff] [blame] | 1096 | |
Jean-Luc Brouillet | 2e205d0 | 2014-06-02 21:06:52 -0700 | [diff] [blame] | 1097 | genPrivateExportVariable(TypeName, VarName); |
Zonr Chang | 92b344a | 2010-10-05 20:39:03 +0800 | [diff] [blame] | 1098 | |
| 1099 | // set_*() |
| 1100 | if (!EV->isConst()) { |
Stephen Hines | 0d26cef | 2012-05-01 19:23:01 -0700 | [diff] [blame] | 1101 | const char *FieldPackerName = "fp"; |
Jean-Luc Brouillet | 2e205d0 | 2014-06-02 21:06:52 -0700 | [diff] [blame] | 1102 | startFunction(AM_PublicSynchronized, false, "void", "set_" + VarName, 1, |
| 1103 | TypeName.c_str(), "v"); |
Jean-Luc Brouillet | f33e156 | 2014-06-03 17:55:57 -0700 | [diff] [blame] | 1104 | mOut.indent() << RS_EXPORT_VAR_PREFIX << VarName << " = v;\n"; |
Zonr Chang | 92b344a | 2010-10-05 20:39:03 +0800 | [diff] [blame] | 1105 | |
Jean-Luc Brouillet | 2e205d0 | 2014-06-02 21:06:52 -0700 | [diff] [blame] | 1106 | if (genCreateFieldPacker(ET, FieldPackerName)) |
| 1107 | genPackVarOfType(ET, "v", FieldPackerName); |
Jean-Luc Brouillet | f33e156 | 2014-06-03 17:55:57 -0700 | [diff] [blame] | 1108 | mOut.indent() << "setVar(" RS_EXPORT_VAR_INDEX_PREFIX << VarName << ", " |
| 1109 | << FieldPackerName << ");\n"; |
Zonr Chang | 92b344a | 2010-10-05 20:39:03 +0800 | [diff] [blame] | 1110 | |
Jean-Luc Brouillet | 2e205d0 | 2014-06-02 21:06:52 -0700 | [diff] [blame] | 1111 | endFunction(); |
Zonr Chang | 92b344a | 2010-10-05 20:39:03 +0800 | [diff] [blame] | 1112 | } |
| 1113 | |
Jean-Luc Brouillet | 2e205d0 | 2014-06-02 21:06:52 -0700 | [diff] [blame] | 1114 | genGetExportVariable(TypeName, VarName); |
| 1115 | genGetFieldID(VarName); |
Zonr Chang | 92b344a | 2010-10-05 20:39:03 +0800 | [diff] [blame] | 1116 | } |
| 1117 | |
Jean-Luc Brouillet | 602def7 | 2014-05-27 16:11:37 -0700 | [diff] [blame] | 1118 | void |
Jean-Luc Brouillet | 2e205d0 | 2014-06-02 21:06:52 -0700 | [diff] [blame] | 1119 | RSReflectionJava::genConstantArrayTypeExportVariable(const RSExportVar *EV) { |
Jean-Luc Brouillet | 602def7 | 2014-05-27 16:11:37 -0700 | [diff] [blame] | 1120 | slangAssert( |
| 1121 | (EV->getType()->getClass() == RSExportType::ExportClassConstantArray) && |
| 1122 | "Variable should be type of constant array here"); |
Zonr Chang | 2e1dba6 | 2010-10-05 22:20:11 +0800 | [diff] [blame] | 1123 | |
Stephen Hines | 0d26cef | 2012-05-01 19:23:01 -0700 | [diff] [blame] | 1124 | std::string TypeName = GetTypeName(EV->getType()); |
| 1125 | std::string VarName = EV->getName(); |
Zonr Chang | 2e1dba6 | 2010-10-05 22:20:11 +0800 | [diff] [blame] | 1126 | |
Jean-Luc Brouillet | 2e205d0 | 2014-06-02 21:06:52 -0700 | [diff] [blame] | 1127 | genPrivateExportVariable(TypeName, VarName); |
| 1128 | genSetExportVariable(TypeName, EV); |
| 1129 | genGetExportVariable(TypeName, VarName); |
| 1130 | genGetFieldID(VarName); |
Zonr Chang | 2e1dba6 | 2010-10-05 22:20:11 +0800 | [diff] [blame] | 1131 | } |
| 1132 | |
Jean-Luc Brouillet | 2e205d0 | 2014-06-02 21:06:52 -0700 | [diff] [blame] | 1133 | void RSReflectionJava::genRecordTypeExportVariable(const RSExportVar *EV) { |
Stephen Hines | 6e6578a | 2011-02-07 18:05:48 -0800 | [diff] [blame] | 1134 | slangAssert((EV->getType()->getClass() == RSExportType::ExportClassRecord) && |
| 1135 | "Variable should be type of struct here"); |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 1136 | |
Stephen Hines | 0d26cef | 2012-05-01 19:23:01 -0700 | [diff] [blame] | 1137 | std::string TypeName = GetTypeName(EV->getType()); |
| 1138 | std::string VarName = EV->getName(); |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 1139 | |
Jean-Luc Brouillet | 2e205d0 | 2014-06-02 21:06:52 -0700 | [diff] [blame] | 1140 | genPrivateExportVariable(TypeName, VarName); |
| 1141 | genSetExportVariable(TypeName, EV); |
| 1142 | genGetExportVariable(TypeName, VarName); |
| 1143 | genGetFieldID(VarName); |
Stephen Hines | 0d26cef | 2012-05-01 19:23:01 -0700 | [diff] [blame] | 1144 | } |
| 1145 | |
Jean-Luc Brouillet | 2e205d0 | 2014-06-02 21:06:52 -0700 | [diff] [blame] | 1146 | void RSReflectionJava::genPrivateExportVariable(const std::string &TypeName, |
Jean-Luc Brouillet | 602def7 | 2014-05-27 16:11:37 -0700 | [diff] [blame] | 1147 | const std::string &VarName) { |
Jean-Luc Brouillet | f33e156 | 2014-06-03 17:55:57 -0700 | [diff] [blame] | 1148 | mOut.indent() << "private " << TypeName << " " << RS_EXPORT_VAR_PREFIX |
| 1149 | << VarName << ";\n"; |
Stephen Hines | 0d26cef | 2012-05-01 19:23:01 -0700 | [diff] [blame] | 1150 | } |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 1151 | |
Jean-Luc Brouillet | 2e205d0 | 2014-06-02 21:06:52 -0700 | [diff] [blame] | 1152 | void RSReflectionJava::genSetExportVariable(const std::string &TypeName, |
Jean-Luc Brouillet | 602def7 | 2014-05-27 16:11:37 -0700 | [diff] [blame] | 1153 | const RSExportVar *EV) { |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 1154 | if (!EV->isConst()) { |
Stephen Hines | 0d26cef | 2012-05-01 19:23:01 -0700 | [diff] [blame] | 1155 | const char *FieldPackerName = "fp"; |
| 1156 | std::string VarName = EV->getName(); |
| 1157 | const RSExportType *ET = EV->getType(); |
Jean-Luc Brouillet | 2e205d0 | 2014-06-02 21:06:52 -0700 | [diff] [blame] | 1158 | startFunction(AM_PublicSynchronized, false, "void", "set_" + VarName, 1, |
| 1159 | TypeName.c_str(), "v"); |
Jean-Luc Brouillet | f33e156 | 2014-06-03 17:55:57 -0700 | [diff] [blame] | 1160 | mOut.indent() << RS_EXPORT_VAR_PREFIX << VarName << " = v;\n"; |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 1161 | |
Jean-Luc Brouillet | 2e205d0 | 2014-06-02 21:06:52 -0700 | [diff] [blame] | 1162 | if (genCreateFieldPacker(ET, FieldPackerName)) |
| 1163 | genPackVarOfType(ET, "v", FieldPackerName); |
Stephen Hines | a6b5414 | 2012-04-09 18:25:08 -0700 | [diff] [blame] | 1164 | |
| 1165 | if (mRSContext->getTargetAPI() < SLANG_JB_TARGET_API) { |
| 1166 | // Legacy apps must use the old setVar() without Element/dim components. |
Jean-Luc Brouillet | f33e156 | 2014-06-03 17:55:57 -0700 | [diff] [blame] | 1167 | mOut.indent() << "setVar(" << RS_EXPORT_VAR_INDEX_PREFIX << VarName |
| 1168 | << ", " << FieldPackerName << ");\n"; |
Stephen Hines | a6b5414 | 2012-04-09 18:25:08 -0700 | [diff] [blame] | 1169 | } else { |
| 1170 | // We only have support for one-dimensional array reflection today, |
| 1171 | // but the entry point (i.e. setVar()) takes an array of dimensions. |
Jean-Luc Brouillet | f33e156 | 2014-06-03 17:55:57 -0700 | [diff] [blame] | 1172 | mOut.indent() << "int []__dimArr = new int[1];\n"; |
| 1173 | mOut.indent() << "__dimArr[0] = " << ET->getSize() << ";\n"; |
| 1174 | mOut.indent() << "setVar(" << RS_EXPORT_VAR_INDEX_PREFIX << VarName |
| 1175 | << ", " << FieldPackerName << ", " << RS_ELEM_PREFIX |
| 1176 | << ET->getElementName() << ", __dimArr);\n"; |
Stephen Hines | a6b5414 | 2012-04-09 18:25:08 -0700 | [diff] [blame] | 1177 | } |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 1178 | |
Jean-Luc Brouillet | 2e205d0 | 2014-06-02 21:06:52 -0700 | [diff] [blame] | 1179 | endFunction(); |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 1180 | } |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 1181 | } |
| 1182 | |
Jean-Luc Brouillet | 2e205d0 | 2014-06-02 21:06:52 -0700 | [diff] [blame] | 1183 | void RSReflectionJava::genGetExportVariable(const std::string &TypeName, |
Jean-Luc Brouillet | 602def7 | 2014-05-27 16:11:37 -0700 | [diff] [blame] | 1184 | const std::string &VarName) { |
Jean-Luc Brouillet | 2e205d0 | 2014-06-02 21:06:52 -0700 | [diff] [blame] | 1185 | startFunction(AM_Public, false, TypeName.c_str(), "get_" + VarName, 0); |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 1186 | |
Jean-Luc Brouillet | f33e156 | 2014-06-03 17:55:57 -0700 | [diff] [blame] | 1187 | mOut.indent() << "return " << RS_EXPORT_VAR_PREFIX << VarName << ";\n"; |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 1188 | |
Jean-Luc Brouillet | 2e205d0 | 2014-06-02 21:06:52 -0700 | [diff] [blame] | 1189 | endFunction(); |
Stephen Hines | 28d60bc | 2012-10-15 15:54:16 -0700 | [diff] [blame] | 1190 | } |
| 1191 | |
Jean-Luc Brouillet | 2e205d0 | 2014-06-02 21:06:52 -0700 | [diff] [blame] | 1192 | void RSReflectionJava::genGetFieldID(const std::string &VarName) { |
Stephen Hines | 28d60bc | 2012-10-15 15:54:16 -0700 | [diff] [blame] | 1193 | // We only generate getFieldID_*() for non-Pointer (bind) types. |
Tim Murray | b81a993 | 2012-10-10 15:56:02 -0700 | [diff] [blame] | 1194 | if (mRSContext->getTargetAPI() >= SLANG_JB_MR1_TARGET_API) { |
Jean-Luc Brouillet | 2e205d0 | 2014-06-02 21:06:52 -0700 | [diff] [blame] | 1195 | startFunction(AM_Public, false, "Script.FieldID", "getFieldID_" + VarName, |
| 1196 | 0); |
Tim Murray | b81a993 | 2012-10-10 15:56:02 -0700 | [diff] [blame] | 1197 | |
Jean-Luc Brouillet | f33e156 | 2014-06-03 17:55:57 -0700 | [diff] [blame] | 1198 | mOut.indent() << "return createFieldID(" << RS_EXPORT_VAR_INDEX_PREFIX |
| 1199 | << VarName << ", null);\n"; |
Tim Murray | b81a993 | 2012-10-10 15:56:02 -0700 | [diff] [blame] | 1200 | |
Jean-Luc Brouillet | 2e205d0 | 2014-06-02 21:06:52 -0700 | [diff] [blame] | 1201 | endFunction(); |
Tim Murray | b81a993 | 2012-10-10 15:56:02 -0700 | [diff] [blame] | 1202 | } |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 1203 | } |
| 1204 | |
| 1205 | /******************* Methods to generate script class /end *******************/ |
| 1206 | |
Jean-Luc Brouillet | 2e205d0 | 2014-06-02 21:06:52 -0700 | [diff] [blame] | 1207 | bool RSReflectionJava::genCreateFieldPacker(const RSExportType *ET, |
Jean-Luc Brouillet | 602def7 | 2014-05-27 16:11:37 -0700 | [diff] [blame] | 1208 | const char *FieldPackerName) { |
Jean-Luc Brouillet | c95381a | 2014-05-14 21:24:45 -0700 | [diff] [blame] | 1209 | size_t AllocSize = ET->getAllocSize(); |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 1210 | if (AllocSize > 0) |
Jean-Luc Brouillet | f33e156 | 2014-06-03 17:55:57 -0700 | [diff] [blame] | 1211 | mOut.indent() << "FieldPacker " << FieldPackerName << " = new FieldPacker(" |
| 1212 | << AllocSize << ");\n"; |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 1213 | else |
| 1214 | return false; |
| 1215 | return true; |
| 1216 | } |
| 1217 | |
Jean-Luc Brouillet | 2e205d0 | 2014-06-02 21:06:52 -0700 | [diff] [blame] | 1218 | void RSReflectionJava::genPackVarOfType(const RSExportType *ET, |
Jean-Luc Brouillet | 602def7 | 2014-05-27 16:11:37 -0700 | [diff] [blame] | 1219 | const char *VarName, |
| 1220 | const char *FieldPackerName) { |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 1221 | switch (ET->getClass()) { |
Jean-Luc Brouillet | 2ce118e | 2014-05-27 17:41:22 -0700 | [diff] [blame] | 1222 | case RSExportType::ExportClassPrimitive: |
| 1223 | case RSExportType::ExportClassVector: { |
Jean-Luc Brouillet | f33e156 | 2014-06-03 17:55:57 -0700 | [diff] [blame] | 1224 | mOut.indent() << FieldPackerName << "." |
| 1225 | << GetPackerAPIName( |
| 1226 | static_cast<const RSExportPrimitiveType *>(ET)) << "(" |
| 1227 | << VarName << ");\n"; |
Jean-Luc Brouillet | 2ce118e | 2014-05-27 17:41:22 -0700 | [diff] [blame] | 1228 | break; |
| 1229 | } |
| 1230 | case RSExportType::ExportClassPointer: { |
| 1231 | // Must reflect as type Allocation in Java |
| 1232 | const RSExportType *PointeeType = |
| 1233 | static_cast<const RSExportPointerType *>(ET)->getPointeeType(); |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 1234 | |
Jean-Luc Brouillet | f33e156 | 2014-06-03 17:55:57 -0700 | [diff] [blame] | 1235 | if (PointeeType->getClass() != RSExportType::ExportClassRecord) { |
| 1236 | mOut.indent() << FieldPackerName << ".addI32(" << VarName |
| 1237 | << ".getPtr());\n"; |
| 1238 | } else { |
| 1239 | mOut.indent() << FieldPackerName << ".addI32(" << VarName |
| 1240 | << ".getAllocation().getPtr());\n"; |
| 1241 | } |
Jean-Luc Brouillet | 2ce118e | 2014-05-27 17:41:22 -0700 | [diff] [blame] | 1242 | break; |
| 1243 | } |
| 1244 | case RSExportType::ExportClassMatrix: { |
Jean-Luc Brouillet | f33e156 | 2014-06-03 17:55:57 -0700 | [diff] [blame] | 1245 | mOut.indent() << FieldPackerName << ".addMatrix(" << VarName << ");\n"; |
Jean-Luc Brouillet | 2ce118e | 2014-05-27 17:41:22 -0700 | [diff] [blame] | 1246 | break; |
| 1247 | } |
| 1248 | case RSExportType::ExportClassConstantArray: { |
| 1249 | const RSExportConstantArrayType *ECAT = |
| 1250 | static_cast<const RSExportConstantArrayType *>(ET); |
| 1251 | |
| 1252 | // TODO(zonr): more elegant way. Currently, we obtain the unique index |
| 1253 | // variable (this method involves recursive call which means |
| 1254 | // we may have more than one level loop, therefore we can't |
| 1255 | // always use the same index variable name here) name given |
| 1256 | // in the for-loop from counting the '.' in @VarName. |
| 1257 | unsigned Level = 0; |
| 1258 | size_t LastDotPos = 0; |
| 1259 | std::string ElementVarName(VarName); |
| 1260 | |
| 1261 | while (LastDotPos != std::string::npos) { |
| 1262 | LastDotPos = ElementVarName.find_first_of('.', LastDotPos + 1); |
| 1263 | Level++; |
| 1264 | } |
| 1265 | std::string IndexVarName("ct"); |
| 1266 | IndexVarName.append(llvm::utostr_32(Level)); |
| 1267 | |
Jean-Luc Brouillet | f33e156 | 2014-06-03 17:55:57 -0700 | [diff] [blame] | 1268 | mOut.indent() << "for (int " << IndexVarName << " = 0; " << IndexVarName |
| 1269 | << " < " << ECAT->getSize() << "; " << IndexVarName << "++)"; |
| 1270 | mOut.startBlock(); |
Jean-Luc Brouillet | 2ce118e | 2014-05-27 17:41:22 -0700 | [diff] [blame] | 1271 | |
| 1272 | ElementVarName.append("[" + IndexVarName + "]"); |
Jean-Luc Brouillet | 2e205d0 | 2014-06-02 21:06:52 -0700 | [diff] [blame] | 1273 | genPackVarOfType(ECAT->getElementType(), ElementVarName.c_str(), |
Jean-Luc Brouillet | 2ce118e | 2014-05-27 17:41:22 -0700 | [diff] [blame] | 1274 | FieldPackerName); |
| 1275 | |
Jean-Luc Brouillet | f33e156 | 2014-06-03 17:55:57 -0700 | [diff] [blame] | 1276 | mOut.endBlock(); |
Jean-Luc Brouillet | 2ce118e | 2014-05-27 17:41:22 -0700 | [diff] [blame] | 1277 | break; |
| 1278 | } |
| 1279 | case RSExportType::ExportClassRecord: { |
| 1280 | const RSExportRecordType *ERT = static_cast<const RSExportRecordType *>(ET); |
| 1281 | // Relative pos from now on in field packer |
| 1282 | unsigned Pos = 0; |
| 1283 | |
| 1284 | for (RSExportRecordType::const_field_iterator I = ERT->fields_begin(), |
| 1285 | E = ERT->fields_end(); |
| 1286 | I != E; I++) { |
| 1287 | const RSExportRecordType::Field *F = *I; |
| 1288 | std::string FieldName; |
| 1289 | size_t FieldOffset = F->getOffsetInParent(); |
| 1290 | const RSExportType *T = F->getType(); |
| 1291 | size_t FieldStoreSize = T->getStoreSize(); |
| 1292 | size_t FieldAllocSize = T->getAllocSize(); |
| 1293 | |
| 1294 | if (VarName != NULL) |
| 1295 | FieldName = VarName + ("." + F->getName()); |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 1296 | else |
Jean-Luc Brouillet | 2ce118e | 2014-05-27 17:41:22 -0700 | [diff] [blame] | 1297 | FieldName = F->getName(); |
Zonr Chang | 89273bd | 2010-10-14 20:57:38 +0800 | [diff] [blame] | 1298 | |
Jean-Luc Brouillet | f33e156 | 2014-06-03 17:55:57 -0700 | [diff] [blame] | 1299 | if (FieldOffset > Pos) { |
| 1300 | mOut.indent() << FieldPackerName << ".skip(" << (FieldOffset - Pos) |
| 1301 | << ");\n"; |
| 1302 | } |
Zonr Chang | 89273bd | 2010-10-14 20:57:38 +0800 | [diff] [blame] | 1303 | |
Jean-Luc Brouillet | 2e205d0 | 2014-06-02 21:06:52 -0700 | [diff] [blame] | 1304 | genPackVarOfType(F->getType(), FieldName.c_str(), FieldPackerName); |
Zonr Chang | 89273bd | 2010-10-14 20:57:38 +0800 | [diff] [blame] | 1305 | |
Jean-Luc Brouillet | 2ce118e | 2014-05-27 17:41:22 -0700 | [diff] [blame] | 1306 | // There is padding in the field type |
Jean-Luc Brouillet | f33e156 | 2014-06-03 17:55:57 -0700 | [diff] [blame] | 1307 | if (FieldAllocSize > FieldStoreSize) { |
| 1308 | mOut.indent() << FieldPackerName << ".skip(" |
| 1309 | << (FieldAllocSize - FieldStoreSize) << ");\n"; |
| 1310 | } |
Jean-Luc Brouillet | 2ce118e | 2014-05-27 17:41:22 -0700 | [diff] [blame] | 1311 | |
| 1312 | Pos = FieldOffset + FieldAllocSize; |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 1313 | } |
Jean-Luc Brouillet | 2ce118e | 2014-05-27 17:41:22 -0700 | [diff] [blame] | 1314 | |
| 1315 | // There maybe some padding after the struct |
Jean-Luc Brouillet | f33e156 | 2014-06-03 17:55:57 -0700 | [diff] [blame] | 1316 | if (ERT->getAllocSize() > Pos) { |
| 1317 | mOut.indent() << FieldPackerName << ".skip(" << ERT->getAllocSize() - Pos |
| 1318 | << ");\n"; |
| 1319 | } |
Jean-Luc Brouillet | 2ce118e | 2014-05-27 17:41:22 -0700 | [diff] [blame] | 1320 | break; |
| 1321 | } |
| 1322 | default: { slangAssert(false && "Unknown class of type"); } |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 1323 | } |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 1324 | } |
| 1325 | |
Jean-Luc Brouillet | 2e205d0 | 2014-06-02 21:06:52 -0700 | [diff] [blame] | 1326 | void RSReflectionJava::genAllocateVarOfType(const RSExportType *T, |
Jean-Luc Brouillet | 602def7 | 2014-05-27 16:11:37 -0700 | [diff] [blame] | 1327 | const std::string &VarName) { |
Zonr Chang | 2e1dba6 | 2010-10-05 22:20:11 +0800 | [diff] [blame] | 1328 | switch (T->getClass()) { |
Jean-Luc Brouillet | 2ce118e | 2014-05-27 17:41:22 -0700 | [diff] [blame] | 1329 | case RSExportType::ExportClassPrimitive: { |
| 1330 | // Primitive type like int in Java has its own storage once it's declared. |
| 1331 | // |
| 1332 | // FIXME: Should we allocate storage for RS object? |
| 1333 | // if (static_cast<const RSExportPrimitiveType *>(T)->isRSObjectType()) |
Jean-Luc Brouillet | f33e156 | 2014-06-03 17:55:57 -0700 | [diff] [blame] | 1334 | // mOut.indent() << VarName << " = new " << GetTypeName(T) << "();\n"; |
Jean-Luc Brouillet | 2ce118e | 2014-05-27 17:41:22 -0700 | [diff] [blame] | 1335 | break; |
| 1336 | } |
| 1337 | case RSExportType::ExportClassPointer: { |
| 1338 | // Pointer type is an instance of Allocation or a TypeClass whose value is |
| 1339 | // expected to be assigned by programmer later in Java program. Therefore |
| 1340 | // we don't reflect things like [VarName] = new Allocation(); |
Jean-Luc Brouillet | f33e156 | 2014-06-03 17:55:57 -0700 | [diff] [blame] | 1341 | mOut.indent() << VarName << " = null;\n"; |
Jean-Luc Brouillet | 2ce118e | 2014-05-27 17:41:22 -0700 | [diff] [blame] | 1342 | break; |
| 1343 | } |
| 1344 | case RSExportType::ExportClassConstantArray: { |
| 1345 | const RSExportConstantArrayType *ECAT = |
| 1346 | static_cast<const RSExportConstantArrayType *>(T); |
| 1347 | const RSExportType *ElementType = ECAT->getElementType(); |
Zonr Chang | 2e1dba6 | 2010-10-05 22:20:11 +0800 | [diff] [blame] | 1348 | |
Jean-Luc Brouillet | f33e156 | 2014-06-03 17:55:57 -0700 | [diff] [blame] | 1349 | mOut.indent() << VarName << " = new " << GetTypeName(ElementType) << "[" |
| 1350 | << ECAT->getSize() << "];\n"; |
Zonr Chang | 2f1451c | 2010-10-14 02:58:28 +0800 | [diff] [blame] | 1351 | |
Jean-Luc Brouillet | 2ce118e | 2014-05-27 17:41:22 -0700 | [diff] [blame] | 1352 | // Primitive type element doesn't need allocation code. |
| 1353 | if (ElementType->getClass() != RSExportType::ExportClassPrimitive) { |
Jean-Luc Brouillet | f33e156 | 2014-06-03 17:55:57 -0700 | [diff] [blame] | 1354 | mOut.indent() << "for (int $ct = 0; $ct < " << ECAT->getSize() |
| 1355 | << "; $ct++)"; |
| 1356 | mOut.startBlock(); |
Zonr Chang | 2e1dba6 | 2010-10-05 22:20:11 +0800 | [diff] [blame] | 1357 | |
Jean-Luc Brouillet | 2ce118e | 2014-05-27 17:41:22 -0700 | [diff] [blame] | 1358 | std::string ElementVarName(VarName); |
| 1359 | ElementVarName.append("[$ct]"); |
Jean-Luc Brouillet | 2e205d0 | 2014-06-02 21:06:52 -0700 | [diff] [blame] | 1360 | genAllocateVarOfType(ElementType, ElementVarName); |
Zonr Chang | 2e1dba6 | 2010-10-05 22:20:11 +0800 | [diff] [blame] | 1361 | |
Jean-Luc Brouillet | f33e156 | 2014-06-03 17:55:57 -0700 | [diff] [blame] | 1362 | mOut.endBlock(); |
Zonr Chang | 2e1dba6 | 2010-10-05 22:20:11 +0800 | [diff] [blame] | 1363 | } |
Jean-Luc Brouillet | 2ce118e | 2014-05-27 17:41:22 -0700 | [diff] [blame] | 1364 | break; |
| 1365 | } |
| 1366 | case RSExportType::ExportClassVector: |
| 1367 | case RSExportType::ExportClassMatrix: |
| 1368 | case RSExportType::ExportClassRecord: { |
Jean-Luc Brouillet | f33e156 | 2014-06-03 17:55:57 -0700 | [diff] [blame] | 1369 | mOut.indent() << VarName << " = new " << GetTypeName(T) << "();\n"; |
Jean-Luc Brouillet | 2ce118e | 2014-05-27 17:41:22 -0700 | [diff] [blame] | 1370 | break; |
| 1371 | } |
Zonr Chang | 2e1dba6 | 2010-10-05 22:20:11 +0800 | [diff] [blame] | 1372 | } |
Zonr Chang | 2e1dba6 | 2010-10-05 22:20:11 +0800 | [diff] [blame] | 1373 | } |
| 1374 | |
Jean-Luc Brouillet | 2e205d0 | 2014-06-02 21:06:52 -0700 | [diff] [blame] | 1375 | void RSReflectionJava::genNewItemBufferIfNull(const char *Index) { |
Jean-Luc Brouillet | f33e156 | 2014-06-03 17:55:57 -0700 | [diff] [blame] | 1376 | mOut.indent() << "if (" << RS_TYPE_ITEM_BUFFER_NAME " == null) "; |
| 1377 | mOut << RS_TYPE_ITEM_BUFFER_NAME << " = new " << RS_TYPE_ITEM_CLASS_NAME |
| 1378 | << "[getType().getX() /* count */];\n"; |
| 1379 | if (Index != NULL) { |
| 1380 | mOut.indent() << "if (" << RS_TYPE_ITEM_BUFFER_NAME << "[" << Index |
| 1381 | << "] == null) "; |
| 1382 | mOut << RS_TYPE_ITEM_BUFFER_NAME << "[" << Index << "] = new " |
| 1383 | << RS_TYPE_ITEM_CLASS_NAME << "();\n"; |
| 1384 | } |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 1385 | } |
| 1386 | |
Jean-Luc Brouillet | 2e205d0 | 2014-06-02 21:06:52 -0700 | [diff] [blame] | 1387 | void RSReflectionJava::genNewItemBufferPackerIfNull() { |
Jean-Luc Brouillet | f33e156 | 2014-06-03 17:55:57 -0700 | [diff] [blame] | 1388 | mOut.indent() << "if (" << RS_TYPE_ITEM_BUFFER_PACKER_NAME << " == null) "; |
| 1389 | mOut << RS_TYPE_ITEM_BUFFER_PACKER_NAME " = new FieldPacker(" |
Tim Murray | 3a38b74 | 2014-07-02 10:41:08 -0700 | [diff] [blame] | 1390 | << mItemSizeof << " * getType().getX()/* count */);\n"; |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 1391 | } |
| 1392 | |
| 1393 | /********************** Methods to generate type class **********************/ |
Jean-Luc Brouillet | 2e205d0 | 2014-06-02 21:06:52 -0700 | [diff] [blame] | 1394 | bool RSReflectionJava::genTypeClass(const RSExportRecordType *ERT, |
Jean-Luc Brouillet | 602def7 | 2014-05-27 16:11:37 -0700 | [diff] [blame] | 1395 | std::string &ErrorMsg) { |
Stephen Hines | a6b5414 | 2012-04-09 18:25:08 -0700 | [diff] [blame] | 1396 | std::string ClassName = ERT->getElementName(); |
Jean-Luc Brouillet | 2e205d0 | 2014-06-02 21:06:52 -0700 | [diff] [blame] | 1397 | std::string superClassName = getRSPackageName(); |
Tim Murray | f69e1e5 | 2013-01-17 13:57:24 -0800 | [diff] [blame] | 1398 | superClassName += RS_TYPE_CLASS_SUPER_CLASS_NAME; |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 1399 | |
Jean-Luc Brouillet | 2e205d0 | 2014-06-02 21:06:52 -0700 | [diff] [blame] | 1400 | if (!startClass(AM_Public, false, ClassName, superClassName.c_str(), |
| 1401 | ErrorMsg)) |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 1402 | return false; |
| 1403 | |
Stephen Hines | 4cc67fc | 2011-01-31 16:48:57 -0800 | [diff] [blame] | 1404 | mGeneratedFileNames->push_back(ClassName); |
| 1405 | |
Jean-Luc Brouillet | 2e205d0 | 2014-06-02 21:06:52 -0700 | [diff] [blame] | 1406 | genTypeItemClass(ERT); |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 1407 | |
| 1408 | // Declare item buffer and item buffer packer |
Jean-Luc Brouillet | f33e156 | 2014-06-03 17:55:57 -0700 | [diff] [blame] | 1409 | mOut.indent() << "private " << RS_TYPE_ITEM_CLASS_NAME << " " |
| 1410 | << RS_TYPE_ITEM_BUFFER_NAME << "[];\n"; |
| 1411 | mOut.indent() << "private FieldPacker " << RS_TYPE_ITEM_BUFFER_PACKER_NAME |
| 1412 | << ";\n"; |
| 1413 | mOut.indent() << "private static java.lang.ref.WeakReference<Element> " |
| 1414 | << RS_TYPE_ELEMENT_REF_NAME |
| 1415 | << " = new java.lang.ref.WeakReference<Element>(null);\n"; |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 1416 | |
Jean-Luc Brouillet | 2e205d0 | 2014-06-02 21:06:52 -0700 | [diff] [blame] | 1417 | genTypeClassConstructor(ERT); |
| 1418 | genTypeClassCopyToArrayLocal(ERT); |
| 1419 | genTypeClassCopyToArray(ERT); |
| 1420 | genTypeClassItemSetter(ERT); |
| 1421 | genTypeClassItemGetter(ERT); |
| 1422 | genTypeClassComponentSetter(ERT); |
| 1423 | genTypeClassComponentGetter(ERT); |
| 1424 | genTypeClassCopyAll(ERT); |
Stephen Hines | 82754d8 | 2013-01-18 19:46:06 -0800 | [diff] [blame] | 1425 | if (!mRSContext->isCompatLib()) { |
| 1426 | // Skip the resize method if we are targeting a compatibility library. |
Jean-Luc Brouillet | 2e205d0 | 2014-06-02 21:06:52 -0700 | [diff] [blame] | 1427 | genTypeClassResize(); |
Stephen Hines | 82754d8 | 2013-01-18 19:46:06 -0800 | [diff] [blame] | 1428 | } |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 1429 | |
Jean-Luc Brouillet | 2e205d0 | 2014-06-02 21:06:52 -0700 | [diff] [blame] | 1430 | endClass(); |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 1431 | |
Jean-Luc Brouillet | 2e205d0 | 2014-06-02 21:06:52 -0700 | [diff] [blame] | 1432 | resetFieldIndex(); |
| 1433 | clearFieldIndexMap(); |
Zonr Chang | 66aa299 | 2010-10-05 15:56:31 +0800 | [diff] [blame] | 1434 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 1435 | return true; |
| 1436 | } |
| 1437 | |
Jean-Luc Brouillet | 2e205d0 | 2014-06-02 21:06:52 -0700 | [diff] [blame] | 1438 | void RSReflectionJava::genTypeItemClass(const RSExportRecordType *ERT) { |
Jean-Luc Brouillet | f33e156 | 2014-06-03 17:55:57 -0700 | [diff] [blame] | 1439 | mOut.indent() << "static public class " RS_TYPE_ITEM_CLASS_NAME; |
| 1440 | mOut.startBlock(); |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 1441 | |
Tim Murray | 3a38b74 | 2014-07-02 10:41:08 -0700 | [diff] [blame] | 1442 | // Sizeof should not be exposed for 64-bit; it is not accurate |
| 1443 | if (mRSContext->getTargetAPI() < 21) { |
| 1444 | mOut.indent() << "public static final int sizeof = " << ERT->getAllocSize() |
| 1445 | << ";\n"; |
| 1446 | } |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 1447 | |
| 1448 | // Member elements |
Jean-Luc Brouillet | f33e156 | 2014-06-03 17:55:57 -0700 | [diff] [blame] | 1449 | mOut << "\n"; |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 1450 | for (RSExportRecordType::const_field_iterator FI = ERT->fields_begin(), |
Jean-Luc Brouillet | 2ce118e | 2014-05-27 17:41:22 -0700 | [diff] [blame] | 1451 | FE = ERT->fields_end(); |
| 1452 | FI != FE; FI++) { |
Jean-Luc Brouillet | f33e156 | 2014-06-03 17:55:57 -0700 | [diff] [blame] | 1453 | mOut.indent() << GetTypeName((*FI)->getType()) << " " << (*FI)->getName() |
| 1454 | << ";\n"; |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 1455 | } |
| 1456 | |
| 1457 | // Constructor |
Jean-Luc Brouillet | f33e156 | 2014-06-03 17:55:57 -0700 | [diff] [blame] | 1458 | mOut << "\n"; |
| 1459 | mOut.indent() << RS_TYPE_ITEM_CLASS_NAME << "()"; |
| 1460 | mOut.startBlock(); |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 1461 | |
| 1462 | for (RSExportRecordType::const_field_iterator FI = ERT->fields_begin(), |
Jean-Luc Brouillet | 2ce118e | 2014-05-27 17:41:22 -0700 | [diff] [blame] | 1463 | FE = ERT->fields_end(); |
| 1464 | FI != FE; FI++) { |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 1465 | const RSExportRecordType::Field *F = *FI; |
Jean-Luc Brouillet | 2e205d0 | 2014-06-02 21:06:52 -0700 | [diff] [blame] | 1466 | genAllocateVarOfType(F->getType(), F->getName()); |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 1467 | } |
| 1468 | |
| 1469 | // end Constructor |
Jean-Luc Brouillet | f33e156 | 2014-06-03 17:55:57 -0700 | [diff] [blame] | 1470 | mOut.endBlock(); |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 1471 | |
| 1472 | // end Item class |
Jean-Luc Brouillet | f33e156 | 2014-06-03 17:55:57 -0700 | [diff] [blame] | 1473 | mOut.endBlock(); |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 1474 | } |
| 1475 | |
Jean-Luc Brouillet | 2e205d0 | 2014-06-02 21:06:52 -0700 | [diff] [blame] | 1476 | void RSReflectionJava::genTypeClassConstructor(const RSExportRecordType *ERT) { |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 1477 | const char *RenderScriptVar = "rs"; |
| 1478 | |
Jean-Luc Brouillet | 2e205d0 | 2014-06-02 21:06:52 -0700 | [diff] [blame] | 1479 | startFunction(AM_Public, true, "Element", "createElement", 1, "RenderScript", |
| 1480 | RenderScriptVar); |
Jason Sams | 381e95f | 2011-11-30 14:01:43 -0800 | [diff] [blame] | 1481 | |
Stephen Hines | e67239d | 2012-02-24 15:08:36 -0800 | [diff] [blame] | 1482 | // TODO(all): Fix weak-refs + multi-context issue. |
Jean-Luc Brouillet | f33e156 | 2014-06-03 17:55:57 -0700 | [diff] [blame] | 1483 | // mOut.indent() << "Element e = " << RS_TYPE_ELEMENT_REF_NAME |
Jean-Luc Brouillet | 2968921 | 2014-05-27 13:25:31 -0700 | [diff] [blame] | 1484 | // << ".get();\n"; |
Jean-Luc Brouillet | f33e156 | 2014-06-03 17:55:57 -0700 | [diff] [blame] | 1485 | // mOut.indent() << "if (e != null) return e;\n"; |
Jean-Luc Brouillet | c643ceb | 2014-06-05 10:17:23 -0700 | [diff] [blame] | 1486 | RSReflectionJavaElementBuilder builder("eb", ERT, RenderScriptVar, &mOut, |
| 1487 | mRSContext, this); |
| 1488 | builder.generate(); |
| 1489 | |
Jean-Luc Brouillet | f33e156 | 2014-06-03 17:55:57 -0700 | [diff] [blame] | 1490 | mOut.indent() << "return eb.create();\n"; |
| 1491 | // mOut.indent() << "e = eb.create();\n"; |
| 1492 | // mOut.indent() << RS_TYPE_ELEMENT_REF_NAME |
Jean-Luc Brouillet | 2968921 | 2014-05-27 13:25:31 -0700 | [diff] [blame] | 1493 | // << " = new java.lang.ref.WeakReference<Element>(e);\n"; |
Jean-Luc Brouillet | f33e156 | 2014-06-03 17:55:57 -0700 | [diff] [blame] | 1494 | // mOut.indent() << "return e;\n"; |
Jean-Luc Brouillet | 2e205d0 | 2014-06-02 21:06:52 -0700 | [diff] [blame] | 1495 | endFunction(); |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 1496 | |
Jason Sams | 381e95f | 2011-11-30 14:01:43 -0800 | [diff] [blame] | 1497 | // private with element |
Jean-Luc Brouillet | 2e205d0 | 2014-06-02 21:06:52 -0700 | [diff] [blame] | 1498 | startFunction(AM_Private, false, NULL, getClassName(), 1, "RenderScript", |
| 1499 | RenderScriptVar); |
Jean-Luc Brouillet | f33e156 | 2014-06-03 17:55:57 -0700 | [diff] [blame] | 1500 | mOut.indent() << RS_TYPE_ITEM_BUFFER_NAME << " = null;\n"; |
| 1501 | mOut.indent() << RS_TYPE_ITEM_BUFFER_PACKER_NAME << " = null;\n"; |
| 1502 | mOut.indent() << "mElement = createElement(" << RenderScriptVar << ");\n"; |
Jean-Luc Brouillet | 2e205d0 | 2014-06-02 21:06:52 -0700 | [diff] [blame] | 1503 | endFunction(); |
Jason Sams | 381e95f | 2011-11-30 14:01:43 -0800 | [diff] [blame] | 1504 | |
| 1505 | // 1D without usage |
Jean-Luc Brouillet | 2e205d0 | 2014-06-02 21:06:52 -0700 | [diff] [blame] | 1506 | startFunction(AM_Public, false, NULL, getClassName(), 2, "RenderScript", |
| 1507 | RenderScriptVar, "int", "count"); |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 1508 | |
Jean-Luc Brouillet | f33e156 | 2014-06-03 17:55:57 -0700 | [diff] [blame] | 1509 | mOut.indent() << RS_TYPE_ITEM_BUFFER_NAME << " = null;\n"; |
| 1510 | mOut.indent() << RS_TYPE_ITEM_BUFFER_PACKER_NAME << " = null;\n"; |
| 1511 | mOut.indent() << "mElement = createElement(" << RenderScriptVar << ");\n"; |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 1512 | // Call init() in super class |
Jean-Luc Brouillet | f33e156 | 2014-06-03 17:55:57 -0700 | [diff] [blame] | 1513 | mOut.indent() << "init(" << RenderScriptVar << ", count);\n"; |
Jean-Luc Brouillet | 2e205d0 | 2014-06-02 21:06:52 -0700 | [diff] [blame] | 1514 | endFunction(); |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 1515 | |
Jason Sams | 381e95f | 2011-11-30 14:01:43 -0800 | [diff] [blame] | 1516 | // 1D with usage |
Jean-Luc Brouillet | 2e205d0 | 2014-06-02 21:06:52 -0700 | [diff] [blame] | 1517 | startFunction(AM_Public, false, NULL, getClassName(), 3, "RenderScript", |
| 1518 | RenderScriptVar, "int", "count", "int", "usages"); |
Jason Sams | 91fe83b | 2010-12-06 17:07:12 -0800 | [diff] [blame] | 1519 | |
Jean-Luc Brouillet | f33e156 | 2014-06-03 17:55:57 -0700 | [diff] [blame] | 1520 | mOut.indent() << RS_TYPE_ITEM_BUFFER_NAME << " = null;\n"; |
| 1521 | mOut.indent() << RS_TYPE_ITEM_BUFFER_PACKER_NAME << " = null;\n"; |
| 1522 | mOut.indent() << "mElement = createElement(" << RenderScriptVar << ");\n"; |
Jason Sams | 91fe83b | 2010-12-06 17:07:12 -0800 | [diff] [blame] | 1523 | // Call init() in super class |
Jean-Luc Brouillet | f33e156 | 2014-06-03 17:55:57 -0700 | [diff] [blame] | 1524 | mOut.indent() << "init(" << RenderScriptVar << ", count, usages);\n"; |
Jean-Luc Brouillet | 2e205d0 | 2014-06-02 21:06:52 -0700 | [diff] [blame] | 1525 | endFunction(); |
Jason Sams | 91fe83b | 2010-12-06 17:07:12 -0800 | [diff] [blame] | 1526 | |
Jason Sams | 381e95f | 2011-11-30 14:01:43 -0800 | [diff] [blame] | 1527 | // create1D with usage |
Jean-Luc Brouillet | 2e205d0 | 2014-06-02 21:06:52 -0700 | [diff] [blame] | 1528 | startFunction(AM_Public, true, getClassName().c_str(), "create1D", 3, |
| 1529 | "RenderScript", RenderScriptVar, "int", "dimX", "int", |
| 1530 | "usages"); |
Jean-Luc Brouillet | f33e156 | 2014-06-03 17:55:57 -0700 | [diff] [blame] | 1531 | mOut.indent() << getClassName() << " obj = new " << getClassName() << "(" |
| 1532 | << RenderScriptVar << ");\n"; |
| 1533 | mOut.indent() << "obj.mAllocation = Allocation.createSized(" |
| 1534 | "rs, obj.mElement, dimX, usages);\n"; |
| 1535 | mOut.indent() << "return obj;\n"; |
Jean-Luc Brouillet | 2e205d0 | 2014-06-02 21:06:52 -0700 | [diff] [blame] | 1536 | endFunction(); |
Jason Sams | 381e95f | 2011-11-30 14:01:43 -0800 | [diff] [blame] | 1537 | |
| 1538 | // create1D without usage |
Jean-Luc Brouillet | 2e205d0 | 2014-06-02 21:06:52 -0700 | [diff] [blame] | 1539 | startFunction(AM_Public, true, getClassName().c_str(), "create1D", 2, |
| 1540 | "RenderScript", RenderScriptVar, "int", "dimX"); |
Jean-Luc Brouillet | f33e156 | 2014-06-03 17:55:57 -0700 | [diff] [blame] | 1541 | mOut.indent() << "return create1D(" << RenderScriptVar |
| 1542 | << ", dimX, Allocation.USAGE_SCRIPT);\n"; |
Jean-Luc Brouillet | 2e205d0 | 2014-06-02 21:06:52 -0700 | [diff] [blame] | 1543 | endFunction(); |
Jason Sams | 381e95f | 2011-11-30 14:01:43 -0800 | [diff] [blame] | 1544 | |
Jason Sams | 381e95f | 2011-11-30 14:01:43 -0800 | [diff] [blame] | 1545 | // create2D without usage |
Jean-Luc Brouillet | 2e205d0 | 2014-06-02 21:06:52 -0700 | [diff] [blame] | 1546 | startFunction(AM_Public, true, getClassName().c_str(), "create2D", 3, |
| 1547 | "RenderScript", RenderScriptVar, "int", "dimX", "int", "dimY"); |
Jean-Luc Brouillet | f33e156 | 2014-06-03 17:55:57 -0700 | [diff] [blame] | 1548 | mOut.indent() << "return create2D(" << RenderScriptVar |
| 1549 | << ", dimX, dimY, Allocation.USAGE_SCRIPT);\n"; |
Jean-Luc Brouillet | 2e205d0 | 2014-06-02 21:06:52 -0700 | [diff] [blame] | 1550 | endFunction(); |
Jason Sams | 381e95f | 2011-11-30 14:01:43 -0800 | [diff] [blame] | 1551 | |
| 1552 | // create2D with usage |
Jean-Luc Brouillet | 2e205d0 | 2014-06-02 21:06:52 -0700 | [diff] [blame] | 1553 | startFunction(AM_Public, true, getClassName().c_str(), "create2D", 4, |
| 1554 | "RenderScript", RenderScriptVar, "int", "dimX", "int", "dimY", |
| 1555 | "int", "usages"); |
Jason Sams | 381e95f | 2011-11-30 14:01:43 -0800 | [diff] [blame] | 1556 | |
Jean-Luc Brouillet | f33e156 | 2014-06-03 17:55:57 -0700 | [diff] [blame] | 1557 | mOut.indent() << getClassName() << " obj = new " << getClassName() << "(" |
| 1558 | << RenderScriptVar << ");\n"; |
| 1559 | mOut.indent() << "Type.Builder b = new Type.Builder(rs, obj.mElement);\n"; |
| 1560 | mOut.indent() << "b.setX(dimX);\n"; |
| 1561 | mOut.indent() << "b.setY(dimY);\n"; |
| 1562 | mOut.indent() << "Type t = b.create();\n"; |
| 1563 | mOut.indent() << "obj.mAllocation = Allocation.createTyped(rs, t, usages);\n"; |
| 1564 | mOut.indent() << "return obj;\n"; |
Jean-Luc Brouillet | 2e205d0 | 2014-06-02 21:06:52 -0700 | [diff] [blame] | 1565 | endFunction(); |
Jason Sams | 381e95f | 2011-11-30 14:01:43 -0800 | [diff] [blame] | 1566 | |
Jason Sams | 381e95f | 2011-11-30 14:01:43 -0800 | [diff] [blame] | 1567 | // createTypeBuilder |
Jean-Luc Brouillet | 2e205d0 | 2014-06-02 21:06:52 -0700 | [diff] [blame] | 1568 | startFunction(AM_Public, true, "Type.Builder", "createTypeBuilder", 1, |
| 1569 | "RenderScript", RenderScriptVar); |
Jean-Luc Brouillet | f33e156 | 2014-06-03 17:55:57 -0700 | [diff] [blame] | 1570 | mOut.indent() << "Element e = createElement(" << RenderScriptVar << ");\n"; |
| 1571 | mOut.indent() << "return new Type.Builder(rs, e);\n"; |
Jean-Luc Brouillet | 2e205d0 | 2014-06-02 21:06:52 -0700 | [diff] [blame] | 1572 | endFunction(); |
Jason Sams | 381e95f | 2011-11-30 14:01:43 -0800 | [diff] [blame] | 1573 | |
| 1574 | // createCustom with usage |
Jean-Luc Brouillet | 2e205d0 | 2014-06-02 21:06:52 -0700 | [diff] [blame] | 1575 | startFunction(AM_Public, true, getClassName().c_str(), "createCustom", 3, |
| 1576 | "RenderScript", RenderScriptVar, "Type.Builder", "tb", "int", |
| 1577 | "usages"); |
Jean-Luc Brouillet | f33e156 | 2014-06-03 17:55:57 -0700 | [diff] [blame] | 1578 | mOut.indent() << getClassName() << " obj = new " << getClassName() << "(" |
| 1579 | << RenderScriptVar << ");\n"; |
| 1580 | mOut.indent() << "Type t = tb.create();\n"; |
| 1581 | mOut.indent() << "if (t.getElement() != obj.mElement) {\n"; |
| 1582 | mOut.indent() << " throw new RSIllegalArgumentException(" |
| 1583 | "\"Type.Builder did not match expected element type.\");\n"; |
| 1584 | mOut.indent() << "}\n"; |
| 1585 | mOut.indent() << "obj.mAllocation = Allocation.createTyped(rs, t, usages);\n"; |
| 1586 | mOut.indent() << "return obj;\n"; |
Jean-Luc Brouillet | 2e205d0 | 2014-06-02 21:06:52 -0700 | [diff] [blame] | 1587 | endFunction(); |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 1588 | } |
| 1589 | |
Jean-Luc Brouillet | 2e205d0 | 2014-06-02 21:06:52 -0700 | [diff] [blame] | 1590 | void RSReflectionJava::genTypeClassCopyToArray(const RSExportRecordType *ERT) { |
| 1591 | startFunction(AM_Private, false, "void", "copyToArray", 2, |
| 1592 | RS_TYPE_ITEM_CLASS_NAME, "i", "int", "index"); |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 1593 | |
Jean-Luc Brouillet | 2e205d0 | 2014-06-02 21:06:52 -0700 | [diff] [blame] | 1594 | genNewItemBufferPackerIfNull(); |
Jean-Luc Brouillet | f33e156 | 2014-06-03 17:55:57 -0700 | [diff] [blame] | 1595 | mOut.indent() << RS_TYPE_ITEM_BUFFER_PACKER_NAME << ".reset(index * " |
Tim Murray | 3a38b74 | 2014-07-02 10:41:08 -0700 | [diff] [blame] | 1596 | << mItemSizeof << ");\n"; |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 1597 | |
Jean-Luc Brouillet | f33e156 | 2014-06-03 17:55:57 -0700 | [diff] [blame] | 1598 | mOut.indent() << "copyToArrayLocal(i, " RS_TYPE_ITEM_BUFFER_PACKER_NAME |
| 1599 | ");\n"; |
Alex Sakhartchouk | 38eca1a | 2011-08-25 10:47:52 -0700 | [diff] [blame] | 1600 | |
Jean-Luc Brouillet | 2e205d0 | 2014-06-02 21:06:52 -0700 | [diff] [blame] | 1601 | endFunction(); |
Alex Sakhartchouk | 38eca1a | 2011-08-25 10:47:52 -0700 | [diff] [blame] | 1602 | } |
| 1603 | |
Jean-Luc Brouillet | 602def7 | 2014-05-27 16:11:37 -0700 | [diff] [blame] | 1604 | void |
Jean-Luc Brouillet | 2e205d0 | 2014-06-02 21:06:52 -0700 | [diff] [blame] | 1605 | RSReflectionJava::genTypeClassCopyToArrayLocal(const RSExportRecordType *ERT) { |
| 1606 | startFunction(AM_Private, false, "void", "copyToArrayLocal", 2, |
| 1607 | RS_TYPE_ITEM_CLASS_NAME, "i", "FieldPacker", "fp"); |
Alex Sakhartchouk | 38eca1a | 2011-08-25 10:47:52 -0700 | [diff] [blame] | 1608 | |
Jean-Luc Brouillet | 2e205d0 | 2014-06-02 21:06:52 -0700 | [diff] [blame] | 1609 | genPackVarOfType(ERT, "i", "fp"); |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 1610 | |
Jean-Luc Brouillet | 2e205d0 | 2014-06-02 21:06:52 -0700 | [diff] [blame] | 1611 | endFunction(); |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 1612 | } |
| 1613 | |
Jean-Luc Brouillet | 2e205d0 | 2014-06-02 21:06:52 -0700 | [diff] [blame] | 1614 | void RSReflectionJava::genTypeClassItemSetter(const RSExportRecordType *ERT) { |
| 1615 | startFunction(AM_PublicSynchronized, false, "void", "set", 3, |
| 1616 | RS_TYPE_ITEM_CLASS_NAME, "i", "int", "index", "boolean", |
| 1617 | "copyNow"); |
| 1618 | genNewItemBufferIfNull(NULL); |
Jean-Luc Brouillet | f33e156 | 2014-06-03 17:55:57 -0700 | [diff] [blame] | 1619 | mOut.indent() << RS_TYPE_ITEM_BUFFER_NAME << "[index] = i;\n"; |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 1620 | |
Jean-Luc Brouillet | f33e156 | 2014-06-03 17:55:57 -0700 | [diff] [blame] | 1621 | mOut.indent() << "if (copyNow) "; |
| 1622 | mOut.startBlock(); |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 1623 | |
Jean-Luc Brouillet | f33e156 | 2014-06-03 17:55:57 -0700 | [diff] [blame] | 1624 | mOut.indent() << "copyToArray(i, index);\n"; |
Tim Murray | 3a38b74 | 2014-07-02 10:41:08 -0700 | [diff] [blame] | 1625 | mOut.indent() << "FieldPacker fp = new FieldPacker(" << mItemSizeof << ");\n"; |
Jean-Luc Brouillet | f33e156 | 2014-06-03 17:55:57 -0700 | [diff] [blame] | 1626 | mOut.indent() << "copyToArrayLocal(i, fp);\n"; |
| 1627 | mOut.indent() << "mAllocation.setFromFieldPacker(index, fp);\n"; |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 1628 | |
| 1629 | // End of if (copyNow) |
Jean-Luc Brouillet | f33e156 | 2014-06-03 17:55:57 -0700 | [diff] [blame] | 1630 | mOut.endBlock(); |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 1631 | |
Jean-Luc Brouillet | 2e205d0 | 2014-06-02 21:06:52 -0700 | [diff] [blame] | 1632 | endFunction(); |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 1633 | } |
| 1634 | |
Jean-Luc Brouillet | 2e205d0 | 2014-06-02 21:06:52 -0700 | [diff] [blame] | 1635 | void RSReflectionJava::genTypeClassItemGetter(const RSExportRecordType *ERT) { |
| 1636 | startFunction(AM_PublicSynchronized, false, RS_TYPE_ITEM_CLASS_NAME, "get", 1, |
| 1637 | "int", "index"); |
Jean-Luc Brouillet | f33e156 | 2014-06-03 17:55:57 -0700 | [diff] [blame] | 1638 | mOut.indent() << "if (" << RS_TYPE_ITEM_BUFFER_NAME |
| 1639 | << " == null) return null;\n"; |
| 1640 | mOut.indent() << "return " << RS_TYPE_ITEM_BUFFER_NAME << "[index];\n"; |
Jean-Luc Brouillet | 2e205d0 | 2014-06-02 21:06:52 -0700 | [diff] [blame] | 1641 | endFunction(); |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 1642 | } |
| 1643 | |
Jean-Luc Brouillet | 602def7 | 2014-05-27 16:11:37 -0700 | [diff] [blame] | 1644 | void |
Jean-Luc Brouillet | 2e205d0 | 2014-06-02 21:06:52 -0700 | [diff] [blame] | 1645 | RSReflectionJava::genTypeClassComponentSetter(const RSExportRecordType *ERT) { |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 1646 | for (RSExportRecordType::const_field_iterator FI = ERT->fields_begin(), |
Jean-Luc Brouillet | 602def7 | 2014-05-27 16:11:37 -0700 | [diff] [blame] | 1647 | FE = ERT->fields_end(); |
| 1648 | FI != FE; FI++) { |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 1649 | const RSExportRecordType::Field *F = *FI; |
| 1650 | size_t FieldOffset = F->getOffsetInParent(); |
Jean-Luc Brouillet | c95381a | 2014-05-14 21:24:45 -0700 | [diff] [blame] | 1651 | size_t FieldStoreSize = F->getType()->getStoreSize(); |
Jean-Luc Brouillet | 2e205d0 | 2014-06-02 21:06:52 -0700 | [diff] [blame] | 1652 | unsigned FieldIndex = getFieldIndex(F); |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 1653 | |
Jean-Luc Brouillet | 2e205d0 | 2014-06-02 21:06:52 -0700 | [diff] [blame] | 1654 | startFunction(AM_PublicSynchronized, false, "void", "set_" + F->getName(), |
| 1655 | 3, "int", "index", GetTypeName(F->getType()).c_str(), "v", |
| 1656 | "boolean", "copyNow"); |
| 1657 | genNewItemBufferPackerIfNull(); |
| 1658 | genNewItemBufferIfNull("index"); |
Jean-Luc Brouillet | f33e156 | 2014-06-03 17:55:57 -0700 | [diff] [blame] | 1659 | mOut.indent() << RS_TYPE_ITEM_BUFFER_NAME << "[index]." << F->getName() |
| 1660 | << " = v;\n"; |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 1661 | |
Jean-Luc Brouillet | f33e156 | 2014-06-03 17:55:57 -0700 | [diff] [blame] | 1662 | mOut.indent() << "if (copyNow) "; |
| 1663 | mOut.startBlock(); |
Shih-wei Liao | 2dd42ff | 2010-06-15 00:34:38 -0700 | [diff] [blame] | 1664 | |
Jean-Luc Brouillet | f33e156 | 2014-06-03 17:55:57 -0700 | [diff] [blame] | 1665 | if (FieldOffset > 0) { |
| 1666 | mOut.indent() << RS_TYPE_ITEM_BUFFER_PACKER_NAME << ".reset(index * " |
Tim Murray | 3a38b74 | 2014-07-02 10:41:08 -0700 | [diff] [blame] | 1667 | << mItemSizeof << " + " << FieldOffset |
Jean-Luc Brouillet | f33e156 | 2014-06-03 17:55:57 -0700 | [diff] [blame] | 1668 | << ");\n"; |
| 1669 | } else { |
| 1670 | mOut.indent() << RS_TYPE_ITEM_BUFFER_PACKER_NAME << ".reset(index * " |
Tim Murray | 3a38b74 | 2014-07-02 10:41:08 -0700 | [diff] [blame] | 1671 | << mItemSizeof << ");\n"; |
Jean-Luc Brouillet | f33e156 | 2014-06-03 17:55:57 -0700 | [diff] [blame] | 1672 | } |
Jean-Luc Brouillet | 2e205d0 | 2014-06-02 21:06:52 -0700 | [diff] [blame] | 1673 | genPackVarOfType(F->getType(), "v", RS_TYPE_ITEM_BUFFER_PACKER_NAME); |
| 1674 | |
Jean-Luc Brouillet | f33e156 | 2014-06-03 17:55:57 -0700 | [diff] [blame] | 1675 | mOut.indent() << "FieldPacker fp = new FieldPacker(" << FieldStoreSize |
| 1676 | << ");\n"; |
Jean-Luc Brouillet | 2e205d0 | 2014-06-02 21:06:52 -0700 | [diff] [blame] | 1677 | genPackVarOfType(F->getType(), "v", "fp"); |
Jean-Luc Brouillet | f33e156 | 2014-06-03 17:55:57 -0700 | [diff] [blame] | 1678 | mOut.indent() << "mAllocation.setFromFieldPacker(index, " << FieldIndex |
| 1679 | << ", fp);\n"; |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 1680 | |
| 1681 | // End of if (copyNow) |
Jean-Luc Brouillet | f33e156 | 2014-06-03 17:55:57 -0700 | [diff] [blame] | 1682 | mOut.endBlock(); |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 1683 | |
Jean-Luc Brouillet | 2e205d0 | 2014-06-02 21:06:52 -0700 | [diff] [blame] | 1684 | endFunction(); |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 1685 | } |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 1686 | } |
| 1687 | |
Jean-Luc Brouillet | 602def7 | 2014-05-27 16:11:37 -0700 | [diff] [blame] | 1688 | void |
Jean-Luc Brouillet | 2e205d0 | 2014-06-02 21:06:52 -0700 | [diff] [blame] | 1689 | RSReflectionJava::genTypeClassComponentGetter(const RSExportRecordType *ERT) { |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 1690 | for (RSExportRecordType::const_field_iterator FI = ERT->fields_begin(), |
Jean-Luc Brouillet | 602def7 | 2014-05-27 16:11:37 -0700 | [diff] [blame] | 1691 | FE = ERT->fields_end(); |
| 1692 | FI != FE; FI++) { |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 1693 | const RSExportRecordType::Field *F = *FI; |
Jean-Luc Brouillet | 2e205d0 | 2014-06-02 21:06:52 -0700 | [diff] [blame] | 1694 | startFunction(AM_PublicSynchronized, false, |
| 1695 | GetTypeName(F->getType()).c_str(), "get_" + F->getName(), 1, |
| 1696 | "int", "index"); |
Jean-Luc Brouillet | f33e156 | 2014-06-03 17:55:57 -0700 | [diff] [blame] | 1697 | mOut.indent() << "if (" RS_TYPE_ITEM_BUFFER_NAME << " == null) return " |
| 1698 | << GetTypeNullValue(F->getType()) << ";\n"; |
| 1699 | mOut.indent() << "return " RS_TYPE_ITEM_BUFFER_NAME << "[index]." |
| 1700 | << F->getName() << ";\n"; |
Jean-Luc Brouillet | 2e205d0 | 2014-06-02 21:06:52 -0700 | [diff] [blame] | 1701 | endFunction(); |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 1702 | } |
Shih-wei Liao | 9c631ff | 2010-09-17 11:57:29 -0700 | [diff] [blame] | 1703 | } |
| 1704 | |
Jean-Luc Brouillet | 2e205d0 | 2014-06-02 21:06:52 -0700 | [diff] [blame] | 1705 | void RSReflectionJava::genTypeClassCopyAll(const RSExportRecordType *ERT) { |
| 1706 | startFunction(AM_PublicSynchronized, false, "void", "copyAll", 0); |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 1707 | |
Jean-Luc Brouillet | f33e156 | 2014-06-03 17:55:57 -0700 | [diff] [blame] | 1708 | mOut.indent() << "for (int ct = 0; ct < " << RS_TYPE_ITEM_BUFFER_NAME |
| 1709 | << ".length; ct++)" |
| 1710 | << " copyToArray(" << RS_TYPE_ITEM_BUFFER_NAME |
| 1711 | << "[ct], ct);\n"; |
| 1712 | mOut.indent() << "mAllocation.setFromFieldPacker(0, " |
| 1713 | << RS_TYPE_ITEM_BUFFER_PACKER_NAME ");\n"; |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 1714 | |
Jean-Luc Brouillet | 2e205d0 | 2014-06-02 21:06:52 -0700 | [diff] [blame] | 1715 | endFunction(); |
Shih-wei Liao | 9c631ff | 2010-09-17 11:57:29 -0700 | [diff] [blame] | 1716 | } |
| 1717 | |
Jean-Luc Brouillet | 2e205d0 | 2014-06-02 21:06:52 -0700 | [diff] [blame] | 1718 | void RSReflectionJava::genTypeClassResize() { |
| 1719 | startFunction(AM_PublicSynchronized, false, "void", "resize", 1, "int", |
| 1720 | "newSize"); |
Zonr Chang | d42a429 | 2010-10-17 02:38:43 +0800 | [diff] [blame] | 1721 | |
Jean-Luc Brouillet | f33e156 | 2014-06-03 17:55:57 -0700 | [diff] [blame] | 1722 | mOut.indent() << "if (mItemArray != null) "; |
| 1723 | mOut.startBlock(); |
| 1724 | mOut.indent() << "int oldSize = mItemArray.length;\n"; |
| 1725 | mOut.indent() << "int copySize = Math.min(oldSize, newSize);\n"; |
| 1726 | mOut.indent() << "if (newSize == oldSize) return;\n"; |
| 1727 | mOut.indent() << "Item ni[] = new Item[newSize];\n"; |
| 1728 | mOut.indent() << "System.arraycopy(mItemArray, 0, ni, 0, copySize);\n"; |
| 1729 | mOut.indent() << "mItemArray = ni;\n"; |
| 1730 | mOut.endBlock(); |
| 1731 | mOut.indent() << "mAllocation.resize(newSize);\n"; |
Zonr Chang | d42a429 | 2010-10-17 02:38:43 +0800 | [diff] [blame] | 1732 | |
Jean-Luc Brouillet | f33e156 | 2014-06-03 17:55:57 -0700 | [diff] [blame] | 1733 | mOut.indent() << "if (" RS_TYPE_ITEM_BUFFER_PACKER_NAME |
| 1734 | " != null) " RS_TYPE_ITEM_BUFFER_PACKER_NAME " = " |
Tim Murray | 3a38b74 | 2014-07-02 10:41:08 -0700 | [diff] [blame] | 1735 | "new FieldPacker(" << mItemSizeof << " * getType().getX()/* count */);\n"; |
Jason Sams | cedffd9 | 2010-12-16 15:29:49 -0800 | [diff] [blame] | 1736 | |
Jean-Luc Brouillet | 2e205d0 | 2014-06-02 21:06:52 -0700 | [diff] [blame] | 1737 | endFunction(); |
Zonr Chang | d42a429 | 2010-10-17 02:38:43 +0800 | [diff] [blame] | 1738 | } |
| 1739 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 1740 | /******************** Methods to generate type class /end ********************/ |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 1741 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 1742 | /********** Methods to create Element in Java of given record type ***********/ |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 1743 | |
Jean-Luc Brouillet | c643ceb | 2014-06-05 10:17:23 -0700 | [diff] [blame] | 1744 | RSReflectionJavaElementBuilder::RSReflectionJavaElementBuilder( |
| 1745 | const char *ElementBuilderName, const RSExportRecordType *ERT, |
| 1746 | const char *RenderScriptVar, GeneratedFile *Out, const RSContext *RSContext, |
| 1747 | RSReflectionJava *Reflection) |
| 1748 | : mElementBuilderName(ElementBuilderName), mERT(ERT), |
| 1749 | mRenderScriptVar(RenderScriptVar), mOut(Out), mPaddingFieldIndex(1), |
| 1750 | mRSContext(RSContext), mReflection(Reflection) { |
| 1751 | if (mRSContext->getTargetAPI() < SLANG_ICS_TARGET_API) { |
| 1752 | mPaddingPrefix = "#padding_"; |
| 1753 | } else { |
| 1754 | mPaddingPrefix = "#rs_padding_"; |
| 1755 | } |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 1756 | } |
| 1757 | |
Jean-Luc Brouillet | c643ceb | 2014-06-05 10:17:23 -0700 | [diff] [blame] | 1758 | void RSReflectionJavaElementBuilder::generate() { |
| 1759 | mOut->indent() << "Element.Builder " << mElementBuilderName |
| 1760 | << " = new Element.Builder(" << mRenderScriptVar << ");\n"; |
| 1761 | genAddElement(mERT, "", /* ArraySize = */ 0); |
| 1762 | } |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 1763 | |
Jean-Luc Brouillet | c643ceb | 2014-06-05 10:17:23 -0700 | [diff] [blame] | 1764 | void RSReflectionJavaElementBuilder::genAddElement(const RSExportType *ET, |
| 1765 | const std::string &VarName, |
| 1766 | unsigned ArraySize) { |
Stephen Hines | 47aca4e | 2012-03-08 20:07:28 -0800 | [diff] [blame] | 1767 | std::string ElementConstruct = GetBuiltinElementConstruct(ET); |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 1768 | |
Stephen Hines | 47aca4e | 2012-03-08 20:07:28 -0800 | [diff] [blame] | 1769 | if (ElementConstruct != "") { |
Jean-Luc Brouillet | c643ceb | 2014-06-05 10:17:23 -0700 | [diff] [blame] | 1770 | genAddStatementStart(); |
| 1771 | *mOut << ElementConstruct << "(" << mRenderScriptVar << ")"; |
| 1772 | genAddStatementEnd(VarName, ArraySize); |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 1773 | } else { |
Jean-Luc Brouillet | c643ceb | 2014-06-05 10:17:23 -0700 | [diff] [blame] | 1774 | |
| 1775 | switch (ET->getClass()) { |
| 1776 | case RSExportType::ExportClassPrimitive: { |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 1777 | const RSExportPrimitiveType *EPT = |
Jean-Luc Brouillet | 2ce118e | 2014-05-27 17:41:22 -0700 | [diff] [blame] | 1778 | static_cast<const RSExportPrimitiveType *>(ET); |
Stephen Hines | 47aca4e | 2012-03-08 20:07:28 -0800 | [diff] [blame] | 1779 | const char *DataTypeName = |
| 1780 | RSExportPrimitiveType::getRSReflectionType(EPT)->rs_type; |
Jean-Luc Brouillet | c643ceb | 2014-06-05 10:17:23 -0700 | [diff] [blame] | 1781 | genAddStatementStart(); |
| 1782 | *mOut << "Element.createUser(" << mRenderScriptVar |
| 1783 | << ", Element.DataType." << DataTypeName << ")"; |
| 1784 | genAddStatementEnd(VarName, ArraySize); |
| 1785 | break; |
| 1786 | } |
| 1787 | case RSExportType::ExportClassVector: { |
| 1788 | const RSExportVectorType *EVT = |
| 1789 | static_cast<const RSExportVectorType *>(ET); |
| 1790 | const char *DataTypeName = |
| 1791 | RSExportPrimitiveType::getRSReflectionType(EVT)->rs_type; |
| 1792 | genAddStatementStart(); |
| 1793 | *mOut << "Element.createVector(" << mRenderScriptVar |
| 1794 | << ", Element.DataType." << DataTypeName << ", " |
| 1795 | << EVT->getNumElement() << ")"; |
| 1796 | genAddStatementEnd(VarName, ArraySize); |
| 1797 | break; |
| 1798 | } |
| 1799 | case RSExportType::ExportClassPointer: |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 1800 | // Pointer type variable should be resolved in |
| 1801 | // GetBuiltinElementConstruct() |
Stephen Hines | 6e6578a | 2011-02-07 18:05:48 -0800 | [diff] [blame] | 1802 | slangAssert(false && "??"); |
Jean-Luc Brouillet | c643ceb | 2014-06-05 10:17:23 -0700 | [diff] [blame] | 1803 | break; |
| 1804 | case RSExportType::ExportClassMatrix: |
Zonr Chang | 92b344a | 2010-10-05 20:39:03 +0800 | [diff] [blame] | 1805 | // Matrix type variable should be resolved |
| 1806 | // in GetBuiltinElementConstruct() |
Stephen Hines | 6e6578a | 2011-02-07 18:05:48 -0800 | [diff] [blame] | 1807 | slangAssert(false && "??"); |
Jean-Luc Brouillet | c643ceb | 2014-06-05 10:17:23 -0700 | [diff] [blame] | 1808 | break; |
| 1809 | case RSExportType::ExportClassConstantArray: { |
Zonr Chang | 2e1dba6 | 2010-10-05 22:20:11 +0800 | [diff] [blame] | 1810 | const RSExportConstantArrayType *ECAT = |
| 1811 | static_cast<const RSExportConstantArrayType *>(ET); |
Zonr Chang | 2e1dba6 | 2010-10-05 22:20:11 +0800 | [diff] [blame] | 1812 | |
Zonr Chang | 89273bd | 2010-10-14 20:57:38 +0800 | [diff] [blame] | 1813 | const RSExportType *ElementType = ECAT->getElementType(); |
| 1814 | if (ElementType->getClass() != RSExportType::ExportClassRecord) { |
Jean-Luc Brouillet | c643ceb | 2014-06-05 10:17:23 -0700 | [diff] [blame] | 1815 | genAddElement(ECAT->getElementType(), VarName, ECAT->getSize()); |
Zonr Chang | 89273bd | 2010-10-14 20:57:38 +0800 | [diff] [blame] | 1816 | } else { |
Jean-Luc Brouillet | c643ceb | 2014-06-05 10:17:23 -0700 | [diff] [blame] | 1817 | std::string NewElementBuilderName(mElementBuilderName); |
Zonr Chang | 89273bd | 2010-10-14 20:57:38 +0800 | [diff] [blame] | 1818 | NewElementBuilderName.append(1, '_'); |
Zonr Chang | 2e1dba6 | 2010-10-05 22:20:11 +0800 | [diff] [blame] | 1819 | |
Jean-Luc Brouillet | c643ceb | 2014-06-05 10:17:23 -0700 | [diff] [blame] | 1820 | RSReflectionJavaElementBuilder builder( |
| 1821 | NewElementBuilderName.c_str(), |
| 1822 | static_cast<const RSExportRecordType *>(ElementType), |
| 1823 | mRenderScriptVar, mOut, mRSContext, mReflection); |
| 1824 | builder.generate(); |
| 1825 | |
Zonr Chang | 89273bd | 2010-10-14 20:57:38 +0800 | [diff] [blame] | 1826 | ArraySize = ECAT->getSize(); |
Jean-Luc Brouillet | c643ceb | 2014-06-05 10:17:23 -0700 | [diff] [blame] | 1827 | genAddStatementStart(); |
| 1828 | *mOut << NewElementBuilderName << ".create()"; |
| 1829 | genAddStatementEnd(VarName, ArraySize); |
Zonr Chang | 89273bd | 2010-10-14 20:57:38 +0800 | [diff] [blame] | 1830 | } |
Jean-Luc Brouillet | c643ceb | 2014-06-05 10:17:23 -0700 | [diff] [blame] | 1831 | break; |
| 1832 | } |
| 1833 | case RSExportType::ExportClassRecord: { |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 1834 | // Simalar to case of RSExportType::ExportClassRecord in genPackVarOfType. |
| 1835 | // |
zonr | 6315f76 | 2010-10-05 15:35:14 +0800 | [diff] [blame] | 1836 | // TODO(zonr): Generalize these two function such that there's no |
| 1837 | // duplicated codes. |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 1838 | const RSExportRecordType *ERT = |
Jean-Luc Brouillet | 2ce118e | 2014-05-27 17:41:22 -0700 | [diff] [blame] | 1839 | static_cast<const RSExportRecordType *>(ET); |
| 1840 | int Pos = 0; // relative pos from now on |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 1841 | |
| 1842 | for (RSExportRecordType::const_field_iterator I = ERT->fields_begin(), |
Jean-Luc Brouillet | 2ce118e | 2014-05-27 17:41:22 -0700 | [diff] [blame] | 1843 | E = ERT->fields_end(); |
| 1844 | I != E; I++) { |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 1845 | const RSExportRecordType::Field *F = *I; |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 1846 | int FieldOffset = F->getOffsetInParent(); |
Jean-Luc Brouillet | c95381a | 2014-05-14 21:24:45 -0700 | [diff] [blame] | 1847 | const RSExportType *T = F->getType(); |
| 1848 | int FieldStoreSize = T->getStoreSize(); |
| 1849 | int FieldAllocSize = T->getAllocSize(); |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 1850 | |
Jean-Luc Brouillet | c643ceb | 2014-06-05 10:17:23 -0700 | [diff] [blame] | 1851 | std::string FieldName; |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 1852 | if (!VarName.empty()) |
| 1853 | FieldName = VarName + "." + F->getName(); |
| 1854 | else |
| 1855 | FieldName = F->getName(); |
| 1856 | |
| 1857 | // Alignment |
Jean-Luc Brouillet | c643ceb | 2014-06-05 10:17:23 -0700 | [diff] [blame] | 1858 | genAddPadding(FieldOffset - Pos); |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 1859 | |
| 1860 | // eb.add(...) |
Jean-Luc Brouillet | c643ceb | 2014-06-05 10:17:23 -0700 | [diff] [blame] | 1861 | mReflection->addFieldIndexMapping(F); |
Zonr Chang | 89273bd | 2010-10-14 20:57:38 +0800 | [diff] [blame] | 1862 | if (F->getType()->getClass() != RSExportType::ExportClassRecord) { |
Jean-Luc Brouillet | c643ceb | 2014-06-05 10:17:23 -0700 | [diff] [blame] | 1863 | genAddElement(F->getType(), FieldName, 0); |
Zonr Chang | 89273bd | 2010-10-14 20:57:38 +0800 | [diff] [blame] | 1864 | } else { |
Jean-Luc Brouillet | c643ceb | 2014-06-05 10:17:23 -0700 | [diff] [blame] | 1865 | std::string NewElementBuilderName(mElementBuilderName); |
Zonr Chang | 89273bd | 2010-10-14 20:57:38 +0800 | [diff] [blame] | 1866 | NewElementBuilderName.append(1, '_'); |
| 1867 | |
Jean-Luc Brouillet | c643ceb | 2014-06-05 10:17:23 -0700 | [diff] [blame] | 1868 | RSReflectionJavaElementBuilder builder( |
| 1869 | NewElementBuilderName.c_str(), |
| 1870 | static_cast<const RSExportRecordType *>(F->getType()), |
| 1871 | mRenderScriptVar, mOut, mRSContext, mReflection); |
| 1872 | builder.generate(); |
Zonr Chang | 89273bd | 2010-10-14 20:57:38 +0800 | [diff] [blame] | 1873 | |
Jean-Luc Brouillet | c643ceb | 2014-06-05 10:17:23 -0700 | [diff] [blame] | 1874 | genAddStatementStart(); |
| 1875 | *mOut << NewElementBuilderName << ".create()"; |
| 1876 | genAddStatementEnd(FieldName, ArraySize); |
Zonr Chang | 89273bd | 2010-10-14 20:57:38 +0800 | [diff] [blame] | 1877 | } |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 1878 | |
Stephen Hines | a9ae5ae | 2011-11-11 21:16:59 -0800 | [diff] [blame] | 1879 | if (mRSContext->getTargetAPI() < SLANG_ICS_TARGET_API) { |
| 1880 | // There is padding within the field type. This is only necessary |
| 1881 | // for HC-targeted APIs. |
Jean-Luc Brouillet | c643ceb | 2014-06-05 10:17:23 -0700 | [diff] [blame] | 1882 | genAddPadding(FieldAllocSize - FieldStoreSize); |
Stephen Hines | a9ae5ae | 2011-11-11 21:16:59 -0800 | [diff] [blame] | 1883 | } |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 1884 | |
| 1885 | Pos = FieldOffset + FieldAllocSize; |
| 1886 | } |
| 1887 | |
| 1888 | // There maybe some padding after the struct |
Jean-Luc Brouillet | c95381a | 2014-05-14 21:24:45 -0700 | [diff] [blame] | 1889 | size_t RecordAllocSize = ERT->getAllocSize(); |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 1890 | |
Jean-Luc Brouillet | c643ceb | 2014-06-05 10:17:23 -0700 | [diff] [blame] | 1891 | genAddPadding(RecordAllocSize - Pos); |
| 1892 | break; |
| 1893 | } |
| 1894 | default: |
Stephen Hines | 6e6578a | 2011-02-07 18:05:48 -0800 | [diff] [blame] | 1895 | slangAssert(false && "Unknown class of type"); |
Jean-Luc Brouillet | c643ceb | 2014-06-05 10:17:23 -0700 | [diff] [blame] | 1896 | break; |
Shih-wei Liao | b1a28e7 | 2010-06-16 16:31:32 -0700 | [diff] [blame] | 1897 | } |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 1898 | } |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 1899 | } |
| 1900 | |
Jean-Luc Brouillet | c643ceb | 2014-06-05 10:17:23 -0700 | [diff] [blame] | 1901 | void RSReflectionJavaElementBuilder::genAddPadding(int PaddingSize) { |
| 1902 | while (PaddingSize > 0) { |
| 1903 | const std::string &VarName = createPaddingField(); |
| 1904 | genAddStatementStart(); |
| 1905 | if (PaddingSize >= 4) { |
| 1906 | *mOut << "Element.U32(" << mRenderScriptVar << ")"; |
| 1907 | PaddingSize -= 4; |
| 1908 | } else if (PaddingSize >= 2) { |
| 1909 | *mOut << "Element.U16(" << mRenderScriptVar << ")"; |
| 1910 | PaddingSize -= 2; |
| 1911 | } else if (PaddingSize >= 1) { |
| 1912 | *mOut << "Element.U8(" << mRenderScriptVar << ")"; |
| 1913 | PaddingSize -= 1; |
| 1914 | } |
| 1915 | genAddStatementEnd(VarName, 0); |
| 1916 | } |
| 1917 | } |
| 1918 | |
| 1919 | void RSReflectionJavaElementBuilder::genAddStatementStart() { |
| 1920 | mOut->indent() << mElementBuilderName << ".add("; |
| 1921 | } |
| 1922 | |
Jean-Luc Brouillet | 602def7 | 2014-05-27 16:11:37 -0700 | [diff] [blame] | 1923 | void |
Jean-Luc Brouillet | c643ceb | 2014-06-05 10:17:23 -0700 | [diff] [blame] | 1924 | RSReflectionJavaElementBuilder::genAddStatementEnd(const std::string &VarName, |
| 1925 | unsigned ArraySize) { |
| 1926 | *mOut << ", \"" << VarName << "\""; |
| 1927 | if (ArraySize > 0) { |
| 1928 | *mOut << ", " << ArraySize; |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 1929 | } |
Jean-Luc Brouillet | c643ceb | 2014-06-05 10:17:23 -0700 | [diff] [blame] | 1930 | *mOut << ");\n"; |
| 1931 | // TODO Review incFieldIndex. It's probably better to assign the numbers at |
| 1932 | // the start rather |
| 1933 | // than as we're generating the code. |
| 1934 | mReflection->incFieldIndex(); |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 1935 | } |
| 1936 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 1937 | /******** Methods to create Element in Java of given record type /end ********/ |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 1938 | |
Jean-Luc Brouillet | 59f22c3 | 2014-06-04 14:53:48 -0700 | [diff] [blame] | 1939 | bool RSReflectionJava::reflect() { |
| 1940 | std::string ErrorMsg; |
| 1941 | if (!genScriptClass(mScriptClassName, ErrorMsg)) { |
| 1942 | std::cerr << "Failed to generate class " << mScriptClassName << " (" |
Jean-Luc Brouillet | 2e205d0 | 2014-06-02 21:06:52 -0700 | [diff] [blame] | 1943 | << ErrorMsg << ")\n"; |
| 1944 | return false; |
| 1945 | } |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 1946 | |
Jean-Luc Brouillet | 59f22c3 | 2014-06-04 14:53:48 -0700 | [diff] [blame] | 1947 | mGeneratedFileNames->push_back(mScriptClassName); |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 1948 | |
Jean-Luc Brouillet | 2e205d0 | 2014-06-02 21:06:52 -0700 | [diff] [blame] | 1949 | // class ScriptField_<TypeName> |
| 1950 | for (RSContext::const_export_type_iterator |
| 1951 | TI = mRSContext->export_types_begin(), |
| 1952 | TE = mRSContext->export_types_end(); |
| 1953 | TI != TE; TI++) { |
| 1954 | const RSExportType *ET = TI->getValue(); |
| 1955 | |
| 1956 | if (ET->getClass() == RSExportType::ExportClassRecord) { |
| 1957 | const RSExportRecordType *ERT = |
| 1958 | static_cast<const RSExportRecordType *>(ET); |
| 1959 | |
| 1960 | if (!ERT->isArtificial() && !genTypeClass(ERT, ErrorMsg)) { |
| 1961 | std::cerr << "Failed to generate type class for struct '" |
| 1962 | << ERT->getName() << "' (" << ErrorMsg << ")\n"; |
| 1963 | return false; |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 1964 | } |
| 1965 | } |
| 1966 | } |
| 1967 | |
| 1968 | return true; |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 1969 | } |
| 1970 | |
Jean-Luc Brouillet | 2e205d0 | 2014-06-02 21:06:52 -0700 | [diff] [blame] | 1971 | const char *RSReflectionJava::AccessModifierStr(AccessModifier AM) { |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 1972 | switch (AM) { |
Jean-Luc Brouillet | 2ce118e | 2014-05-27 17:41:22 -0700 | [diff] [blame] | 1973 | case AM_Public: |
| 1974 | return "public"; |
| 1975 | break; |
| 1976 | case AM_Protected: |
| 1977 | return "protected"; |
| 1978 | break; |
| 1979 | case AM_Private: |
| 1980 | return "private"; |
| 1981 | break; |
| 1982 | case AM_PublicSynchronized: |
| 1983 | return "public synchronized"; |
| 1984 | break; |
| 1985 | default: |
| 1986 | return ""; |
| 1987 | break; |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 1988 | } |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 1989 | } |
| 1990 | |
Jean-Luc Brouillet | 2e205d0 | 2014-06-02 21:06:52 -0700 | [diff] [blame] | 1991 | bool RSReflectionJava::startClass(AccessModifier AM, bool IsStatic, |
| 1992 | const std::string &ClassName, |
| 1993 | const char *SuperClassName, |
| 1994 | std::string &ErrorMsg) { |
Zonr Chang | 8c6d9b2 | 2010-10-07 18:01:19 +0800 | [diff] [blame] | 1995 | // Open file for class |
Jean-Luc Brouillet | f33e156 | 2014-06-03 17:55:57 -0700 | [diff] [blame] | 1996 | std::string FileName = ClassName + ".java"; |
Jean-Luc Brouillet | 59f22c3 | 2014-06-04 14:53:48 -0700 | [diff] [blame] | 1997 | if (!mOut.startFile(mOutputDirectory, FileName, mRSSourceFileName, |
Stephen Hines | fc4f78b | 2014-06-10 18:07:10 -0700 | [diff] [blame] | 1998 | mRSContext->getLicenseNote(), true, |
| 1999 | mRSContext->getVerbose())) { |
Zonr Chang | 8c6d9b2 | 2010-10-07 18:01:19 +0800 | [diff] [blame] | 2000 | return false; |
Jean-Luc Brouillet | f33e156 | 2014-06-03 17:55:57 -0700 | [diff] [blame] | 2001 | } |
Ying Wang | 4e34844 | 2010-08-18 10:29:12 -0700 | [diff] [blame] | 2002 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 2003 | // Package |
Jean-Luc Brouillet | f33e156 | 2014-06-03 17:55:57 -0700 | [diff] [blame] | 2004 | if (!mPackageName.empty()) { |
| 2005 | mOut << "package " << mPackageName << ";\n"; |
| 2006 | } |
| 2007 | mOut << "\n"; |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 2008 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 2009 | // Imports |
Jean-Luc Brouillet | f33e156 | 2014-06-03 17:55:57 -0700 | [diff] [blame] | 2010 | mOut << "import " << mRSPackageName << ".*;\n"; |
Stephen Hines | 44d495d | 2014-05-22 19:42:55 -0700 | [diff] [blame] | 2011 | if (getEmbedBitcodeInJava()) { |
Jean-Luc Brouillet | f33e156 | 2014-06-03 17:55:57 -0700 | [diff] [blame] | 2012 | mOut << "import " << mPackageName << "." |
Stephen Hines | 44d495d | 2014-05-22 19:42:55 -0700 | [diff] [blame] | 2013 | << RSSlangReflectUtils::JavaBitcodeClassNameFromRSFileName( |
Jean-Luc Brouillet | 59f22c3 | 2014-06-04 14:53:48 -0700 | [diff] [blame] | 2014 | mRSSourceFileName.c_str()) << ";\n"; |
Stephen Hines | 44d495d | 2014-05-22 19:42:55 -0700 | [diff] [blame] | 2015 | } else { |
Jean-Luc Brouillet | f33e156 | 2014-06-03 17:55:57 -0700 | [diff] [blame] | 2016 | mOut << "import android.content.res.Resources;\n"; |
Stephen Hines | 44d495d | 2014-05-22 19:42:55 -0700 | [diff] [blame] | 2017 | } |
Jean-Luc Brouillet | f33e156 | 2014-06-03 17:55:57 -0700 | [diff] [blame] | 2018 | mOut << "\n"; |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 2019 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 2020 | // All reflected classes should be annotated as hidden, so that they won't |
| 2021 | // be exposed in SDK. |
Jean-Luc Brouillet | f33e156 | 2014-06-03 17:55:57 -0700 | [diff] [blame] | 2022 | mOut << "/**\n"; |
| 2023 | mOut << " * @hide\n"; |
| 2024 | mOut << " */\n"; |
Ying Wang | 4e34844 | 2010-08-18 10:29:12 -0700 | [diff] [blame] | 2025 | |
Jean-Luc Brouillet | f33e156 | 2014-06-03 17:55:57 -0700 | [diff] [blame] | 2026 | mOut << AccessModifierStr(AM) << ((IsStatic) ? " static" : "") << " class " |
| 2027 | << ClassName; |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 2028 | if (SuperClassName != NULL) |
Jean-Luc Brouillet | f33e156 | 2014-06-03 17:55:57 -0700 | [diff] [blame] | 2029 | mOut << " extends " << SuperClassName; |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 2030 | |
Jean-Luc Brouillet | f33e156 | 2014-06-03 17:55:57 -0700 | [diff] [blame] | 2031 | mOut.startBlock(); |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 2032 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 2033 | mClassName = ClassName; |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 2034 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 2035 | return true; |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 2036 | } |
| 2037 | |
Jean-Luc Brouillet | 2e205d0 | 2014-06-02 21:06:52 -0700 | [diff] [blame] | 2038 | void RSReflectionJava::endClass() { |
Jean-Luc Brouillet | f33e156 | 2014-06-03 17:55:57 -0700 | [diff] [blame] | 2039 | mOut.endBlock(); |
| 2040 | mOut.closeFile(); |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 2041 | clear(); |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 2042 | } |
| 2043 | |
Jean-Luc Brouillet | 2e205d0 | 2014-06-02 21:06:52 -0700 | [diff] [blame] | 2044 | void RSReflectionJava::startTypeClass(const std::string &ClassName) { |
Jean-Luc Brouillet | f33e156 | 2014-06-03 17:55:57 -0700 | [diff] [blame] | 2045 | mOut.indent() << "public static class " << ClassName; |
| 2046 | mOut.startBlock(); |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 2047 | } |
| 2048 | |
Jean-Luc Brouillet | f33e156 | 2014-06-03 17:55:57 -0700 | [diff] [blame] | 2049 | void RSReflectionJava::endTypeClass() { mOut.endBlock(); } |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 2050 | |
Jean-Luc Brouillet | 2e205d0 | 2014-06-02 21:06:52 -0700 | [diff] [blame] | 2051 | void RSReflectionJava::startFunction(AccessModifier AM, bool IsStatic, |
| 2052 | const char *ReturnType, |
| 2053 | const std::string &FunctionName, int Argc, |
| 2054 | ...) { |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 2055 | ArgTy Args; |
| 2056 | va_list vl; |
| 2057 | va_start(vl, Argc); |
| 2058 | |
zonr | 6315f76 | 2010-10-05 15:35:14 +0800 | [diff] [blame] | 2059 | for (int i = 0; i < Argc; i++) { |
Jean-Luc Brouillet | 2ce118e | 2014-05-27 17:41:22 -0700 | [diff] [blame] | 2060 | const char *ArgType = va_arg(vl, const char *); |
| 2061 | const char *ArgName = va_arg(vl, const char *); |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 2062 | |
zonr | 6315f76 | 2010-10-05 15:35:14 +0800 | [diff] [blame] | 2063 | Args.push_back(std::make_pair(ArgType, ArgName)); |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 2064 | } |
| 2065 | va_end(vl); |
| 2066 | |
| 2067 | startFunction(AM, IsStatic, ReturnType, FunctionName, Args); |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 2068 | } |
| 2069 | |
Jean-Luc Brouillet | 2e205d0 | 2014-06-02 21:06:52 -0700 | [diff] [blame] | 2070 | void RSReflectionJava::startFunction(AccessModifier AM, bool IsStatic, |
| 2071 | const char *ReturnType, |
| 2072 | const std::string &FunctionName, |
| 2073 | const ArgTy &Args) { |
Jean-Luc Brouillet | f33e156 | 2014-06-03 17:55:57 -0700 | [diff] [blame] | 2074 | mOut.indent() << AccessModifierStr(AM) << ((IsStatic) ? " static " : " ") |
| 2075 | << ((ReturnType) ? ReturnType : "") << " " << FunctionName |
| 2076 | << "("; |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 2077 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 2078 | bool FirstArg = true; |
Jean-Luc Brouillet | 2ce118e | 2014-05-27 17:41:22 -0700 | [diff] [blame] | 2079 | for (ArgTy::const_iterator I = Args.begin(), E = Args.end(); I != E; I++) { |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 2080 | if (!FirstArg) |
Jean-Luc Brouillet | f33e156 | 2014-06-03 17:55:57 -0700 | [diff] [blame] | 2081 | mOut << ", "; |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 2082 | else |
| 2083 | FirstArg = false; |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 2084 | |
Jean-Luc Brouillet | f33e156 | 2014-06-03 17:55:57 -0700 | [diff] [blame] | 2085 | mOut << I->first << " " << I->second; |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 2086 | } |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 2087 | |
Jean-Luc Brouillet | f33e156 | 2014-06-03 17:55:57 -0700 | [diff] [blame] | 2088 | mOut << ")"; |
| 2089 | mOut.startBlock(); |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 2090 | } |
| 2091 | |
Jean-Luc Brouillet | f33e156 | 2014-06-03 17:55:57 -0700 | [diff] [blame] | 2092 | void RSReflectionJava::endFunction() { mOut.endBlock(); } |
Stephen Hines | e639eb5 | 2010-11-08 19:27:20 -0800 | [diff] [blame] | 2093 | |
Jean-Luc Brouillet | 2e205d0 | 2014-06-02 21:06:52 -0700 | [diff] [blame] | 2094 | bool RSReflectionJava::addTypeNameForElement(const std::string &TypeName) { |
Stephen Hines | 1f6c331 | 2012-07-03 17:23:33 -0700 | [diff] [blame] | 2095 | if (mTypesToCheck.find(TypeName) == mTypesToCheck.end()) { |
| 2096 | mTypesToCheck.insert(TypeName); |
| 2097 | return true; |
| 2098 | } else { |
| 2099 | return false; |
| 2100 | } |
| 2101 | } |
| 2102 | |
Jean-Luc Brouillet | 2e205d0 | 2014-06-02 21:06:52 -0700 | [diff] [blame] | 2103 | bool RSReflectionJava::addTypeNameForFieldPacker(const std::string &TypeName) { |
Stephen Hines | 1f6c331 | 2012-07-03 17:23:33 -0700 | [diff] [blame] | 2104 | if (mFieldPackerTypes.find(TypeName) == mFieldPackerTypes.end()) { |
| 2105 | mFieldPackerTypes.insert(TypeName); |
| 2106 | return true; |
| 2107 | } else { |
| 2108 | return false; |
| 2109 | } |
| 2110 | } |
| 2111 | |
Jean-Luc Brouillet | 2ce118e | 2014-05-27 17:41:22 -0700 | [diff] [blame] | 2112 | } // namespace slang |