slang_rs_reflection.h revision a9ae5ae8866d937a99601d24a922c8f3f4223f59
1/*
2 * Copyright 2010-2011, 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
32namespace slang {
33
34  class RSContext;
35  class RSExportVar;
36  class RSExportFunc;
37  class RSExportForEach;
38
39class RSReflection {
40 private:
41  const RSContext *mRSContext;
42
43  std::string mLastError;
44  std::vector<std::string> *mGeneratedFileNames;
45
46  inline void setError(const std::string &Error) { mLastError = Error; }
47
48  class Context {
49   private:
50    static const char *const ApacheLicenseNote;
51
52    static const char *const Import[];
53
54    bool mVerbose;
55
56    std::string mOutputPathBase;
57
58    std::string mInputRSFile;
59
60    std::string mPackageName;
61    std::string mResourceId;
62    std::string mPaddingPrefix;
63
64    std::string mClassName;
65
66    std::string mLicenseNote;
67
68    std::string mIndent;
69
70    int mPaddingFieldIndex;
71
72    int mNextExportVarSlot;
73    int mNextExportFuncSlot;
74    int mNextExportForEachSlot;
75
76    // A mapping from a field in a record type to its index in the rsType
77    // instance. Only used when generates TypeClass (ScriptField_*).
78    typedef std::map<const RSExportRecordType::Field*, unsigned>
79        FieldIndexMapTy;
80    FieldIndexMapTy mFieldIndexMap;
81    // Field index of current processing TypeClass.
82    unsigned mFieldIndex;
83
84    inline void clear() {
85      mClassName = "";
86      mIndent = "";
87      mPaddingFieldIndex = 1;
88      mNextExportVarSlot = 0;
89      mNextExportFuncSlot = 0;
90      mNextExportForEachSlot = 0;
91      return;
92    }
93
94    bool openClassFile(const std::string &ClassName,
95                       std::string &ErrorMsg);
96
97   public:
98    typedef enum {
99      AM_Public,
100      AM_Protected,
101      AM_Private,
102      AM_PublicSynchronized
103    } AccessModifier;
104
105    bool mUseStdout;
106    mutable std::ofstream mOF;
107
108    std::set<std::string> mTypesToCheck;
109
110    static const char *AccessModifierStr(AccessModifier AM);
111
112    Context(const std::string &OutputPathBase,
113            const std::string &InputRSFile,
114            const std::string &PackageName,
115            const std::string &ResourceId,
116            const std::string &PaddingPrefix,
117            bool UseStdout)
118        : mVerbose(true),
119          mOutputPathBase(OutputPathBase),
120          mInputRSFile(InputRSFile),
121          mPackageName(PackageName),
122          mResourceId(ResourceId),
123          mPaddingPrefix(PaddingPrefix),
124          mLicenseNote(ApacheLicenseNote),
125          mUseStdout(UseStdout) {
126      clear();
127      resetFieldIndex();
128      clearFieldIndexMap();
129      return;
130    }
131
132    inline std::ostream &out() const {
133      return ((mUseStdout) ? std::cout : mOF);
134    }
135    inline std::ostream &indent() const {
136      out() << mIndent;
137      return out();
138    }
139
140    inline void incIndentLevel() {
141      mIndent.append(4, ' ');
142      return;
143    }
144
145    inline void decIndentLevel() {
146      slangAssert(getIndentLevel() > 0 && "No indent");
147      mIndent.erase(0, 4);
148      return;
149    }
150
151    inline int getIndentLevel() { return (mIndent.length() >> 2); }
152
153    inline int getNextExportVarSlot() { return mNextExportVarSlot++; }
154
155    inline int getNextExportFuncSlot() { return mNextExportFuncSlot++; }
156    inline int getNextExportForEachSlot() { return mNextExportForEachSlot++; }
157
158    // Will remove later due to field name information is not necessary for
159    // C-reflect-to-Java
160    inline std::string createPaddingField() {
161      return mPaddingPrefix + llvm::itostr(mPaddingFieldIndex++);
162    }
163
164    inline void setLicenseNote(const std::string &LicenseNote) {
165      mLicenseNote = LicenseNote;
166    }
167
168    bool startClass(AccessModifier AM,
169                    bool IsStatic,
170                    const std::string &ClassName,
171                    const char *SuperClassName,
172                    std::string &ErrorMsg);
173    void endClass();
174
175    void startFunction(AccessModifier AM,
176                       bool IsStatic,
177                       const char *ReturnType,
178                       const std::string &FunctionName,
179                       int Argc, ...);
180
181    typedef std::vector<std::pair<std::string, std::string> > ArgTy;
182    void startFunction(AccessModifier AM,
183                       bool IsStatic,
184                       const char *ReturnType,
185                       const std::string &FunctionName,
186                       const ArgTy &Args);
187    void endFunction();
188
189    void startBlock(bool ShouldIndent = false);
190    void endBlock();
191
192    inline const std::string &getPackageName() const { return mPackageName; }
193    inline const std::string &getClassName() const { return mClassName; }
194    inline const std::string &getResourceId() const { return mResourceId; }
195
196    void startTypeClass(const std::string &ClassName);
197    void endTypeClass();
198
199    inline void incFieldIndex() { mFieldIndex++; }
200
201    inline void resetFieldIndex() { mFieldIndex = 0; }
202
203    inline void addFieldIndexMapping(const RSExportRecordType::Field *F) {
204      slangAssert((mFieldIndexMap.find(F) == mFieldIndexMap.end()) &&
205                  "Nested structure never occurs in C language.");
206      mFieldIndexMap.insert(std::make_pair(F, mFieldIndex));
207    }
208
209    inline unsigned getFieldIndex(const RSExportRecordType::Field *F) const {
210      FieldIndexMapTy::const_iterator I = mFieldIndexMap.find(F);
211      slangAssert((I != mFieldIndexMap.end()) &&
212                  "Requesting field is out of scope.");
213      return I->second;
214    }
215
216    inline void clearFieldIndexMap() { mFieldIndexMap.clear(); }
217  };
218
219  bool genScriptClass(Context &C,
220                      const std::string &ClassName,
221                      std::string &ErrorMsg);
222  void genScriptClassConstructor(Context &C);
223
224  void genInitBoolExportVariable(Context &C,
225                                 const std::string &VarName,
226                                 const clang::APValue &Val);
227  void genInitPrimitiveExportVariable(Context &C,
228                                      const std::string &VarName,
229                                      const clang::APValue &Val);
230  void genInitExportVariable(Context &C,
231                             const RSExportType *ET,
232                             const std::string &VarName,
233                             const clang::APValue &Val);
234  void genExportVariable(Context &C, const RSExportVar *EV);
235  void genPrimitiveTypeExportVariable(Context &C, const RSExportVar *EV);
236  void genPointerTypeExportVariable(Context &C, const RSExportVar *EV);
237  void genVectorTypeExportVariable(Context &C, const RSExportVar *EV);
238  void genMatrixTypeExportVariable(Context &C, const RSExportVar *EV);
239  void genConstantArrayTypeExportVariable(Context &C, const RSExportVar *EV);
240  void genRecordTypeExportVariable(Context &C, const RSExportVar *EV);
241  void genGetExportVariable(Context &C,
242                            const std::string &TypeName,
243                            const std::string &VarName);
244
245  void genExportFunction(Context &C,
246                         const RSExportFunc *EF);
247
248  void genExportForEach(Context &C,
249                        const RSExportForEach *EF);
250
251  static void genTypeCheck(Context &C,
252                           const RSExportType *ET,
253                           const char *VarName);
254
255  static void genTypeInstance(Context &C,
256                              const RSExportType *ET);
257
258  bool genTypeClass(Context &C,
259                    const RSExportRecordType *ERT,
260                    std::string &ErrorMsg);
261  void genTypeItemClass(Context &C, const RSExportRecordType *ERT);
262  void genTypeClassConstructor(Context &C, const RSExportRecordType *ERT);
263  void genTypeClassCopyToArray(Context &C, const RSExportRecordType *ERT);
264  void genTypeClassCopyToArrayLocal(Context &C, const RSExportRecordType *ERT);
265  void genTypeClassItemSetter(Context &C, const RSExportRecordType *ERT);
266  void genTypeClassItemGetter(Context &C, const RSExportRecordType *ERT);
267  void genTypeClassComponentSetter(Context &C, const RSExportRecordType *ERT);
268  void genTypeClassComponentGetter(Context &C, const RSExportRecordType *ERT);
269  void genTypeClassCopyAll(Context &C, const RSExportRecordType *ERT);
270  void genTypeClassResize(Context &C);
271
272  void genBuildElement(Context &C,
273                       const char *ElementBuilderName,
274                       const RSExportRecordType *ERT,
275                       const char *RenderScriptVar,
276                       bool IsInline);
277  void genAddElementToElementBuilder(Context &C,
278                                     const RSExportType *ERT,
279                                     const std::string &VarName,
280                                     const char *ElementBuilderName,
281                                     const char *RenderScriptVar,
282                                     unsigned ArraySize);
283  void genAddPaddingToElementBuiler(Context &C,
284                                    int PaddingSize,
285                                    const char *ElementBuilderName,
286                                    const char *RenderScriptVar);
287
288  bool genCreateFieldPacker(Context &C,
289                            const RSExportType *T,
290                            const char *FieldPackerName);
291  void genPackVarOfType(Context &C,
292                        const RSExportType *T,
293                        const char *VarName,
294                        const char *FieldPackerName);
295  void genAllocateVarOfType(Context &C,
296                            const RSExportType *T,
297                            const std::string &VarName);
298  void genNewItemBufferIfNull(Context &C, const char *Index);
299  void genNewItemBufferPackerIfNull(Context &C);
300
301 public:
302  explicit RSReflection(const RSContext *Context,
303      std::vector<std::string> *GeneratedFileNames)
304      : mRSContext(Context),
305        mLastError(""),
306        mGeneratedFileNames(GeneratedFileNames) {
307    slangAssert(mGeneratedFileNames && "Must supply GeneratedFileNames");
308    return;
309  }
310
311  bool reflect(const std::string &OutputPathBase,
312               const std::string &OutputPackageName,
313               const std::string &InputFileName,
314               const std::string &OutputBCFileName);
315
316  inline const char *getLastError() const {
317    if (mLastError.empty())
318      return NULL;
319    else
320      return mLastError.c_str();
321  }
322};  // class RSReflection
323
324}   // namespace slang
325
326#endif  // _FRAMEWORKS_COMPILE_SLANG_SLANG_RS_REFLECTION_H_  NOLINT
327