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