blob: c613d79d232ca23b60b3a8abc1602a4e308b5bde [file] [log] [blame]
Jason Sams1b6a0882012-03-12 15:07:58 -07001/*
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
Jean-Luc Brouilletefcff102014-06-03 16:13:51 -070017#ifndef _FRAMEWORKS_COMPILE_SLANG_SLANG_RS_REFLECTION_CPP_H_ // NOLINT
Jason Sams1b6a0882012-03-12 15:07:58 -070018#define _FRAMEWORKS_COMPILE_SLANG_SLANG_RS_REFLECTION_CPP_H_
19
Jean-Luc Brouillet1f9d1212014-06-02 20:27:29 -070020#include "slang_rs_reflect_utils.h"
Jason Sams1b6a0882012-03-12 15:07:58 -070021
Stephen Hines003ac662013-08-21 00:37:51 -070022#include <set>
23#include <string>
24
Jean-Luc Brouillet2ce118e2014-05-27 17:41:22 -070025#define RS_EXPORT_VAR_PREFIX "mExportVar_"
Stephen Hines80706832013-08-28 18:08:57 -070026
Jason Sams1b6a0882012-03-12 15:07:58 -070027namespace slang {
28
Jean-Luc Brouilletefcff102014-06-03 16:13:51 -070029class RSReflectionCpp {
30 public:
Jean-Luc Brouillet59f22c32014-06-04 14:53:48 -070031 RSReflectionCpp(const RSContext *Context, const std::string &OutputDirectory,
32 const std::string &RSSourceFileName,
33 const std::string &BitCodeFileName);
Stephen Hines02a98262012-11-14 12:40:26 -080034 virtual ~RSReflectionCpp();
35
Jean-Luc Brouillet59f22c32014-06-04 14:53:48 -070036 bool reflect();
Jason Sams1b6a0882012-03-12 15:07:58 -070037
Jean-Luc Brouilletefcff102014-06-03 16:13:51 -070038 private:
Matt Wala1c6b9272015-08-03 14:06:07 -070039 struct Argument {
40 std::string Type;
41 std::string Name;
42 std::string DefaultValue;
43 Argument(std::string Type, std::string Name, std::string DefaultValue = "")
44 : Type(Type), Name(Name), DefaultValue(DefaultValue) {}
45 };
46 typedef std::vector<Argument> ArgumentList;
Jean-Luc Brouilletefcff102014-06-03 16:13:51 -070047
48 // Information coming from the compiler about the code we're reflecting.
49 const RSContext *mRSContext;
50
51 // Path to the *.rs file for which we're generating C++ code.
52 std::string mRSSourceFilePath;
53 // Path to the file that contains the byte code generated from the *.rs file.
54 std::string mBitCodeFilePath;
55 // The directory where we'll generate the C++ files.
56 std::string mOutputDirectory;
57 // A cleaned up version of the *.rs file name that can be used in generating
58 // C++ identifiers.
59 std::string mCleanedRSFileName;
60 // The name of the generated C++ class.
61 std::string mClassName;
62
63 // TODO document
Stephen Hines7dd6da22012-11-15 19:56:03 -080064 unsigned int mNextExportVarSlot;
65 unsigned int mNextExportFuncSlot;
66 unsigned int mNextExportForEachSlot;
67
Jean-Luc Brouilletefcff102014-06-03 16:13:51 -070068 // Generated RS Elements for type-checking code.
69 std::set<std::string> mTypesToCheck;
70
Stephen Hines7dd6da22012-11-15 19:56:03 -080071 inline void clear() {
72 mNextExportVarSlot = 0;
73 mNextExportFuncSlot = 0;
74 mNextExportForEachSlot = 0;
Stephen Hines003ac662013-08-21 00:37:51 -070075 mTypesToCheck.clear();
Stephen Hines7dd6da22012-11-15 19:56:03 -080076 }
77
Jean-Luc Brouillet1f9d1212014-06-02 20:27:29 -070078 // The file we are currently generating, either the header or the
79 // implementation file.
80 GeneratedFile mOut;
81
Jean-Luc Brouilletefcff102014-06-03 16:13:51 -070082 void genInitValue(const clang::APValue &Val, bool asBool = false);
83 static const char *getVectorAccessor(unsigned index);
84
Jean-Luc Brouillet2ce118e2014-05-27 17:41:22 -070085 inline unsigned int getNextExportVarSlot() { return mNextExportVarSlot++; }
Stephen Hines7dd6da22012-11-15 19:56:03 -080086
Jean-Luc Brouillet2ce118e2014-05-27 17:41:22 -070087 inline unsigned int getNextExportFuncSlot() { return mNextExportFuncSlot++; }
Stephen Hines7dd6da22012-11-15 19:56:03 -080088
89 inline unsigned int getNextExportForEachSlot() {
90 return mNextExportForEachSlot++;
91 }
92
Jean-Luc Brouilleteb8b99e2014-06-03 20:59:01 -070093 bool writeHeaderFile();
94 bool writeImplementationFile();
Matt Wala1c6b9272015-08-03 14:06:07 -070095
96 // Write out signatures both in the header and implementation.
Jean-Luc Brouillet1f9d1212014-06-02 20:27:29 -070097 void makeFunctionSignature(bool isDefinition, const RSExportFunc *ef);
Matt Wala1c6b9272015-08-03 14:06:07 -070098
Jean-Luc Brouilleteb8b99e2014-06-03 20:59:01 -070099 bool genEncodedBitCode();
Jean-Luc Brouillet1cea2712014-06-05 13:54:11 -0700100 void genFieldsToStoreExportVariableValues();
101 void genTypeInstancesUsedInForEach();
102 void genFieldsForAllocationTypeVerification();
Matt Wala1c6b9272015-08-03 14:06:07 -0700103
104 // Write out the code for the getters and setters.
Jean-Luc Brouillet1cea2712014-06-05 13:54:11 -0700105 void genExportVariablesGetterAndSetter();
Matt Wala1c6b9272015-08-03 14:06:07 -0700106
107 // Write out the code for the declaration of the kernel entry points.
Jean-Luc Brouillet1cea2712014-06-05 13:54:11 -0700108 void genForEachDeclarations();
109 void genExportFunctionDeclarations();
Jason Sams1b6a0882012-03-12 15:07:58 -0700110
Matt Wala1c6b9272015-08-03 14:06:07 -0700111 // Write out code for the definitions of the kernel entry points.
112 void genExportForEachBodies();
Matt Wala1c6b9272015-08-03 14:06:07 -0700113 void genExportFunctionBodies();
114
Stephen Hines02a98262012-11-14 12:40:26 -0800115 bool startScriptHeader();
Stephen Hines7dd6da22012-11-15 19:56:03 -0800116
Stephen Hines80706832013-08-28 18:08:57 -0700117 // Write out code for an export variable initialization.
Jean-Luc Brouillet2ce118e2014-05-27 17:41:22 -0700118 void genInitExportVariable(const RSExportType *ET, const std::string &VarName,
Stephen Hines80706832013-08-28 18:08:57 -0700119 const clang::APValue &Val);
120 void genZeroInitExportVariable(const std::string &VarName);
121 void genInitBoolExportVariable(const std::string &VarName,
122 const clang::APValue &Val);
123 void genInitPrimitiveExportVariable(const std::string &VarName,
124 const clang::APValue &Val);
125
Stephen Hines7dd6da22012-11-15 19:56:03 -0800126 // Produce an argument string of the form "T1 t, T2 u, T3 v".
Jean-Luc Brouilleteb8b99e2014-06-03 20:59:01 -0700127 void genArguments(const ArgumentList &Args, int Offset);
Stephen Hines7dd6da22012-11-15 19:56:03 -0800128
Stephen Hines7dd6da22012-11-15 19:56:03 -0800129 void genPointerTypeExportVariable(const RSExportVar *EV);
Stephen Hines7dd6da22012-11-15 19:56:03 -0800130 void genMatrixTypeExportVariable(const RSExportVar *EV);
Stephen Hines7dd6da22012-11-15 19:56:03 -0800131 void genRecordTypeExportVariable(const RSExportVar *EV);
132
Jean-Luc Brouillet1cea2712014-06-05 13:54:11 -0700133 void genGetterAndSetter(const RSExportPrimitiveType *EPT, const RSExportVar* EV);
134 void genGetterAndSetter(const RSExportVectorType *EVT, const RSExportVar* EV);
135 void genGetterAndSetter(const RSExportConstantArrayType *AT, const RSExportVar* EV);
136 void genGetterAndSetter(const RSExportRecordType *ERT, const RSExportVar *EV);
137
Stephen Hines7dd6da22012-11-15 19:56:03 -0800138 // Write out a local FieldPacker (if necessary).
Jean-Luc Brouillet2ce118e2014-05-27 17:41:22 -0700139 bool genCreateFieldPacker(const RSExportType *T, const char *FieldPackerName);
Stephen Hines7dd6da22012-11-15 19:56:03 -0800140
141 // Populate (write) the FieldPacker with add() operations.
Jean-Luc Brouillet2ce118e2014-05-27 17:41:22 -0700142 void genPackVarOfType(const RSExportType *ET, const char *VarName,
Stephen Hines7dd6da22012-11-15 19:56:03 -0800143 const char *FieldPackerName);
Stephen Hines003ac662013-08-21 00:37:51 -0700144
145 // Generate a runtime type check for VarName.
146 void genTypeCheck(const RSExportType *ET, const char *VarName);
147
Matt Wala1c6b9272015-08-03 14:06:07 -0700148 // Generate a type instance for a given type.
Stephen Hines003ac662013-08-21 00:37:51 -0700149 void genTypeInstanceFromPointer(const RSExportType *ET);
150 void genTypeInstance(const RSExportType *ET);
151
Jean-Luc Brouillet2ce118e2014-05-27 17:41:22 -0700152}; // class RSReflectionCpp
Jason Sams1b6a0882012-03-12 15:07:58 -0700153
Jean-Luc Brouillet2ce118e2014-05-27 17:41:22 -0700154} // namespace slang
Jason Sams1b6a0882012-03-12 15:07:58 -0700155
Jean-Luc Brouillet2ce118e2014-05-27 17:41:22 -0700156#endif // _FRAMEWORKS_COMPILE_SLANG_SLANG_RS_REFLECTION_CPP_H_ NOLINT