slang_rs_reflection.cpp revision a036a8a7186f7c2f088917107f2c854fbd2187b8
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 "addMatrix", // RSExportPrimitiveType::DataTypeRSMatrix2x2 218 "addMatrix", // RSExportPrimitiveType::DataTypeRSMatrix3x3 219 "addMatrix", // 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 return; 543} 544 545void RSReflection::genInitBoolExportVariable(Context &C, 546 const std::string &VarName, 547 const clang::APValue &Val) { 548 assert(!Val.isUninit() && "Not a valid initializer"); 549 550 C.indent() << RS_EXPORT_VAR_PREFIX << VarName << " = "; 551 assert((Val.getKind() == clang::APValue::Int) && 552 "Bool type has wrong initial APValue"); 553 554 C.out() << ((Val.getInt().getSExtValue() == 0) ? "false" : "true") 555 << ";" << std::endl; 556 557 return; 558} 559 560void RSReflection::genInitPrimitiveExportVariable( 561 Context &C, 562 const std::string &VarName, 563 const clang::APValue &Val) { 564 assert(!Val.isUninit() && "Not a valid initializer"); 565 566 C.indent() << RS_EXPORT_VAR_PREFIX << VarName << " = "; 567 switch (Val.getKind()) { 568 case clang::APValue::Int: { 569 llvm::APInt api = Val.getInt(); 570 C.out() << api.getSExtValue(); 571 if (api.getBitWidth() > 32) { 572 C.out() << "L"; 573 } 574 break; 575 } 576 case clang::APValue::Float: { 577 llvm::APFloat apf = Val.getFloat(); 578 if (&apf.getSemantics() == &llvm::APFloat::IEEEsingle) { 579 C.out() << apf.convertToFloat() << "f"; 580 } else { 581 C.out() << apf.convertToDouble(); 582 } 583 break; 584 } 585 586 case clang::APValue::ComplexInt: 587 case clang::APValue::ComplexFloat: 588 case clang::APValue::LValue: 589 case clang::APValue::Vector: { 590 assert(false && "Primitive type cannot have such kind of initializer"); 591 break; 592 } 593 default: { 594 assert(false && "Unknown kind of initializer"); 595 } 596 } 597 C.out() << ";" << std::endl; 598 599 return; 600} 601 602void RSReflection::genInitExportVariable(Context &C, 603 const RSExportType *ET, 604 const std::string &VarName, 605 const clang::APValue &Val) { 606 assert(!Val.isUninit() && "Not a valid initializer"); 607 608 switch (ET->getClass()) { 609 case RSExportType::ExportClassPrimitive: { 610 const RSExportPrimitiveType *EPT = 611 static_cast<const RSExportPrimitiveType*>(ET); 612 if (EPT->getType() == RSExportPrimitiveType::DataTypeBoolean) { 613 genInitBoolExportVariable(C, VarName, Val); 614 } else { 615 genInitPrimitiveExportVariable(C, VarName, Val); 616 } 617 break; 618 } 619 case RSExportType::ExportClassPointer: { 620 if (!Val.isInt() || Val.getInt().getSExtValue() != 0) 621 std::cout << "Initializer which is non-NULL to pointer type variable " 622 "will be ignored" << std::endl; 623 break; 624 } 625 case RSExportType::ExportClassVector: { 626 const RSExportVectorType *EVT = 627 static_cast<const RSExportVectorType*>(ET); 628 switch (Val.getKind()) { 629 case clang::APValue::Int: 630 case clang::APValue::Float: { 631 for (unsigned i = 0; i < EVT->getNumElement(); i++) { 632 std::string Name = VarName + "." + GetVectorAccessor(i); 633 genInitPrimitiveExportVariable(C, Name, Val); 634 } 635 break; 636 } 637 case clang::APValue::Vector: { 638 C.indent() << RS_EXPORT_VAR_PREFIX << VarName << " = new " 639 << GetVectorTypeName(EVT) << "();" << std::endl; 640 641 unsigned NumElements = 642 std::min(static_cast<unsigned>(EVT->getNumElement()), 643 Val.getVectorLength()); 644 for (unsigned i = 0; i < NumElements; i++) { 645 const clang::APValue &ElementVal = Val.getVectorElt(i); 646 std::string Name = VarName + "." + GetVectorAccessor(i); 647 genInitPrimitiveExportVariable(C, Name, ElementVal); 648 } 649 break; 650 } 651 case clang::APValue::Uninitialized: 652 case clang::APValue::ComplexInt: 653 case clang::APValue::ComplexFloat: 654 case clang::APValue::LValue: { 655 assert(false && "Unexpected type of value of initializer."); 656 } 657 } 658 break; 659 } 660 // TODO(zonr): Resolving initializer of a record (and matrix) type variable 661 // is complex. It cannot obtain by just simply evaluating the initializer 662 // expression. 663 case RSExportType::ExportClassMatrix: 664 case RSExportType::ExportClassConstantArray: 665 case RSExportType::ExportClassRecord: { 666#if 0 667 unsigned InitIndex = 0; 668 const RSExportRecordType *ERT = 669 static_cast<const RSExportRecordType*>(ET); 670 671 assert((Val.getKind() == clang::APValue::Vector) && "Unexpected type of " 672 "initializer for record type variable"); 673 674 C.indent() << RS_EXPORT_VAR_PREFIX << VarName 675 << " = new "RS_TYPE_CLASS_NAME_PREFIX << ERT->getName() 676 << "."RS_TYPE_ITEM_CLASS_NAME"();" << std::endl; 677 678 for (RSExportRecordType::const_field_iterator I = ERT->fields_begin(), 679 E = ERT->fields_end(); 680 I != E; 681 I++) { 682 const RSExportRecordType::Field *F = *I; 683 std::string FieldName = VarName + "." + F->getName(); 684 685 if (InitIndex > Val.getVectorLength()) 686 break; 687 688 genInitPrimitiveExportVariable(C, 689 FieldName, 690 Val.getVectorElt(InitIndex++)); 691 } 692#endif 693 assert(false && "Unsupported initializer for record/matrix/constant " 694 "array type variable currently"); 695 break; 696 } 697 default: { 698 assert(false && "Unknown class of type"); 699 } 700 } 701 return; 702} 703 704void RSReflection::genExportVariable(Context &C, const RSExportVar *EV) { 705 const RSExportType *ET = EV->getType(); 706 707 C.indent() << "private final static int "RS_EXPORT_VAR_INDEX_PREFIX 708 << EV->getName() << " = " << C.getNextExportVarSlot() << ";" 709 << std::endl; 710 711 switch (ET->getClass()) { 712 case RSExportType::ExportClassPrimitive: { 713 genPrimitiveTypeExportVariable(C, EV); 714 break; 715 } 716 case RSExportType::ExportClassPointer: { 717 genPointerTypeExportVariable(C, EV); 718 break; 719 } 720 case RSExportType::ExportClassVector: { 721 genVectorTypeExportVariable(C, EV); 722 break; 723 } 724 case RSExportType::ExportClassMatrix: { 725 genMatrixTypeExportVariable(C, EV); 726 break; 727 } 728 case RSExportType::ExportClassConstantArray: { 729 genConstantArrayTypeExportVariable(C, EV); 730 break; 731 } 732 case RSExportType::ExportClassRecord: { 733 genRecordTypeExportVariable(C, EV); 734 break; 735 } 736 default: { 737 assert(false && "Unknown class of type"); 738 } 739 } 740 741 return; 742} 743 744void RSReflection::genExportFunction(Context &C, const RSExportFunc *EF) { 745 C.indent() << "private final static int "RS_EXPORT_FUNC_INDEX_PREFIX 746 << EF->getName() << " = " << C.getNextExportFuncSlot() << ";" 747 << std::endl; 748 749 // invoke_*() 750 Context::ArgTy Args; 751 752 if (EF->hasParam()) { 753 for (RSExportFunc::const_param_iterator I = EF->params_begin(), 754 E = EF->params_end(); 755 I != E; 756 I++) { 757 Args.push_back(std::make_pair(GetTypeName((*I)->getType()), 758 (*I)->getName())); 759 } 760 } 761 762 C.startFunction(Context::AM_Public, 763 false, 764 "void", 765 "invoke_" + EF->getName(), 766 Args); 767 768 if (!EF->hasParam()) { 769 C.indent() << "invoke("RS_EXPORT_FUNC_INDEX_PREFIX << EF->getName() << ");" 770 << std::endl; 771 } else { 772 const RSExportRecordType *ERT = EF->getParamPacketType(); 773 std::string FieldPackerName = EF->getName() + "_fp"; 774 775 if (genCreateFieldPacker(C, ERT, FieldPackerName.c_str())) 776 genPackVarOfType(C, ERT, NULL, FieldPackerName.c_str()); 777 778 C.indent() << "invoke("RS_EXPORT_FUNC_INDEX_PREFIX << EF->getName() << ", " 779 << FieldPackerName << ");" << std::endl; 780 } 781 782 C.endFunction(); 783 return; 784} 785 786void RSReflection::genPrimitiveTypeExportVariable( 787 Context &C, 788 const RSExportVar *EV) { 789 assert((EV->getType()->getClass() == RSExportType::ExportClassPrimitive) && 790 "Variable should be type of primitive here"); 791 792 const RSExportPrimitiveType *EPT = 793 static_cast<const RSExportPrimitiveType*>(EV->getType()); 794 const char *TypeName = GetPrimitiveTypeName(EPT); 795 796 C.indent() << "private " << TypeName << " "RS_EXPORT_VAR_PREFIX 797 << EV->getName() << ";" << std::endl; 798 799 // set_*() 800 if (!EV->isConst()) { 801 C.startFunction(Context::AM_Public, 802 false, 803 "void", 804 "set_" + EV->getName(), 805 1, 806 TypeName, "v"); 807 C.indent() << RS_EXPORT_VAR_PREFIX << EV->getName() << " = v;" << std::endl; 808 809 C.indent() << "setVar("RS_EXPORT_VAR_INDEX_PREFIX << EV->getName() 810 << ", v);" << std::endl; 811 812 C.endFunction(); 813 } 814 815 genGetExportVariable(C, TypeName, EV->getName()); 816 817 return; 818} 819 820void RSReflection::genPointerTypeExportVariable(Context &C, 821 const RSExportVar *EV) { 822 const RSExportType *ET = EV->getType(); 823 const RSExportType *PointeeType; 824 std::string TypeName; 825 826 assert((ET->getClass() == RSExportType::ExportClassPointer) && 827 "Variable should be type of pointer here"); 828 829 PointeeType = static_cast<const RSExportPointerType*>(ET)->getPointeeType(); 830 TypeName = GetTypeName(ET); 831 832 C.indent() << "private " << TypeName << " "RS_EXPORT_VAR_PREFIX 833 << EV->getName() << ";" << std::endl; 834 835 // bind_*() 836 C.startFunction(Context::AM_Public, 837 false, 838 "void", 839 "bind_" + EV->getName(), 840 1, 841 TypeName.c_str(), "v"); 842 843 C.indent() << RS_EXPORT_VAR_PREFIX << EV->getName() << " = v;" << std::endl; 844 C.indent() << "if (v == null) bindAllocation(null, "RS_EXPORT_VAR_INDEX_PREFIX 845 << EV->getName() << ");" << std::endl; 846 847 if (PointeeType->getClass() == RSExportType::ExportClassRecord) 848 C.indent() << "else bindAllocation(v.getAllocation(), " 849 RS_EXPORT_VAR_INDEX_PREFIX << EV->getName() << ");" 850 << std::endl; 851 else 852 C.indent() << "else bindAllocation(v, "RS_EXPORT_VAR_INDEX_PREFIX 853 << EV->getName() << ");" << std::endl; 854 855 C.endFunction(); 856 857 genGetExportVariable(C, TypeName, EV->getName()); 858 859 return; 860} 861 862void RSReflection::genVectorTypeExportVariable(Context &C, 863 const RSExportVar *EV) { 864 assert((EV->getType()->getClass() == RSExportType::ExportClassVector) && 865 "Variable should be type of vector here"); 866 867 const RSExportVectorType *EVT = 868 static_cast<const RSExportVectorType*>(EV->getType()); 869 const char *TypeName = GetVectorTypeName(EVT); 870 const char *FieldPackerName = "fp"; 871 872 C.indent() << "private " << TypeName << " "RS_EXPORT_VAR_PREFIX 873 << EV->getName() << ";" << std::endl; 874 875 // set_*() 876 if (!EV->isConst()) { 877 C.startFunction(Context::AM_Public, 878 false, 879 "void", 880 "set_" + EV->getName(), 881 1, 882 TypeName, "v"); 883 C.indent() << RS_EXPORT_VAR_PREFIX << EV->getName() << " = v;" << std::endl; 884 885 if (genCreateFieldPacker(C, EVT, FieldPackerName)) 886 genPackVarOfType(C, EVT, "v", FieldPackerName); 887 C.indent() << "setVar("RS_EXPORT_VAR_INDEX_PREFIX << EV->getName() << ", " 888 << FieldPackerName << ");" << std::endl; 889 890 C.endFunction(); 891 } 892 893 genGetExportVariable(C, TypeName, EV->getName()); 894 return; 895} 896 897void RSReflection::genMatrixTypeExportVariable(Context &C, 898 const RSExportVar *EV) { 899 assert((EV->getType()->getClass() == RSExportType::ExportClassMatrix) && 900 "Variable should be type of matrix here"); 901 902 const RSExportMatrixType *EMT = 903 static_cast<const RSExportMatrixType*>(EV->getType()); 904 const char *TypeName = GetMatrixTypeName(EMT); 905 const char *FieldPackerName = "fp"; 906 907 C.indent() << "private " << TypeName << " "RS_EXPORT_VAR_PREFIX 908 << EV->getName() << ";" << std::endl; 909 910 // set_*() 911 if (!EV->isConst()) { 912 C.startFunction(Context::AM_Public, 913 false, 914 "void", 915 "set_" + EV->getName(), 916 1, 917 TypeName, "v"); 918 C.indent() << RS_EXPORT_VAR_PREFIX << EV->getName() << " = v;" << std::endl; 919 920 if (genCreateFieldPacker(C, EMT, FieldPackerName)) 921 genPackVarOfType(C, EMT, "v", FieldPackerName); 922 C.indent() << "setVar("RS_EXPORT_VAR_INDEX_PREFIX << EV->getName() << ", " 923 << FieldPackerName << ");" << std::endl; 924 925 C.endFunction(); 926 } 927 928 genGetExportVariable(C, TypeName, EV->getName()); 929 return; 930} 931 932void RSReflection::genConstantArrayTypeExportVariable( 933 Context &C, 934 const RSExportVar *EV) { 935 assert((EV->getType()->getClass() == RSExportType::ExportClassConstantArray)&& 936 "Variable should be type of constant array here"); 937 938 const RSExportConstantArrayType *ECAT = 939 static_cast<const RSExportConstantArrayType*>(EV->getType()); 940 std::string TypeName = GetTypeName(ECAT); 941 const char *FieldPackerName = "fp"; 942 943 C.indent() << "private " << TypeName << " "RS_EXPORT_VAR_PREFIX 944 << EV->getName() << ";" << std::endl; 945 946 // set_*() 947 if (!EV->isConst()) { 948 C.startFunction(Context::AM_Public, 949 false, 950 "void", 951 "set_" + EV->getName(), 952 1, 953 TypeName.c_str(), "v"); 954 C.indent() << RS_EXPORT_VAR_PREFIX << EV->getName() << " = v;" << std::endl; 955 956 if (genCreateFieldPacker(C, ECAT, FieldPackerName)) 957 genPackVarOfType(C, ECAT, "v", FieldPackerName); 958 C.indent() << "setVar("RS_EXPORT_VAR_INDEX_PREFIX << EV->getName() << ", " 959 << FieldPackerName << ");" << std::endl; 960 961 C.endFunction(); 962 } 963 964 genGetExportVariable(C, TypeName, EV->getName()); 965 return; 966} 967 968void RSReflection::genRecordTypeExportVariable(Context &C, 969 const RSExportVar *EV) { 970 assert((EV->getType()->getClass() == RSExportType::ExportClassRecord) && 971 "Variable should be type of struct here"); 972 973 const RSExportRecordType *ERT = 974 static_cast<const RSExportRecordType*>(EV->getType()); 975 std::string TypeName = 976 RS_TYPE_CLASS_NAME_PREFIX + ERT->getName() + "."RS_TYPE_ITEM_CLASS_NAME; 977 const char *FieldPackerName = "fp"; 978 979 C.indent() << "private " << TypeName << " "RS_EXPORT_VAR_PREFIX 980 << EV->getName() << ";" << std::endl; 981 982 // set_*() 983 if (!EV->isConst()) { 984 C.startFunction(Context::AM_Public, 985 false, 986 "void", 987 "set_" + EV->getName(), 988 1, 989 TypeName.c_str(), "v"); 990 C.indent() << RS_EXPORT_VAR_PREFIX << EV->getName() << " = v;" << std::endl; 991 992 if (genCreateFieldPacker(C, ERT, FieldPackerName)) 993 genPackVarOfType(C, ERT, "v", FieldPackerName); 994 C.indent() << "setVar("RS_EXPORT_VAR_INDEX_PREFIX << EV->getName() 995 << ", " << FieldPackerName << ");" << std::endl; 996 997 C.endFunction(); 998 } 999 1000 genGetExportVariable(C, TypeName.c_str(), EV->getName()); 1001 return; 1002} 1003 1004void RSReflection::genGetExportVariable(Context &C, 1005 const std::string &TypeName, 1006 const std::string &VarName) { 1007 C.startFunction(Context::AM_Public, 1008 false, 1009 TypeName.c_str(), 1010 "get_" + VarName, 1011 0); 1012 1013 C.indent() << "return "RS_EXPORT_VAR_PREFIX << VarName << ";" << std::endl; 1014 1015 C.endFunction(); 1016 return; 1017} 1018 1019/******************* Methods to generate script class /end *******************/ 1020 1021bool RSReflection::genCreateFieldPacker(Context &C, 1022 const RSExportType *ET, 1023 const char *FieldPackerName) { 1024 size_t AllocSize = RSExportType::GetTypeAllocSize(ET); 1025 if (AllocSize > 0) 1026 C.indent() << "FieldPacker " << FieldPackerName << " = new FieldPacker(" 1027 << AllocSize << ");" << std::endl; 1028 else 1029 return false; 1030 return true; 1031} 1032 1033void RSReflection::genPackVarOfType(Context &C, 1034 const RSExportType *ET, 1035 const char *VarName, 1036 const char *FieldPackerName) { 1037 switch (ET->getClass()) { 1038 case RSExportType::ExportClassPrimitive: 1039 case RSExportType::ExportClassVector: { 1040 C.indent() << FieldPackerName << "." 1041 << GetPackerAPIName( 1042 static_cast<const RSExportPrimitiveType*>(ET)) 1043 << "(" << VarName << ");" << std::endl; 1044 break; 1045 } 1046 case RSExportType::ExportClassPointer: { 1047 // Must reflect as type Allocation in Java 1048 const RSExportType *PointeeType = 1049 static_cast<const RSExportPointerType*>(ET)->getPointeeType(); 1050 1051 if (PointeeType->getClass() != RSExportType::ExportClassRecord) 1052 C.indent() << FieldPackerName << ".addI32(" << VarName 1053 << ".getPtr());" << std::endl; 1054 else 1055 C.indent() << FieldPackerName << ".addI32(" << VarName 1056 << ".getAllocation().getPtr());" << std::endl; 1057 break; 1058 } 1059 case RSExportType::ExportClassMatrix: { 1060 C.indent() << FieldPackerName << ".addMatrix(" << VarName << ");" 1061 << std::endl; 1062 break; 1063 } 1064 case RSExportType::ExportClassConstantArray: { 1065 const RSExportConstantArrayType *ECAT = 1066 static_cast<const RSExportConstantArrayType *>(ET); 1067 1068 // TODO(zonr): more elegant way. Currently, we obtain the unique index 1069 // variable (this method involves recursive call which means 1070 // we may have more than one level loop, therefore we can't 1071 // always use the same index variable name here) name given 1072 // in the for-loop from counting the '.' in @VarName. 1073 unsigned Level = 0; 1074 size_t LastDotPos = 0; 1075 std::string ElementVarName(VarName); 1076 1077 while (LastDotPos != std::string::npos) { 1078 LastDotPos = ElementVarName.find_first_of('.', LastDotPos + 1); 1079 Level++; 1080 } 1081 std::string IndexVarName("ct"); 1082 IndexVarName.append(llvm::utostr_32(Level)); 1083 1084 C.indent() << "for (int " << IndexVarName << " = 0; " << 1085 IndexVarName << " < " << ECAT->getSize() << "; " << 1086 IndexVarName << "++)"; 1087 C.startBlock(); 1088 1089 ElementVarName.append("[" + IndexVarName + "]"); 1090 genPackVarOfType(C, ECAT->getElementType(), ElementVarName.c_str(), 1091 FieldPackerName); 1092 1093 C.endBlock(); 1094 break; 1095 } 1096 case RSExportType::ExportClassRecord: { 1097 const RSExportRecordType *ERT = 1098 static_cast<const RSExportRecordType*>(ET); 1099 // Relative pos from now on in field packer 1100 unsigned Pos = 0; 1101 1102 for (RSExportRecordType::const_field_iterator I = ERT->fields_begin(), 1103 E = ERT->fields_end(); 1104 I != E; 1105 I++) { 1106 const RSExportRecordType::Field *F = *I; 1107 std::string FieldName; 1108 size_t FieldOffset = F->getOffsetInParent(); 1109 size_t FieldStoreSize = RSExportType::GetTypeStoreSize(F->getType()); 1110 size_t FieldAllocSize = RSExportType::GetTypeAllocSize(F->getType()); 1111 1112 if (VarName != NULL) 1113 FieldName = VarName + ("." + F->getName()); 1114 else 1115 FieldName = F->getName(); 1116 1117 if (FieldOffset > Pos) 1118 C.indent() << FieldPackerName << ".skip(" 1119 << (FieldOffset - Pos) << ");" << std::endl; 1120 1121 genPackVarOfType(C, F->getType(), FieldName.c_str(), FieldPackerName); 1122 1123 // There is padding in the field type 1124 if (FieldAllocSize > FieldStoreSize) 1125 C.indent() << FieldPackerName << ".skip(" 1126 << (FieldAllocSize - FieldStoreSize) 1127 << ");" << std::endl; 1128 1129 Pos = FieldOffset + FieldAllocSize; 1130 } 1131 1132 // There maybe some padding after the struct 1133 size_t Padding = RSExportType::GetTypeAllocSize(ERT) - Pos; 1134 if (Padding > 0) 1135 C.indent() << FieldPackerName << ".skip(" << Padding << ");" 1136 << std::endl; 1137 break; 1138 } 1139 default: { 1140 assert(false && "Unknown class of type"); 1141 } 1142 } 1143 1144 return; 1145} 1146 1147void RSReflection::genAllocateVarOfType(Context &C, 1148 const RSExportType *T, 1149 const std::string &VarName) { 1150 switch (T->getClass()) { 1151 case RSExportType::ExportClassPrimitive: { 1152 // Primitive type like int in Java has its own storage once it's declared. 1153 // 1154 // FIXME: Should we allocate storage for RS object? 1155 // if (static_cast<const RSExportPrimitiveType *>(T)->isRSObjectType()) 1156 // C.indent() << VarName << " = new " << GetTypeName(T) << "();" 1157 // << std::endl; 1158 break; 1159 } 1160 case RSExportType::ExportClassPointer: { 1161 // Pointer type is an instance of Allocation or a TypeClass whose value is 1162 // expected to be assigned by programmer later in Java program. Therefore 1163 // we don't reflect things like [VarName] = new Allocation(); 1164 C.indent() << VarName << " = null;" << std::endl; 1165 break; 1166 } 1167 case RSExportType::ExportClassConstantArray: { 1168 const RSExportConstantArrayType *ECAT = 1169 static_cast<const RSExportConstantArrayType *>(T); 1170 const RSExportType *ElementType = ECAT->getElementType(); 1171 1172 C.indent() << VarName << " = new " << GetTypeName(ElementType) 1173 << "[" << ECAT->getSize() << "];" << std::endl; 1174 1175 // Primitive type element doesn't need allocation code. 1176 if (ElementType->getClass() != RSExportType::ExportClassPrimitive) { 1177 C.indent() << "for (int $ct = 0; $ct < " << ECAT->getSize() << "; " 1178 "$ct++)"; 1179 C.startBlock(); 1180 1181 std::string ElementVarName(VarName); 1182 ElementVarName.append("[$ct]"); 1183 genAllocateVarOfType(C, ElementType, ElementVarName); 1184 1185 C.endBlock(); 1186 } 1187 break; 1188 } 1189 case RSExportType::ExportClassVector: 1190 case RSExportType::ExportClassMatrix: 1191 case RSExportType::ExportClassRecord: { 1192 C.indent() << VarName << " = new " << GetTypeName(T) << "();" 1193 << std::endl; 1194 break; 1195 } 1196 } 1197 return; 1198} 1199 1200void RSReflection::genNewItemBufferIfNull(Context &C, 1201 const char *Index) { 1202 C.indent() << "if (" RS_TYPE_ITEM_BUFFER_NAME " == null) " 1203 RS_TYPE_ITEM_BUFFER_NAME " = " 1204 "new " RS_TYPE_ITEM_CLASS_NAME 1205 "[getType().getX() /* count */];" 1206 << std::endl; 1207 if (Index != NULL) 1208 C.indent() << "if ("RS_TYPE_ITEM_BUFFER_NAME"[" << Index << "] == null) " 1209 RS_TYPE_ITEM_BUFFER_NAME"[" << Index << "] = " 1210 "new "RS_TYPE_ITEM_CLASS_NAME"();" << std::endl; 1211 return; 1212} 1213 1214void RSReflection::genNewItemBufferPackerIfNull(Context &C) { 1215 C.indent() << "if (" RS_TYPE_ITEM_BUFFER_PACKER_NAME " == null) " 1216 RS_TYPE_ITEM_BUFFER_PACKER_NAME " = " 1217 "new FieldPacker(" RS_TYPE_ITEM_CLASS_NAME 1218 ".sizeof * getType().getX()/* count */" 1219 ");" << std::endl; 1220 return; 1221} 1222 1223/********************** Methods to generate type class **********************/ 1224bool RSReflection::genTypeClass(Context &C, 1225 const RSExportRecordType *ERT, 1226 std::string &ErrorMsg) { 1227 std::string ClassName = RS_TYPE_CLASS_NAME_PREFIX + ERT->getName(); 1228 1229 if (!C.startClass(Context::AM_Public, 1230 false, 1231 ClassName, 1232 RS_TYPE_CLASS_SUPER_CLASS_NAME, 1233 ErrorMsg)) 1234 return false; 1235 1236 genTypeItemClass(C, ERT); 1237 1238 // Declare item buffer and item buffer packer 1239 C.indent() << "private "RS_TYPE_ITEM_CLASS_NAME" "RS_TYPE_ITEM_BUFFER_NAME"[]" 1240 ";" << std::endl; 1241 C.indent() << "private FieldPacker "RS_TYPE_ITEM_BUFFER_PACKER_NAME";" 1242 << std::endl; 1243 1244 genTypeClassConstructor(C, ERT); 1245 genTypeClassCopyToArray(C, ERT); 1246 genTypeClassItemSetter(C, ERT); 1247 genTypeClassItemGetter(C, ERT); 1248 genTypeClassComponentSetter(C, ERT); 1249 genTypeClassComponentGetter(C, ERT); 1250 genTypeClassCopyAll(C, ERT); 1251 genTypeClassResize(C); 1252 1253 C.endClass(); 1254 1255 C.resetFieldIndex(); 1256 C.clearFieldIndexMap(); 1257 1258 return true; 1259} 1260 1261void RSReflection::genTypeItemClass(Context &C, 1262 const RSExportRecordType *ERT) { 1263 C.indent() << "static public class "RS_TYPE_ITEM_CLASS_NAME; 1264 C.startBlock(); 1265 1266 C.indent() << "public static final int sizeof = " 1267 << RSExportType::GetTypeAllocSize(ERT) << ";" << std::endl; 1268 1269 // Member elements 1270 C.out() << std::endl; 1271 for (RSExportRecordType::const_field_iterator FI = ERT->fields_begin(), 1272 FE = ERT->fields_end(); 1273 FI != FE; 1274 FI++) { 1275 C.indent() << GetTypeName((*FI)->getType()) << " " << (*FI)->getName() 1276 << ";" << std::endl; 1277 } 1278 1279 // Constructor 1280 C.out() << std::endl; 1281 C.indent() << RS_TYPE_ITEM_CLASS_NAME"()"; 1282 C.startBlock(); 1283 1284 for (RSExportRecordType::const_field_iterator FI = ERT->fields_begin(), 1285 FE = ERT->fields_end(); 1286 FI != FE; 1287 FI++) { 1288 const RSExportRecordType::Field *F = *FI; 1289 genAllocateVarOfType(C, F->getType(), F->getName()); 1290 } 1291 1292 // end Constructor 1293 C.endBlock(); 1294 1295 // end Item class 1296 C.endBlock(); 1297 1298 return; 1299} 1300 1301void RSReflection::genTypeClassConstructor(Context &C, 1302 const RSExportRecordType *ERT) { 1303 const char *RenderScriptVar = "rs"; 1304 1305 C.startFunction(Context::AM_Public, 1306 true, 1307 "Element", 1308 "createElement", 1309 1, 1310 "RenderScript", RenderScriptVar); 1311 genBuildElement(C, "eb", ERT, RenderScriptVar, /* IsInline = */false); 1312 C.endFunction(); 1313 1314 C.startFunction(Context::AM_Public, 1315 false, 1316 NULL, 1317 C.getClassName(), 1318 2, 1319 "RenderScript", RenderScriptVar, 1320 "int", "count"); 1321 1322 C.indent() << RS_TYPE_ITEM_BUFFER_NAME" = null;" << std::endl; 1323 C.indent() << RS_TYPE_ITEM_BUFFER_PACKER_NAME" = null;" << std::endl; 1324 C.indent() << "mElement = createElement(" << RenderScriptVar << ");" 1325 << std::endl; 1326 // Call init() in super class 1327 C.indent() << "init(" << RenderScriptVar << ", count);" << std::endl; 1328 C.endFunction(); 1329 1330 C.startFunction(Context::AM_Public, 1331 false, 1332 NULL, 1333 C.getClassName(), 1334 3, 1335 "RenderScript", RenderScriptVar, 1336 "int", "count", 1337 "int", "usages"); 1338 1339 C.indent() << RS_TYPE_ITEM_BUFFER_NAME" = null;" << std::endl; 1340 C.indent() << RS_TYPE_ITEM_BUFFER_PACKER_NAME" = null;" << std::endl; 1341 C.indent() << "mElement = createElement(" << RenderScriptVar << ");" 1342 << std::endl; 1343 // Call init() in super class 1344 C.indent() << "init(" << RenderScriptVar << ", count, usages);" << std::endl; 1345 C.endFunction(); 1346 1347 return; 1348} 1349 1350void RSReflection::genTypeClassCopyToArray(Context &C, 1351 const RSExportRecordType *ERT) { 1352 C.startFunction(Context::AM_Private, 1353 false, 1354 "void", 1355 "copyToArray", 1356 2, 1357 RS_TYPE_ITEM_CLASS_NAME, "i", 1358 "int", "index"); 1359 1360 genNewItemBufferPackerIfNull(C); 1361 C.indent() << RS_TYPE_ITEM_BUFFER_PACKER_NAME 1362 ".reset(index * "RS_TYPE_ITEM_CLASS_NAME".sizeof);" 1363 << std::endl; 1364 1365 genPackVarOfType(C, ERT, "i", RS_TYPE_ITEM_BUFFER_PACKER_NAME); 1366 1367 C.endFunction(); 1368 return; 1369} 1370 1371void RSReflection::genTypeClassItemSetter(Context &C, 1372 const RSExportRecordType *ERT) { 1373 C.startFunction(Context::AM_Public, 1374 false, 1375 "void", 1376 "set", 1377 3, 1378 RS_TYPE_ITEM_CLASS_NAME, "i", 1379 "int", "index", 1380 "boolean", "copyNow"); 1381 genNewItemBufferIfNull(C, NULL); 1382 C.indent() << RS_TYPE_ITEM_BUFFER_NAME"[index] = i;" << std::endl; 1383 1384 C.indent() << "if (copyNow) "; 1385 C.startBlock(); 1386 1387 C.indent() << "copyToArray(i, index);" << std::endl; 1388 C.indent() << "mAllocation.setFromFieldPacker(index, " 1389 RS_TYPE_ITEM_BUFFER_PACKER_NAME");" << std::endl; 1390 1391 // End of if (copyNow) 1392 C.endBlock(); 1393 1394 C.endFunction(); 1395 return; 1396} 1397 1398void RSReflection::genTypeClassItemGetter(Context &C, 1399 const RSExportRecordType *ERT) { 1400 C.startFunction(Context::AM_Public, 1401 false, 1402 RS_TYPE_ITEM_CLASS_NAME, 1403 "get", 1404 1, 1405 "int", "index"); 1406 C.indent() << "if ("RS_TYPE_ITEM_BUFFER_NAME" == null) return null;" 1407 << std::endl; 1408 C.indent() << "return "RS_TYPE_ITEM_BUFFER_NAME"[index];" << std::endl; 1409 C.endFunction(); 1410 return; 1411} 1412 1413void RSReflection::genTypeClassComponentSetter(Context &C, 1414 const RSExportRecordType *ERT) { 1415 for (RSExportRecordType::const_field_iterator FI = ERT->fields_begin(), 1416 FE = ERT->fields_end(); 1417 FI != FE; 1418 FI++) { 1419 const RSExportRecordType::Field *F = *FI; 1420 size_t FieldOffset = F->getOffsetInParent(); 1421 size_t FieldStoreSize = RSExportType::GetTypeStoreSize(F->getType()); 1422 unsigned FieldIndex = C.getFieldIndex(F); 1423 1424 C.startFunction(Context::AM_Public, 1425 false, 1426 "void", 1427 "set_" + F->getName(), 3, 1428 "int", "index", 1429 GetTypeName(F->getType()).c_str(), "v", 1430 "boolean", "copyNow"); 1431 genNewItemBufferPackerIfNull(C); 1432 genNewItemBufferIfNull(C, "index"); 1433 C.indent() << RS_TYPE_ITEM_BUFFER_NAME"[index]." << F->getName() 1434 << " = v;" << std::endl; 1435 1436 C.indent() << "if (copyNow) "; 1437 C.startBlock(); 1438 1439 if (FieldOffset > 0) 1440 C.indent() << RS_TYPE_ITEM_BUFFER_PACKER_NAME 1441 ".reset(index * "RS_TYPE_ITEM_CLASS_NAME".sizeof + " 1442 << FieldOffset << ");" << std::endl; 1443 else 1444 C.indent() << RS_TYPE_ITEM_BUFFER_PACKER_NAME 1445 ".reset(index * "RS_TYPE_ITEM_CLASS_NAME".sizeof);" 1446 << std::endl; 1447 genPackVarOfType(C, F->getType(), "v", RS_TYPE_ITEM_BUFFER_PACKER_NAME); 1448 1449 C.indent() << "FieldPacker fp = new FieldPacker(" << FieldStoreSize << ");" 1450 << std::endl; 1451 genPackVarOfType(C, F->getType(), "v", "fp"); 1452 C.indent() << "mAllocation.setFromFieldPacker(index, " << FieldIndex 1453 << ", fp);" 1454 << std::endl; 1455 1456 // End of if (copyNow) 1457 C.endBlock(); 1458 1459 C.endFunction(); 1460 } 1461 return; 1462} 1463 1464void RSReflection::genTypeClassComponentGetter(Context &C, 1465 const RSExportRecordType *ERT) { 1466 for (RSExportRecordType::const_field_iterator FI = ERT->fields_begin(), 1467 FE = ERT->fields_end(); 1468 FI != FE; 1469 FI++) { 1470 const RSExportRecordType::Field *F = *FI; 1471 C.startFunction(Context::AM_Public, 1472 false, 1473 GetTypeName(F->getType()).c_str(), 1474 "get_" + F->getName(), 1475 1, 1476 "int", "index"); 1477 C.indent() << "if ("RS_TYPE_ITEM_BUFFER_NAME" == null) return " 1478 << GetTypeNullValue(F->getType()) << ";" << std::endl; 1479 C.indent() << "return "RS_TYPE_ITEM_BUFFER_NAME"[index]." << F->getName() 1480 << ";" << std::endl; 1481 C.endFunction(); 1482 } 1483 return; 1484} 1485 1486void RSReflection::genTypeClassCopyAll(Context &C, 1487 const RSExportRecordType *ERT) { 1488 C.startFunction(Context::AM_Public, false, "void", "copyAll", 0); 1489 1490 C.indent() << "for (int ct = 0; ct < "RS_TYPE_ITEM_BUFFER_NAME".length; ct++)" 1491 " copyToArray("RS_TYPE_ITEM_BUFFER_NAME"[ct], ct);" 1492 << std::endl; 1493 C.indent() << "mAllocation.copyFrom("RS_TYPE_ITEM_BUFFER_PACKER_NAME 1494 ".getData());" 1495 << std::endl; 1496 1497 C.endFunction(); 1498 return; 1499} 1500 1501void RSReflection::genTypeClassResize(Context &C) { 1502 C.startFunction(Context::AM_Public, 1503 false, 1504 "void", 1505 "resize", 1506 1, 1507 "int", "newSize"); 1508 1509 C.indent() << "if (mItemArray != null) "; 1510 C.startBlock(); 1511 C.indent() << "int oldSize = mItemArray.length;" << std::endl; 1512 C.indent() << "int copySize = Math.min(oldSize, newSize);" << std::endl; 1513 C.indent() << "if (newSize == oldSize) return;" << std::endl; 1514 C.indent() << "Item ni[] = new Item[newSize];" << std::endl; 1515 C.indent() << "System.arraycopy(mItemArray, 0, ni, 0, copySize);" 1516 << std::endl; 1517 C.indent() << "mItemArray = ni;" << std::endl; 1518 C.endBlock(); 1519 C.indent() << "mAllocation.resize(newSize);" << std::endl; 1520 1521 C.indent() << "if (" RS_TYPE_ITEM_BUFFER_PACKER_NAME " != null) " 1522 RS_TYPE_ITEM_BUFFER_PACKER_NAME " = " 1523 "new FieldPacker(" RS_TYPE_ITEM_CLASS_NAME 1524 ".sizeof * getType().getX()/* count */" 1525 ");" << std::endl; 1526 1527 C.endFunction(); 1528 return; 1529} 1530 1531/******************** Methods to generate type class /end ********************/ 1532 1533/********** Methods to create Element in Java of given record type ***********/ 1534void RSReflection::genBuildElement(Context &C, 1535 const char *ElementBuilderName, 1536 const RSExportRecordType *ERT, 1537 const char *RenderScriptVar, 1538 bool IsInline) { 1539 C.indent() << "Element.Builder " << ElementBuilderName << " = " 1540 "new Element.Builder(" << RenderScriptVar << ");" << std::endl; 1541 1542 // eb.add(...) 1543 genAddElementToElementBuilder(C, 1544 ERT, 1545 "", 1546 ElementBuilderName, 1547 RenderScriptVar, 1548 /* ArraySize = */0); 1549 1550 if (!IsInline) 1551 C.indent() << "return " << ElementBuilderName << ".create();" << std::endl; 1552 return; 1553} 1554 1555#define EB_ADD(x) do { \ 1556 C.indent() << ElementBuilderName \ 1557 << ".add(" << x << ", \"" << VarName << "\""; \ 1558 if (ArraySize > 0) \ 1559 C.out() << ", " << ArraySize; \ 1560 C.out() << ");" << std::endl; \ 1561 C.incFieldIndex(); \ 1562} while (false) 1563 1564void RSReflection::genAddElementToElementBuilder(Context &C, 1565 const RSExportType *ET, 1566 const std::string &VarName, 1567 const char *ElementBuilderName, 1568 const char *RenderScriptVar, 1569 unsigned ArraySize) { 1570 const char *ElementConstruct = GetBuiltinElementConstruct(ET); 1571 1572 if (ElementConstruct != NULL) { 1573 EB_ADD(ElementConstruct << "(" << RenderScriptVar << ")"); 1574 } else { 1575 if ((ET->getClass() == RSExportType::ExportClassPrimitive) || 1576 (ET->getClass() == RSExportType::ExportClassVector)) { 1577 const RSExportPrimitiveType *EPT = 1578 static_cast<const RSExportPrimitiveType*>(ET); 1579 const char *DataKindName = GetElementDataKindName(EPT->getKind()); 1580 const char *DataTypeName = GetElementDataTypeName(EPT->getType()); 1581 int Size = (ET->getClass() == RSExportType::ExportClassVector) ? 1582 static_cast<const RSExportVectorType*>(ET)->getNumElement() : 1583 1; 1584 1585 switch (EPT->getKind()) { 1586 case RSExportPrimitiveType::DataKindPixelL: 1587 case RSExportPrimitiveType::DataKindPixelA: 1588 case RSExportPrimitiveType::DataKindPixelLA: 1589 case RSExportPrimitiveType::DataKindPixelRGB: 1590 case RSExportPrimitiveType::DataKindPixelRGBA: { 1591 // Element.createPixel() 1592 EB_ADD("Element.createPixel(" << RenderScriptVar << ", " 1593 << DataTypeName << ", " 1594 << DataKindName << ")"); 1595 break; 1596 } 1597 case RSExportPrimitiveType::DataKindUser: 1598 default: { 1599 if (EPT->getClass() == RSExportType::ExportClassPrimitive) { 1600 // Element.createUser() 1601 EB_ADD("Element.createUser(" << RenderScriptVar << ", " 1602 << DataTypeName << ")"); 1603 } else { 1604 assert((ET->getClass() == RSExportType::ExportClassVector) && 1605 "Unexpected type."); 1606 EB_ADD("Element.createVector(" << RenderScriptVar << ", " 1607 << DataTypeName << ", " 1608 << Size << ")"); 1609 } 1610 break; 1611 } 1612 } 1613#ifndef NDEBUG 1614 } else if (ET->getClass() == RSExportType::ExportClassPointer) { 1615 // Pointer type variable should be resolved in 1616 // GetBuiltinElementConstruct() 1617 assert(false && "??"); 1618 } else if (ET->getClass() == RSExportType::ExportClassMatrix) { 1619 // Matrix type variable should be resolved 1620 // in GetBuiltinElementConstruct() 1621 assert(false && "??"); 1622#endif 1623 } else if (ET->getClass() == RSExportType::ExportClassConstantArray) { 1624 const RSExportConstantArrayType *ECAT = 1625 static_cast<const RSExportConstantArrayType *>(ET); 1626 1627 const RSExportType *ElementType = ECAT->getElementType(); 1628 if (ElementType->getClass() != RSExportType::ExportClassRecord) { 1629 genAddElementToElementBuilder(C, 1630 ECAT->getElementType(), 1631 VarName, 1632 ElementBuilderName, 1633 RenderScriptVar, 1634 ECAT->getSize()); 1635 } else { 1636 std::string NewElementBuilderName(ElementBuilderName); 1637 NewElementBuilderName.append(1, '_'); 1638 1639 genBuildElement(C, 1640 NewElementBuilderName.c_str(), 1641 static_cast<const RSExportRecordType*>(ElementType), 1642 RenderScriptVar, 1643 /* IsInline = */true); 1644 ArraySize = ECAT->getSize(); 1645 EB_ADD(NewElementBuilderName << ".create()"); 1646 } 1647 } else if (ET->getClass() == RSExportType::ExportClassRecord) { 1648 // Simalar to case of RSExportType::ExportClassRecord in genPackVarOfType. 1649 // 1650 // TODO(zonr): Generalize these two function such that there's no 1651 // duplicated codes. 1652 const RSExportRecordType *ERT = 1653 static_cast<const RSExportRecordType*>(ET); 1654 int Pos = 0; // relative pos from now on 1655 1656 for (RSExportRecordType::const_field_iterator I = ERT->fields_begin(), 1657 E = ERT->fields_end(); 1658 I != E; 1659 I++) { 1660 const RSExportRecordType::Field *F = *I; 1661 std::string FieldName; 1662 int FieldOffset = F->getOffsetInParent(); 1663 int FieldStoreSize = RSExportType::GetTypeStoreSize(F->getType()); 1664 int FieldAllocSize = RSExportType::GetTypeAllocSize(F->getType()); 1665 1666 if (!VarName.empty()) 1667 FieldName = VarName + "." + F->getName(); 1668 else 1669 FieldName = F->getName(); 1670 1671 // Alignment 1672 genAddPaddingToElementBuiler(C, 1673 (FieldOffset - Pos), 1674 ElementBuilderName, 1675 RenderScriptVar); 1676 1677 // eb.add(...) 1678 C.addFieldIndexMapping(F); 1679 if (F->getType()->getClass() != RSExportType::ExportClassRecord) { 1680 genAddElementToElementBuilder(C, 1681 F->getType(), 1682 FieldName, 1683 ElementBuilderName, 1684 RenderScriptVar, 1685 0); 1686 } else { 1687 std::string NewElementBuilderName(ElementBuilderName); 1688 NewElementBuilderName.append(1, '_'); 1689 1690 genBuildElement(C, 1691 NewElementBuilderName.c_str(), 1692 static_cast<const RSExportRecordType*>(F->getType()), 1693 RenderScriptVar, 1694 /* IsInline = */true); 1695 1696 const std::string &VarName = FieldName; // Hack for EB_ADD macro 1697 EB_ADD(NewElementBuilderName << ".create()"); 1698 } 1699 1700 // There is padding within the field type 1701 genAddPaddingToElementBuiler(C, 1702 (FieldAllocSize - FieldStoreSize), 1703 ElementBuilderName, 1704 RenderScriptVar); 1705 1706 Pos = FieldOffset + FieldAllocSize; 1707 } 1708 1709 // There maybe some padding after the struct 1710 size_t RecordStoreSize = RSExportType::GetTypeStoreSize(ERT); 1711 1712 genAddPaddingToElementBuiler(C, 1713 RecordStoreSize - Pos, 1714 ElementBuilderName, 1715 RenderScriptVar); 1716 } else { 1717 assert(false && "Unknown class of type"); 1718 } 1719 } 1720} 1721 1722void RSReflection::genAddPaddingToElementBuiler(Context &C, 1723 int PaddingSize, 1724 const char *ElementBuilderName, 1725 const char *RenderScriptVar) { 1726 unsigned ArraySize = 0; // Hack the EB_ADD macro 1727 while (PaddingSize > 0) { 1728 const std::string &VarName = C.createPaddingField(); 1729 if (PaddingSize >= 4) { 1730 EB_ADD("Element.U32(" << RenderScriptVar << ")"); 1731 PaddingSize -= 4; 1732 } else if (PaddingSize >= 2) { 1733 EB_ADD("Element.U16(" << RenderScriptVar << ")"); 1734 PaddingSize -= 2; 1735 } else if (PaddingSize >= 1) { 1736 EB_ADD("Element.U8(" << RenderScriptVar << ")"); 1737 PaddingSize -= 1; 1738 } 1739 } 1740 return; 1741} 1742 1743#undef EB_ADD 1744/******** Methods to create Element in Java of given record type /end ********/ 1745 1746bool RSReflection::reflect(const std::string &OutputPathBase, 1747 const std::string &OutputPackageName, 1748 const std::string &InputFileName, 1749 const std::string &OutputBCFileName) { 1750 Context *C = NULL; 1751 std::string ResourceId = ""; 1752 1753 if (!GetClassNameFromFileName(OutputBCFileName, ResourceId)) 1754 return false; 1755 1756 if (ResourceId.empty()) 1757 ResourceId = "<Resource ID>"; 1758 1759 if (OutputPackageName.empty() || OutputPackageName == "-") 1760 C = new Context(OutputPathBase, InputFileName, "<Package Name>", 1761 ResourceId, true); 1762 else 1763 C = new Context(OutputPathBase, InputFileName, OutputPackageName, 1764 ResourceId, false); 1765 1766 if (C != NULL) { 1767 std::string ErrorMsg, ScriptClassName; 1768 // class ScriptC_<ScriptName> 1769 if (!GetClassNameFromFileName(InputFileName, ScriptClassName)) 1770 return false; 1771 1772 if (ScriptClassName.empty()) 1773 ScriptClassName = "<Input Script Name>"; 1774 1775 ScriptClassName.insert(0, RS_SCRIPT_CLASS_NAME_PREFIX); 1776 1777 if (mRSContext->getLicenseNote() != NULL) { 1778 C->setLicenseNote(*(mRSContext->getLicenseNote())); 1779 } 1780 1781 if (!genScriptClass(*C, ScriptClassName, ErrorMsg)) { 1782 std::cerr << "Failed to generate class " << ScriptClassName << " (" 1783 << ErrorMsg << ")" << std::endl; 1784 return false; 1785 } 1786 1787 // class ScriptField_<TypeName> 1788 for (RSContext::const_export_type_iterator TI = 1789 mRSContext->export_types_begin(), 1790 TE = mRSContext->export_types_end(); 1791 TI != TE; 1792 TI++) { 1793 const RSExportType *ET = TI->getValue(); 1794 1795 if (ET->getClass() == RSExportType::ExportClassRecord) { 1796 const RSExportRecordType *ERT = 1797 static_cast<const RSExportRecordType*>(ET); 1798 1799 if (!ERT->isArtificial() && !genTypeClass(*C, ERT, ErrorMsg)) { 1800 std::cerr << "Failed to generate type class for struct '" 1801 << ERT->getName() << "' (" << ErrorMsg << ")" << std::endl; 1802 return false; 1803 } 1804 } 1805 } 1806 } 1807 1808 return true; 1809} 1810 1811/************************** RSReflection::Context **************************/ 1812const char *const RSReflection::Context::ApacheLicenseNote = 1813 "/*\n" 1814 " * Copyright (C) 2010 The Android Open Source Project\n" 1815 " *\n" 1816 " * Licensed under the Apache License, Version 2.0 (the \"License\");\n" 1817 " * you may not use this file except in compliance with the License.\n" 1818 " * You may obtain a copy of the License at\n" 1819 " *\n" 1820 " * http://www.apache.org/licenses/LICENSE-2.0\n" 1821 " *\n" 1822 " * Unless required by applicable law or agreed to in writing, software\n" 1823 " * distributed under the License is distributed on an \"AS IS\" BASIS,\n" 1824 " * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or " 1825 "implied.\n" 1826 " * See the License for the specific language governing permissions and\n" 1827 " * limitations under the License.\n" 1828 " */\n" 1829 "\n"; 1830 1831const char *const RSReflection::Context::Import[] = { 1832 // RenderScript java class 1833 "android.renderscript.*", 1834 // Import R 1835 "android.content.res.Resources", 1836 // Import for debugging 1837 "android.util.Log", 1838}; 1839 1840bool RSReflection::Context::openClassFile(const std::string &ClassName, 1841 std::string &ErrorMsg) { 1842 if (!mUseStdout) { 1843 mOF.clear(); 1844 std::string Path = 1845 RSSlangReflectUtils::ComputePackagedPath(mOutputPathBase.c_str(), 1846 mPackageName.c_str()); 1847 1848 if (!SlangUtils::CreateDirectoryWithParents(Path, &ErrorMsg)) 1849 return false; 1850 1851 std::string ClassFile = Path + "/" + ClassName + ".java"; 1852 1853 mOF.open(ClassFile.c_str()); 1854 if (!mOF.good()) { 1855 ErrorMsg = "failed to open file '" + ClassFile + "' for write"; 1856 return false; 1857 } 1858 } 1859 return true; 1860} 1861 1862const char *RSReflection::Context::AccessModifierStr(AccessModifier AM) { 1863 switch (AM) { 1864 case AM_Public: return "public"; break; 1865 case AM_Protected: return "protected"; break; 1866 case AM_Private: return "private"; break; 1867 default: return ""; break; 1868 } 1869} 1870 1871bool RSReflection::Context::startClass(AccessModifier AM, 1872 bool IsStatic, 1873 const std::string &ClassName, 1874 const char *SuperClassName, 1875 std::string &ErrorMsg) { 1876 if (mVerbose) 1877 std::cout << "Generating " << ClassName << ".java ..." << std::endl; 1878 1879 // Open file for class 1880 if (!openClassFile(ClassName, ErrorMsg)) 1881 return false; 1882 1883 // License 1884 out() << mLicenseNote; 1885 1886 // Notice of generated file 1887 out() << "/*" << std::endl; 1888 out() << " * This file is auto-generated. DO NOT MODIFY!" << std::endl; 1889 out() << " * The source RenderScript file: " << mInputRSFile << std::endl; 1890 out() << " */" << std::endl; 1891 1892 // Package 1893 if (!mPackageName.empty()) 1894 out() << "package " << mPackageName << ";" << std::endl; 1895 out() << std::endl; 1896 1897 // Imports 1898 for (unsigned i = 0; i < (sizeof(Import) / sizeof(const char*)); i++) 1899 out() << "import " << Import[i] << ";" << std::endl; 1900 out() << std::endl; 1901 1902 // All reflected classes should be annotated as hidden, so that they won't 1903 // be exposed in SDK. 1904 out() << "/**" << std::endl; 1905 out() << " * @hide" << std::endl; 1906 out() << " */" << std::endl; 1907 1908 out() << AccessModifierStr(AM) << ((IsStatic) ? " static" : "") << " class " 1909 << ClassName; 1910 if (SuperClassName != NULL) 1911 out() << " extends " << SuperClassName; 1912 1913 startBlock(); 1914 1915 mClassName = ClassName; 1916 1917 return true; 1918} 1919 1920void RSReflection::Context::endClass() { 1921 endBlock(); 1922 if (!mUseStdout) 1923 mOF.close(); 1924 clear(); 1925 return; 1926} 1927 1928void RSReflection::Context::startBlock(bool ShouldIndent) { 1929 if (ShouldIndent) 1930 indent() << "{" << std::endl; 1931 else 1932 out() << " {" << std::endl; 1933 incIndentLevel(); 1934 return; 1935} 1936 1937void RSReflection::Context::endBlock() { 1938 decIndentLevel(); 1939 indent() << "}" << std::endl << std::endl; 1940 return; 1941} 1942 1943void RSReflection::Context::startTypeClass(const std::string &ClassName) { 1944 indent() << "public static class " << ClassName; 1945 startBlock(); 1946 return; 1947} 1948 1949void RSReflection::Context::endTypeClass() { 1950 endBlock(); 1951 return; 1952} 1953 1954void RSReflection::Context::startFunction(AccessModifier AM, 1955 bool IsStatic, 1956 const char *ReturnType, 1957 const std::string &FunctionName, 1958 int Argc, ...) { 1959 ArgTy Args; 1960 va_list vl; 1961 va_start(vl, Argc); 1962 1963 for (int i = 0; i < Argc; i++) { 1964 const char *ArgType = va_arg(vl, const char*); 1965 const char *ArgName = va_arg(vl, const char*); 1966 1967 Args.push_back(std::make_pair(ArgType, ArgName)); 1968 } 1969 va_end(vl); 1970 1971 startFunction(AM, IsStatic, ReturnType, FunctionName, Args); 1972 1973 return; 1974} 1975 1976void RSReflection::Context::startFunction(AccessModifier AM, 1977 bool IsStatic, 1978 const char *ReturnType, 1979 const std::string &FunctionName, 1980 const ArgTy &Args) { 1981 indent() << AccessModifierStr(AM) << ((IsStatic) ? " static " : " ") 1982 << ((ReturnType) ? ReturnType : "") << " " << FunctionName << "("; 1983 1984 bool FirstArg = true; 1985 for (ArgTy::const_iterator I = Args.begin(), E = Args.end(); 1986 I != E; 1987 I++) { 1988 if (!FirstArg) 1989 out() << ", "; 1990 else 1991 FirstArg = false; 1992 1993 out() << I->first << " " << I->second; 1994 } 1995 1996 out() << ")"; 1997 startBlock(); 1998 1999 return; 2000} 2001 2002void RSReflection::Context::endFunction() { 2003 endBlock(); 2004 return; 2005} 2006 2007} // namespace slang 2008