slang_rs_reflection.cpp revision 8d5a2f6ab321615bfb3a46f68aff0b643a71caa0
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 "os_sep.h" 32#include "slang_rs_context.h" 33#include "slang_rs_export_var.h" 34#include "slang_rs_export_func.h" 35#include "slang_rs_reflect_utils.h" 36#include "slang_utils.h" 37 38#define RS_SCRIPT_CLASS_NAME_PREFIX "ScriptC_" 39#define RS_SCRIPT_CLASS_SUPER_CLASS_NAME "ScriptC" 40 41#define RS_TYPE_CLASS_NAME_PREFIX "ScriptField_" 42#define RS_TYPE_CLASS_SUPER_CLASS_NAME "android.renderscript.Script.FieldBase" 43 44#define RS_TYPE_ITEM_CLASS_NAME "Item" 45 46#define RS_TYPE_ITEM_BUFFER_NAME "mItemArray" 47#define RS_TYPE_ITEM_BUFFER_PACKER_NAME "mIOBuffer" 48 49#define RS_EXPORT_VAR_INDEX_PREFIX "mExportVarIdx_" 50#define RS_EXPORT_VAR_PREFIX "mExportVar_" 51 52#define RS_EXPORT_FUNC_INDEX_PREFIX "mExportFuncIdx_" 53 54#define RS_EXPORT_VAR_ALLOCATION_PREFIX "mAlloction_" 55#define RS_EXPORT_VAR_DATA_STORAGE_PREFIX "mData_" 56 57namespace slang { 58 59// Some utility function using internal in RSReflection 60static bool GetClassNameFromFileName(const std::string &FileName, 61 std::string &ClassName) { 62 ClassName.clear(); 63 64 if (FileName.empty() || (FileName == "-")) 65 return true; 66 67 ClassName = 68 RSSlangReflectUtils::JavaClassNameFromRSFileName(FileName.c_str()); 69 70 return true; 71} 72 73static const char *GetPrimitiveTypeName(const RSExportPrimitiveType *EPT) { 74 static const char *PrimitiveTypeJavaNameMap[] = { 75 "", // RSExportPrimitiveType::DataTypeFloat16 76 "float", // RSExportPrimitiveType::DataTypeFloat32 77 "double", // RSExportPrimitiveType::DataTypeFloat64 78 "byte", // RSExportPrimitiveType::DataTypeSigned8 79 "short", // RSExportPrimitiveType::DataTypeSigned16 80 "int", // RSExportPrimitiveType::DataTypeSigned32 81 "long", // RSExportPrimitiveType::DataTypeSigned64 82 "short", // RSExportPrimitiveType::DataTypeUnsigned8 83 "int", // RSExportPrimitiveType::DataTypeUnsigned16 84 "long", // RSExportPrimitiveType::DataTypeUnsigned32 85 "long", // RSExportPrimitiveType::DataTypeUnsigned64 86 "boolean", // RSExportPrimitiveType::DataTypeBoolean 87 88 "int", // RSExportPrimitiveType::DataTypeUnsigned565 89 "int", // RSExportPrimitiveType::DataTypeUnsigned5551 90 "int", // RSExportPrimitiveType::DataTypeUnsigned4444 91 92 "", // (Dummy) RSExportPrimitiveType::DataTypeRSMatrix2x2 93 "", // (Dummy) RSExportPrimitiveType::DataTypeRSMatrix3x3 94 "", // (Dummy) RSExportPrimitiveType::DataTypeRSMatrix4x4 95 96 "Element", // RSExportPrimitiveType::DataTypeRSElement 97 "Type", // RSExportPrimitiveType::DataTypeRSType 98 "Allocation", // RSExportPrimitiveType::DataTypeRSAllocation 99 "Sampler", // RSExportPrimitiveType::DataTypeRSSampler 100 "Script", // RSExportPrimitiveType::DataTypeRSScript 101 "Mesh", // RSExportPrimitiveType::DataTypeRSMesh 102 "ProgramFragment", // RSExportPrimitiveType::DataTypeRSProgramFragment 103 "ProgramVertex", // RSExportPrimitiveType::DataTypeRSProgramVertex 104 "ProgramRaster", // RSExportPrimitiveType::DataTypeRSProgramRaster 105 "ProgramStore", // RSExportPrimitiveType::DataTypeRSProgramStore 106 "Font", // RSExportPrimitiveType::DataTypeRSFont 107 }; 108 unsigned TypeId = EPT->getType(); 109 110 if (TypeId < (sizeof(PrimitiveTypeJavaNameMap) / sizeof(const char*))) { 111 return PrimitiveTypeJavaNameMap[ EPT->getType() ]; 112 } 113 114 slangAssert(false && "GetPrimitiveTypeName : Unknown primitive data type"); 115 return NULL; 116} 117 118static const char *GetVectorTypeName(const RSExportVectorType *EVT) { 119 static const char *VectorTypeJavaNameMap[][3] = { 120 /* 0 */ { "Byte2", "Byte3", "Byte4" }, 121 /* 1 */ { "Short2", "Short3", "Short4" }, 122 /* 2 */ { "Int2", "Int3", "Int4" }, 123 /* 3 */ { "Long2", "Long3", "Long4" }, 124 /* 4 */ { "Float2", "Float3", "Float4" }, 125 }; 126 127 const char **BaseElement = NULL; 128 129 switch (EVT->getType()) { 130 case RSExportPrimitiveType::DataTypeSigned8: 131 case RSExportPrimitiveType::DataTypeBoolean: { 132 BaseElement = VectorTypeJavaNameMap[0]; 133 break; 134 } 135 case RSExportPrimitiveType::DataTypeSigned16: 136 case RSExportPrimitiveType::DataTypeUnsigned8: { 137 BaseElement = VectorTypeJavaNameMap[1]; 138 break; 139 } 140 case RSExportPrimitiveType::DataTypeSigned32: 141 case RSExportPrimitiveType::DataTypeUnsigned16: { 142 BaseElement = VectorTypeJavaNameMap[2]; 143 break; 144 } 145 case RSExportPrimitiveType::DataTypeSigned64: 146 case RSExportPrimitiveType::DataTypeUnsigned64: 147 case RSExportPrimitiveType::DataTypeUnsigned32: { 148 BaseElement = VectorTypeJavaNameMap[3]; 149 break; 150 } 151 case RSExportPrimitiveType::DataTypeFloat32: { 152 BaseElement = VectorTypeJavaNameMap[4]; 153 break; 154 } 155 default: { 156 slangAssert(false && "RSReflection::genElementTypeName : Unsupported " 157 "vector element data type"); 158 break; 159 } 160 } 161 162 slangAssert((EVT->getNumElement() > 1) && 163 (EVT->getNumElement() <= 4) && 164 "Number of element in vector type is invalid"); 165 166 return BaseElement[EVT->getNumElement() - 2]; 167} 168 169static const char *GetMatrixTypeName(const RSExportMatrixType *EMT) { 170 static const char *MatrixTypeJavaNameMap[] = { 171 /* 2x2 */ "Matrix2f", 172 /* 3x3 */ "Matrix3f", 173 /* 4x4 */ "Matrix4f", 174 }; 175 unsigned Dim = EMT->getDim(); 176 177 if ((Dim - 2) < (sizeof(MatrixTypeJavaNameMap) / sizeof(const char*))) 178 return MatrixTypeJavaNameMap[ EMT->getDim() - 2 ]; 179 180 slangAssert(false && "GetMatrixTypeName : Unsupported matrix dimension"); 181 return NULL; 182} 183 184static const char *GetVectorAccessor(unsigned Index) { 185 static const char *VectorAccessorMap[] = { 186 /* 0 */ "x", 187 /* 1 */ "y", 188 /* 2 */ "z", 189 /* 3 */ "w", 190 }; 191 192 slangAssert((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 slangAssert(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 slangAssert(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 slangAssert(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: slangAssert(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 slangAssert(!Val.isUninit() && "Not a valid initializer"); 549 550 C.indent() << RS_EXPORT_VAR_PREFIX << VarName << " = "; 551 slangAssert((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 slangAssert(!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 slangAssert(false && 591 "Primitive type cannot have such kind of initializer"); 592 break; 593 } 594 default: { 595 slangAssert(false && "Unknown kind of initializer"); 596 } 597 } 598 C.out() << ";" << std::endl; 599 600 return; 601} 602 603void RSReflection::genInitExportVariable(Context &C, 604 const RSExportType *ET, 605 const std::string &VarName, 606 const clang::APValue &Val) { 607 slangAssert(!Val.isUninit() && "Not a valid initializer"); 608 609 switch (ET->getClass()) { 610 case RSExportType::ExportClassPrimitive: { 611 const RSExportPrimitiveType *EPT = 612 static_cast<const RSExportPrimitiveType*>(ET); 613 if (EPT->getType() == RSExportPrimitiveType::DataTypeBoolean) { 614 genInitBoolExportVariable(C, VarName, Val); 615 } else { 616 genInitPrimitiveExportVariable(C, VarName, Val); 617 } 618 break; 619 } 620 case RSExportType::ExportClassPointer: { 621 if (!Val.isInt() || Val.getInt().getSExtValue() != 0) 622 std::cout << "Initializer which is non-NULL to pointer type variable " 623 "will be ignored" << std::endl; 624 break; 625 } 626 case RSExportType::ExportClassVector: { 627 const RSExportVectorType *EVT = 628 static_cast<const RSExportVectorType*>(ET); 629 switch (Val.getKind()) { 630 case clang::APValue::Int: 631 case clang::APValue::Float: { 632 for (unsigned i = 0; i < EVT->getNumElement(); i++) { 633 std::string Name = VarName + "." + GetVectorAccessor(i); 634 genInitPrimitiveExportVariable(C, Name, Val); 635 } 636 break; 637 } 638 case clang::APValue::Vector: { 639 C.indent() << RS_EXPORT_VAR_PREFIX << VarName << " = new " 640 << GetVectorTypeName(EVT) << "();" << std::endl; 641 642 unsigned NumElements = 643 std::min(static_cast<unsigned>(EVT->getNumElement()), 644 Val.getVectorLength()); 645 for (unsigned i = 0; i < NumElements; i++) { 646 const clang::APValue &ElementVal = Val.getVectorElt(i); 647 std::string Name = VarName + "." + GetVectorAccessor(i); 648 genInitPrimitiveExportVariable(C, Name, ElementVal); 649 } 650 break; 651 } 652 case clang::APValue::Uninitialized: 653 case clang::APValue::ComplexInt: 654 case clang::APValue::ComplexFloat: 655 case clang::APValue::LValue: { 656 slangAssert(false && "Unexpected type of value of initializer."); 657 } 658 } 659 break; 660 } 661 // TODO(zonr): Resolving initializer of a record (and matrix) type variable 662 // is complex. It cannot obtain by just simply evaluating the initializer 663 // expression. 664 case RSExportType::ExportClassMatrix: 665 case RSExportType::ExportClassConstantArray: 666 case RSExportType::ExportClassRecord: { 667#if 0 668 unsigned InitIndex = 0; 669 const RSExportRecordType *ERT = 670 static_cast<const RSExportRecordType*>(ET); 671 672 slangAssert((Val.getKind() == clang::APValue::Vector) && 673 "Unexpected type of initializer for record type variable"); 674 675 C.indent() << RS_EXPORT_VAR_PREFIX << VarName 676 << " = new "RS_TYPE_CLASS_NAME_PREFIX << ERT->getName() 677 << "."RS_TYPE_ITEM_CLASS_NAME"();" << std::endl; 678 679 for (RSExportRecordType::const_field_iterator I = ERT->fields_begin(), 680 E = ERT->fields_end(); 681 I != E; 682 I++) { 683 const RSExportRecordType::Field *F = *I; 684 std::string FieldName = VarName + "." + F->getName(); 685 686 if (InitIndex > Val.getVectorLength()) 687 break; 688 689 genInitPrimitiveExportVariable(C, 690 FieldName, 691 Val.getVectorElt(InitIndex++)); 692 } 693#endif 694 slangAssert(false && "Unsupported initializer for record/matrix/constant " 695 "array type variable currently"); 696 break; 697 } 698 default: { 699 slangAssert(false && "Unknown class of type"); 700 } 701 } 702 return; 703} 704 705void RSReflection::genExportVariable(Context &C, const RSExportVar *EV) { 706 const RSExportType *ET = EV->getType(); 707 708 C.indent() << "private final static int "RS_EXPORT_VAR_INDEX_PREFIX 709 << EV->getName() << " = " << C.getNextExportVarSlot() << ";" 710 << std::endl; 711 712 switch (ET->getClass()) { 713 case RSExportType::ExportClassPrimitive: { 714 genPrimitiveTypeExportVariable(C, EV); 715 break; 716 } 717 case RSExportType::ExportClassPointer: { 718 genPointerTypeExportVariable(C, EV); 719 break; 720 } 721 case RSExportType::ExportClassVector: { 722 genVectorTypeExportVariable(C, EV); 723 break; 724 } 725 case RSExportType::ExportClassMatrix: { 726 genMatrixTypeExportVariable(C, EV); 727 break; 728 } 729 case RSExportType::ExportClassConstantArray: { 730 genConstantArrayTypeExportVariable(C, EV); 731 break; 732 } 733 case RSExportType::ExportClassRecord: { 734 genRecordTypeExportVariable(C, EV); 735 break; 736 } 737 default: { 738 slangAssert(false && "Unknown class of type"); 739 } 740 } 741 742 return; 743} 744 745void RSReflection::genExportFunction(Context &C, const RSExportFunc *EF) { 746 C.indent() << "private final static int "RS_EXPORT_FUNC_INDEX_PREFIX 747 << EF->getName() << " = " << C.getNextExportFuncSlot() << ";" 748 << std::endl; 749 750 // invoke_*() 751 Context::ArgTy Args; 752 753 if (EF->hasParam()) { 754 for (RSExportFunc::const_param_iterator I = EF->params_begin(), 755 E = EF->params_end(); 756 I != E; 757 I++) { 758 Args.push_back(std::make_pair(GetTypeName((*I)->getType()), 759 (*I)->getName())); 760 } 761 } 762 763 C.startFunction(Context::AM_Public, 764 false, 765 "void", 766 "invoke_" + EF->getName(), 767 Args); 768 769 if (!EF->hasParam()) { 770 C.indent() << "invoke("RS_EXPORT_FUNC_INDEX_PREFIX << EF->getName() << ");" 771 << std::endl; 772 } else { 773 const RSExportRecordType *ERT = EF->getParamPacketType(); 774 std::string FieldPackerName = EF->getName() + "_fp"; 775 776 if (genCreateFieldPacker(C, ERT, FieldPackerName.c_str())) 777 genPackVarOfType(C, ERT, NULL, FieldPackerName.c_str()); 778 779 C.indent() << "invoke("RS_EXPORT_FUNC_INDEX_PREFIX << EF->getName() << ", " 780 << FieldPackerName << ");" << std::endl; 781 } 782 783 C.endFunction(); 784 return; 785} 786 787void RSReflection::genPrimitiveTypeExportVariable( 788 Context &C, 789 const RSExportVar *EV) { 790 slangAssert((EV->getType()->getClass() == RSExportType::ExportClassPrimitive) 791 && "Variable should be type of primitive here"); 792 793 const RSExportPrimitiveType *EPT = 794 static_cast<const RSExportPrimitiveType*>(EV->getType()); 795 const char *TypeName = GetPrimitiveTypeName(EPT); 796 797 C.indent() << "private " << TypeName << " "RS_EXPORT_VAR_PREFIX 798 << EV->getName() << ";" << std::endl; 799 800 // set_*() 801 if (!EV->isConst()) { 802 C.startFunction(Context::AM_Public, 803 false, 804 "void", 805 "set_" + EV->getName(), 806 1, 807 TypeName, "v"); 808 C.indent() << RS_EXPORT_VAR_PREFIX << EV->getName() << " = v;" << std::endl; 809 810 C.indent() << "setVar("RS_EXPORT_VAR_INDEX_PREFIX << EV->getName() 811 << ", v);" << std::endl; 812 813 C.endFunction(); 814 } 815 816 genGetExportVariable(C, TypeName, EV->getName()); 817 818 return; 819} 820 821void RSReflection::genPointerTypeExportVariable(Context &C, 822 const RSExportVar *EV) { 823 const RSExportType *ET = EV->getType(); 824 const RSExportType *PointeeType; 825 std::string TypeName; 826 827 slangAssert((ET->getClass() == RSExportType::ExportClassPointer) && 828 "Variable should be type of pointer here"); 829 830 PointeeType = static_cast<const RSExportPointerType*>(ET)->getPointeeType(); 831 TypeName = GetTypeName(ET); 832 833 C.indent() << "private " << TypeName << " "RS_EXPORT_VAR_PREFIX 834 << EV->getName() << ";" << std::endl; 835 836 // bind_*() 837 C.startFunction(Context::AM_Public, 838 false, 839 "void", 840 "bind_" + EV->getName(), 841 1, 842 TypeName.c_str(), "v"); 843 844 C.indent() << RS_EXPORT_VAR_PREFIX << EV->getName() << " = v;" << std::endl; 845 C.indent() << "if (v == null) bindAllocation(null, "RS_EXPORT_VAR_INDEX_PREFIX 846 << EV->getName() << ");" << std::endl; 847 848 if (PointeeType->getClass() == RSExportType::ExportClassRecord) 849 C.indent() << "else bindAllocation(v.getAllocation(), " 850 RS_EXPORT_VAR_INDEX_PREFIX << EV->getName() << ");" 851 << std::endl; 852 else 853 C.indent() << "else bindAllocation(v, "RS_EXPORT_VAR_INDEX_PREFIX 854 << EV->getName() << ");" << std::endl; 855 856 C.endFunction(); 857 858 genGetExportVariable(C, TypeName, EV->getName()); 859 860 return; 861} 862 863void RSReflection::genVectorTypeExportVariable(Context &C, 864 const RSExportVar *EV) { 865 slangAssert((EV->getType()->getClass() == RSExportType::ExportClassVector) && 866 "Variable should be type of vector here"); 867 868 const RSExportVectorType *EVT = 869 static_cast<const RSExportVectorType*>(EV->getType()); 870 const char *TypeName = GetVectorTypeName(EVT); 871 const char *FieldPackerName = "fp"; 872 873 C.indent() << "private " << TypeName << " "RS_EXPORT_VAR_PREFIX 874 << EV->getName() << ";" << std::endl; 875 876 // set_*() 877 if (!EV->isConst()) { 878 C.startFunction(Context::AM_Public, 879 false, 880 "void", 881 "set_" + EV->getName(), 882 1, 883 TypeName, "v"); 884 C.indent() << RS_EXPORT_VAR_PREFIX << EV->getName() << " = v;" << std::endl; 885 886 if (genCreateFieldPacker(C, EVT, FieldPackerName)) 887 genPackVarOfType(C, EVT, "v", FieldPackerName); 888 C.indent() << "setVar("RS_EXPORT_VAR_INDEX_PREFIX << EV->getName() << ", " 889 << FieldPackerName << ");" << std::endl; 890 891 C.endFunction(); 892 } 893 894 genGetExportVariable(C, TypeName, EV->getName()); 895 return; 896} 897 898void RSReflection::genMatrixTypeExportVariable(Context &C, 899 const RSExportVar *EV) { 900 slangAssert((EV->getType()->getClass() == RSExportType::ExportClassMatrix) && 901 "Variable should be type of matrix here"); 902 903 const RSExportMatrixType *EMT = 904 static_cast<const RSExportMatrixType*>(EV->getType()); 905 const char *TypeName = GetMatrixTypeName(EMT); 906 const char *FieldPackerName = "fp"; 907 908 C.indent() << "private " << TypeName << " "RS_EXPORT_VAR_PREFIX 909 << EV->getName() << ";" << std::endl; 910 911 // set_*() 912 if (!EV->isConst()) { 913 C.startFunction(Context::AM_Public, 914 false, 915 "void", 916 "set_" + EV->getName(), 917 1, 918 TypeName, "v"); 919 C.indent() << RS_EXPORT_VAR_PREFIX << EV->getName() << " = v;" << std::endl; 920 921 if (genCreateFieldPacker(C, EMT, FieldPackerName)) 922 genPackVarOfType(C, EMT, "v", FieldPackerName); 923 C.indent() << "setVar("RS_EXPORT_VAR_INDEX_PREFIX << EV->getName() << ", " 924 << FieldPackerName << ");" << std::endl; 925 926 C.endFunction(); 927 } 928 929 genGetExportVariable(C, TypeName, EV->getName()); 930 return; 931} 932 933void RSReflection::genConstantArrayTypeExportVariable( 934 Context &C, 935 const RSExportVar *EV) { 936 slangAssert((EV->getType()->getClass() == 937 RSExportType::ExportClassConstantArray) && 938 "Variable should be type of constant array here"); 939 940 const RSExportConstantArrayType *ECAT = 941 static_cast<const RSExportConstantArrayType*>(EV->getType()); 942 std::string TypeName = GetTypeName(ECAT); 943 const char *FieldPackerName = "fp"; 944 945 C.indent() << "private " << TypeName << " "RS_EXPORT_VAR_PREFIX 946 << EV->getName() << ";" << std::endl; 947 948 // set_*() 949 if (!EV->isConst()) { 950 C.startFunction(Context::AM_Public, 951 false, 952 "void", 953 "set_" + EV->getName(), 954 1, 955 TypeName.c_str(), "v"); 956 C.indent() << RS_EXPORT_VAR_PREFIX << EV->getName() << " = v;" << std::endl; 957 958 if (genCreateFieldPacker(C, ECAT, FieldPackerName)) 959 genPackVarOfType(C, ECAT, "v", FieldPackerName); 960 C.indent() << "setVar("RS_EXPORT_VAR_INDEX_PREFIX << EV->getName() << ", " 961 << FieldPackerName << ");" << std::endl; 962 963 C.endFunction(); 964 } 965 966 genGetExportVariable(C, TypeName, EV->getName()); 967 return; 968} 969 970void RSReflection::genRecordTypeExportVariable(Context &C, 971 const RSExportVar *EV) { 972 slangAssert((EV->getType()->getClass() == RSExportType::ExportClassRecord) && 973 "Variable should be type of struct here"); 974 975 const RSExportRecordType *ERT = 976 static_cast<const RSExportRecordType*>(EV->getType()); 977 std::string TypeName = 978 RS_TYPE_CLASS_NAME_PREFIX + ERT->getName() + "."RS_TYPE_ITEM_CLASS_NAME; 979 const char *FieldPackerName = "fp"; 980 981 C.indent() << "private " << TypeName << " "RS_EXPORT_VAR_PREFIX 982 << EV->getName() << ";" << std::endl; 983 984 // set_*() 985 if (!EV->isConst()) { 986 C.startFunction(Context::AM_Public, 987 false, 988 "void", 989 "set_" + EV->getName(), 990 1, 991 TypeName.c_str(), "v"); 992 C.indent() << RS_EXPORT_VAR_PREFIX << EV->getName() << " = v;" << std::endl; 993 994 if (genCreateFieldPacker(C, ERT, FieldPackerName)) 995 genPackVarOfType(C, ERT, "v", FieldPackerName); 996 C.indent() << "setVar("RS_EXPORT_VAR_INDEX_PREFIX << EV->getName() 997 << ", " << FieldPackerName << ");" << std::endl; 998 999 C.endFunction(); 1000 } 1001 1002 genGetExportVariable(C, TypeName.c_str(), EV->getName()); 1003 return; 1004} 1005 1006void RSReflection::genGetExportVariable(Context &C, 1007 const std::string &TypeName, 1008 const std::string &VarName) { 1009 C.startFunction(Context::AM_Public, 1010 false, 1011 TypeName.c_str(), 1012 "get_" + VarName, 1013 0); 1014 1015 C.indent() << "return "RS_EXPORT_VAR_PREFIX << VarName << ";" << std::endl; 1016 1017 C.endFunction(); 1018 return; 1019} 1020 1021/******************* Methods to generate script class /end *******************/ 1022 1023bool RSReflection::genCreateFieldPacker(Context &C, 1024 const RSExportType *ET, 1025 const char *FieldPackerName) { 1026 size_t AllocSize = RSExportType::GetTypeAllocSize(ET); 1027 if (AllocSize > 0) 1028 C.indent() << "FieldPacker " << FieldPackerName << " = new FieldPacker(" 1029 << AllocSize << ");" << std::endl; 1030 else 1031 return false; 1032 return true; 1033} 1034 1035void RSReflection::genPackVarOfType(Context &C, 1036 const RSExportType *ET, 1037 const char *VarName, 1038 const char *FieldPackerName) { 1039 switch (ET->getClass()) { 1040 case RSExportType::ExportClassPrimitive: 1041 case RSExportType::ExportClassVector: { 1042 C.indent() << FieldPackerName << "." 1043 << GetPackerAPIName( 1044 static_cast<const RSExportPrimitiveType*>(ET)) 1045 << "(" << VarName << ");" << std::endl; 1046 break; 1047 } 1048 case RSExportType::ExportClassPointer: { 1049 // Must reflect as type Allocation in Java 1050 const RSExportType *PointeeType = 1051 static_cast<const RSExportPointerType*>(ET)->getPointeeType(); 1052 1053 if (PointeeType->getClass() != RSExportType::ExportClassRecord) 1054 C.indent() << FieldPackerName << ".addI32(" << VarName 1055 << ".getPtr());" << std::endl; 1056 else 1057 C.indent() << FieldPackerName << ".addI32(" << VarName 1058 << ".getAllocation().getPtr());" << std::endl; 1059 break; 1060 } 1061 case RSExportType::ExportClassMatrix: { 1062 C.indent() << FieldPackerName << ".addMatrix(" << VarName << ");" 1063 << std::endl; 1064 break; 1065 } 1066 case RSExportType::ExportClassConstantArray: { 1067 const RSExportConstantArrayType *ECAT = 1068 static_cast<const RSExportConstantArrayType *>(ET); 1069 1070 // TODO(zonr): more elegant way. Currently, we obtain the unique index 1071 // variable (this method involves recursive call which means 1072 // we may have more than one level loop, therefore we can't 1073 // always use the same index variable name here) name given 1074 // in the for-loop from counting the '.' in @VarName. 1075 unsigned Level = 0; 1076 size_t LastDotPos = 0; 1077 std::string ElementVarName(VarName); 1078 1079 while (LastDotPos != std::string::npos) { 1080 LastDotPos = ElementVarName.find_first_of('.', LastDotPos + 1); 1081 Level++; 1082 } 1083 std::string IndexVarName("ct"); 1084 IndexVarName.append(llvm::utostr_32(Level)); 1085 1086 C.indent() << "for (int " << IndexVarName << " = 0; " << 1087 IndexVarName << " < " << ECAT->getSize() << "; " << 1088 IndexVarName << "++)"; 1089 C.startBlock(); 1090 1091 ElementVarName.append("[" + IndexVarName + "]"); 1092 genPackVarOfType(C, ECAT->getElementType(), ElementVarName.c_str(), 1093 FieldPackerName); 1094 1095 C.endBlock(); 1096 break; 1097 } 1098 case RSExportType::ExportClassRecord: { 1099 const RSExportRecordType *ERT = 1100 static_cast<const RSExportRecordType*>(ET); 1101 // Relative pos from now on in field packer 1102 unsigned Pos = 0; 1103 1104 for (RSExportRecordType::const_field_iterator I = ERT->fields_begin(), 1105 E = ERT->fields_end(); 1106 I != E; 1107 I++) { 1108 const RSExportRecordType::Field *F = *I; 1109 std::string FieldName; 1110 size_t FieldOffset = F->getOffsetInParent(); 1111 size_t FieldStoreSize = RSExportType::GetTypeStoreSize(F->getType()); 1112 size_t FieldAllocSize = RSExportType::GetTypeAllocSize(F->getType()); 1113 1114 if (VarName != NULL) 1115 FieldName = VarName + ("." + F->getName()); 1116 else 1117 FieldName = F->getName(); 1118 1119 if (FieldOffset > Pos) 1120 C.indent() << FieldPackerName << ".skip(" 1121 << (FieldOffset - Pos) << ");" << std::endl; 1122 1123 genPackVarOfType(C, F->getType(), FieldName.c_str(), FieldPackerName); 1124 1125 // There is padding in the field type 1126 if (FieldAllocSize > FieldStoreSize) 1127 C.indent() << FieldPackerName << ".skip(" 1128 << (FieldAllocSize - FieldStoreSize) 1129 << ");" << std::endl; 1130 1131 Pos = FieldOffset + FieldAllocSize; 1132 } 1133 1134 // There maybe some padding after the struct 1135 size_t Padding = RSExportType::GetTypeAllocSize(ERT) - Pos; 1136 if (Padding > 0) 1137 C.indent() << FieldPackerName << ".skip(" << Padding << ");" 1138 << std::endl; 1139 break; 1140 } 1141 default: { 1142 slangAssert(false && "Unknown class of type"); 1143 } 1144 } 1145 1146 return; 1147} 1148 1149void RSReflection::genAllocateVarOfType(Context &C, 1150 const RSExportType *T, 1151 const std::string &VarName) { 1152 switch (T->getClass()) { 1153 case RSExportType::ExportClassPrimitive: { 1154 // Primitive type like int in Java has its own storage once it's declared. 1155 // 1156 // FIXME: Should we allocate storage for RS object? 1157 // if (static_cast<const RSExportPrimitiveType *>(T)->isRSObjectType()) 1158 // C.indent() << VarName << " = new " << GetTypeName(T) << "();" 1159 // << std::endl; 1160 break; 1161 } 1162 case RSExportType::ExportClassPointer: { 1163 // Pointer type is an instance of Allocation or a TypeClass whose value is 1164 // expected to be assigned by programmer later in Java program. Therefore 1165 // we don't reflect things like [VarName] = new Allocation(); 1166 C.indent() << VarName << " = null;" << std::endl; 1167 break; 1168 } 1169 case RSExportType::ExportClassConstantArray: { 1170 const RSExportConstantArrayType *ECAT = 1171 static_cast<const RSExportConstantArrayType *>(T); 1172 const RSExportType *ElementType = ECAT->getElementType(); 1173 1174 C.indent() << VarName << " = new " << GetTypeName(ElementType) 1175 << "[" << ECAT->getSize() << "];" << std::endl; 1176 1177 // Primitive type element doesn't need allocation code. 1178 if (ElementType->getClass() != RSExportType::ExportClassPrimitive) { 1179 C.indent() << "for (int $ct = 0; $ct < " << ECAT->getSize() << "; " 1180 "$ct++)"; 1181 C.startBlock(); 1182 1183 std::string ElementVarName(VarName); 1184 ElementVarName.append("[$ct]"); 1185 genAllocateVarOfType(C, ElementType, ElementVarName); 1186 1187 C.endBlock(); 1188 } 1189 break; 1190 } 1191 case RSExportType::ExportClassVector: 1192 case RSExportType::ExportClassMatrix: 1193 case RSExportType::ExportClassRecord: { 1194 C.indent() << VarName << " = new " << GetTypeName(T) << "();" 1195 << std::endl; 1196 break; 1197 } 1198 } 1199 return; 1200} 1201 1202void RSReflection::genNewItemBufferIfNull(Context &C, 1203 const char *Index) { 1204 C.indent() << "if (" RS_TYPE_ITEM_BUFFER_NAME " == null) " 1205 RS_TYPE_ITEM_BUFFER_NAME " = " 1206 "new " RS_TYPE_ITEM_CLASS_NAME 1207 "[getType().getX() /* count */];" 1208 << std::endl; 1209 if (Index != NULL) 1210 C.indent() << "if ("RS_TYPE_ITEM_BUFFER_NAME"[" << Index << "] == null) " 1211 RS_TYPE_ITEM_BUFFER_NAME"[" << Index << "] = " 1212 "new "RS_TYPE_ITEM_CLASS_NAME"();" << std::endl; 1213 return; 1214} 1215 1216void RSReflection::genNewItemBufferPackerIfNull(Context &C) { 1217 C.indent() << "if (" RS_TYPE_ITEM_BUFFER_PACKER_NAME " == null) " 1218 RS_TYPE_ITEM_BUFFER_PACKER_NAME " = " 1219 "new FieldPacker(" RS_TYPE_ITEM_CLASS_NAME 1220 ".sizeof * getType().getX()/* count */" 1221 ");" << std::endl; 1222 return; 1223} 1224 1225/********************** Methods to generate type class **********************/ 1226bool RSReflection::genTypeClass(Context &C, 1227 const RSExportRecordType *ERT, 1228 std::string &ErrorMsg) { 1229 std::string ClassName = RS_TYPE_CLASS_NAME_PREFIX + ERT->getName(); 1230 1231 if (!C.startClass(Context::AM_Public, 1232 false, 1233 ClassName, 1234 RS_TYPE_CLASS_SUPER_CLASS_NAME, 1235 ErrorMsg)) 1236 return false; 1237 1238 mGeneratedFileNames->push_back(ClassName); 1239 1240 genTypeItemClass(C, ERT); 1241 1242 // Declare item buffer and item buffer packer 1243 C.indent() << "private "RS_TYPE_ITEM_CLASS_NAME" "RS_TYPE_ITEM_BUFFER_NAME"[]" 1244 ";" << std::endl; 1245 C.indent() << "private FieldPacker "RS_TYPE_ITEM_BUFFER_PACKER_NAME";" 1246 << std::endl; 1247 1248 genTypeClassConstructor(C, ERT); 1249 genTypeClassCopyToArray(C, ERT); 1250 genTypeClassItemSetter(C, ERT); 1251 genTypeClassItemGetter(C, ERT); 1252 genTypeClassComponentSetter(C, ERT); 1253 genTypeClassComponentGetter(C, ERT); 1254 genTypeClassCopyAll(C, ERT); 1255 genTypeClassResize(C); 1256 1257 C.endClass(); 1258 1259 C.resetFieldIndex(); 1260 C.clearFieldIndexMap(); 1261 1262 return true; 1263} 1264 1265void RSReflection::genTypeItemClass(Context &C, 1266 const RSExportRecordType *ERT) { 1267 C.indent() << "static public class "RS_TYPE_ITEM_CLASS_NAME; 1268 C.startBlock(); 1269 1270 C.indent() << "public static final int sizeof = " 1271 << RSExportType::GetTypeAllocSize(ERT) << ";" << std::endl; 1272 1273 // Member elements 1274 C.out() << std::endl; 1275 for (RSExportRecordType::const_field_iterator FI = ERT->fields_begin(), 1276 FE = ERT->fields_end(); 1277 FI != FE; 1278 FI++) { 1279 C.indent() << GetTypeName((*FI)->getType()) << " " << (*FI)->getName() 1280 << ";" << std::endl; 1281 } 1282 1283 // Constructor 1284 C.out() << std::endl; 1285 C.indent() << RS_TYPE_ITEM_CLASS_NAME"()"; 1286 C.startBlock(); 1287 1288 for (RSExportRecordType::const_field_iterator FI = ERT->fields_begin(), 1289 FE = ERT->fields_end(); 1290 FI != FE; 1291 FI++) { 1292 const RSExportRecordType::Field *F = *FI; 1293 genAllocateVarOfType(C, F->getType(), F->getName()); 1294 } 1295 1296 // end Constructor 1297 C.endBlock(); 1298 1299 // end Item class 1300 C.endBlock(); 1301 1302 return; 1303} 1304 1305void RSReflection::genTypeClassConstructor(Context &C, 1306 const RSExportRecordType *ERT) { 1307 const char *RenderScriptVar = "rs"; 1308 1309 C.startFunction(Context::AM_Public, 1310 true, 1311 "Element", 1312 "createElement", 1313 1, 1314 "RenderScript", RenderScriptVar); 1315 genBuildElement(C, "eb", ERT, RenderScriptVar, /* IsInline = */false); 1316 C.endFunction(); 1317 1318 C.startFunction(Context::AM_Public, 1319 false, 1320 NULL, 1321 C.getClassName(), 1322 2, 1323 "RenderScript", RenderScriptVar, 1324 "int", "count"); 1325 1326 C.indent() << RS_TYPE_ITEM_BUFFER_NAME" = null;" << std::endl; 1327 C.indent() << RS_TYPE_ITEM_BUFFER_PACKER_NAME" = null;" << std::endl; 1328 C.indent() << "mElement = createElement(" << RenderScriptVar << ");" 1329 << std::endl; 1330 // Call init() in super class 1331 C.indent() << "init(" << RenderScriptVar << ", count);" << std::endl; 1332 C.endFunction(); 1333 1334 C.startFunction(Context::AM_Public, 1335 false, 1336 NULL, 1337 C.getClassName(), 1338 3, 1339 "RenderScript", RenderScriptVar, 1340 "int", "count", 1341 "int", "usages"); 1342 1343 C.indent() << RS_TYPE_ITEM_BUFFER_NAME" = null;" << std::endl; 1344 C.indent() << RS_TYPE_ITEM_BUFFER_PACKER_NAME" = null;" << std::endl; 1345 C.indent() << "mElement = createElement(" << RenderScriptVar << ");" 1346 << std::endl; 1347 // Call init() in super class 1348 C.indent() << "init(" << RenderScriptVar << ", count, usages);" << std::endl; 1349 C.endFunction(); 1350 1351 return; 1352} 1353 1354void RSReflection::genTypeClassCopyToArray(Context &C, 1355 const RSExportRecordType *ERT) { 1356 C.startFunction(Context::AM_Private, 1357 false, 1358 "void", 1359 "copyToArray", 1360 2, 1361 RS_TYPE_ITEM_CLASS_NAME, "i", 1362 "int", "index"); 1363 1364 genNewItemBufferPackerIfNull(C); 1365 C.indent() << RS_TYPE_ITEM_BUFFER_PACKER_NAME 1366 ".reset(index * "RS_TYPE_ITEM_CLASS_NAME".sizeof);" 1367 << std::endl; 1368 1369 genPackVarOfType(C, ERT, "i", RS_TYPE_ITEM_BUFFER_PACKER_NAME); 1370 1371 C.endFunction(); 1372 return; 1373} 1374 1375void RSReflection::genTypeClassItemSetter(Context &C, 1376 const RSExportRecordType *ERT) { 1377 C.startFunction(Context::AM_Public, 1378 false, 1379 "void", 1380 "set", 1381 3, 1382 RS_TYPE_ITEM_CLASS_NAME, "i", 1383 "int", "index", 1384 "boolean", "copyNow"); 1385 genNewItemBufferIfNull(C, NULL); 1386 C.indent() << RS_TYPE_ITEM_BUFFER_NAME"[index] = i;" << std::endl; 1387 1388 C.indent() << "if (copyNow) "; 1389 C.startBlock(); 1390 1391 C.indent() << "copyToArray(i, index);" << std::endl; 1392 C.indent() << "mAllocation.setFromFieldPacker(index, " 1393 RS_TYPE_ITEM_BUFFER_PACKER_NAME");" << std::endl; 1394 1395 // End of if (copyNow) 1396 C.endBlock(); 1397 1398 C.endFunction(); 1399 return; 1400} 1401 1402void RSReflection::genTypeClassItemGetter(Context &C, 1403 const RSExportRecordType *ERT) { 1404 C.startFunction(Context::AM_Public, 1405 false, 1406 RS_TYPE_ITEM_CLASS_NAME, 1407 "get", 1408 1, 1409 "int", "index"); 1410 C.indent() << "if ("RS_TYPE_ITEM_BUFFER_NAME" == null) return null;" 1411 << std::endl; 1412 C.indent() << "return "RS_TYPE_ITEM_BUFFER_NAME"[index];" << std::endl; 1413 C.endFunction(); 1414 return; 1415} 1416 1417void RSReflection::genTypeClassComponentSetter(Context &C, 1418 const RSExportRecordType *ERT) { 1419 for (RSExportRecordType::const_field_iterator FI = ERT->fields_begin(), 1420 FE = ERT->fields_end(); 1421 FI != FE; 1422 FI++) { 1423 const RSExportRecordType::Field *F = *FI; 1424 size_t FieldOffset = F->getOffsetInParent(); 1425 size_t FieldStoreSize = RSExportType::GetTypeStoreSize(F->getType()); 1426 unsigned FieldIndex = C.getFieldIndex(F); 1427 1428 C.startFunction(Context::AM_Public, 1429 false, 1430 "void", 1431 "set_" + F->getName(), 3, 1432 "int", "index", 1433 GetTypeName(F->getType()).c_str(), "v", 1434 "boolean", "copyNow"); 1435 genNewItemBufferPackerIfNull(C); 1436 genNewItemBufferIfNull(C, "index"); 1437 C.indent() << RS_TYPE_ITEM_BUFFER_NAME"[index]." << F->getName() 1438 << " = v;" << std::endl; 1439 1440 C.indent() << "if (copyNow) "; 1441 C.startBlock(); 1442 1443 if (FieldOffset > 0) 1444 C.indent() << RS_TYPE_ITEM_BUFFER_PACKER_NAME 1445 ".reset(index * "RS_TYPE_ITEM_CLASS_NAME".sizeof + " 1446 << FieldOffset << ");" << std::endl; 1447 else 1448 C.indent() << RS_TYPE_ITEM_BUFFER_PACKER_NAME 1449 ".reset(index * "RS_TYPE_ITEM_CLASS_NAME".sizeof);" 1450 << std::endl; 1451 genPackVarOfType(C, F->getType(), "v", RS_TYPE_ITEM_BUFFER_PACKER_NAME); 1452 1453 C.indent() << "FieldPacker fp = new FieldPacker(" << FieldStoreSize << ");" 1454 << std::endl; 1455 genPackVarOfType(C, F->getType(), "v", "fp"); 1456 C.indent() << "mAllocation.setFromFieldPacker(index, " << FieldIndex 1457 << ", fp);" 1458 << std::endl; 1459 1460 // End of if (copyNow) 1461 C.endBlock(); 1462 1463 C.endFunction(); 1464 } 1465 return; 1466} 1467 1468void RSReflection::genTypeClassComponentGetter(Context &C, 1469 const RSExportRecordType *ERT) { 1470 for (RSExportRecordType::const_field_iterator FI = ERT->fields_begin(), 1471 FE = ERT->fields_end(); 1472 FI != FE; 1473 FI++) { 1474 const RSExportRecordType::Field *F = *FI; 1475 C.startFunction(Context::AM_Public, 1476 false, 1477 GetTypeName(F->getType()).c_str(), 1478 "get_" + F->getName(), 1479 1, 1480 "int", "index"); 1481 C.indent() << "if ("RS_TYPE_ITEM_BUFFER_NAME" == null) return " 1482 << GetTypeNullValue(F->getType()) << ";" << std::endl; 1483 C.indent() << "return "RS_TYPE_ITEM_BUFFER_NAME"[index]." << F->getName() 1484 << ";" << std::endl; 1485 C.endFunction(); 1486 } 1487 return; 1488} 1489 1490void RSReflection::genTypeClassCopyAll(Context &C, 1491 const RSExportRecordType *ERT) { 1492 C.startFunction(Context::AM_Public, false, "void", "copyAll", 0); 1493 1494 C.indent() << "for (int ct = 0; ct < "RS_TYPE_ITEM_BUFFER_NAME".length; ct++)" 1495 " copyToArray("RS_TYPE_ITEM_BUFFER_NAME"[ct], ct);" 1496 << std::endl; 1497 C.indent() << "mAllocation.setFromFieldPacker(0, " 1498 RS_TYPE_ITEM_BUFFER_PACKER_NAME");" 1499 << std::endl; 1500 1501 C.endFunction(); 1502 return; 1503} 1504 1505void RSReflection::genTypeClassResize(Context &C) { 1506 C.startFunction(Context::AM_Public, 1507 false, 1508 "void", 1509 "resize", 1510 1, 1511 "int", "newSize"); 1512 1513 C.indent() << "if (mItemArray != null) "; 1514 C.startBlock(); 1515 C.indent() << "int oldSize = mItemArray.length;" << std::endl; 1516 C.indent() << "int copySize = Math.min(oldSize, newSize);" << std::endl; 1517 C.indent() << "if (newSize == oldSize) return;" << std::endl; 1518 C.indent() << "Item ni[] = new Item[newSize];" << std::endl; 1519 C.indent() << "System.arraycopy(mItemArray, 0, ni, 0, copySize);" 1520 << std::endl; 1521 C.indent() << "mItemArray = ni;" << std::endl; 1522 C.endBlock(); 1523 C.indent() << "mAllocation.resize(newSize);" << std::endl; 1524 1525 C.indent() << "if (" RS_TYPE_ITEM_BUFFER_PACKER_NAME " != null) " 1526 RS_TYPE_ITEM_BUFFER_PACKER_NAME " = " 1527 "new FieldPacker(" RS_TYPE_ITEM_CLASS_NAME 1528 ".sizeof * getType().getX()/* count */" 1529 ");" << 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 slangAssert((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 slangAssert(false && "??"); 1622 } else if (ET->getClass() == RSExportType::ExportClassMatrix) { 1623 // Matrix type variable should be resolved 1624 // in GetBuiltinElementConstruct() 1625 slangAssert(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 slangAssert(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 mGeneratedFileNames->push_back(ScriptClassName); 1792 1793 // class ScriptField_<TypeName> 1794 for (RSContext::const_export_type_iterator TI = 1795 mRSContext->export_types_begin(), 1796 TE = mRSContext->export_types_end(); 1797 TI != TE; 1798 TI++) { 1799 const RSExportType *ET = TI->getValue(); 1800 1801 if (ET->getClass() == RSExportType::ExportClassRecord) { 1802 const RSExportRecordType *ERT = 1803 static_cast<const RSExportRecordType*>(ET); 1804 1805 if (!ERT->isArtificial() && !genTypeClass(*C, ERT, ErrorMsg)) { 1806 std::cerr << "Failed to generate type class for struct '" 1807 << ERT->getName() << "' (" << ErrorMsg << ")" << std::endl; 1808 return false; 1809 } 1810 } 1811 } 1812 } 1813 1814 return true; 1815} 1816 1817/************************** RSReflection::Context **************************/ 1818const char *const RSReflection::Context::ApacheLicenseNote = 1819 "/*\n" 1820 " * Copyright (C) 2011 The Android Open Source Project\n" 1821 " *\n" 1822 " * Licensed under the Apache License, Version 2.0 (the \"License\");\n" 1823 " * you may not use this file except in compliance with the License.\n" 1824 " * You may obtain a copy of the License at\n" 1825 " *\n" 1826 " * http://www.apache.org/licenses/LICENSE-2.0\n" 1827 " *\n" 1828 " * Unless required by applicable law or agreed to in writing, software\n" 1829 " * distributed under the License is distributed on an \"AS IS\" BASIS,\n" 1830 " * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or " 1831 "implied.\n" 1832 " * See the License for the specific language governing permissions and\n" 1833 " * limitations under the License.\n" 1834 " */\n" 1835 "\n"; 1836 1837const char *const RSReflection::Context::Import[] = { 1838 // RenderScript java class 1839 "android.renderscript.*", 1840 // Import R 1841 "android.content.res.Resources", 1842 // Import for debugging 1843 // "android.util.Log", 1844}; 1845 1846bool RSReflection::Context::openClassFile(const std::string &ClassName, 1847 std::string &ErrorMsg) { 1848 if (!mUseStdout) { 1849 mOF.clear(); 1850 std::string Path = 1851 RSSlangReflectUtils::ComputePackagedPath(mOutputPathBase.c_str(), 1852 mPackageName.c_str()); 1853 1854 if (!SlangUtils::CreateDirectoryWithParents(Path, &ErrorMsg)) 1855 return false; 1856 1857 std::string ClassFile = Path + OS_PATH_SEPARATOR_STR + ClassName + ".java"; 1858 1859 mOF.open(ClassFile.c_str()); 1860 if (!mOF.good()) { 1861 ErrorMsg = "failed to open file '" + ClassFile + "' for write"; 1862 return false; 1863 } 1864 } 1865 return true; 1866} 1867 1868const char *RSReflection::Context::AccessModifierStr(AccessModifier AM) { 1869 switch (AM) { 1870 case AM_Public: return "public"; break; 1871 case AM_Protected: return "protected"; break; 1872 case AM_Private: return "private"; break; 1873 default: return ""; break; 1874 } 1875} 1876 1877bool RSReflection::Context::startClass(AccessModifier AM, 1878 bool IsStatic, 1879 const std::string &ClassName, 1880 const char *SuperClassName, 1881 std::string &ErrorMsg) { 1882 if (mVerbose) 1883 std::cout << "Generating " << ClassName << ".java ..." << std::endl; 1884 1885 // Open file for class 1886 if (!openClassFile(ClassName, ErrorMsg)) 1887 return false; 1888 1889 // License 1890 out() << mLicenseNote; 1891 1892 // Notice of generated file 1893 out() << "/*" << std::endl; 1894 out() << " * This file is auto-generated. DO NOT MODIFY!" << std::endl; 1895 out() << " * The source RenderScript file: " << mInputRSFile << std::endl; 1896 out() << " */" << std::endl; 1897 1898 // Package 1899 if (!mPackageName.empty()) 1900 out() << "package " << mPackageName << ";" << std::endl; 1901 out() << std::endl; 1902 1903 // Imports 1904 for (unsigned i = 0; i < (sizeof(Import) / sizeof(const char*)); i++) 1905 out() << "import " << Import[i] << ";" << std::endl; 1906 out() << std::endl; 1907 1908 // All reflected classes should be annotated as hidden, so that they won't 1909 // be exposed in SDK. 1910 out() << "/**" << std::endl; 1911 out() << " * @hide" << std::endl; 1912 out() << " */" << std::endl; 1913 1914 out() << AccessModifierStr(AM) << ((IsStatic) ? " static" : "") << " class " 1915 << ClassName; 1916 if (SuperClassName != NULL) 1917 out() << " extends " << SuperClassName; 1918 1919 startBlock(); 1920 1921 mClassName = ClassName; 1922 1923 return true; 1924} 1925 1926void RSReflection::Context::endClass() { 1927 endBlock(); 1928 if (!mUseStdout) 1929 mOF.close(); 1930 clear(); 1931 return; 1932} 1933 1934void RSReflection::Context::startBlock(bool ShouldIndent) { 1935 if (ShouldIndent) 1936 indent() << "{" << std::endl; 1937 else 1938 out() << " {" << std::endl; 1939 incIndentLevel(); 1940 return; 1941} 1942 1943void RSReflection::Context::endBlock() { 1944 decIndentLevel(); 1945 indent() << "}" << std::endl << std::endl; 1946 return; 1947} 1948 1949void RSReflection::Context::startTypeClass(const std::string &ClassName) { 1950 indent() << "public static class " << ClassName; 1951 startBlock(); 1952 return; 1953} 1954 1955void RSReflection::Context::endTypeClass() { 1956 endBlock(); 1957 return; 1958} 1959 1960void RSReflection::Context::startFunction(AccessModifier AM, 1961 bool IsStatic, 1962 const char *ReturnType, 1963 const std::string &FunctionName, 1964 int Argc, ...) { 1965 ArgTy Args; 1966 va_list vl; 1967 va_start(vl, Argc); 1968 1969 for (int i = 0; i < Argc; i++) { 1970 const char *ArgType = va_arg(vl, const char*); 1971 const char *ArgName = va_arg(vl, const char*); 1972 1973 Args.push_back(std::make_pair(ArgType, ArgName)); 1974 } 1975 va_end(vl); 1976 1977 startFunction(AM, IsStatic, ReturnType, FunctionName, Args); 1978 1979 return; 1980} 1981 1982void RSReflection::Context::startFunction(AccessModifier AM, 1983 bool IsStatic, 1984 const char *ReturnType, 1985 const std::string &FunctionName, 1986 const ArgTy &Args) { 1987 indent() << AccessModifierStr(AM) << ((IsStatic) ? " static " : " ") 1988 << ((ReturnType) ? ReturnType : "") << " " << FunctionName << "("; 1989 1990 bool FirstArg = true; 1991 for (ArgTy::const_iterator I = Args.begin(), E = Args.end(); 1992 I != E; 1993 I++) { 1994 if (!FirstArg) 1995 out() << ", "; 1996 else 1997 FirstArg = false; 1998 1999 out() << I->first << " " << I->second; 2000 } 2001 2002 out() << ")"; 2003 startBlock(); 2004 2005 return; 2006} 2007 2008void RSReflection::Context::endFunction() { 2009 endBlock(); 2010 return; 2011} 2012 2013} // namespace slang 2014