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