slang_rs_reflection_cpp.h revision 80706836b18127b5733d790613a5d1b9f97cbb1d
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_reflection_base.h"
21
22#include <set>
23#include <string>
24
25#define RS_EXPORT_VAR_PREFIX             "mExportVar_"
26
27namespace slang {
28
29class RSReflectionCpp : public RSReflectionBase {
30 public:
31  explicit RSReflectionCpp(const RSContext *);
32  virtual ~RSReflectionCpp();
33
34  bool reflect(const std::string &OutputPathBase,
35               const std::string &InputFileName,
36               const std::string &OutputBCFileName);
37
38
39 private:
40  unsigned int mNextExportVarSlot;
41  unsigned int mNextExportFuncSlot;
42  unsigned int mNextExportForEachSlot;
43
44  inline void clear() {
45    mNextExportVarSlot = 0;
46    mNextExportFuncSlot = 0;
47    mNextExportForEachSlot = 0;
48    mTypesToCheck.clear();
49  }
50
51  inline unsigned int getNextExportVarSlot() {
52    return mNextExportVarSlot++;
53  }
54
55  inline unsigned int getNextExportFuncSlot() {
56    return mNextExportFuncSlot++;
57  }
58
59  inline unsigned int getNextExportForEachSlot() {
60    return mNextExportForEachSlot++;
61  }
62
63  bool makeHeader(const std::string &baseClass);
64  bool makeImpl(const std::string &baseClass);
65  void makeFunctionSignature(std::stringstream &ss, bool isDefinition,
66                             const RSExportFunc *ef);
67  bool writeBC();
68
69  bool startScriptHeader();
70
71
72  // Write out code for an export variable initialization.
73  void genInitExportVariable(const RSExportType *ET,
74                             const std::string &VarName,
75                             const clang::APValue &Val);
76  void genZeroInitExportVariable(const std::string &VarName);
77  void genInitBoolExportVariable(const std::string &VarName,
78                                 const clang::APValue &Val);
79  void genInitPrimitiveExportVariable(const std::string &VarName,
80                                      const clang::APValue &Val);
81
82  // Produce an argument string of the form "T1 t, T2 u, T3 v".
83  void makeArgs(std::stringstream &ss, const ArgTy& Args);
84
85  // Write out code for an export variable.
86  void genExportVariable(const RSExportVar *EV);
87
88  void genPrimitiveTypeExportVariable(const RSExportVar *EV);
89  void genPointerTypeExportVariable(const RSExportVar *EV);
90  void genVectorTypeExportVariable(const RSExportVar *EV);
91  void genMatrixTypeExportVariable(const RSExportVar *EV);
92  void genConstantArrayTypeExportVariable(const RSExportVar *EV);
93  void genRecordTypeExportVariable(const RSExportVar *EV);
94
95  // Write out a local FieldPacker (if necessary).
96  bool genCreateFieldPacker(const RSExportType *T,
97                            const char *FieldPackerName);
98
99  // Populate (write) the FieldPacker with add() operations.
100  void genPackVarOfType(const RSExportType *ET,
101                        const char *VarName,
102                        const char *FieldPackerName);
103
104  // Generate a runtime type check for VarName.
105  void genTypeCheck(const RSExportType *ET, const char *VarName);
106
107  // Generate a type instance for a given forEach argument type.
108  void genTypeInstanceFromPointer(const RSExportType *ET);
109  void genTypeInstance(const RSExportType *ET);
110
111};  // class RSReflectionCpp
112
113}   // namespace slang
114
115#endif  // _FRAMEWORKS_COMPILE_SLANG_SLANG_RS_REFLECTION_CPP_H_  NOLINT
116