slang_rs_reflection.h revision c9454afec1649846512993d0ef65a9f868976bb4
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  int mNextExportVarSlot;
79  int mNextExportFuncSlot;
80  int mNextExportForEachSlot;
81
82  GeneratedFile mOut;
83
84  std::string mLastError;
85  std::vector<std::string> *mGeneratedFileNames;
86
87  // A mapping from a field in a record type to its index in the rsType
88  // instance. Only used when generates TypeClass (ScriptField_*).
89  typedef std::map<const RSExportRecordType::Field *, unsigned> FieldIndexMapTy;
90  FieldIndexMapTy mFieldIndexMap;
91  // Field index of current processing TypeClass.
92  unsigned mFieldIndex;
93
94  inline void setError(const std::string &Error) { mLastError = Error; }
95
96  inline void clear() {
97    mClassName = "";
98    mNextExportVarSlot = 0;
99    mNextExportFuncSlot = 0;
100    mNextExportForEachSlot = 0;
101  }
102
103public:
104  typedef enum {
105    AM_Public,
106    AM_Protected,
107    AM_Private,
108    AM_PublicSynchronized
109  } AccessModifier;
110
111  // Generated RS Elements for type-checking code.
112  std::set<std::string> mTypesToCheck;
113
114  // Generated FieldPackers for unsigned setters/validation.
115  std::set<std::string> mFieldPackerTypes;
116
117  bool addTypeNameForElement(const std::string &TypeName);
118  bool addTypeNameForFieldPacker(const std::string &TypeName);
119
120  static const char *AccessModifierStr(AccessModifier AM);
121
122  inline bool getEmbedBitcodeInJava() const { return mEmbedBitcodeInJava; }
123
124  inline int getNextExportVarSlot() { return mNextExportVarSlot++; }
125  inline int getNextExportFuncSlot() { return mNextExportFuncSlot++; }
126  inline int getNextExportForEachSlot() { return mNextExportForEachSlot++; }
127
128  bool startClass(AccessModifier AM, bool IsStatic,
129                  const std::string &ClassName, const char *SuperClassName,
130                  std::string &ErrorMsg);
131  void endClass();
132
133  void startFunction(AccessModifier AM, bool IsStatic, const char *ReturnType,
134                     const std::string &FunctionName, int Argc, ...);
135
136  typedef std::vector<std::pair<std::string, std::string>> ArgTy;
137  void startFunction(AccessModifier AM, bool IsStatic, const char *ReturnType,
138                     const std::string &FunctionName, const ArgTy &Args);
139  void endFunction();
140
141  inline const std::string &getPackageName() const { return mPackageName; }
142  inline const std::string &getRSPackageName() const { return mRSPackageName; }
143  inline const std::string &getClassName() const { return mClassName; }
144  inline const std::string &getResourceId() const { return mResourceId; }
145
146  void startTypeClass(const std::string &ClassName);
147  void endTypeClass();
148
149  inline void incFieldIndex() { mFieldIndex++; }
150
151  inline void resetFieldIndex() { mFieldIndex = 0; }
152
153  inline void addFieldIndexMapping(const RSExportRecordType::Field *F) {
154    slangAssert((mFieldIndexMap.find(F) == mFieldIndexMap.end()) &&
155                "Nested structure never occurs in C language.");
156    mFieldIndexMap.insert(std::make_pair(F, mFieldIndex));
157  }
158
159  inline unsigned getFieldIndex(const RSExportRecordType::Field *F) const {
160    FieldIndexMapTy::const_iterator I = mFieldIndexMap.find(F);
161    slangAssert((I != mFieldIndexMap.end()) &&
162                "Requesting field is out of scope.");
163    return I->second;
164  }
165
166  inline void clearFieldIndexMap() { mFieldIndexMap.clear(); }
167
168private:
169  bool genScriptClass(const std::string &ClassName, std::string &ErrorMsg);
170  void genScriptClassConstructor();
171
172  void genInitBoolExportVariable(const std::string &VarName,
173                                 const clang::APValue &Val);
174  void genInitPrimitiveExportVariable(const std::string &VarName,
175                                      const clang::APValue &Val);
176  void genInitExportVariable(const RSExportType *ET, const std::string &VarName,
177                             const clang::APValue &Val);
178  void genInitValue(const clang::APValue &Val, bool asBool);
179  void genExportVariable(const RSExportVar *EV);
180  void genPrimitiveTypeExportVariable(const RSExportVar *EV);
181  void genPointerTypeExportVariable(const RSExportVar *EV);
182  void genVectorTypeExportVariable(const RSExportVar *EV);
183  void genMatrixTypeExportVariable(const RSExportVar *EV);
184  void genConstantArrayTypeExportVariable(const RSExportVar *EV);
185  void genRecordTypeExportVariable(const RSExportVar *EV);
186  void genPrivateExportVariable(const std::string &TypeName,
187                                const std::string &VarName);
188  void genSetExportVariable(const std::string &TypeName, const RSExportVar *EV);
189  void genGetExportVariable(const std::string &TypeName,
190                            const std::string &VarName);
191  void genGetFieldID(const std::string &VarName);
192
193  void genExportFunction(const RSExportFunc *EF);
194
195  void genExportForEach(const RSExportForEach *EF);
196
197  void genTypeCheck(const RSExportType *ET, const char *VarName);
198
199  void genTypeInstanceFromPointer(const RSExportType *ET);
200
201  void genTypeInstance(const RSExportType *ET);
202
203  void genFieldPackerInstance(const RSExportType *ET);
204
205  bool genTypeClass(const RSExportRecordType *ERT, std::string &ErrorMsg);
206  void genTypeItemClass(const RSExportRecordType *ERT);
207  void genTypeClassConstructor(const RSExportRecordType *ERT);
208  void genTypeClassCopyToArray(const RSExportRecordType *ERT);
209  void genTypeClassCopyToArrayLocal(const RSExportRecordType *ERT);
210  void genTypeClassItemSetter(const RSExportRecordType *ERT);
211  void genTypeClassItemGetter(const RSExportRecordType *ERT);
212  void genTypeClassComponentSetter(const RSExportRecordType *ERT);
213  void genTypeClassComponentGetter(const RSExportRecordType *ERT);
214  void genTypeClassCopyAll(const RSExportRecordType *ERT);
215  void genTypeClassResize();
216
217  void genBuildElement(const char *ElementBuilderName,
218                       const RSExportRecordType *ERT,
219                       const char *RenderScriptVar, bool IsInline);
220  void genAddElementToElementBuilder(const RSExportType *ERT,
221                                     const std::string &VarName,
222                                     const char *ElementBuilderName,
223                                     const char *RenderScriptVar,
224                                     unsigned ArraySize);
225
226  bool genCreateFieldPacker(const RSExportType *T, const char *FieldPackerName);
227  void genPackVarOfType(const RSExportType *T, const char *VarName,
228                        const char *FieldPackerName);
229  void genAllocateVarOfType(const RSExportType *T, const std::string &VarName);
230  void genNewItemBufferIfNull(const char *Index);
231  void genNewItemBufferPackerIfNull();
232
233  void genPairwiseDimCheck(std::string name0, std::string name1);
234
235public:
236  RSReflectionJava(const RSContext *Context,
237                   std::vector<std::string> *GeneratedFileNames,
238                   const std::string &OutputBaseDirectory,
239                   const std::string &RSSourceFilename,
240                   const std::string &BitCodeFileName,
241                   bool EmbedBitcodeInJava);
242
243  bool reflect();
244
245  inline const char *getLastError() const {
246    if (mLastError.empty())
247      return NULL;
248    else
249      return mLastError.c_str();
250  }
251}; // class RSReflectionJava
252
253} // namespace slang
254
255#endif // _FRAMEWORKS_COMPILE_SLANG_SLANG_RS_REFLECTION_H_  NOLINT
256