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