slang_rs_export_foreach.h revision fbfd7f5fd97458b6b23437556025ac1d55e98fd4
1/*
2 * Copyright 2011, 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_EXPORT_FOREACH_H_  // NOLINT
18#define _FRAMEWORKS_COMPILE_SLANG_SLANG_RS_EXPORT_FOREACH_H_
19
20#include "llvm/ADT/StringRef.h"
21#include "llvm/Support/raw_ostream.h"
22
23#include "clang/AST/Decl.h"
24
25#include "slang_assert.h"
26#include "slang_rs_context.h"
27#include "slang_rs_exportable.h"
28#include "slang_rs_export_type.h"
29
30namespace clang {
31  class FunctionDecl;
32}  // namespace clang
33
34namespace slang {
35
36// Base class for reflecting control-side forEach (currently for root()
37// functions that fit appropriate criteria)
38class RSExportForEach : public RSExportable {
39 private:
40  std::string mName;
41  RSExportRecordType *mParamPacketType;
42  RSExportType *mInType;
43  RSExportType *mOutType;
44  size_t numParams;
45
46  unsigned int mMetadataEncoding;
47
48  const clang::ParmVarDecl *mIn;
49  const clang::ParmVarDecl *mOut;
50  const clang::ParmVarDecl *mUsrData;
51  const clang::ParmVarDecl *mX;
52  const clang::ParmVarDecl *mY;
53  const clang::ParmVarDecl *mZ;
54  const clang::ParmVarDecl *mAr;
55
56  // TODO(all): Add support for LOD/face when we have them
57  RSExportForEach(RSContext *Context, const llvm::StringRef &Name,
58         const clang::FunctionDecl *FD)
59    : RSExportable(Context, RSExportable::EX_FOREACH),
60      mName(Name.data(), Name.size()), mParamPacketType(NULL), mInType(NULL),
61      mOutType(NULL), numParams(0), mMetadataEncoding(0),
62      mIn(NULL), mOut(NULL), mUsrData(NULL),
63      mX(NULL), mY(NULL), mZ(NULL), mAr(NULL) {
64    return;
65  }
66
67  bool validateAndConstructParams(RSContext *Context,
68                                  const clang::FunctionDecl *FD);
69
70 public:
71  static RSExportForEach *Create(RSContext *Context,
72                                 const clang::FunctionDecl *FD);
73
74  inline const std::string &getName() const {
75    return mName;
76  }
77
78  inline size_t getNumParameters() const {
79    return numParams;
80  }
81
82  inline bool hasIn() const {
83    return (mIn != NULL);
84  }
85
86  inline bool hasOut() const {
87    return (mOut != NULL);
88  }
89
90  inline bool hasUsrData() const {
91    return (mUsrData != NULL);
92  }
93
94  inline const RSExportType *getInType() const {
95    return mInType;
96  }
97
98  inline const RSExportType *getOutType() const {
99    return mOutType;
100  }
101
102  inline const RSExportRecordType *getParamPacketType() const {
103    return mParamPacketType;
104  }
105
106  inline unsigned int getMetadataEncoding() const {
107    return mMetadataEncoding;
108  }
109
110  typedef RSExportRecordType::const_field_iterator const_param_iterator;
111
112  inline const_param_iterator params_begin() const {
113    slangAssert((mParamPacketType != NULL) &&
114                "Get parameter from export foreach having no parameter!");
115    return mParamPacketType->fields_begin();
116  }
117
118  inline const_param_iterator params_end() const {
119    slangAssert((mParamPacketType != NULL) &&
120                "Get parameter from export foreach having no parameter!");
121    return mParamPacketType->fields_end();
122  }
123
124  inline static bool isInitRSFunc(const clang::FunctionDecl *FD) {
125    if (!FD) {
126      return false;
127    }
128    const llvm::StringRef Name = FD->getName();
129    static llvm::StringRef FuncInit("init");
130    return Name.equals(FuncInit);
131  }
132
133  inline static bool isRootRSFunc(const clang::FunctionDecl *FD) {
134    if (!FD) {
135      return false;
136    }
137    const llvm::StringRef Name = FD->getName();
138    static llvm::StringRef FuncRoot("root");
139    return Name.equals(FuncRoot);
140  }
141
142  inline static bool isDtorRSFunc(const clang::FunctionDecl *FD) {
143    if (!FD) {
144      return false;
145    }
146    const llvm::StringRef Name = FD->getName();
147    static llvm::StringRef FuncDtor(".rs.dtor");
148    return Name.equals(FuncDtor);
149  }
150
151  static bool isRSForEachFunc(int targetAPI, const clang::FunctionDecl *FD);
152
153  inline static bool isSpecialRSFunc(const clang::FunctionDecl *FD) {
154    return isRootRSFunc(FD) || isInitRSFunc(FD) || isDtorRSFunc(FD);
155  }
156
157  static bool validateSpecialFuncDecl(int targetAPI,
158                                      clang::DiagnosticsEngine *DiagEngine,
159                                      const clang::FunctionDecl *FD);
160};  // RSExportForEach
161
162}  // namespace slang
163
164#endif  // _FRAMEWORKS_COMPILE_SLANG_SLANG_RS_EXPORT_FOREACH_H_  NOLINT
165