slang_rs_reflection.h revision 59f22c376b2c1cd109735280689224fadfe40b42
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#ifndef _FRAMEWORKS_COMPILE_SLANG_SLANG_RS_REFLECTION_H_ // NOLINT
18#define _FRAMEWORKS_COMPILE_SLANG_SLANG_RS_REFLECTION_H_
19
20#include <fstream>
21#include <iostream>
22#include <map>
23#include <set>
24#include <string>
25#include <vector>
26
27#include "llvm/ADT/StringExtras.h"
28
29#include "slang_assert.h"
30#include "slang_rs_export_type.h"
31#include "slang_rs_reflect_utils.h"
32
33namespace slang {
34
35class RSContext;
36class RSExportVar;
37class RSExportFunc;
38class RSExportForEach;
39
40class RSReflectionJava {
41private:
42  const RSContext *mRSContext;
43
44  // The name of the Java package name we're creating this file for,
45  // e.g. com.example.android.rs.flashlight
46  std::string mPackageName;
47  // The name of the Java Renderscript package we'll be using,
48  // e.g. android.renderscript
49  // e.g. android.support.v8.renderscript
50  std::string mRSPackageName;
51
52  // The directory under which we'll create the Java files, in appropriate subdirectories,
53  // e.g. /tmp/myout
54  std::string mOutputBaseDirectory;
55  // The output directory for the specfied package (mPackageName),
56  // e.g. /tmp/myout/com/example/android/rs/flashlight/
57  // TODO This includes the terminating separator.  Needed?
58  std::string mOutputDirectory;
59
60  // The full path of the .rs file that we are reflecting.
61  std::string mRSSourceFileName;
62  // The full path where the generated bit code can be read.
63  std::string mBitCodeFileName;
64
65  // The name of the resource we pass to the RenderScript constructor
66  // e.g. flashlight
67  std::string mResourceId;
68  // The name of the Java class we are generating for this script.
69  // e.g. ScriptC_flashlight
70  std::string mScriptClassName;
71
72
73  // This is set by startClass() and will change for the multiple classes generated.
74  std::string mClassName;
75
76  bool mEmbedBitcodeInJava;
77
78  std::string mPaddingPrefix;
79  int mPaddingFieldIndex;
80
81  int mNextExportVarSlot;
82  int mNextExportFuncSlot;
83  int mNextExportForEachSlot;
84
85  GeneratedFile mOut;
86
87  std::string mLastError;
88  std::vector<std::string> *mGeneratedFileNames;
89
90  // A mapping from a field in a record type to its index in the rsType
91  // instance. Only used when generates TypeClass (ScriptField_*).
92  typedef std::map<const RSExportRecordType::Field *, unsigned> FieldIndexMapTy;
93  FieldIndexMapTy mFieldIndexMap;
94  // Field index of current processing TypeClass.
95  unsigned mFieldIndex;
96
97  inline void setError(const std::string &Error) { mLastError = Error; }
98
99  inline void clear() {
100    mClassName = "";
101    mPaddingFieldIndex = 1;
102    mNextExportVarSlot = 0;
103    mNextExportFuncSlot = 0;
104    mNextExportForEachSlot = 0;
105  }
106
107public:
108  typedef enum {
109    AM_Public,
110    AM_Protected,
111    AM_Private,
112    AM_PublicSynchronized
113  } AccessModifier;
114
115  // Generated RS Elements for type-checking code.
116  std::set<std::string> mTypesToCheck;
117
118  // Generated FieldPackers for unsigned setters/validation.
119  std::set<std::string> mFieldPackerTypes;
120
121  bool addTypeNameForElement(const std::string &TypeName);
122  bool addTypeNameForFieldPacker(const std::string &TypeName);
123
124  static const char *AccessModifierStr(AccessModifier AM);
125
126  inline bool getEmbedBitcodeInJava() const { return mEmbedBitcodeInJava; }
127
128  inline int getNextExportVarSlot() { return mNextExportVarSlot++; }
129  inline int getNextExportFuncSlot() { return mNextExportFuncSlot++; }
130  inline int getNextExportForEachSlot() { return mNextExportForEachSlot++; }
131
132  // Will remove later due to field name information is not necessary for
133  // C-reflect-to-Java
134  inline std::string createPaddingField() {
135    return mPaddingPrefix + llvm::itostr(mPaddingFieldIndex++);
136  }
137
138  bool startClass(AccessModifier AM, bool IsStatic,
139                  const std::string &ClassName, const char *SuperClassName,
140                  std::string &ErrorMsg);
141  void endClass();
142
143  void startFunction(AccessModifier AM, bool IsStatic, const char *ReturnType,
144                     const std::string &FunctionName, int Argc, ...);
145
146  typedef std::vector<std::pair<std::string, std::string>> ArgTy;
147  void startFunction(AccessModifier AM, bool IsStatic, const char *ReturnType,
148                     const std::string &FunctionName, const ArgTy &Args);
149  void endFunction();
150
151  inline const std::string &getPackageName() const { return mPackageName; }
152  inline const std::string &getRSPackageName() const { return mRSPackageName; }
153  inline const std::string &getClassName() const { return mClassName; }
154  inline const std::string &getResourceId() const { return mResourceId; }
155
156  void startTypeClass(const std::string &ClassName);
157  void endTypeClass();
158
159  inline void incFieldIndex() { mFieldIndex++; }
160
161  inline void resetFieldIndex() { mFieldIndex = 0; }
162
163  inline void addFieldIndexMapping(const RSExportRecordType::Field *F) {
164    slangAssert((mFieldIndexMap.find(F) == mFieldIndexMap.end()) &&
165                "Nested structure never occurs in C language.");
166    mFieldIndexMap.insert(std::make_pair(F, mFieldIndex));
167  }
168
169  inline unsigned getFieldIndex(const RSExportRecordType::Field *F) const {
170    FieldIndexMapTy::const_iterator I = mFieldIndexMap.find(F);
171    slangAssert((I != mFieldIndexMap.end()) &&
172                "Requesting field is out of scope.");
173    return I->second;
174  }
175
176  inline void clearFieldIndexMap() { mFieldIndexMap.clear(); }
177
178private:
179  bool genScriptClass(const std::string &ClassName, std::string &ErrorMsg);
180  void genScriptClassConstructor();
181
182  void genInitBoolExportVariable(const std::string &VarName,
183                                 const clang::APValue &Val);
184  void genInitPrimitiveExportVariable(const std::string &VarName,
185                                      const clang::APValue &Val);
186  void genInitExportVariable(const RSExportType *ET, const std::string &VarName,
187                             const clang::APValue &Val);
188  void genInitValue(const clang::APValue &Val, bool asBool);
189  void genExportVariable(const RSExportVar *EV);
190  void genPrimitiveTypeExportVariable(const RSExportVar *EV);
191  void genPointerTypeExportVariable(const RSExportVar *EV);
192  void genVectorTypeExportVariable(const RSExportVar *EV);
193  void genMatrixTypeExportVariable(const RSExportVar *EV);
194  void genConstantArrayTypeExportVariable(const RSExportVar *EV);
195  void genRecordTypeExportVariable(const RSExportVar *EV);
196  void genPrivateExportVariable(const std::string &TypeName,
197                                const std::string &VarName);
198  void genSetExportVariable(const std::string &TypeName, const RSExportVar *EV);
199  void genGetExportVariable(const std::string &TypeName,
200                            const std::string &VarName);
201  void genGetFieldID(const std::string &VarName);
202
203  void genExportFunction(const RSExportFunc *EF);
204
205  void genExportForEach(const RSExportForEach *EF);
206
207  void genTypeCheck(const RSExportType *ET, const char *VarName);
208
209  void genTypeInstanceFromPointer(const RSExportType *ET);
210
211  void genTypeInstance(const RSExportType *ET);
212
213  void genFieldPackerInstance(const RSExportType *ET);
214
215  bool genTypeClass(const RSExportRecordType *ERT, std::string &ErrorMsg);
216  void genTypeItemClass(const RSExportRecordType *ERT);
217  void genTypeClassConstructor(const RSExportRecordType *ERT);
218  void genTypeClassCopyToArray(const RSExportRecordType *ERT);
219  void genTypeClassCopyToArrayLocal(const RSExportRecordType *ERT);
220  void genTypeClassItemSetter(const RSExportRecordType *ERT);
221  void genTypeClassItemGetter(const RSExportRecordType *ERT);
222  void genTypeClassComponentSetter(const RSExportRecordType *ERT);
223  void genTypeClassComponentGetter(const RSExportRecordType *ERT);
224  void genTypeClassCopyAll(const RSExportRecordType *ERT);
225  void genTypeClassResize();
226
227  void genBuildElement(const char *ElementBuilderName,
228                       const RSExportRecordType *ERT,
229                       const char *RenderScriptVar, bool IsInline);
230  void genAddElementToElementBuilder(const RSExportType *ERT,
231                                     const std::string &VarName,
232                                     const char *ElementBuilderName,
233                                     const char *RenderScriptVar,
234                                     unsigned ArraySize);
235  void genAddPaddingToElementBuilder(int PaddingSize,
236                                     const char *ElementBuilderName,
237                                     const char *RenderScriptVar);
238
239  bool genCreateFieldPacker(const RSExportType *T, const char *FieldPackerName);
240  void genPackVarOfType(const RSExportType *T, const char *VarName,
241                        const char *FieldPackerName);
242  void genAllocateVarOfType(const RSExportType *T, const std::string &VarName);
243  void genNewItemBufferIfNull(const char *Index);
244  void genNewItemBufferPackerIfNull();
245
246public:
247  RSReflectionJava(const RSContext *Context,
248                   std::vector<std::string> *GeneratedFileNames,
249                   const std::string &OutputBaseDirectory,
250                   const std::string &RSSourceFilename,
251                   const std::string &BitCodeFileName,
252                   bool EmbedBitcodeInJava);
253
254  bool reflect();
255
256  inline const char *getLastError() const {
257    if (mLastError.empty())
258      return NULL;
259    else
260      return mLastError.c_str();
261  }
262}; // class RSReflectionJava
263
264} // namespace slang
265
266#endif // _FRAMEWORKS_COMPILE_SLANG_SLANG_RS_REFLECTION_H_  NOLINT
267