slang_rs_reflection_cpp.h 5.4 KB

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