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