blob: 98a1aba42bcb764c14f0ac8e213e4881812b2773 [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;
Matt Wala7682b662015-07-30 15:53:47 -070084 int mNextExportReduceSlot;
Shih-wei Liao462aefd2010-06-04 15:32:04 -070085
Jean-Luc Brouilletf33e1562014-06-03 17:55:57 -070086 GeneratedFile mOut;
87
Jean-Luc Brouillet59f22c32014-06-04 14:53:48 -070088 std::string mLastError;
89 std::vector<std::string> *mGeneratedFileNames;
90
Jean-Luc Brouillet2e205d02014-06-02 21:06:52 -070091 // A mapping from a field in a record type to its index in the rsType
92 // instance. Only used when generates TypeClass (ScriptField_*).
93 typedef std::map<const RSExportRecordType::Field *, unsigned> FieldIndexMapTy;
94 FieldIndexMapTy mFieldIndexMap;
95 // Field index of current processing TypeClass.
96 unsigned mFieldIndex;
Shih-wei Liao462aefd2010-06-04 15:32:04 -070097
Jean-Luc Brouillet59f22c32014-06-04 14:53:48 -070098 inline void setError(const std::string &Error) { mLastError = Error; }
99
Jean-Luc Brouillet2e205d02014-06-02 21:06:52 -0700100 inline void clear() {
101 mClassName = "";
Jean-Luc Brouillet2e205d02014-06-02 21:06:52 -0700102 mNextExportVarSlot = 0;
103 mNextExportFuncSlot = 0;
104 mNextExportForEachSlot = 0;
Matt Wala7682b662015-07-30 15:53:47 -0700105 mNextExportReduceSlot = 0;
Jean-Luc Brouillet2e205d02014-06-02 21:06:52 -0700106 }
Shih-wei Liao9c631ff2010-09-17 11:57:29 -0700107
Jean-Luc Brouillet2e205d02014-06-02 21:06:52 -0700108public:
109 typedef enum {
110 AM_Public,
111 AM_Protected,
112 AM_Private,
113 AM_PublicSynchronized
114 } AccessModifier;
Zonr Chang8c6d9b22010-10-07 18:01:19 +0800115
Jean-Luc Brouillet2e205d02014-06-02 21:06:52 -0700116 // Generated RS Elements for type-checking code.
117 std::set<std::string> mTypesToCheck;
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700118
Jean-Luc Brouillet2e205d02014-06-02 21:06:52 -0700119 // Generated FieldPackers for unsigned setters/validation.
120 std::set<std::string> mFieldPackerTypes;
Stephen Hines48b72bf2011-06-10 15:37:27 -0700121
Jean-Luc Brouillet2e205d02014-06-02 21:06:52 -0700122 bool addTypeNameForElement(const std::string &TypeName);
123 bool addTypeNameForFieldPacker(const std::string &TypeName);
Stephen Hines1f6c3312012-07-03 17:23:33 -0700124
Jean-Luc Brouillet2e205d02014-06-02 21:06:52 -0700125 static const char *AccessModifierStr(AccessModifier AM);
Stephen Hines1f6c3312012-07-03 17:23:33 -0700126
Jean-Luc Brouillet2e205d02014-06-02 21:06:52 -0700127 inline bool getEmbedBitcodeInJava() const { return mEmbedBitcodeInJava; }
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700128
Jean-Luc Brouillet2e205d02014-06-02 21:06:52 -0700129 inline int getNextExportVarSlot() { return mNextExportVarSlot++; }
Jean-Luc Brouillet2e205d02014-06-02 21:06:52 -0700130 inline int getNextExportFuncSlot() { return mNextExportFuncSlot++; }
131 inline int getNextExportForEachSlot() { return mNextExportForEachSlot++; }
Matt Wala7682b662015-07-30 15:53:47 -0700132 inline int getNextExportReduceSlot() { return mNextExportReduceSlot++; }
Stephen Hines44d495d2014-05-22 19:42:55 -0700133
Jean-Luc Brouillet2e205d02014-06-02 21:06:52 -0700134 bool startClass(AccessModifier AM, bool IsStatic,
135 const std::string &ClassName, const char *SuperClassName,
136 std::string &ErrorMsg);
137 void endClass();
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700138
Jean-Luc Brouillet2e205d02014-06-02 21:06:52 -0700139 void startFunction(AccessModifier AM, bool IsStatic, const char *ReturnType,
140 const std::string &FunctionName, int Argc, ...);
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700141
Jean-Luc Brouillet2e205d02014-06-02 21:06:52 -0700142 typedef std::vector<std::pair<std::string, std::string>> ArgTy;
143 void startFunction(AccessModifier AM, bool IsStatic, const char *ReturnType,
144 const std::string &FunctionName, const ArgTy &Args);
145 void endFunction();
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700146
Jean-Luc Brouillet2e205d02014-06-02 21:06:52 -0700147 inline const std::string &getPackageName() const { return mPackageName; }
148 inline const std::string &getRSPackageName() const { return mRSPackageName; }
149 inline const std::string &getClassName() const { return mClassName; }
150 inline const std::string &getResourceId() const { return mResourceId; }
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700151
Jean-Luc Brouillet2e205d02014-06-02 21:06:52 -0700152 void startTypeClass(const std::string &ClassName);
153 void endTypeClass();
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700154
Jean-Luc Brouillet2e205d02014-06-02 21:06:52 -0700155 inline void incFieldIndex() { mFieldIndex++; }
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700156
Jean-Luc Brouillet2e205d02014-06-02 21:06:52 -0700157 inline void resetFieldIndex() { mFieldIndex = 0; }
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700158
Jean-Luc Brouillet2e205d02014-06-02 21:06:52 -0700159 inline void addFieldIndexMapping(const RSExportRecordType::Field *F) {
160 slangAssert((mFieldIndexMap.find(F) == mFieldIndexMap.end()) &&
161 "Nested structure never occurs in C language.");
162 mFieldIndexMap.insert(std::make_pair(F, mFieldIndex));
163 }
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700164
Jean-Luc Brouillet2e205d02014-06-02 21:06:52 -0700165 inline unsigned getFieldIndex(const RSExportRecordType::Field *F) const {
166 FieldIndexMapTy::const_iterator I = mFieldIndexMap.find(F);
167 slangAssert((I != mFieldIndexMap.end()) &&
168 "Requesting field is out of scope.");
169 return I->second;
170 }
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700171
Jean-Luc Brouillet2e205d02014-06-02 21:06:52 -0700172 inline void clearFieldIndexMap() { mFieldIndexMap.clear(); }
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700173
Jean-Luc Brouillet2e205d02014-06-02 21:06:52 -0700174private:
175 bool genScriptClass(const std::string &ClassName, std::string &ErrorMsg);
176 void genScriptClassConstructor();
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700177
Jean-Luc Brouillet2e205d02014-06-02 21:06:52 -0700178 void genInitBoolExportVariable(const std::string &VarName,
179 const clang::APValue &Val);
180 void genInitPrimitiveExportVariable(const std::string &VarName,
181 const clang::APValue &Val);
182 void genInitExportVariable(const RSExportType *ET, const std::string &VarName,
183 const clang::APValue &Val);
Jean-Luc Brouilletefcff102014-06-03 16:13:51 -0700184 void genInitValue(const clang::APValue &Val, bool asBool);
Jean-Luc Brouillet2e205d02014-06-02 21:06:52 -0700185 void genExportVariable(const RSExportVar *EV);
186 void genPrimitiveTypeExportVariable(const RSExportVar *EV);
187 void genPointerTypeExportVariable(const RSExportVar *EV);
188 void genVectorTypeExportVariable(const RSExportVar *EV);
189 void genMatrixTypeExportVariable(const RSExportVar *EV);
190 void genConstantArrayTypeExportVariable(const RSExportVar *EV);
191 void genRecordTypeExportVariable(const RSExportVar *EV);
192 void genPrivateExportVariable(const std::string &TypeName,
Stephen Hines0d26cef2012-05-01 19:23:01 -0700193 const std::string &VarName);
Jean-Luc Brouillet2e205d02014-06-02 21:06:52 -0700194 void genSetExportVariable(const std::string &TypeName, const RSExportVar *EV);
195 void genGetExportVariable(const std::string &TypeName,
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700196 const std::string &VarName);
Jean-Luc Brouillet2e205d02014-06-02 21:06:52 -0700197 void genGetFieldID(const std::string &VarName);
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700198
Jean-Luc Brouillet2e205d02014-06-02 21:06:52 -0700199 void genExportFunction(const RSExportFunc *EF);
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700200
Jean-Luc Brouillet2e205d02014-06-02 21:06:52 -0700201 void genExportForEach(const RSExportForEach *EF);
Stephen Hines593a8942011-05-10 15:29:50 -0700202
Matt Wala7682b662015-07-30 15:53:47 -0700203 void genExportReduce(const RSExportReduce *ER);
204 void genExportReduceAllocationVariant(const RSExportType *Type,
205 const std::string &KernelName);
206 void genExportReduceArrayVariant(const RSExportType *Type,
207 const std::string &KernelName);
208
Jean-Luc Brouillet2e205d02014-06-02 21:06:52 -0700209 void genTypeCheck(const RSExportType *ET, const char *VarName);
Stephen Hines48b72bf2011-06-10 15:37:27 -0700210
Jean-Luc Brouillet2e205d02014-06-02 21:06:52 -0700211 void genTypeInstanceFromPointer(const RSExportType *ET);
Stephen Hinesa6b54142012-04-09 18:25:08 -0700212
Jean-Luc Brouillet2e205d02014-06-02 21:06:52 -0700213 void genTypeInstance(const RSExportType *ET);
Stephen Hinesb5a89fb2011-05-17 14:48:02 -0700214
Jean-Luc Brouillet2e205d02014-06-02 21:06:52 -0700215 void genFieldPackerInstance(const RSExportType *ET);
Stephen Hines1f6c3312012-07-03 17:23:33 -0700216
Jean-Luc Brouillet2e205d02014-06-02 21:06:52 -0700217 bool genTypeClass(const RSExportRecordType *ERT, std::string &ErrorMsg);
218 void genTypeItemClass(const RSExportRecordType *ERT);
219 void genTypeClassConstructor(const RSExportRecordType *ERT);
220 void genTypeClassCopyToArray(const RSExportRecordType *ERT);
221 void genTypeClassCopyToArrayLocal(const RSExportRecordType *ERT);
222 void genTypeClassItemSetter(const RSExportRecordType *ERT);
223 void genTypeClassItemGetter(const RSExportRecordType *ERT);
224 void genTypeClassComponentSetter(const RSExportRecordType *ERT);
225 void genTypeClassComponentGetter(const RSExportRecordType *ERT);
226 void genTypeClassCopyAll(const RSExportRecordType *ERT);
227 void genTypeClassResize();
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700228
Jean-Luc Brouillet2e205d02014-06-02 21:06:52 -0700229 void genBuildElement(const char *ElementBuilderName,
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700230 const RSExportRecordType *ERT,
Jean-Luc Brouillet2ce118e2014-05-27 17:41:22 -0700231 const char *RenderScriptVar, bool IsInline);
Jean-Luc Brouillet2e205d02014-06-02 21:06:52 -0700232 void genAddElementToElementBuilder(const RSExportType *ERT,
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700233 const std::string &VarName,
234 const char *ElementBuilderName,
Zonr Chang89273bd2010-10-14 20:57:38 +0800235 const char *RenderScriptVar,
236 unsigned ArraySize);
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700237
Jean-Luc Brouillet2e205d02014-06-02 21:06:52 -0700238 bool genCreateFieldPacker(const RSExportType *T, const char *FieldPackerName);
239 void genPackVarOfType(const RSExportType *T, const char *VarName,
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700240 const char *FieldPackerName);
Jean-Luc Brouillet2e205d02014-06-02 21:06:52 -0700241 void genAllocateVarOfType(const RSExportType *T, const std::string &VarName);
242 void genNewItemBufferIfNull(const char *Index);
243 void genNewItemBufferPackerIfNull();
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700244
Chris Wailesc9454af2014-06-13 17:25:40 -0700245 void genPairwiseDimCheck(std::string name0, std::string name1);
Matt Wala7682b662015-07-30 15:53:47 -0700246 void genVectorLengthCompatibilityCheck(const std::string &ArrayName, unsigned VecSize);
247 void genNullOrEmptyArrayCheck(const std::string &ArrayName);
248 void gen1DCheck(const std::string &Name);
Chris Wailesc9454af2014-06-13 17:25:40 -0700249
Jean-Luc Brouillet2ce118e2014-05-27 17:41:22 -0700250public:
Jean-Luc Brouillet59f22c32014-06-04 14:53:48 -0700251 RSReflectionJava(const RSContext *Context,
252 std::vector<std::string> *GeneratedFileNames,
253 const std::string &OutputBaseDirectory,
254 const std::string &RSSourceFilename,
255 const std::string &BitCodeFileName,
256 bool EmbedBitcodeInJava);
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700257
Jean-Luc Brouillet59f22c32014-06-04 14:53:48 -0700258 bool reflect();
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700259
260 inline const char *getLastError() const {
261 if (mLastError.empty())
Chris Wailes5abbe0e2014-08-12 15:58:29 -0700262 return nullptr;
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700263 else
264 return mLastError.c_str();
265 }
Jean-Luc Brouillet2ce118e2014-05-27 17:41:22 -0700266}; // class RSReflectionJava
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700267
Jean-Luc Brouillet2ce118e2014-05-27 17:41:22 -0700268} // namespace slang
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700269
Jean-Luc Brouillet2ce118e2014-05-27 17:41:22 -0700270#endif // _FRAMEWORKS_COMPILE_SLANG_SLANG_RS_REFLECTION_H_ NOLINT