1 /* 2 * Copyright 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_CPP_H_ // NOLINT 18 #define _FRAMEWORKS_COMPILE_SLANG_SLANG_RS_REFLECTION_CPP_H_ 19 20 #include "slang_rs_reflect_utils.h" 21 22 #include <set> 23 #include <string> 24 25 #define RS_EXPORT_VAR_PREFIX "mExportVar_" 26 27 namespace slang { 28 29 class RSReflectionCpp { 30 public: 31 RSReflectionCpp(const RSContext *Context, const std::string &OutputDirectory, 32 const std::string &RSSourceFileName, 33 const std::string &BitCodeFileName); 34 virtual ~RSReflectionCpp(); 35 36 bool reflect(); 37 38 private: 39 // List of of (type, name) pairs. 40 typedef std::vector<std::pair<std::string, std::string> > ArgumentList; 41 42 // Information coming from the compiler about the code we're reflecting. 43 const RSContext *mRSContext; 44 45 // Path to the *.rs file for which we're generating C++ code. 46 std::string mRSSourceFilePath; 47 // Path to the file that contains the byte code generated from the *.rs file. 48 std::string mBitCodeFilePath; 49 // The directory where we'll generate the C++ files. 50 std::string mOutputDirectory; 51 // A cleaned up version of the *.rs file name that can be used in generating 52 // C++ identifiers. 53 std::string mCleanedRSFileName; 54 // The name of the generated C++ class. 55 std::string mClassName; 56 57 // TODO document 58 unsigned int mNextExportVarSlot; 59 unsigned int mNextExportFuncSlot; 60 unsigned int mNextExportForEachSlot; 61 62 // Generated RS Elements for type-checking code. 63 std::set<std::string> mTypesToCheck; 64 clear()65 inline void clear() { 66 mNextExportVarSlot = 0; 67 mNextExportFuncSlot = 0; 68 mNextExportForEachSlot = 0; 69 mTypesToCheck.clear(); 70 } 71 72 // The file we are currently generating, either the header or the 73 // implementation file. 74 GeneratedFile mOut; 75 76 void genInitValue(const clang::APValue &Val, bool asBool = false); 77 static const char *getVectorAccessor(unsigned index); 78 getNextExportVarSlot()79 inline unsigned int getNextExportVarSlot() { return mNextExportVarSlot++; } 80 getNextExportFuncSlot()81 inline unsigned int getNextExportFuncSlot() { return mNextExportFuncSlot++; } 82 getNextExportForEachSlot()83 inline unsigned int getNextExportForEachSlot() { 84 return mNextExportForEachSlot++; 85 } 86 87 bool writeHeaderFile(); 88 bool writeImplementationFile(); 89 void makeFunctionSignature(bool isDefinition, const RSExportFunc *ef); 90 bool genEncodedBitCode(); 91 void genFieldsToStoreExportVariableValues(); 92 void genTypeInstancesUsedInForEach(); 93 void genFieldsForAllocationTypeVerification(); 94 void genExportVariablesGetterAndSetter(); 95 void genForEachDeclarations(); 96 void genExportFunctionDeclarations(); 97 98 bool startScriptHeader(); 99 100 // Write out code for an export variable initialization. 101 void genInitExportVariable(const RSExportType *ET, const std::string &VarName, 102 const clang::APValue &Val); 103 void genZeroInitExportVariable(const std::string &VarName); 104 void genInitBoolExportVariable(const std::string &VarName, 105 const clang::APValue &Val); 106 void genInitPrimitiveExportVariable(const std::string &VarName, 107 const clang::APValue &Val); 108 109 // Produce an argument string of the form "T1 t, T2 u, T3 v". 110 void genArguments(const ArgumentList &Args, int Offset); 111 112 void genPointerTypeExportVariable(const RSExportVar *EV); 113 void genMatrixTypeExportVariable(const RSExportVar *EV); 114 void genRecordTypeExportVariable(const RSExportVar *EV); 115 116 void genGetterAndSetter(const RSExportPrimitiveType *EPT, const RSExportVar* EV); 117 void genGetterAndSetter(const RSExportVectorType *EVT, const RSExportVar* EV); 118 void genGetterAndSetter(const RSExportConstantArrayType *AT, const RSExportVar* EV); 119 void genGetterAndSetter(const RSExportRecordType *ERT, const RSExportVar *EV); 120 121 // Write out a local FieldPacker (if necessary). 122 bool genCreateFieldPacker(const RSExportType *T, const char *FieldPackerName); 123 124 // Populate (write) the FieldPacker with add() operations. 125 void genPackVarOfType(const RSExportType *ET, const char *VarName, 126 const char *FieldPackerName); 127 128 // Generate a runtime type check for VarName. 129 void genTypeCheck(const RSExportType *ET, const char *VarName); 130 131 // Generate a type instance for a given forEach argument type. 132 void genTypeInstanceFromPointer(const RSExportType *ET); 133 void genTypeInstance(const RSExportType *ET); 134 135 }; // class RSReflectionCpp 136 137 } // namespace slang 138 139 #endif // _FRAMEWORKS_COMPILE_SLANG_SLANG_RS_REFLECTION_CPP_H_ NOLINT 140