blob: 0c2b7736eb084159621c6e36a4766d39acdccb0b [file] [log] [blame]
Zonr Changc383a502010-10-12 01:52:08 +08001/*
Stephen Hines0a813a32012-08-03 16:52:40 -07002 * Copyright 2010-2012, The Android Open Source Project
Zonr Changc383a502010-10-12 01:52:08 +08003 *
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
Jean-Luc Brouillet2ce118e2014-05-27 17:41:22 -070017#ifndef _FRAMEWORKS_COMPILE_SLANG_SLANG_RS_REFLECTION_H_ // NOLINT
Stephen Hinese639eb52010-11-08 19:27:20 -080018#define _FRAMEWORKS_COMPILE_SLANG_SLANG_RS_REFLECTION_H_
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070019
Shih-wei Liao462aefd2010-06-04 15:32:04 -070020#include <fstream>
21#include <iostream>
Stephen Hinese639eb52010-11-08 19:27:20 -080022#include <map>
Stephen Hines48b72bf2011-06-10 15:37:27 -070023#include <set>
Stephen Hinese639eb52010-11-08 19:27:20 -080024#include <string>
25#include <vector>
Shih-wei Liao462aefd2010-06-04 15:32:04 -070026
zonr6315f762010-10-05 15:35:14 +080027#include "llvm/ADT/StringExtras.h"
28
Stephen Hines6e6578a2011-02-07 18:05:48 -080029#include "slang_assert.h"
zonr6315f762010-10-05 15:35:14 +080030#include "slang_rs_export_type.h"
Jean-Luc Brouilletf33e1562014-06-03 17:55:57 -070031#include "slang_rs_reflect_utils.h"
zonr6315f762010-10-05 15:35:14 +080032
Shih-wei Liao462aefd2010-06-04 15:32:04 -070033namespace slang {
34
Jean-Luc Brouillet2ce118e2014-05-27 17:41:22 -070035class RSContext;
36class RSExportVar;
37class RSExportFunc;
38class RSExportForEach;
Shih-wei Liao462aefd2010-06-04 15:32:04 -070039
Jean-Luc Brouillet602def72014-05-27 16:11:37 -070040class RSReflectionJava {
Jean-Luc Brouillet2ce118e2014-05-27 17:41:22 -070041private:
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070042 const RSContext *mRSContext;
Shih-wei Liao462aefd2010-06-04 15:32:04 -070043
Jean-Luc Brouillet59f22c32014-06-04 14:53:48 -070044 // The name of the Java package name we're creating this file for,
45 // e.g. com.example.android.rs.flashlight
Jean-Luc Brouillet2e205d02014-06-02 21:06:52 -070046 std::string mPackageName;
Jean-Luc Brouillet59f22c32014-06-04 14:53:48 -070047 // The name of the Java Renderscript package we'll be using,
48 // e.g. android.renderscript
49 // e.g. android.support.v8.renderscript
Jean-Luc Brouillet2e205d02014-06-02 21:06:52 -070050 std::string mRSPackageName;
Shih-wei Liao462aefd2010-06-04 15:32:04 -070051
Jean-Luc Brouillet59f22c32014-06-04 14:53:48 -070052 // 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.
Jean-Luc Brouillet2e205d02014-06-02 21:06:52 -070074 std::string mClassName;
Shih-wei Liao462aefd2010-06-04 15:32:04 -070075
Tim Murray3a38b742014-07-02 10:41:08 -070076 // This is the token used for determining the size of a given ScriptField.Item.
77 std::string mItemSizeof;
78
Jean-Luc Brouillet2e205d02014-06-02 21:06:52 -070079 bool mEmbedBitcodeInJava;
Stephen Hines44d495d2014-05-22 19:42:55 -070080
Jean-Luc Brouillet2e205d02014-06-02 21:06:52 -070081 int mNextExportVarSlot;
82 int mNextExportFuncSlot;
83 int mNextExportForEachSlot;
Shih-wei Liao462aefd2010-06-04 15:32:04 -070084
Jean-Luc Brouilletf33e1562014-06-03 17:55:57 -070085 GeneratedFile mOut;
86
Jean-Luc Brouillet59f22c32014-06-04 14:53:48 -070087 std::string mLastError;
88 std::vector<std::string> *mGeneratedFileNames;
89
Jean-Luc Brouillet2e205d02014-06-02 21:06:52 -070090 // 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;
Shih-wei Liao462aefd2010-06-04 15:32:04 -070096
Jean-Luc Brouillet59f22c32014-06-04 14:53:48 -070097 inline void setError(const std::string &Error) { mLastError = Error; }
98
Jean-Luc Brouillet2e205d02014-06-02 21:06:52 -070099 inline void clear() {
100 mClassName = "";
Jean-Luc Brouillet2e205d02014-06-02 21:06:52 -0700101 mNextExportVarSlot = 0;
102 mNextExportFuncSlot = 0;
103 mNextExportForEachSlot = 0;
104 }
Shih-wei Liao9c631ff2010-09-17 11:57:29 -0700105
Jean-Luc Brouillet2e205d02014-06-02 21:06:52 -0700106public:
107 typedef enum {
108 AM_Public,
109 AM_Protected,
110 AM_Private,
111 AM_PublicSynchronized
112 } AccessModifier;
Zonr Chang8c6d9b22010-10-07 18:01:19 +0800113
Jean-Luc Brouillet2e205d02014-06-02 21:06:52 -0700114 // Generated RS Elements for type-checking code.
115 std::set<std::string> mTypesToCheck;
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700116
Jean-Luc Brouillet2e205d02014-06-02 21:06:52 -0700117 // Generated FieldPackers for unsigned setters/validation.
118 std::set<std::string> mFieldPackerTypes;
Stephen Hines48b72bf2011-06-10 15:37:27 -0700119
Jean-Luc Brouillet2e205d02014-06-02 21:06:52 -0700120 bool addTypeNameForElement(const std::string &TypeName);
121 bool addTypeNameForFieldPacker(const std::string &TypeName);
Stephen Hines1f6c3312012-07-03 17:23:33 -0700122
Jean-Luc Brouillet2e205d02014-06-02 21:06:52 -0700123 static const char *AccessModifierStr(AccessModifier AM);
Stephen Hines1f6c3312012-07-03 17:23:33 -0700124
Jean-Luc Brouillet2e205d02014-06-02 21:06:52 -0700125 inline bool getEmbedBitcodeInJava() const { return mEmbedBitcodeInJava; }
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700126
Jean-Luc Brouillet2e205d02014-06-02 21:06:52 -0700127 inline int getNextExportVarSlot() { return mNextExportVarSlot++; }
Jean-Luc Brouillet2e205d02014-06-02 21:06:52 -0700128 inline int getNextExportFuncSlot() { return mNextExportFuncSlot++; }
129 inline int getNextExportForEachSlot() { return mNextExportForEachSlot++; }
Stephen Hines44d495d2014-05-22 19:42:55 -0700130
Jean-Luc Brouillet2e205d02014-06-02 21:06:52 -0700131 bool startClass(AccessModifier AM, bool IsStatic,
132 const std::string &ClassName, const char *SuperClassName,
133 std::string &ErrorMsg);
134 void endClass();
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700135
Jean-Luc Brouillet2e205d02014-06-02 21:06:52 -0700136 void startFunction(AccessModifier AM, bool IsStatic, const char *ReturnType,
137 const std::string &FunctionName, int Argc, ...);
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700138
Jean-Luc Brouillet2e205d02014-06-02 21:06:52 -0700139 typedef std::vector<std::pair<std::string, std::string>> ArgTy;
140 void startFunction(AccessModifier AM, bool IsStatic, const char *ReturnType,
141 const std::string &FunctionName, const ArgTy &Args);
142 void endFunction();
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700143
Jean-Luc Brouillet2e205d02014-06-02 21:06:52 -0700144 inline const std::string &getPackageName() const { return mPackageName; }
145 inline const std::string &getRSPackageName() const { return mRSPackageName; }
146 inline const std::string &getClassName() const { return mClassName; }
147 inline const std::string &getResourceId() const { return mResourceId; }
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700148
Jean-Luc Brouillet2e205d02014-06-02 21:06:52 -0700149 void startTypeClass(const std::string &ClassName);
150 void endTypeClass();
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700151
Jean-Luc Brouillet2e205d02014-06-02 21:06:52 -0700152 inline void incFieldIndex() { mFieldIndex++; }
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700153
Jean-Luc Brouillet2e205d02014-06-02 21:06:52 -0700154 inline void resetFieldIndex() { mFieldIndex = 0; }
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700155
Jean-Luc Brouillet2e205d02014-06-02 21:06:52 -0700156 inline void addFieldIndexMapping(const RSExportRecordType::Field *F) {
157 slangAssert((mFieldIndexMap.find(F) == mFieldIndexMap.end()) &&
158 "Nested structure never occurs in C language.");
159 mFieldIndexMap.insert(std::make_pair(F, mFieldIndex));
160 }
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700161
Jean-Luc Brouillet2e205d02014-06-02 21:06:52 -0700162 inline unsigned getFieldIndex(const RSExportRecordType::Field *F) const {
163 FieldIndexMapTy::const_iterator I = mFieldIndexMap.find(F);
164 slangAssert((I != mFieldIndexMap.end()) &&
165 "Requesting field is out of scope.");
166 return I->second;
167 }
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700168
Jean-Luc Brouillet2e205d02014-06-02 21:06:52 -0700169 inline void clearFieldIndexMap() { mFieldIndexMap.clear(); }
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700170
Jean-Luc Brouillet2e205d02014-06-02 21:06:52 -0700171private:
172 bool genScriptClass(const std::string &ClassName, std::string &ErrorMsg);
173 void genScriptClassConstructor();
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700174
Jean-Luc Brouillet2e205d02014-06-02 21:06:52 -0700175 void genInitBoolExportVariable(const std::string &VarName,
176 const clang::APValue &Val);
177 void genInitPrimitiveExportVariable(const std::string &VarName,
178 const clang::APValue &Val);
179 void genInitExportVariable(const RSExportType *ET, const std::string &VarName,
180 const clang::APValue &Val);
Jean-Luc Brouilletefcff102014-06-03 16:13:51 -0700181 void genInitValue(const clang::APValue &Val, bool asBool);
Jean-Luc Brouillet2e205d02014-06-02 21:06:52 -0700182 void genExportVariable(const RSExportVar *EV);
183 void genPrimitiveTypeExportVariable(const RSExportVar *EV);
184 void genPointerTypeExportVariable(const RSExportVar *EV);
185 void genVectorTypeExportVariable(const RSExportVar *EV);
186 void genMatrixTypeExportVariable(const RSExportVar *EV);
187 void genConstantArrayTypeExportVariable(const RSExportVar *EV);
188 void genRecordTypeExportVariable(const RSExportVar *EV);
189 void genPrivateExportVariable(const std::string &TypeName,
Stephen Hines0d26cef2012-05-01 19:23:01 -0700190 const std::string &VarName);
Jean-Luc Brouillet2e205d02014-06-02 21:06:52 -0700191 void genSetExportVariable(const std::string &TypeName, const RSExportVar *EV);
192 void genGetExportVariable(const std::string &TypeName,
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700193 const std::string &VarName);
Jean-Luc Brouillet2e205d02014-06-02 21:06:52 -0700194 void genGetFieldID(const std::string &VarName);
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700195
Jean-Luc Brouillet2e205d02014-06-02 21:06:52 -0700196 void genExportFunction(const RSExportFunc *EF);
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700197
Jean-Luc Brouillet2e205d02014-06-02 21:06:52 -0700198 void genExportForEach(const RSExportForEach *EF);
Stephen Hines593a8942011-05-10 15:29:50 -0700199
Jean-Luc Brouillet2e205d02014-06-02 21:06:52 -0700200 void genTypeCheck(const RSExportType *ET, const char *VarName);
Stephen Hines48b72bf2011-06-10 15:37:27 -0700201
Jean-Luc Brouillet2e205d02014-06-02 21:06:52 -0700202 void genTypeInstanceFromPointer(const RSExportType *ET);
Stephen Hinesa6b54142012-04-09 18:25:08 -0700203
Jean-Luc Brouillet2e205d02014-06-02 21:06:52 -0700204 void genTypeInstance(const RSExportType *ET);
Stephen Hinesb5a89fb2011-05-17 14:48:02 -0700205
Jean-Luc Brouillet2e205d02014-06-02 21:06:52 -0700206 void genFieldPackerInstance(const RSExportType *ET);
Stephen Hines1f6c3312012-07-03 17:23:33 -0700207
Jean-Luc Brouillet2e205d02014-06-02 21:06:52 -0700208 bool genTypeClass(const RSExportRecordType *ERT, std::string &ErrorMsg);
209 void genTypeItemClass(const RSExportRecordType *ERT);
210 void genTypeClassConstructor(const RSExportRecordType *ERT);
211 void genTypeClassCopyToArray(const RSExportRecordType *ERT);
212 void genTypeClassCopyToArrayLocal(const RSExportRecordType *ERT);
213 void genTypeClassItemSetter(const RSExportRecordType *ERT);
214 void genTypeClassItemGetter(const RSExportRecordType *ERT);
215 void genTypeClassComponentSetter(const RSExportRecordType *ERT);
216 void genTypeClassComponentGetter(const RSExportRecordType *ERT);
217 void genTypeClassCopyAll(const RSExportRecordType *ERT);
218 void genTypeClassResize();
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700219
Jean-Luc Brouillet2e205d02014-06-02 21:06:52 -0700220 void genBuildElement(const char *ElementBuilderName,
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700221 const RSExportRecordType *ERT,
Jean-Luc Brouillet2ce118e2014-05-27 17:41:22 -0700222 const char *RenderScriptVar, bool IsInline);
Jean-Luc Brouillet2e205d02014-06-02 21:06:52 -0700223 void genAddElementToElementBuilder(const RSExportType *ERT,
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700224 const std::string &VarName,
225 const char *ElementBuilderName,
Zonr Chang89273bd2010-10-14 20:57:38 +0800226 const char *RenderScriptVar,
227 unsigned ArraySize);
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700228
Jean-Luc Brouillet2e205d02014-06-02 21:06:52 -0700229 bool genCreateFieldPacker(const RSExportType *T, const char *FieldPackerName);
230 void genPackVarOfType(const RSExportType *T, const char *VarName,
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700231 const char *FieldPackerName);
Jean-Luc Brouillet2e205d02014-06-02 21:06:52 -0700232 void genAllocateVarOfType(const RSExportType *T, const std::string &VarName);
233 void genNewItemBufferIfNull(const char *Index);
234 void genNewItemBufferPackerIfNull();
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700235
Chris Wailesc9454af2014-06-13 17:25:40 -0700236 void genPairwiseDimCheck(std::string name0, std::string name1);
237
Jean-Luc Brouillet2ce118e2014-05-27 17:41:22 -0700238public:
Jean-Luc Brouillet59f22c32014-06-04 14:53:48 -0700239 RSReflectionJava(const RSContext *Context,
240 std::vector<std::string> *GeneratedFileNames,
241 const std::string &OutputBaseDirectory,
242 const std::string &RSSourceFilename,
243 const std::string &BitCodeFileName,
244 bool EmbedBitcodeInJava);
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700245
Jean-Luc Brouillet59f22c32014-06-04 14:53:48 -0700246 bool reflect();
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700247
248 inline const char *getLastError() const {
249 if (mLastError.empty())
250 return NULL;
251 else
252 return mLastError.c_str();
253 }
Jean-Luc Brouillet2ce118e2014-05-27 17:41:22 -0700254}; // class RSReflectionJava
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700255
Jean-Luc Brouillet2ce118e2014-05-27 17:41:22 -0700256} // namespace slang
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700257
Jean-Luc Brouillet2ce118e2014-05-27 17:41:22 -0700258#endif // _FRAMEWORKS_COMPILE_SLANG_SLANG_RS_REFLECTION_H_ NOLINT