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