slang_rs_reflection.cpp revision e639eb5caa2c386b4a60659a4929e8a6141a2cbe
1/* 2 * Copyright 2010, The Android Open Source Project 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 17#include "slang_rs_reflection.h" 18 19#include <sys/stat.h> 20 21#include <cstdarg> 22#include <cctype> 23 24#include <algorithm> 25#include <string> 26#include <utility> 27 28#include "llvm/ADT/APFloat.h" 29#include "llvm/ADT/StringExtras.h" 30 31#include "slang_rs_context.h" 32#include "slang_rs_export_var.h" 33#include "slang_rs_export_func.h" 34#include "slang_rs_reflect_utils.h" 35#include "slang_utils.h" 36 37#define RS_SCRIPT_CLASS_NAME_PREFIX "ScriptC_" 38#define RS_SCRIPT_CLASS_SUPER_CLASS_NAME "ScriptC" 39 40#define RS_TYPE_CLASS_NAME_PREFIX "ScriptField_" 41#define RS_TYPE_CLASS_SUPER_CLASS_NAME "android.renderscript.Script.FieldBase" 42 43#define RS_TYPE_ITEM_CLASS_NAME "Item" 44 45#define RS_TYPE_ITEM_BUFFER_NAME "mItemArray" 46#define RS_TYPE_ITEM_BUFFER_PACKER_NAME "mIOBuffer" 47 48#define RS_EXPORT_VAR_INDEX_PREFIX "mExportVarIdx_" 49#define RS_EXPORT_VAR_PREFIX "mExportVar_" 50 51#define RS_EXPORT_FUNC_INDEX_PREFIX "mExportFuncIdx_" 52 53#define RS_EXPORT_VAR_ALLOCATION_PREFIX "mAlloction_" 54#define RS_EXPORT_VAR_DATA_STORAGE_PREFIX "mData_" 55 56namespace slang { 57 58// Some utility function using internal in RSReflection 59static bool GetClassNameFromFileName(const std::string &FileName, 60 std::string &ClassName) { 61 ClassName.clear(); 62 63 if (FileName.empty() || (FileName == "-")) 64 return true; 65 66 ClassName = 67 RSSlangReflectUtils::JavaClassNameFromRSFileName(FileName.c_str()); 68 69 return true; 70} 71 72static const char *GetPrimitiveTypeName(const RSExportPrimitiveType *EPT) { 73 static const char *PrimitiveTypeJavaNameMap[] = { 74 "", // RSExportPrimitiveType::DataTypeFloat16 75 "float", // RSExportPrimitiveType::DataTypeFloat32 76 "double", // RSExportPrimitiveType::DataTypeFloat64 77 "byte", // RSExportPrimitiveType::DataTypeSigned8 78 "short", // RSExportPrimitiveType::DataTypeSigned16 79 "int", // RSExportPrimitiveType::DataTypeSigned32 80 "long", // RSExportPrimitiveType::DataTypeSigned64 81 "short", // RSExportPrimitiveType::DataTypeUnsigned8 82 "int", // RSExportPrimitiveType::DataTypeUnsigned16 83 "long", // RSExportPrimitiveType::DataTypeUnsigned32 84 "long", // RSExportPrimitiveType::DataTypeUnsigned64 85 "boolean", // RSExportPrimitiveType::DataTypeBoolean 86 87 "int", // RSExportPrimitiveType::DataTypeUnsigned565 88 "int", // RSExportPrimitiveType::DataTypeUnsigned5551 89 "int", // RSExportPrimitiveType::DataTypeUnsigned4444 90 91 "", // (Dummy) RSExportPrimitiveType::DataTypeRSMatrix2x2 92 "", // (Dummy) RSExportPrimitiveType::DataTypeRSMatrix3x3 93 "", // (Dummy) RSExportPrimitiveType::DataTypeRSMatrix4x4 94 95 "Element", // RSExportPrimitiveType::DataTypeRSElement 96 "Type", // RSExportPrimitiveType::DataTypeRSType 97 "Allocation", // RSExportPrimitiveType::DataTypeRSAllocation 98 "Sampler", // RSExportPrimitiveType::DataTypeRSSampler 99 "Script", // RSExportPrimitiveType::DataTypeRSScript 100 "Mesh", // RSExportPrimitiveType::DataTypeRSMesh 101 "ProgramFragment", // RSExportPrimitiveType::DataTypeRSProgramFragment 102 "ProgramVertex", // RSExportPrimitiveType::DataTypeRSProgramVertex 103 "ProgramRaster", // RSExportPrimitiveType::DataTypeRSProgramRaster 104 "ProgramStore", // RSExportPrimitiveType::DataTypeRSProgramStore 105 "Font", // RSExportPrimitiveType::DataTypeRSFont 106 }; 107 unsigned TypeId = EPT->getType(); 108 109 if (TypeId < (sizeof(PrimitiveTypeJavaNameMap) / sizeof(const char*))) { 110 return PrimitiveTypeJavaNameMap[ EPT->getType() ]; 111 } 112 113 assert(false && "GetPrimitiveTypeName : Unknown primitive data type"); 114 return NULL; 115} 116 117static const char *GetVectorTypeName(const RSExportVectorType *EVT) { 118 static const char *VectorTypeJavaNameMap[][3] = { 119 /* 0 */ { "Byte2", "Byte3", "Byte4" }, 120 /* 1 */ { "Short2", "Short3", "Short4" }, 121 /* 2 */ { "Int2", "Int3", "Int4" }, 122 /* 3 */ { "Long2", "Long3", "Long4" }, 123 /* 4 */ { "Float2", "Float3", "Float4" }, 124 }; 125 126 const char **BaseElement = NULL; 127 128 switch (EVT->getType()) { 129 case RSExportPrimitiveType::DataTypeSigned8: 130 case RSExportPrimitiveType::DataTypeBoolean: { 131 BaseElement = VectorTypeJavaNameMap[0]; 132 break; 133 } 134 case RSExportPrimitiveType::DataTypeSigned16: 135 case RSExportPrimitiveType::DataTypeUnsigned8: { 136 BaseElement = VectorTypeJavaNameMap[1]; 137 break; 138 } 139 case RSExportPrimitiveType::DataTypeSigned32: 140 case RSExportPrimitiveType::DataTypeUnsigned16: { 141 BaseElement = VectorTypeJavaNameMap[2]; 142 break; 143 } 144 case RSExportPrimitiveType::DataTypeSigned64: 145 case RSExportPrimitiveType::DataTypeUnsigned64: 146 case RSExportPrimitiveType::DataTypeUnsigned32: { 147 BaseElement = VectorTypeJavaNameMap[3]; 148 break; 149 } 150 case RSExportPrimitiveType::DataTypeFloat32: { 151 BaseElement = VectorTypeJavaNameMap[4]; 152 break; 153 } 154 default: { 155 assert(false && "RSReflection::genElementTypeName : Unsupported vector " 156 "element data type"); 157 break; 158 } 159 } 160 161 assert((EVT->getNumElement() > 1) && 162 (EVT->getNumElement() <= 4) && 163 "Number of element in vector type is invalid"); 164 165 return BaseElement[EVT->getNumElement() - 2]; 166} 167 168static const char *GetMatrixTypeName(const RSExportMatrixType *EMT) { 169 static const char *MatrixTypeJavaNameMap[] = { 170 /* 2x2 */ "Matrix2f", 171 /* 3x3 */ "Matrix3f", 172 /* 4x4 */ "Matrix4f", 173 }; 174 unsigned Dim = EMT->getDim(); 175 176 if ((Dim - 2) < (sizeof(MatrixTypeJavaNameMap) / sizeof(const char*))) 177 return MatrixTypeJavaNameMap[ EMT->getDim() - 2 ]; 178 179 assert(false && "GetMatrixTypeName : Unsupported matrix dimension"); 180 return NULL; 181} 182 183static const char *GetVectorAccessor(int Index) { 184 static const char *VectorAccessorMap[] = { 185 /* 0 */ "x", 186 /* 1 */ "y", 187 /* 2 */ "z", 188 /* 3 */ "w", 189 }; 190 191 assert((Index >= 0) && 192 (Index < (sizeof(VectorAccessorMap) / sizeof(const char*))) && 193 "Out-of-bound index to access vector member"); 194 195 return VectorAccessorMap[Index]; 196} 197 198static const char *GetPackerAPIName(const RSExportPrimitiveType *EPT) { 199 static const char *PrimitiveTypePackerAPINameMap[] = { 200 "", // RSExportPrimitiveType::DataTypeFloat16 201 "addF32", // RSExportPrimitiveType::DataTypeFloat32 202 "addF64", // RSExportPrimitiveType::DataTypeFloat64 203 "addI8", // RSExportPrimitiveType::DataTypeSigned8 204 "addI16", // RSExportPrimitiveType::DataTypeSigned16 205 "addI32", // RSExportPrimitiveType::DataTypeSigned32 206 "addI64", // RSExportPrimitiveType::DataTypeSigned64 207 "addU8", // RSExportPrimitiveType::DataTypeUnsigned8 208 "addU16", // RSExportPrimitiveType::DataTypeUnsigned16 209 "addU32", // RSExportPrimitiveType::DataTypeUnsigned32 210 "addU64", // RSExportPrimitiveType::DataTypeUnsigned64 211 "addBoolean", // RSExportPrimitiveType::DataTypeBoolean 212 213 "addU16", // RSExportPrimitiveType::DataTypeUnsigned565 214 "addU16", // RSExportPrimitiveType::DataTypeUnsigned5551 215 "addU16", // RSExportPrimitiveType::DataTypeUnsigned4444 216 217 "addObj", // RSExportPrimitiveType::DataTypeRSMatrix2x2 218 "addObj", // RSExportPrimitiveType::DataTypeRSMatrix3x3 219 "addObj", // RSExportPrimitiveType::DataTypeRSMatrix4x4 220 221 "addObj", // RSExportPrimitiveType::DataTypeRSElement 222 "addObj", // RSExportPrimitiveType::DataTypeRSType 223 "addObj", // RSExportPrimitiveType::DataTypeRSAllocation 224 "addObj", // RSExportPrimitiveType::DataTypeRSSampler 225 "addObj", // RSExportPrimitiveType::DataTypeRSScript 226 "addObj", // RSExportPrimitiveType::DataTypeRSMesh 227 "addObj", // RSExportPrimitiveType::DataTypeRSProgramFragment 228 "addObj", // RSExportPrimitiveType::DataTypeRSProgramVertex 229 "addObj", // RSExportPrimitiveType::DataTypeRSProgramRaster 230 "addObj", // RSExportPrimitiveType::DataTypeRSProgramStore 231 "addObj", // RSExportPrimitiveType::DataTypeRSFont 232 }; 233 unsigned TypeId = EPT->getType(); 234 235 if (TypeId < (sizeof(PrimitiveTypePackerAPINameMap) / sizeof(const char*))) 236 return PrimitiveTypePackerAPINameMap[ EPT->getType() ]; 237 238 assert(false && "GetPackerAPIName : Unknown primitive data type"); 239 return NULL; 240} 241 242static std::string GetTypeName(const RSExportType *ET) { 243 switch (ET->getClass()) { 244 case RSExportType::ExportClassPrimitive: { 245 return GetPrimitiveTypeName( 246 static_cast<const RSExportPrimitiveType*>(ET)); 247 } 248 case RSExportType::ExportClassPointer: { 249 const RSExportType *PointeeType = 250 static_cast<const RSExportPointerType*>(ET)->getPointeeType(); 251 252 if (PointeeType->getClass() != RSExportType::ExportClassRecord) 253 return "Allocation"; 254 else 255 return RS_TYPE_CLASS_NAME_PREFIX + PointeeType->getName(); 256 } 257 case RSExportType::ExportClassVector: { 258 return GetVectorTypeName(static_cast<const RSExportVectorType*>(ET)); 259 } 260 case RSExportType::ExportClassMatrix: { 261 return GetMatrixTypeName(static_cast<const RSExportMatrixType*>(ET)); 262 } 263 case RSExportType::ExportClassConstantArray: { 264 const RSExportConstantArrayType* CAT = 265 static_cast<const RSExportConstantArrayType*>(ET); 266 std::string ElementTypeName = GetTypeName(CAT->getElementType()); 267 ElementTypeName.append("[]"); 268 return ElementTypeName; 269 } 270 case RSExportType::ExportClassRecord: { 271 return RS_TYPE_CLASS_NAME_PREFIX + ET->getName() + 272 "."RS_TYPE_ITEM_CLASS_NAME; 273 } 274 default: { 275 assert(false && "Unknown class of type"); 276 } 277 } 278 279 return ""; 280} 281 282static const char *GetTypeNullValue(const RSExportType *ET) { 283 switch (ET->getClass()) { 284 case RSExportType::ExportClassPrimitive: { 285 const RSExportPrimitiveType *EPT = 286 static_cast<const RSExportPrimitiveType*>(ET); 287 if (EPT->isRSObjectType()) 288 return "null"; 289 else if (EPT->getType() == RSExportPrimitiveType::DataTypeBoolean) 290 return "false"; 291 else 292 return "0"; 293 break; 294 } 295 case RSExportType::ExportClassPointer: 296 case RSExportType::ExportClassVector: 297 case RSExportType::ExportClassMatrix: 298 case RSExportType::ExportClassConstantArray: 299 case RSExportType::ExportClassRecord: { 300 return "null"; 301 break; 302 } 303 default: { 304 assert(false && "Unknown class of type"); 305 } 306 } 307 return ""; 308} 309 310static const char *GetBuiltinElementConstruct(const RSExportType *ET) { 311 if (ET->getClass() == RSExportType::ExportClassPrimitive) { 312 const RSExportPrimitiveType *EPT = 313 static_cast<const RSExportPrimitiveType*>(ET); 314 if (EPT->getKind() == RSExportPrimitiveType::DataKindUser) { 315 static const char *PrimitiveBuiltinElementConstructMap[] = { 316 NULL, // RSExportPrimitiveType::DataTypeFloat16 317 "Element.F32", // RSExportPrimitiveType::DataTypeFloat32 318 "Element.F64", // RSExportPrimitiveType::DataTypeFloat64 319 "Element.I8", // RSExportPrimitiveType::DataTypeSigned8 320 NULL, // RSExportPrimitiveType::DataTypeSigned16 321 "Element.I32", // RSExportPrimitiveType::DataTypeSigned32 322 "Element.I64", // RSExportPrimitiveType::DataTypeSigned64 323 "Element.U8", // RSExportPrimitiveType::DataTypeUnsigned8 324 NULL, // RSExportPrimitiveType::DataTypeUnsigned16 325 "Element.U32", // RSExportPrimitiveType::DataTypeUnsigned32 326 "Element.U64", // RSExportPrimitiveType::DataTypeUnsigned64 327 "Element.BOOLEAN", // RSExportPrimitiveType::DataTypeBoolean 328 329 NULL, // RSExportPrimitiveType::DataTypeUnsigned565 330 NULL, // RSExportPrimitiveType::DataTypeUnsigned5551 331 NULL, // RSExportPrimitiveType::DataTypeUnsigned4444 332 333 NULL, // (Dummy) RSExportPrimitiveType::DataTypeRSMatrix2x2 334 NULL, // (Dummy) RSExportPrimitiveType::DataTypeRSMatrix3x3 335 NULL, // (Dummy) RSExportPrimitiveType::DataTypeRSMatrix4x4 336 337 "Element.ELEMENT", // RSExportPrimitiveType::DataTypeRSElement 338 "Element.TYPE", // RSExportPrimitiveType::DataTypeRSType 339 "Element.ALLOCATION", // RSExportPrimitiveType::DataTypeRSAllocation 340 "Element.SAMPLER", // RSExportPrimitiveType::DataTypeRSSampler 341 "Element.SCRIPT", // RSExportPrimitiveType::DataTypeRSScript 342 "Element.MESH", // RSExportPrimitiveType::DataTypeRSMesh 343 "Element.PROGRAM_FRAGMENT", 344 // RSExportPrimitiveType::DataTypeRSProgramFragment 345 "Element.PROGRAM_VERTEX", 346 // RSExportPrimitiveType::DataTypeRSProgramVertex 347 "Element.PROGRAM_RASTER", 348 // RSExportPrimitiveType::DataTypeRSProgramRaster 349 "Element.PROGRAM_STORE", 350 // RSExportPrimitiveType::DataTypeRSProgramStore 351 "Element.FONT", 352 // RSExportPrimitiveType::DataTypeRSFont 353 }; 354 unsigned TypeId = EPT->getType(); 355 356 if (TypeId < 357 (sizeof(PrimitiveBuiltinElementConstructMap) / sizeof(const char*))) 358 return PrimitiveBuiltinElementConstructMap[ EPT->getType() ]; 359 } else if (EPT->getKind() == RSExportPrimitiveType::DataKindPixelA) { 360 if (EPT->getType() == RSExportPrimitiveType::DataTypeUnsigned8) 361 return "Element.A_8"; 362 } else if (EPT->getKind() == RSExportPrimitiveType::DataKindPixelRGB) { 363 if (EPT->getType() == RSExportPrimitiveType::DataTypeUnsigned565) 364 return "Element.RGB_565"; 365 else if (EPT->getType() == RSExportPrimitiveType::DataTypeUnsigned8) 366 return "Element.RGB_888"; 367 } else if (EPT->getKind() == RSExportPrimitiveType::DataKindPixelRGBA) { 368 if (EPT->getType() == RSExportPrimitiveType::DataTypeUnsigned5551) 369 return "Element.RGBA_5551"; 370 else if (EPT->getType() == RSExportPrimitiveType::DataTypeUnsigned4444) 371 return "Element.RGBA_4444"; 372 else if (EPT->getType() == RSExportPrimitiveType::DataTypeUnsigned8) 373 return "Element.RGBA_8888"; 374 } 375 } else if (ET->getClass() == RSExportType::ExportClassVector) { 376 const RSExportVectorType *EVT = static_cast<const RSExportVectorType*>(ET); 377 if (EVT->getKind() == RSExportPrimitiveType::DataKindUser) { 378 if (EVT->getType() == RSExportPrimitiveType::DataTypeFloat32) { 379 if (EVT->getNumElement() == 2) 380 return "Element.F32_2"; 381 else if (EVT->getNumElement() == 3) 382 return "Element.F32_3"; 383 else if (EVT->getNumElement() == 4) 384 return "Element.F32_4"; 385 } else if (EVT->getType() == RSExportPrimitiveType::DataTypeUnsigned8) { 386 if (EVT->getNumElement() == 4) 387 return "Element.U8_4"; 388 } 389 } 390 } else if (ET->getClass() == RSExportType::ExportClassMatrix) { 391 const RSExportMatrixType *EMT = static_cast<const RSExportMatrixType *>(ET); 392 switch (EMT->getDim()) { 393 case 2: return "Element.MATRIX_2X2"; 394 case 3: return "Element.MATRIX_3X3"; 395 case 4: return "Element.MATRIX_4X4"; 396 default: assert(false && "Unsupported dimension of matrix"); 397 } 398 } else if (ET->getClass() == RSExportType::ExportClassPointer) { 399 // Treat pointer type variable as unsigned int 400 // TODO(zonr): this is target dependent 401 return "Element.USER_I32"; 402 } 403 404 return NULL; 405} 406 407static const char *GetElementDataKindName(RSExportPrimitiveType::DataKind DK) { 408 static const char *ElementDataKindNameMap[] = { 409 "Element.DataKind.USER", // RSExportPrimitiveType::DataKindUser 410 "Element.DataKind.PIXEL_L", // RSExportPrimitiveType::DataKindPixelL 411 "Element.DataKind.PIXEL_A", // RSExportPrimitiveType::DataKindPixelA 412 "Element.DataKind.PIXEL_LA", // RSExportPrimitiveType::DataKindPixelLA 413 "Element.DataKind.PIXEL_RGB", // RSExportPrimitiveType::DataKindPixelRGB 414 "Element.DataKind.PIXEL_RGBA", // RSExportPrimitiveType::DataKindPixelRGBA 415 }; 416 417 if (static_cast<unsigned>(DK) < 418 (sizeof(ElementDataKindNameMap) / sizeof(const char*))) 419 return ElementDataKindNameMap[ DK ]; 420 else 421 return NULL; 422} 423 424static const char *GetElementDataTypeName(RSExportPrimitiveType::DataType DT) { 425 static const char *ElementDataTypeNameMap[] = { 426 NULL, // RSExportPrimitiveType::DataTypeFloat16 427 "Element.DataType.FLOAT_32", // RSExportPrimitiveType::DataTypeFloat32 428 "Element.DataType.FLOAT_64", // RSExportPrimitiveType::DataTypeFloat64 429 "Element.DataType.SIGNED_8", // RSExportPrimitiveType::DataTypeSigned8 430 "Element.DataType.SIGNED_16", // RSExportPrimitiveType::DataTypeSigned16 431 "Element.DataType.SIGNED_32", // RSExportPrimitiveType::DataTypeSigned32 432 "Element.DataType.SIGNED_64", // RSExportPrimitiveType::DataTypeSigned64 433 "Element.DataType.UNSIGNED_8", // RSExportPrimitiveType::DataTypeUnsigned8 434 "Element.DataType.UNSIGNED_16", 435 // RSExportPrimitiveType::DataTypeUnsigned16 436 "Element.DataType.UNSIGNED_32", 437 // RSExportPrimitiveType::DataTypeUnsigned32 438 "Element.DataType.UNSIGNED_64", 439 // RSExportPrimitiveType::DataTypeUnsigned64 440 "Element.DataType.BOOLEAN", 441 // RSExportPrimitiveType::DataTypeBoolean 442 443 // RSExportPrimitiveType::DataTypeUnsigned565 444 "Element.DataType.UNSIGNED_5_6_5", 445 // RSExportPrimitiveType::DataTypeUnsigned5551 446 "Element.DataType.UNSIGNED_5_5_5_1", 447 // RSExportPrimitiveType::DataTypeUnsigned4444 448 "Element.DataType.UNSIGNED_4_4_4_4", 449 450 // DataTypeRSMatrix* must have been resolved in GetBuiltinElementConstruct() 451 NULL, // (Dummy) RSExportPrimitiveType::DataTypeRSMatrix2x2 452 NULL, // (Dummy) RSExportPrimitiveType::DataTypeRSMatrix3x3 453 NULL, // (Dummy) RSExportPrimitiveType::DataTypeRSMatrix4x4 454 455 "Element.DataType.RS_ELEMENT", // RSExportPrimitiveType::DataTypeRSElement 456 "Element.DataType.RS_TYPE", // RSExportPrimitiveType::DataTypeRSType 457 // RSExportPrimitiveType::DataTypeRSAllocation 458 "Element.DataType.RS_ALLOCATION", 459 // RSExportPrimitiveType::DataTypeRSSampler 460 "Element.DataType.RS_SAMPLER", 461 // RSExportPrimitiveType::DataTypeRSScript 462 "Element.DataType.RS_SCRIPT", 463 // RSExportPrimitiveType::DataTypeRSMesh 464 "Element.DataType.RS_MESH", 465 // RSExportPrimitiveType::DataTypeRSProgramFragment 466 "Element.DataType.RS_PROGRAM_FRAGMENT", 467 // RSExportPrimitiveType::DataTypeRSProgramVertex 468 "Element.DataType.RS_PROGRAM_VERTEX", 469 // RSExportPrimitiveType::DataTypeRSProgramRaster 470 "Element.DataType.RS_PROGRAM_RASTER", 471 // RSExportPrimitiveType::DataTypeRSProgramStore 472 "Element.DataType.RS_PROGRAM_STORE", 473 // RSExportPrimitiveType::DataTypeRSFont 474 "Element.DataType.RS_FONT", 475 }; 476 477 if (static_cast<unsigned>(DT) < 478 (sizeof(ElementDataTypeNameMap) / sizeof(const char*))) 479 return ElementDataTypeNameMap[ DT ]; 480 else 481 return NULL; 482} 483 484/********************** Methods to generate script class **********************/ 485bool RSReflection::genScriptClass(Context &C, 486 const std::string &ClassName, 487 std::string &ErrorMsg) { 488 if (!C.startClass(Context::AM_Public, 489 false, 490 ClassName, 491 RS_SCRIPT_CLASS_SUPER_CLASS_NAME, 492 ErrorMsg)) 493 return false; 494 495 genScriptClassConstructor(C); 496 497 // Reflect export variable 498 for (RSContext::const_export_var_iterator I = mRSContext->export_vars_begin(), 499 E = mRSContext->export_vars_end(); 500 I != E; 501 I++) 502 genExportVariable(C, *I); 503 504 // Reflect export function 505 for (RSContext::const_export_func_iterator 506 I = mRSContext->export_funcs_begin(), 507 E = mRSContext->export_funcs_end(); 508 I != E; I++) 509 genExportFunction(C, *I); 510 511 C.endClass(); 512 513 return true; 514} 515 516void RSReflection::genScriptClassConstructor(Context &C) { 517 C.indent() << "// Constructor" << std::endl; 518 C.startFunction(Context::AM_Public, 519 false, 520 NULL, 521 C.getClassName(), 522 3, 523 "RenderScript", "rs", 524 "Resources", "resources", 525 "int", "id"); 526 // Call constructor of super class 527 C.indent() << "super(rs, resources, id);" << std::endl; 528 529 // If an exported variable has initial value, reflect it 530 531 for (RSContext::const_export_var_iterator I = mRSContext->export_vars_begin(), 532 E = mRSContext->export_vars_end(); 533 I != E; 534 I++) { 535 const RSExportVar *EV = *I; 536 if (!EV->getInit().isUninit()) 537 genInitExportVariable(C, EV->getType(), EV->getName(), EV->getInit()); 538 } 539 540 C.endFunction(); 541 542 C.startFunction(Context::AM_Public, 543 false, 544 NULL, 545 C.getClassName(), 546 4, 547 "RenderScript", "rs", 548 "Resources", "resources", 549 "int", "id", 550 "boolean", "isRoot"); 551 // Call constructor of super class 552 C.indent() << "super(rs, resources, id);" << std::endl; 553 554 // If an exported variable has initial value, reflect it 555 556 for (RSContext::const_export_var_iterator I = mRSContext->export_vars_begin(), 557 E = mRSContext->export_vars_end(); 558 I != E; 559 I++) { 560 const RSExportVar *EV = *I; 561 if (!EV->getInit().isUninit()) 562 genInitExportVariable(C, EV->getType(), EV->getName(), EV->getInit()); 563 } 564 565 C.endFunction(); 566 567 return; 568} 569 570void RSReflection::genInitBoolExportVariable(Context &C, 571 const std::string &VarName, 572 const clang::APValue &Val) { 573 assert(!Val.isUninit() && "Not a valid initializer"); 574 575 C.indent() << RS_EXPORT_VAR_PREFIX << VarName << " = "; 576 assert((Val.getKind() == clang::APValue::Int) && 577 "Bool type has wrong initial APValue"); 578 579 C.out() << ((Val.getInt().getSExtValue() == 0) ? "false" : "true") 580 << ";" << std::endl; 581 582 return; 583} 584 585void RSReflection::genInitPrimitiveExportVariable( 586 Context &C, 587 const std::string &VarName, 588 const clang::APValue &Val) { 589 assert(!Val.isUninit() && "Not a valid initializer"); 590 591 C.indent() << RS_EXPORT_VAR_PREFIX << VarName << " = "; 592 switch (Val.getKind()) { 593 case clang::APValue::Int: { 594 llvm::APInt api = Val.getInt(); 595 C.out() << api.getSExtValue(); 596 if (api.getBitWidth() > 32) { 597 C.out() << "L"; 598 } 599 break; 600 } 601 case clang::APValue::Float: { 602 llvm::APFloat apf = Val.getFloat(); 603 if (&apf.getSemantics() == &llvm::APFloat::IEEEsingle) { 604 C.out() << apf.convertToFloat() << "f"; 605 } else { 606 C.out() << apf.convertToDouble(); 607 } 608 break; 609 } 610 611 case clang::APValue::ComplexInt: 612 case clang::APValue::ComplexFloat: 613 case clang::APValue::LValue: 614 case clang::APValue::Vector: { 615 assert(false && "Primitive type cannot have such kind of initializer"); 616 break; 617 } 618 default: { 619 assert(false && "Unknown kind of initializer"); 620 } 621 } 622 C.out() << ";" << std::endl; 623 624 return; 625} 626 627void RSReflection::genInitExportVariable(Context &C, 628 const RSExportType *ET, 629 const std::string &VarName, 630 const clang::APValue &Val) { 631 assert(!Val.isUninit() && "Not a valid initializer"); 632 633 switch (ET->getClass()) { 634 case RSExportType::ExportClassPrimitive: { 635 const RSExportPrimitiveType *EPT = 636 static_cast<const RSExportPrimitiveType*>(ET); 637 if (EPT->getType() == RSExportPrimitiveType::DataTypeBoolean) { 638 genInitBoolExportVariable(C, VarName, Val); 639 } else { 640 genInitPrimitiveExportVariable(C, VarName, Val); 641 } 642 break; 643 } 644 case RSExportType::ExportClassPointer: { 645 if (!Val.isInt() || Val.getInt().getSExtValue() != 0) 646 std::cout << "Initializer which is non-NULL to pointer type variable " 647 "will be ignored" << std::endl; 648 break; 649 } 650 case RSExportType::ExportClassVector: { 651 const RSExportVectorType *EVT = 652 static_cast<const RSExportVectorType*>(ET); 653 switch (Val.getKind()) { 654 case clang::APValue::Int: 655 case clang::APValue::Float: { 656 for (unsigned i = 0; i < EVT->getNumElement(); i++) { 657 std::string Name = VarName + "." + GetVectorAccessor(i); 658 genInitPrimitiveExportVariable(C, Name, Val); 659 } 660 break; 661 } 662 case clang::APValue::Vector: { 663 C.indent() << RS_EXPORT_VAR_PREFIX << VarName << " = new " 664 << GetVectorTypeName(EVT) << "();" << std::endl; 665 666 unsigned NumElements = 667 std::min(static_cast<unsigned>(EVT->getNumElement()), 668 Val.getVectorLength()); 669 for (unsigned i = 0; i < NumElements; i++) { 670 const clang::APValue &ElementVal = Val.getVectorElt(i); 671 std::string Name = VarName + "." + GetVectorAccessor(i); 672 genInitPrimitiveExportVariable(C, Name, ElementVal); 673 } 674 break; 675 } 676 case clang::APValue::Uninitialized: 677 case clang::APValue::ComplexInt: 678 case clang::APValue::ComplexFloat: 679 case clang::APValue::LValue: { 680 assert(false && "Unexpected type of value of initializer."); 681 } 682 } 683 break; 684 } 685 // TODO(zonr): Resolving initializer of a record (and matrix) type variable 686 // is complex. It cannot obtain by just simply evaluating the initializer 687 // expression. 688 case RSExportType::ExportClassMatrix: 689 case RSExportType::ExportClassConstantArray: 690 case RSExportType::ExportClassRecord: { 691#if 0 692 unsigned InitIndex = 0; 693 const RSExportRecordType *ERT = 694 static_cast<const RSExportRecordType*>(ET); 695 696 assert((Val.getKind() == clang::APValue::Vector) && "Unexpected type of " 697 "initializer for record type variable"); 698 699 C.indent() << RS_EXPORT_VAR_PREFIX << VarName 700 << " = new "RS_TYPE_CLASS_NAME_PREFIX << ERT->getName() 701 << "."RS_TYPE_ITEM_CLASS_NAME"();" << std::endl; 702 703 for (RSExportRecordType::const_field_iterator I = ERT->fields_begin(), 704 E = ERT->fields_end(); 705 I != E; 706 I++) { 707 const RSExportRecordType::Field *F = *I; 708 std::string FieldName = VarName + "." + F->getName(); 709 710 if (InitIndex > Val.getVectorLength()) 711 break; 712 713 genInitPrimitiveExportVariable(C, 714 FieldName, 715 Val.getVectorElt(InitIndex++)); 716 } 717#endif 718 assert(false && "Unsupported initializer for record/matrix/constant " 719 "array type variable currently"); 720 break; 721 } 722 default: { 723 assert(false && "Unknown class of type"); 724 } 725 } 726 return; 727} 728 729void RSReflection::genExportVariable(Context &C, const RSExportVar *EV) { 730 const RSExportType *ET = EV->getType(); 731 732 C.indent() << "private final static int "RS_EXPORT_VAR_INDEX_PREFIX 733 << EV->getName() << " = " << C.getNextExportVarSlot() << ";" 734 << std::endl; 735 736 switch (ET->getClass()) { 737 case RSExportType::ExportClassPrimitive: { 738 genPrimitiveTypeExportVariable(C, EV); 739 break; 740 } 741 case RSExportType::ExportClassPointer: { 742 genPointerTypeExportVariable(C, EV); 743 break; 744 } 745 case RSExportType::ExportClassVector: { 746 genVectorTypeExportVariable(C, EV); 747 break; 748 } 749 case RSExportType::ExportClassMatrix: { 750 genMatrixTypeExportVariable(C, EV); 751 break; 752 } 753 case RSExportType::ExportClassConstantArray: { 754 genConstantArrayTypeExportVariable(C, EV); 755 break; 756 } 757 case RSExportType::ExportClassRecord: { 758 genRecordTypeExportVariable(C, EV); 759 break; 760 } 761 default: { 762 assert(false && "Unknown class of type"); 763 } 764 } 765 766 return; 767} 768 769void RSReflection::genExportFunction(Context &C, const RSExportFunc *EF) { 770 C.indent() << "private final static int "RS_EXPORT_FUNC_INDEX_PREFIX 771 << EF->getName() << " = " << C.getNextExportFuncSlot() << ";" 772 << std::endl; 773 774 // invoke_*() 775 Context::ArgTy Args; 776 777 if (EF->hasParam()) { 778 for (RSExportFunc::const_param_iterator I = EF->params_begin(), 779 E = EF->params_end(); 780 I != E; 781 I++) { 782 Args.push_back(std::make_pair(GetTypeName((*I)->getType()), 783 (*I)->getName())); 784 } 785 } 786 787 C.startFunction(Context::AM_Public, 788 false, 789 "void", 790 "invoke_" + EF->getName(), 791 Args); 792 793 if (!EF->hasParam()) { 794 C.indent() << "invoke("RS_EXPORT_FUNC_INDEX_PREFIX << EF->getName() << ");" 795 << std::endl; 796 } else { 797 const RSExportRecordType *ERT = EF->getParamPacketType(); 798 std::string FieldPackerName = EF->getName() + "_fp"; 799 800 if (genCreateFieldPacker(C, ERT, FieldPackerName.c_str())) 801 genPackVarOfType(C, ERT, NULL, FieldPackerName.c_str()); 802 803 C.indent() << "invoke("RS_EXPORT_FUNC_INDEX_PREFIX << EF->getName() << ", " 804 << FieldPackerName << ");" << std::endl; 805 } 806 807 C.endFunction(); 808 return; 809} 810 811void RSReflection::genPrimitiveTypeExportVariable( 812 Context &C, 813 const RSExportVar *EV) { 814 assert((EV->getType()->getClass() == RSExportType::ExportClassPrimitive) && 815 "Variable should be type of primitive here"); 816 817 const RSExportPrimitiveType *EPT = 818 static_cast<const RSExportPrimitiveType*>(EV->getType()); 819 const char *TypeName = GetPrimitiveTypeName(EPT); 820 821 C.indent() << "private " << TypeName << " "RS_EXPORT_VAR_PREFIX 822 << EV->getName() << ";" << std::endl; 823 824 // set_*() 825 if (!EV->isConst()) { 826 C.startFunction(Context::AM_Public, 827 false, 828 "void", 829 "set_" + EV->getName(), 830 1, 831 TypeName, "v"); 832 C.indent() << RS_EXPORT_VAR_PREFIX << EV->getName() << " = v;" << std::endl; 833 834 if (EPT->isRSObjectType()) 835 C.indent() << "setVar("RS_EXPORT_VAR_INDEX_PREFIX << EV->getName() 836 << ", (v == null) ? 0 : v.getID());" << std::endl; 837 else 838 C.indent() << "setVar("RS_EXPORT_VAR_INDEX_PREFIX << EV->getName() 839 << ", v);" << std::endl; 840 841 C.endFunction(); 842 } 843 844 genGetExportVariable(C, TypeName, EV->getName()); 845 846 return; 847} 848 849void RSReflection::genPointerTypeExportVariable(Context &C, 850 const RSExportVar *EV) { 851 const RSExportType *ET = EV->getType(); 852 const RSExportType *PointeeType; 853 std::string TypeName; 854 855 assert((ET->getClass() == RSExportType::ExportClassPointer) && 856 "Variable should be type of pointer here"); 857 858 PointeeType = static_cast<const RSExportPointerType*>(ET)->getPointeeType(); 859 TypeName = GetTypeName(ET); 860 861 C.indent() << "private " << TypeName << " "RS_EXPORT_VAR_PREFIX 862 << EV->getName() << ";" << std::endl; 863 864 // bind_*() 865 C.startFunction(Context::AM_Public, 866 false, 867 "void", 868 "bind_" + EV->getName(), 869 1, 870 TypeName.c_str(), "v"); 871 872 C.indent() << RS_EXPORT_VAR_PREFIX << EV->getName() << " = v;" << std::endl; 873 C.indent() << "if (v == null) bindAllocation(null, "RS_EXPORT_VAR_INDEX_PREFIX 874 << EV->getName() << ");" << std::endl; 875 876 if (PointeeType->getClass() == RSExportType::ExportClassRecord) 877 C.indent() << "else bindAllocation(v.getAllocation(), " 878 RS_EXPORT_VAR_INDEX_PREFIX << EV->getName() << ");" 879 << std::endl; 880 else 881 C.indent() << "else bindAllocation(v, "RS_EXPORT_VAR_INDEX_PREFIX 882 << EV->getName() << ");" << std::endl; 883 884 C.endFunction(); 885 886 genGetExportVariable(C, TypeName, EV->getName()); 887 888 return; 889} 890 891void RSReflection::genVectorTypeExportVariable(Context &C, 892 const RSExportVar *EV) { 893 assert((EV->getType()->getClass() == RSExportType::ExportClassVector) && 894 "Variable should be type of vector here"); 895 896 const RSExportVectorType *EVT = 897 static_cast<const RSExportVectorType*>(EV->getType()); 898 const char *TypeName = GetVectorTypeName(EVT); 899 const char *FieldPackerName = "fp"; 900 901 C.indent() << "private " << TypeName << " "RS_EXPORT_VAR_PREFIX 902 << EV->getName() << ";" << std::endl; 903 904 // set_*() 905 if (!EV->isConst()) { 906 C.startFunction(Context::AM_Public, 907 false, 908 "void", 909 "set_" + EV->getName(), 910 1, 911 TypeName, "v"); 912 C.indent() << RS_EXPORT_VAR_PREFIX << EV->getName() << " = v;" << std::endl; 913 914 if (genCreateFieldPacker(C, EVT, FieldPackerName)) 915 genPackVarOfType(C, EVT, "v", FieldPackerName); 916 C.indent() << "setVar("RS_EXPORT_VAR_INDEX_PREFIX << EV->getName() << ", " 917 << FieldPackerName << ");" << std::endl; 918 919 C.endFunction(); 920 } 921 922 genGetExportVariable(C, TypeName, EV->getName()); 923 return; 924} 925 926void RSReflection::genMatrixTypeExportVariable(Context &C, 927 const RSExportVar *EV) { 928 assert((EV->getType()->getClass() == RSExportType::ExportClassMatrix) && 929 "Variable should be type of matrix here"); 930 931 const RSExportMatrixType *EMT = 932 static_cast<const RSExportMatrixType*>(EV->getType()); 933 const char *TypeName = GetMatrixTypeName(EMT); 934 const char *FieldPackerName = "fp"; 935 936 C.indent() << "private " << TypeName << " "RS_EXPORT_VAR_PREFIX 937 << EV->getName() << ";" << std::endl; 938 939 // set_*() 940 if (!EV->isConst()) { 941 C.startFunction(Context::AM_Public, 942 false, 943 "void", 944 "set_" + EV->getName(), 945 1, 946 TypeName, "v"); 947 C.indent() << RS_EXPORT_VAR_PREFIX << EV->getName() << " = v;" << std::endl; 948 949 if (genCreateFieldPacker(C, EMT, FieldPackerName)) 950 genPackVarOfType(C, EMT, "v", FieldPackerName); 951 C.indent() << "setVar("RS_EXPORT_VAR_INDEX_PREFIX << EV->getName() << ", " 952 << FieldPackerName << ");" << std::endl; 953 954 C.endFunction(); 955 } 956 957 genGetExportVariable(C, TypeName, EV->getName()); 958 return; 959} 960 961void RSReflection::genConstantArrayTypeExportVariable( 962 Context &C, 963 const RSExportVar *EV) { 964 assert((EV->getType()->getClass() == RSExportType::ExportClassConstantArray)&& 965 "Variable should be type of constant array here"); 966 967 const RSExportConstantArrayType *ECAT = 968 static_cast<const RSExportConstantArrayType*>(EV->getType()); 969 std::string TypeName = GetTypeName(ECAT); 970 const char *FieldPackerName = "fp"; 971 972 C.indent() << "private " << TypeName << " "RS_EXPORT_VAR_PREFIX 973 << EV->getName() << ";" << std::endl; 974 975 // set_*() 976 if (!EV->isConst()) { 977 C.startFunction(Context::AM_Public, 978 false, 979 "void", 980 "set_" + EV->getName(), 981 1, 982 TypeName.c_str(), "v"); 983 C.indent() << RS_EXPORT_VAR_PREFIX << EV->getName() << " = v;" << std::endl; 984 985 if (genCreateFieldPacker(C, ECAT, FieldPackerName)) 986 genPackVarOfType(C, ECAT, "v", FieldPackerName); 987 C.indent() << "setVar("RS_EXPORT_VAR_INDEX_PREFIX << EV->getName() << ", " 988 << FieldPackerName << ");" << std::endl; 989 990 C.endFunction(); 991 } 992 993 genGetExportVariable(C, TypeName, EV->getName()); 994 return; 995} 996 997void RSReflection::genRecordTypeExportVariable(Context &C, 998 const RSExportVar *EV) { 999 assert((EV->getType()->getClass() == RSExportType::ExportClassRecord) && 1000 "Variable should be type of struct here"); 1001 1002 const RSExportRecordType *ERT = 1003 static_cast<const RSExportRecordType*>(EV->getType()); 1004 std::string TypeName = 1005 RS_TYPE_CLASS_NAME_PREFIX + ERT->getName() + "."RS_TYPE_ITEM_CLASS_NAME; 1006 const char *FieldPackerName = "fp"; 1007 1008 C.indent() << "private " << TypeName << " "RS_EXPORT_VAR_PREFIX 1009 << EV->getName() << ";" << std::endl; 1010 1011 // set_*() 1012 if (!EV->isConst()) { 1013 C.startFunction(Context::AM_Public, 1014 false, 1015 "void", 1016 "set_" + EV->getName(), 1017 1, 1018 TypeName.c_str(), "v"); 1019 C.indent() << RS_EXPORT_VAR_PREFIX << EV->getName() << " = v;" << std::endl; 1020 1021 if (genCreateFieldPacker(C, ERT, FieldPackerName)) 1022 genPackVarOfType(C, ERT, "v", FieldPackerName); 1023 C.indent() << "setVar("RS_EXPORT_VAR_INDEX_PREFIX << EV->getName() 1024 << ", " << FieldPackerName << ");" << std::endl; 1025 1026 C.endFunction(); 1027 } 1028 1029 genGetExportVariable(C, TypeName.c_str(), EV->getName()); 1030 return; 1031} 1032 1033void RSReflection::genGetExportVariable(Context &C, 1034 const std::string &TypeName, 1035 const std::string &VarName) { 1036 C.startFunction(Context::AM_Public, 1037 false, 1038 TypeName.c_str(), 1039 "get_" + VarName, 1040 0); 1041 1042 C.indent() << "return "RS_EXPORT_VAR_PREFIX << VarName << ";" << std::endl; 1043 1044 C.endFunction(); 1045 return; 1046} 1047 1048/******************* Methods to generate script class /end *******************/ 1049 1050bool RSReflection::genCreateFieldPacker(Context &C, 1051 const RSExportType *ET, 1052 const char *FieldPackerName) { 1053 size_t AllocSize = RSExportType::GetTypeAllocSize(ET); 1054 if (AllocSize > 0) 1055 C.indent() << "FieldPacker " << FieldPackerName << " = new FieldPacker(" 1056 << AllocSize << ");" << std::endl; 1057 else 1058 return false; 1059 return true; 1060} 1061 1062void RSReflection::genPackVarOfType(Context &C, 1063 const RSExportType *ET, 1064 const char *VarName, 1065 const char *FieldPackerName) { 1066 switch (ET->getClass()) { 1067 case RSExportType::ExportClassPrimitive: 1068 case RSExportType::ExportClassVector: { 1069 C.indent() << FieldPackerName << "." 1070 << GetPackerAPIName( 1071 static_cast<const RSExportPrimitiveType*>(ET)) 1072 << "(" << VarName << ");" << std::endl; 1073 break; 1074 } 1075 case RSExportType::ExportClassPointer: { 1076 // Must reflect as type Allocation in Java 1077 const RSExportType *PointeeType = 1078 static_cast<const RSExportPointerType*>(ET)->getPointeeType(); 1079 1080 if (PointeeType->getClass() != RSExportType::ExportClassRecord) 1081 C.indent() << FieldPackerName << ".addI32(" << VarName 1082 << ".getPtr());" << std::endl; 1083 else 1084 C.indent() << FieldPackerName << ".addI32(" << VarName 1085 << ".getAllocation().getPtr());" << std::endl; 1086 break; 1087 } 1088 case RSExportType::ExportClassMatrix: { 1089 C.indent() << FieldPackerName << ".addObj(" << VarName << ");" 1090 << std::endl; 1091 break; 1092 } 1093 case RSExportType::ExportClassConstantArray: { 1094 const RSExportConstantArrayType *ECAT = 1095 static_cast<const RSExportConstantArrayType *>(ET); 1096 1097 // TODO(zonr): more elegant way. Currently, we obtain the unique index 1098 // variable (this method involves recursive call which means 1099 // we may have more than one level loop, therefore we can't 1100 // always use the same index variable name here) name given 1101 // in the for-loop from counting the '.' in @VarName. 1102 unsigned Level = 0; 1103 size_t LastDotPos = 0; 1104 std::string ElementVarName(VarName); 1105 1106 while (LastDotPos != std::string::npos) { 1107 LastDotPos = ElementVarName.find_first_of('.', LastDotPos + 1); 1108 Level++; 1109 } 1110 std::string IndexVarName("ct"); 1111 IndexVarName.append(llvm::utostr_32(Level)); 1112 1113 C.indent() << "for (int " << IndexVarName << " = 0; " << 1114 IndexVarName << " < " << ECAT->getSize() << "; " << 1115 IndexVarName << "++)"; 1116 C.startBlock(); 1117 1118 ElementVarName.append("[" + IndexVarName + "]"); 1119 genPackVarOfType(C, ECAT->getElementType(), ElementVarName.c_str(), 1120 FieldPackerName); 1121 1122 C.endBlock(); 1123 break; 1124 } 1125 case RSExportType::ExportClassRecord: { 1126 const RSExportRecordType *ERT = 1127 static_cast<const RSExportRecordType*>(ET); 1128 // Relative pos from now on in field packer 1129 unsigned Pos = 0; 1130 1131 for (RSExportRecordType::const_field_iterator I = ERT->fields_begin(), 1132 E = ERT->fields_end(); 1133 I != E; 1134 I++) { 1135 const RSExportRecordType::Field *F = *I; 1136 std::string FieldName; 1137 size_t FieldOffset = F->getOffsetInParent(); 1138 size_t FieldStoreSize = RSExportType::GetTypeStoreSize(F->getType()); 1139 size_t FieldAllocSize = RSExportType::GetTypeAllocSize(F->getType()); 1140 1141 if (VarName != NULL) 1142 FieldName = VarName + ("." + F->getName()); 1143 else 1144 FieldName = F->getName(); 1145 1146 if (FieldOffset > Pos) 1147 C.indent() << FieldPackerName << ".skip(" 1148 << (FieldOffset - Pos) << ");" << std::endl; 1149 1150 genPackVarOfType(C, F->getType(), FieldName.c_str(), FieldPackerName); 1151 1152 // There is padding in the field type 1153 if (FieldAllocSize > FieldStoreSize) 1154 C.indent() << FieldPackerName << ".skip(" 1155 << (FieldAllocSize - FieldStoreSize) 1156 << ");" << std::endl; 1157 1158 Pos = FieldOffset + FieldAllocSize; 1159 } 1160 1161 // There maybe some padding after the struct 1162 size_t Padding = RSExportType::GetTypeAllocSize(ERT) - Pos; 1163 if (Padding > 0) 1164 C.indent() << FieldPackerName << ".skip(" << Padding << ");" 1165 << std::endl; 1166 break; 1167 } 1168 default: { 1169 assert(false && "Unknown class of type"); 1170 } 1171 } 1172 1173 return; 1174} 1175 1176void RSReflection::genAllocateVarOfType(Context &C, 1177 const RSExportType *T, 1178 const std::string &VarName) { 1179 switch (T->getClass()) { 1180 case RSExportType::ExportClassPrimitive: { 1181 // Primitive type like int in Java has its own storage once it's declared. 1182 // 1183 // FIXME: Should we allocate storage for RS object? 1184 // if (static_cast<const RSExportPrimitiveType *>(T)->isRSObjectType()) 1185 // C.indent() << VarName << " = new " << GetTypeName(T) << "();" 1186 // << std::endl; 1187 break; 1188 } 1189 case RSExportType::ExportClassPointer: { 1190 // Pointer type is an instance of Allocation or a TypeClass whose value is 1191 // expected to be assigned by programmer later in Java program. Therefore 1192 // we don't reflect things like [VarName] = new Allocation(); 1193 C.indent() << VarName << " = null;" << std::endl; 1194 break; 1195 } 1196 case RSExportType::ExportClassConstantArray: { 1197 const RSExportConstantArrayType *ECAT = 1198 static_cast<const RSExportConstantArrayType *>(T); 1199 const RSExportType *ElementType = ECAT->getElementType(); 1200 1201 C.indent() << VarName << " = new " << GetTypeName(ElementType) 1202 << "[" << ECAT->getSize() << "];" << std::endl; 1203 1204 // Primitive type element doesn't need allocation code. 1205 if (ElementType->getClass() != RSExportType::ExportClassPrimitive) { 1206 C.indent() << "for (int $ct = 0; $ct < " << ECAT->getSize() << "; " 1207 "$ct++)"; 1208 C.startBlock(); 1209 1210 std::string ElementVarName(VarName); 1211 ElementVarName.append("[$ct]"); 1212 genAllocateVarOfType(C, ElementType, ElementVarName); 1213 1214 C.endBlock(); 1215 } 1216 break; 1217 } 1218 case RSExportType::ExportClassVector: 1219 case RSExportType::ExportClassMatrix: 1220 case RSExportType::ExportClassRecord: { 1221 C.indent() << VarName << " = new " << GetTypeName(T) << "();" 1222 << std::endl; 1223 break; 1224 } 1225 } 1226 return; 1227} 1228 1229void RSReflection::genNewItemBufferIfNull(Context &C, 1230 const char *Index) { 1231 C.indent() << "if (" RS_TYPE_ITEM_BUFFER_NAME " == null) " 1232 RS_TYPE_ITEM_BUFFER_NAME " = " 1233 "new " RS_TYPE_ITEM_CLASS_NAME 1234 "[getType().getX() /* count */];" 1235 << std::endl; 1236 if (Index != NULL) 1237 C.indent() << "if ("RS_TYPE_ITEM_BUFFER_NAME"[" << Index << "] == null) " 1238 RS_TYPE_ITEM_BUFFER_NAME"[" << Index << "] = " 1239 "new "RS_TYPE_ITEM_CLASS_NAME"();" << std::endl; 1240 return; 1241} 1242 1243void RSReflection::genNewItemBufferPackerIfNull(Context &C) { 1244 C.indent() << "if (" RS_TYPE_ITEM_BUFFER_PACKER_NAME " == null) " 1245 RS_TYPE_ITEM_BUFFER_PACKER_NAME " = " 1246 "new FieldPacker(" RS_TYPE_ITEM_CLASS_NAME 1247 ".sizeof * getType().getX()/* count */" 1248 ");" << std::endl; 1249 return; 1250} 1251 1252/********************** Methods to generate type class **********************/ 1253bool RSReflection::genTypeClass(Context &C, 1254 const RSExportRecordType *ERT, 1255 std::string &ErrorMsg) { 1256 std::string ClassName = RS_TYPE_CLASS_NAME_PREFIX + ERT->getName(); 1257 1258 if (!C.startClass(Context::AM_Public, 1259 false, 1260 ClassName, 1261 RS_TYPE_CLASS_SUPER_CLASS_NAME, 1262 ErrorMsg)) 1263 return false; 1264 1265 genTypeItemClass(C, ERT); 1266 1267 // Declare item buffer and item buffer packer 1268 C.indent() << "private "RS_TYPE_ITEM_CLASS_NAME" "RS_TYPE_ITEM_BUFFER_NAME"[]" 1269 ";" << std::endl; 1270 C.indent() << "private FieldPacker "RS_TYPE_ITEM_BUFFER_PACKER_NAME";" 1271 << std::endl; 1272 1273 genTypeClassConstructor(C, ERT); 1274 genTypeClassCopyToArray(C, ERT); 1275 genTypeClassItemSetter(C, ERT); 1276 genTypeClassItemGetter(C, ERT); 1277 genTypeClassComponentSetter(C, ERT); 1278 genTypeClassComponentGetter(C, ERT); 1279 genTypeClassCopyAll(C, ERT); 1280 genTypeClassResize(C); 1281 1282 C.endClass(); 1283 1284 C.resetFieldIndex(); 1285 C.clearFieldIndexMap(); 1286 1287 return true; 1288} 1289 1290void RSReflection::genTypeItemClass(Context &C, 1291 const RSExportRecordType *ERT) { 1292 C.indent() << "static public class "RS_TYPE_ITEM_CLASS_NAME; 1293 C.startBlock(); 1294 1295 C.indent() << "public static final int sizeof = " 1296 << RSExportType::GetTypeAllocSize(ERT) << ";" << std::endl; 1297 1298 // Member elements 1299 C.out() << std::endl; 1300 for (RSExportRecordType::const_field_iterator FI = ERT->fields_begin(), 1301 FE = ERT->fields_end(); 1302 FI != FE; 1303 FI++) { 1304 C.indent() << GetTypeName((*FI)->getType()) << " " << (*FI)->getName() 1305 << ";" << std::endl; 1306 } 1307 1308 // Constructor 1309 C.out() << std::endl; 1310 C.indent() << RS_TYPE_ITEM_CLASS_NAME"()"; 1311 C.startBlock(); 1312 1313 for (RSExportRecordType::const_field_iterator FI = ERT->fields_begin(), 1314 FE = ERT->fields_end(); 1315 FI != FE; 1316 FI++) { 1317 const RSExportRecordType::Field *F = *FI; 1318 genAllocateVarOfType(C, F->getType(), F->getName()); 1319 } 1320 1321 // end Constructor 1322 C.endBlock(); 1323 1324 // end Item class 1325 C.endBlock(); 1326 1327 return; 1328} 1329 1330void RSReflection::genTypeClassConstructor(Context &C, 1331 const RSExportRecordType *ERT) { 1332 const char *RenderScriptVar = "rs"; 1333 1334 C.startFunction(Context::AM_Public, 1335 true, 1336 "Element", 1337 "createElement", 1338 1, 1339 "RenderScript", RenderScriptVar); 1340 genBuildElement(C, "eb", ERT, RenderScriptVar, /* IsInline = */false); 1341 C.endFunction(); 1342 1343 C.startFunction(Context::AM_Public, 1344 false, 1345 NULL, 1346 C.getClassName(), 1347 2, 1348 "RenderScript", RenderScriptVar, 1349 "int", "count"); 1350 1351 C.indent() << RS_TYPE_ITEM_BUFFER_NAME" = null;" << std::endl; 1352 C.indent() << RS_TYPE_ITEM_BUFFER_PACKER_NAME" = null;" << std::endl; 1353 C.indent() << "mElement = createElement(" << RenderScriptVar << ");" 1354 << std::endl; 1355 // Call init() in super class 1356 C.indent() << "init(" << RenderScriptVar << ", count);" << std::endl; 1357 C.endFunction(); 1358 1359 return; 1360} 1361 1362void RSReflection::genTypeClassCopyToArray(Context &C, 1363 const RSExportRecordType *ERT) { 1364 C.startFunction(Context::AM_Private, 1365 false, 1366 "void", 1367 "copyToArray", 1368 2, 1369 RS_TYPE_ITEM_CLASS_NAME, "i", 1370 "int", "index"); 1371 1372 genNewItemBufferPackerIfNull(C); 1373 C.indent() << RS_TYPE_ITEM_BUFFER_PACKER_NAME 1374 ".reset(index * "RS_TYPE_ITEM_CLASS_NAME".sizeof);" 1375 << std::endl; 1376 1377 genPackVarOfType(C, ERT, "i", RS_TYPE_ITEM_BUFFER_PACKER_NAME); 1378 1379 C.endFunction(); 1380 return; 1381} 1382 1383void RSReflection::genTypeClassItemSetter(Context &C, 1384 const RSExportRecordType *ERT) { 1385 C.startFunction(Context::AM_Public, 1386 false, 1387 "void", 1388 "set", 1389 3, 1390 RS_TYPE_ITEM_CLASS_NAME, "i", 1391 "int", "index", 1392 "boolean", "copyNow"); 1393 genNewItemBufferIfNull(C, NULL); 1394 C.indent() << RS_TYPE_ITEM_BUFFER_NAME"[index] = i;" << std::endl; 1395 1396 C.indent() << "if (copyNow) "; 1397 C.startBlock(); 1398 1399 C.indent() << "copyToArray(i, index);" << std::endl; 1400 C.indent() << "mAllocation.subData1D(index, 1, " 1401 RS_TYPE_ITEM_BUFFER_PACKER_NAME".getData());" << std::endl; 1402 1403 // End of if (copyNow) 1404 C.endBlock(); 1405 1406 C.endFunction(); 1407 return; 1408} 1409 1410void RSReflection::genTypeClassItemGetter(Context &C, 1411 const RSExportRecordType *ERT) { 1412 C.startFunction(Context::AM_Public, 1413 false, 1414 RS_TYPE_ITEM_CLASS_NAME, 1415 "get", 1416 1, 1417 "int", "index"); 1418 C.indent() << "if ("RS_TYPE_ITEM_BUFFER_NAME" == null) return null;" 1419 << std::endl; 1420 C.indent() << "return "RS_TYPE_ITEM_BUFFER_NAME"[index];" << std::endl; 1421 C.endFunction(); 1422 return; 1423} 1424 1425void RSReflection::genTypeClassComponentSetter(Context &C, 1426 const RSExportRecordType *ERT) { 1427 for (RSExportRecordType::const_field_iterator FI = ERT->fields_begin(), 1428 FE = ERT->fields_end(); 1429 FI != FE; 1430 FI++) { 1431 const RSExportRecordType::Field *F = *FI; 1432 size_t FieldOffset = F->getOffsetInParent(); 1433 size_t FieldStoreSize = RSExportType::GetTypeStoreSize(F->getType()); 1434 unsigned FieldIndex = C.getFieldIndex(F); 1435 1436 C.startFunction(Context::AM_Public, 1437 false, 1438 "void", 1439 "set_" + F->getName(), 3, 1440 "int", "index", 1441 GetTypeName(F->getType()).c_str(), "v", 1442 "boolean", "copyNow"); 1443 genNewItemBufferPackerIfNull(C); 1444 genNewItemBufferIfNull(C, "index"); 1445 C.indent() << RS_TYPE_ITEM_BUFFER_NAME"[index]." << F->getName() 1446 << " = v;" << std::endl; 1447 1448 C.indent() << "if (copyNow) "; 1449 C.startBlock(); 1450 1451 if (FieldOffset > 0) 1452 C.indent() << RS_TYPE_ITEM_BUFFER_PACKER_NAME 1453 ".reset(index * "RS_TYPE_ITEM_CLASS_NAME".sizeof + " 1454 << FieldOffset << ");" << std::endl; 1455 else 1456 C.indent() << RS_TYPE_ITEM_BUFFER_PACKER_NAME 1457 ".reset(index * "RS_TYPE_ITEM_CLASS_NAME".sizeof);" 1458 << std::endl; 1459 genPackVarOfType(C, F->getType(), "v", RS_TYPE_ITEM_BUFFER_PACKER_NAME); 1460 1461 C.indent() << "FieldPacker fp = new FieldPacker(" << FieldStoreSize << ");" 1462 << std::endl; 1463 genPackVarOfType(C, F->getType(), "v", "fp"); 1464 C.indent() << "mAllocation.subElementData(index, " << FieldIndex << ", fp);" 1465 << std::endl; 1466 1467 // End of if (copyNow) 1468 C.endBlock(); 1469 1470 C.endFunction(); 1471 } 1472 return; 1473} 1474 1475void RSReflection::genTypeClassComponentGetter(Context &C, 1476 const RSExportRecordType *ERT) { 1477 for (RSExportRecordType::const_field_iterator FI = ERT->fields_begin(), 1478 FE = ERT->fields_end(); 1479 FI != FE; 1480 FI++) { 1481 const RSExportRecordType::Field *F = *FI; 1482 C.startFunction(Context::AM_Public, 1483 false, 1484 GetTypeName(F->getType()).c_str(), 1485 "get_" + F->getName(), 1486 1, 1487 "int", "index"); 1488 C.indent() << "if ("RS_TYPE_ITEM_BUFFER_NAME" == null) return " 1489 << GetTypeNullValue(F->getType()) << ";" << std::endl; 1490 C.indent() << "return "RS_TYPE_ITEM_BUFFER_NAME"[index]." << F->getName() 1491 << ";" << std::endl; 1492 C.endFunction(); 1493 } 1494 return; 1495} 1496 1497void RSReflection::genTypeClassCopyAll(Context &C, 1498 const RSExportRecordType *ERT) { 1499 C.startFunction(Context::AM_Public, false, "void", "copyAll", 0); 1500 1501 C.indent() << "for (int ct = 0; ct < "RS_TYPE_ITEM_BUFFER_NAME".length; ct++)" 1502 " copyToArray("RS_TYPE_ITEM_BUFFER_NAME"[ct], ct);" 1503 << std::endl; 1504 C.indent() << "mAllocation.data("RS_TYPE_ITEM_BUFFER_PACKER_NAME".getData());" 1505 << std::endl; 1506 1507 C.endFunction(); 1508 return; 1509} 1510 1511void RSReflection::genTypeClassResize(Context &C) { 1512 C.startFunction(Context::AM_Public, 1513 false, 1514 "void", 1515 "resize", 1516 1, 1517 "int", "newSize"); 1518 1519 C.indent() << "if (mItemArray != null) "; 1520 C.startBlock(); 1521 C.indent() << "int oldSize = mItemArray.length;" << std::endl; 1522 C.indent() << "int copySize = Math.min(oldSize, newSize);" << std::endl; 1523 C.indent() << "if (newSize == oldSize) return;" << std::endl; 1524 C.indent() << "Item ni[] = new Item[newSize];" << std::endl; 1525 C.indent() << "System.arraycopy(mItemArray, 0, ni, 0, copySize);" 1526 << std::endl; 1527 C.indent() << "mItemArray = ni;" << std::endl; 1528 C.endBlock(); 1529 C.indent() << "mAllocation.resize(newSize);" << std::endl; 1530 1531 C.endFunction(); 1532 return; 1533} 1534 1535/******************** Methods to generate type class /end ********************/ 1536 1537/********** Methods to create Element in Java of given record type ***********/ 1538void RSReflection::genBuildElement(Context &C, 1539 const char *ElementBuilderName, 1540 const RSExportRecordType *ERT, 1541 const char *RenderScriptVar, 1542 bool IsInline) { 1543 C.indent() << "Element.Builder " << ElementBuilderName << " = " 1544 "new Element.Builder(" << RenderScriptVar << ");" << std::endl; 1545 1546 // eb.add(...) 1547 genAddElementToElementBuilder(C, 1548 ERT, 1549 "", 1550 ElementBuilderName, 1551 RenderScriptVar, 1552 /* ArraySize = */0); 1553 1554 if (!IsInline) 1555 C.indent() << "return " << ElementBuilderName << ".create();" << std::endl; 1556 return; 1557} 1558 1559#define EB_ADD(x) do { \ 1560 C.indent() << ElementBuilderName \ 1561 << ".add(" << x << ", \"" << VarName << "\""; \ 1562 if (ArraySize > 0) \ 1563 C.out() << ", " << ArraySize; \ 1564 C.out() << ");" << std::endl; \ 1565 C.incFieldIndex(); \ 1566} while (false) 1567 1568void RSReflection::genAddElementToElementBuilder(Context &C, 1569 const RSExportType *ET, 1570 const std::string &VarName, 1571 const char *ElementBuilderName, 1572 const char *RenderScriptVar, 1573 unsigned ArraySize) { 1574 const char *ElementConstruct = GetBuiltinElementConstruct(ET); 1575 1576 if (ElementConstruct != NULL) { 1577 EB_ADD(ElementConstruct << "(" << RenderScriptVar << ")"); 1578 } else { 1579 if ((ET->getClass() == RSExportType::ExportClassPrimitive) || 1580 (ET->getClass() == RSExportType::ExportClassVector)) { 1581 const RSExportPrimitiveType *EPT = 1582 static_cast<const RSExportPrimitiveType*>(ET); 1583 const char *DataKindName = GetElementDataKindName(EPT->getKind()); 1584 const char *DataTypeName = GetElementDataTypeName(EPT->getType()); 1585 int Size = (ET->getClass() == RSExportType::ExportClassVector) ? 1586 static_cast<const RSExportVectorType*>(ET)->getNumElement() : 1587 1; 1588 1589 switch (EPT->getKind()) { 1590 case RSExportPrimitiveType::DataKindPixelL: 1591 case RSExportPrimitiveType::DataKindPixelA: 1592 case RSExportPrimitiveType::DataKindPixelLA: 1593 case RSExportPrimitiveType::DataKindPixelRGB: 1594 case RSExportPrimitiveType::DataKindPixelRGBA: { 1595 // Element.createPixel() 1596 EB_ADD("Element.createPixel(" << RenderScriptVar << ", " 1597 << DataTypeName << ", " 1598 << DataKindName << ")"); 1599 break; 1600 } 1601 case RSExportPrimitiveType::DataKindUser: 1602 default: { 1603 if (EPT->getClass() == RSExportType::ExportClassPrimitive) { 1604 // Element.createUser() 1605 EB_ADD("Element.createUser(" << RenderScriptVar << ", " 1606 << DataTypeName << ")"); 1607 } else { 1608 assert((ET->getClass() == RSExportType::ExportClassVector) && 1609 "Unexpected type."); 1610 EB_ADD("Element.createVector(" << RenderScriptVar << ", " 1611 << DataTypeName << ", " 1612 << Size << ")"); 1613 } 1614 break; 1615 } 1616 } 1617#ifndef NDEBUG 1618 } else if (ET->getClass() == RSExportType::ExportClassPointer) { 1619 // Pointer type variable should be resolved in 1620 // GetBuiltinElementConstruct() 1621 assert(false && "??"); 1622 } else if (ET->getClass() == RSExportType::ExportClassMatrix) { 1623 // Matrix type variable should be resolved 1624 // in GetBuiltinElementConstruct() 1625 assert(false && "??"); 1626#endif 1627 } else if (ET->getClass() == RSExportType::ExportClassConstantArray) { 1628 const RSExportConstantArrayType *ECAT = 1629 static_cast<const RSExportConstantArrayType *>(ET); 1630 1631 const RSExportType *ElementType = ECAT->getElementType(); 1632 if (ElementType->getClass() != RSExportType::ExportClassRecord) { 1633 genAddElementToElementBuilder(C, 1634 ECAT->getElementType(), 1635 VarName, 1636 ElementBuilderName, 1637 RenderScriptVar, 1638 ECAT->getSize()); 1639 } else { 1640 std::string NewElementBuilderName(ElementBuilderName); 1641 NewElementBuilderName.append(1, '_'); 1642 1643 genBuildElement(C, 1644 NewElementBuilderName.c_str(), 1645 static_cast<const RSExportRecordType*>(ElementType), 1646 RenderScriptVar, 1647 /* IsInline = */true); 1648 ArraySize = ECAT->getSize(); 1649 EB_ADD(NewElementBuilderName << ".create()"); 1650 } 1651 } else if (ET->getClass() == RSExportType::ExportClassRecord) { 1652 // Simalar to case of RSExportType::ExportClassRecord in genPackVarOfType. 1653 // 1654 // TODO(zonr): Generalize these two function such that there's no 1655 // duplicated codes. 1656 const RSExportRecordType *ERT = 1657 static_cast<const RSExportRecordType*>(ET); 1658 int Pos = 0; // relative pos from now on 1659 1660 for (RSExportRecordType::const_field_iterator I = ERT->fields_begin(), 1661 E = ERT->fields_end(); 1662 I != E; 1663 I++) { 1664 const RSExportRecordType::Field *F = *I; 1665 std::string FieldName; 1666 int FieldOffset = F->getOffsetInParent(); 1667 int FieldStoreSize = RSExportType::GetTypeStoreSize(F->getType()); 1668 int FieldAllocSize = RSExportType::GetTypeAllocSize(F->getType()); 1669 1670 if (!VarName.empty()) 1671 FieldName = VarName + "." + F->getName(); 1672 else 1673 FieldName = F->getName(); 1674 1675 // Alignment 1676 genAddPaddingToElementBuiler(C, 1677 (FieldOffset - Pos), 1678 ElementBuilderName, 1679 RenderScriptVar); 1680 1681 // eb.add(...) 1682 C.addFieldIndexMapping(F); 1683 if (F->getType()->getClass() != RSExportType::ExportClassRecord) { 1684 genAddElementToElementBuilder(C, 1685 F->getType(), 1686 FieldName, 1687 ElementBuilderName, 1688 RenderScriptVar, 1689 0); 1690 } else { 1691 std::string NewElementBuilderName(ElementBuilderName); 1692 NewElementBuilderName.append(1, '_'); 1693 1694 genBuildElement(C, 1695 NewElementBuilderName.c_str(), 1696 static_cast<const RSExportRecordType*>(F->getType()), 1697 RenderScriptVar, 1698 /* IsInline = */true); 1699 1700 const std::string &VarName = FieldName; // Hack for EB_ADD macro 1701 EB_ADD(NewElementBuilderName << ".create()"); 1702 } 1703 1704 // There is padding within the field type 1705 genAddPaddingToElementBuiler(C, 1706 (FieldAllocSize - FieldStoreSize), 1707 ElementBuilderName, 1708 RenderScriptVar); 1709 1710 Pos = FieldOffset + FieldAllocSize; 1711 } 1712 1713 // There maybe some padding after the struct 1714 size_t RecordStoreSize = RSExportType::GetTypeStoreSize(ERT); 1715 1716 genAddPaddingToElementBuiler(C, 1717 RecordStoreSize - Pos, 1718 ElementBuilderName, 1719 RenderScriptVar); 1720 } else { 1721 assert(false && "Unknown class of type"); 1722 } 1723 } 1724} 1725 1726void RSReflection::genAddPaddingToElementBuiler(Context &C, 1727 int PaddingSize, 1728 const char *ElementBuilderName, 1729 const char *RenderScriptVar) { 1730 unsigned ArraySize = 0; // Hack the EB_ADD macro 1731 while (PaddingSize > 0) { 1732 const std::string &VarName = C.createPaddingField(); 1733 if (PaddingSize >= 4) { 1734 EB_ADD("Element.U32(" << RenderScriptVar << ")"); 1735 PaddingSize -= 4; 1736 } else if (PaddingSize >= 2) { 1737 EB_ADD("Element.U16(" << RenderScriptVar << ")"); 1738 PaddingSize -= 2; 1739 } else if (PaddingSize >= 1) { 1740 EB_ADD("Element.U8(" << RenderScriptVar << ")"); 1741 PaddingSize -= 1; 1742 } 1743 } 1744 return; 1745} 1746 1747#undef EB_ADD 1748/******** Methods to create Element in Java of given record type /end ********/ 1749 1750bool RSReflection::reflect(const std::string &OutputPathBase, 1751 const std::string &OutputPackageName, 1752 const std::string &InputFileName, 1753 const std::string &OutputBCFileName) { 1754 Context *C = NULL; 1755 std::string ResourceId = ""; 1756 1757 if (!GetClassNameFromFileName(OutputBCFileName, ResourceId)) 1758 return false; 1759 1760 if (ResourceId.empty()) 1761 ResourceId = "<Resource ID>"; 1762 1763 if (OutputPackageName.empty() || OutputPackageName == "-") 1764 C = new Context(OutputPathBase, InputFileName, "<Package Name>", 1765 ResourceId, true); 1766 else 1767 C = new Context(OutputPathBase, InputFileName, OutputPackageName, 1768 ResourceId, false); 1769 1770 if (C != NULL) { 1771 std::string ErrorMsg, ScriptClassName; 1772 // class ScriptC_<ScriptName> 1773 if (!GetClassNameFromFileName(InputFileName, ScriptClassName)) 1774 return false; 1775 1776 if (ScriptClassName.empty()) 1777 ScriptClassName = "<Input Script Name>"; 1778 1779 ScriptClassName.insert(0, RS_SCRIPT_CLASS_NAME_PREFIX); 1780 1781 if (mRSContext->getLicenseNote() != NULL) { 1782 C->setLicenseNote(*(mRSContext->getLicenseNote())); 1783 } 1784 1785 if (!genScriptClass(*C, ScriptClassName, ErrorMsg)) { 1786 std::cerr << "Failed to generate class " << ScriptClassName << " (" 1787 << ErrorMsg << ")" << std::endl; 1788 return false; 1789 } 1790 1791 // class ScriptField_<TypeName> 1792 for (RSContext::const_export_type_iterator TI = 1793 mRSContext->export_types_begin(), 1794 TE = mRSContext->export_types_end(); 1795 TI != TE; 1796 TI++) { 1797 const RSExportType *ET = TI->getValue(); 1798 1799 if (ET->getClass() == RSExportType::ExportClassRecord) { 1800 const RSExportRecordType *ERT = 1801 static_cast<const RSExportRecordType*>(ET); 1802 1803 if (!ERT->isArtificial() && !genTypeClass(*C, ERT, ErrorMsg)) { 1804 std::cerr << "Failed to generate type class for struct '" 1805 << ERT->getName() << "' (" << ErrorMsg << ")" << std::endl; 1806 return false; 1807 } 1808 } 1809 } 1810 } 1811 1812 return true; 1813} 1814 1815/************************** RSReflection::Context **************************/ 1816const char *const RSReflection::Context::ApacheLicenseNote = 1817 "/*\n" 1818 " * Copyright (C) 2010 The Android Open Source Project\n" 1819 " *\n" 1820 " * Licensed under the Apache License, Version 2.0 (the \"License\");\n" 1821 " * you may not use this file except in compliance with the License.\n" 1822 " * You may obtain a copy of the License at\n" 1823 " *\n" 1824 " * http://www.apache.org/licenses/LICENSE-2.0\n" 1825 " *\n" 1826 " * Unless required by applicable law or agreed to in writing, software\n" 1827 " * distributed under the License is distributed on an \"AS IS\" BASIS,\n" 1828 " * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or " 1829 "implied.\n" 1830 " * See the License for the specific language governing permissions and\n" 1831 " * limitations under the License.\n" 1832 " */\n" 1833 "\n"; 1834 1835const char *const RSReflection::Context::Import[] = { 1836 // RenderScript java class 1837 "android.renderscript.*", 1838 // Import R 1839 "android.content.res.Resources", 1840 // Import for debugging 1841 "android.util.Log", 1842}; 1843 1844bool RSReflection::Context::openClassFile(const std::string &ClassName, 1845 std::string &ErrorMsg) { 1846 if (!mUseStdout) { 1847 mOF.clear(); 1848 std::string Path = 1849 RSSlangReflectUtils::ComputePackagedPath(mOutputPathBase.c_str(), 1850 mPackageName.c_str()); 1851 1852 if (!SlangUtils::CreateDirectoryWithParents(Path, &ErrorMsg)) 1853 return false; 1854 1855 std::string ClassFile = Path + "/" + ClassName + ".java"; 1856 1857 mOF.open(ClassFile.c_str()); 1858 if (!mOF.good()) { 1859 ErrorMsg = "failed to open file '" + ClassFile + "' for write"; 1860 return false; 1861 } 1862 } 1863 return true; 1864} 1865 1866const char *RSReflection::Context::AccessModifierStr(AccessModifier AM) { 1867 switch (AM) { 1868 case AM_Public: return "public"; break; 1869 case AM_Protected: return "protected"; break; 1870 case AM_Private: return "private"; break; 1871 default: return ""; break; 1872 } 1873} 1874 1875bool RSReflection::Context::startClass(AccessModifier AM, 1876 bool IsStatic, 1877 const std::string &ClassName, 1878 const char *SuperClassName, 1879 std::string &ErrorMsg) { 1880 if (mVerbose) 1881 std::cout << "Generating " << ClassName << ".java ..." << std::endl; 1882 1883 // Open file for class 1884 if (!openClassFile(ClassName, ErrorMsg)) 1885 return false; 1886 1887 // License 1888 out() << mLicenseNote; 1889 1890 // Notice of generated file 1891 out() << "/*" << std::endl; 1892 out() << " * This file is auto-generated. DO NOT MODIFY!" << std::endl; 1893 out() << " * The source RenderScript file: " << mInputRSFile << std::endl; 1894 out() << " */" << std::endl; 1895 1896 // Package 1897 if (!mPackageName.empty()) 1898 out() << "package " << mPackageName << ";" << std::endl; 1899 out() << std::endl; 1900 1901 // Imports 1902 for (unsigned i = 0; i < (sizeof(Import) / sizeof(const char*)); i++) 1903 out() << "import " << Import[i] << ";" << std::endl; 1904 out() << std::endl; 1905 1906 // All reflected classes should be annotated as hidden, so that they won't 1907 // be exposed in SDK. 1908 out() << "/**" << std::endl; 1909 out() << " * @hide" << std::endl; 1910 out() << " */" << std::endl; 1911 1912 out() << AccessModifierStr(AM) << ((IsStatic) ? " static" : "") << " class " 1913 << ClassName; 1914 if (SuperClassName != NULL) 1915 out() << " extends " << SuperClassName; 1916 1917 startBlock(); 1918 1919 mClassName = ClassName; 1920 1921 return true; 1922} 1923 1924void RSReflection::Context::endClass() { 1925 endBlock(); 1926 if (!mUseStdout) 1927 mOF.close(); 1928 clear(); 1929 return; 1930} 1931 1932void RSReflection::Context::startBlock(bool ShouldIndent) { 1933 if (ShouldIndent) 1934 indent() << "{" << std::endl; 1935 else 1936 out() << " {" << std::endl; 1937 incIndentLevel(); 1938 return; 1939} 1940 1941void RSReflection::Context::endBlock() { 1942 decIndentLevel(); 1943 indent() << "}" << std::endl << std::endl; 1944 return; 1945} 1946 1947void RSReflection::Context::startTypeClass(const std::string &ClassName) { 1948 indent() << "public static class " << ClassName; 1949 startBlock(); 1950 return; 1951} 1952 1953void RSReflection::Context::endTypeClass() { 1954 endBlock(); 1955 return; 1956} 1957 1958void RSReflection::Context::startFunction(AccessModifier AM, 1959 bool IsStatic, 1960 const char *ReturnType, 1961 const std::string &FunctionName, 1962 int Argc, ...) { 1963 ArgTy Args; 1964 va_list vl; 1965 va_start(vl, Argc); 1966 1967 for (int i = 0; i < Argc; i++) { 1968 const char *ArgType = va_arg(vl, const char*); 1969 const char *ArgName = va_arg(vl, const char*); 1970 1971 Args.push_back(std::make_pair(ArgType, ArgName)); 1972 } 1973 va_end(vl); 1974 1975 startFunction(AM, IsStatic, ReturnType, FunctionName, Args); 1976 1977 return; 1978} 1979 1980void RSReflection::Context::startFunction(AccessModifier AM, 1981 bool IsStatic, 1982 const char *ReturnType, 1983 const std::string &FunctionName, 1984 const ArgTy &Args) { 1985 indent() << AccessModifierStr(AM) << ((IsStatic) ? " static " : " ") 1986 << ((ReturnType) ? ReturnType : "") << " " << FunctionName << "("; 1987 1988 bool FirstArg = true; 1989 for (ArgTy::const_iterator I = Args.begin(), E = Args.end(); 1990 I != E; 1991 I++) { 1992 if (!FirstArg) 1993 out() << ", "; 1994 else 1995 FirstArg = false; 1996 1997 out() << I->first << " " << I->second; 1998 } 1999 2000 out() << ")"; 2001 startBlock(); 2002 2003 return; 2004} 2005 2006void RSReflection::Context::endFunction() { 2007 endBlock(); 2008 return; 2009} 2010 2011} // namespace slang 2012