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