blob: f401d197ba4ff9bf8a80ceefc6edd3b9fba4d049 [file] [log] [blame]
Stephen Hines593a8942011-05-10 15:29:50 -07001/*
Stephen Hines9999ec32012-02-10 18:22:14 -08002 * Copyright 2011-2012, The Android Open Source Project
Stephen Hines593a8942011-05-10 15:29:50 -07003 *
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"
Chris Wailesc9454af2014-06-13 17:25:40 -070021#include "llvm/ADT/SmallVector.h"
Stephen Hines593a8942011-05-10 15:29:50 -070022#include "llvm/Support/raw_ostream.h"
23
24#include "clang/AST/Decl.h"
25
26#include "slang_assert.h"
27#include "slang_rs_context.h"
28#include "slang_rs_exportable.h"
29#include "slang_rs_export_type.h"
30
31namespace clang {
32 class FunctionDecl;
33} // namespace clang
34
35namespace slang {
36
37// Base class for reflecting control-side forEach (currently for root()
38// functions that fit appropriate criteria)
39class RSExportForEach : public RSExportable {
Chris Wailesc9454af2014-06-13 17:25:40 -070040 public:
41
42 typedef llvm::SmallVectorImpl<const clang::ParmVarDecl*> InVec;
43 typedef llvm::SmallVectorImpl<const RSExportType*> InTypeVec;
44
45 typedef InVec::const_iterator InIter;
46 typedef InTypeVec::const_iterator InTypeIter;
47
Stephen Hines593a8942011-05-10 15:29:50 -070048 private:
49 std::string mName;
50 RSExportRecordType *mParamPacketType;
Chris Wailesc9454af2014-06-13 17:25:40 -070051 llvm::SmallVector<const RSExportType*, 16> mInTypes;
Stephen Hinesb5a89fb2011-05-17 14:48:02 -070052 RSExportType *mOutType;
Stephen Hines593a8942011-05-10 15:29:50 -070053 size_t numParams;
54
Stephen Hines7b51b552012-02-16 00:12:38 -080055 unsigned int mSignatureMetadata;
Stephen Hines4ccf75e2011-08-16 18:21:01 -070056
Chris Wailesc9454af2014-06-13 17:25:40 -070057 llvm::SmallVector<const clang::ParmVarDecl*, 16> mIns;
Stephen Hinesb5a89fb2011-05-17 14:48:02 -070058 const clang::ParmVarDecl *mOut;
59 const clang::ParmVarDecl *mUsrData;
60 const clang::ParmVarDecl *mX;
61 const clang::ParmVarDecl *mY;
Stephen Hinesb5a89fb2011-05-17 14:48:02 -070062
Stephen Hines9ca96e72012-09-13 16:57:06 -070063 clang::QualType mResultType; // return type (if present).
Jean-Luc Brouillet0f2a2392014-01-14 11:40:57 -080064 bool mHasReturnType; // does this kernel have a return type?
65 bool mIsKernelStyle; // is this a pass-by-value kernel?
Stephen Hines9ca96e72012-09-13 16:57:06 -070066
Stephen Hinesc17e1982012-02-22 12:30:45 -080067 bool mDummyRoot;
68
Stephen Hinesb5a89fb2011-05-17 14:48:02 -070069 // TODO(all): Add support for LOD/face when we have them
Stephen Hinesc17e1982012-02-22 12:30:45 -080070 RSExportForEach(RSContext *Context, const llvm::StringRef &Name)
Stephen Hines593a8942011-05-10 15:29:50 -070071 : RSExportable(Context, RSExportable::EX_FOREACH),
Chris Wailesc9454af2014-06-13 17:25:40 -070072 mName(Name.data(), Name.size()), mParamPacketType(NULL),
Stephen Hines7b51b552012-02-16 00:12:38 -080073 mOutType(NULL), numParams(0), mSignatureMetadata(0),
Chris Wailesc9454af2014-06-13 17:25:40 -070074 mOut(NULL), mUsrData(NULL), mX(NULL), mY(NULL),
Narayan Kamath50cab072014-03-25 15:06:36 +000075 mResultType(clang::QualType()), mHasReturnType(false),
Jean-Luc Brouillet0f2a2392014-01-14 11:40:57 -080076 mIsKernelStyle(false), mDummyRoot(false) {
Stephen Hines593a8942011-05-10 15:29:50 -070077 }
78
Stephen Hinesb5a89fb2011-05-17 14:48:02 -070079 bool validateAndConstructParams(RSContext *Context,
80 const clang::FunctionDecl *FD);
81
Jean-Luc Brouillet0f2a2392014-01-14 11:40:57 -080082 bool validateAndConstructOldStyleParams(RSContext *Context,
Jean-Luc Brouillet42f81b22014-01-16 21:50:52 -080083 const clang::FunctionDecl *FD);
Jean-Luc Brouillet0f2a2392014-01-14 11:40:57 -080084
Stephen Hines9ca96e72012-09-13 16:57:06 -070085 bool validateAndConstructKernelParams(RSContext *Context,
86 const clang::FunctionDecl *FD);
87
Jean-Luc Brouillet42f81b22014-01-16 21:50:52 -080088 bool validateIterationParameters(RSContext *Context,
89 const clang::FunctionDecl *FD,
90 size_t *IndexOfFirstIterator);
91
Jean-Luc Brouillet0f2a2392014-01-14 11:40:57 -080092 bool setSignatureMetadata(RSContext *Context,
93 const clang::FunctionDecl *FD);
Stephen Hines593a8942011-05-10 15:29:50 -070094 public:
95 static RSExportForEach *Create(RSContext *Context,
96 const clang::FunctionDecl *FD);
97
Stephen Hinesc17e1982012-02-22 12:30:45 -080098 static RSExportForEach *CreateDummyRoot(RSContext *Context);
99
Stephen Hines593a8942011-05-10 15:29:50 -0700100 inline const std::string &getName() const {
101 return mName;
102 }
103
104 inline size_t getNumParameters() const {
105 return numParams;
106 }
107
Chris Wailesc9454af2014-06-13 17:25:40 -0700108 inline bool hasIns() const {
109 return (!mIns.empty());
Stephen Hinesb5a89fb2011-05-17 14:48:02 -0700110 }
111
112 inline bool hasOut() const {
113 return (mOut != NULL);
114 }
115
116 inline bool hasUsrData() const {
117 return (mUsrData != NULL);
118 }
119
Stephen Hines9ca96e72012-09-13 16:57:06 -0700120 inline bool hasReturn() const {
Jean-Luc Brouillet0f2a2392014-01-14 11:40:57 -0800121 return mHasReturnType;
Stephen Hines9ca96e72012-09-13 16:57:06 -0700122 }
123
Chris Wailesc9454af2014-06-13 17:25:40 -0700124 inline const InVec& getIns() const {
125 return mIns;
126 }
127
128 inline const InTypeVec& getInTypes() const {
129 return mInTypes;
Stephen Hinesb5a89fb2011-05-17 14:48:02 -0700130 }
131
132 inline const RSExportType *getOutType() const {
133 return mOutType;
134 }
135
136 inline const RSExportRecordType *getParamPacketType() const {
137 return mParamPacketType;
138 }
Stephen Hines593a8942011-05-10 15:29:50 -0700139
Stephen Hines7b51b552012-02-16 00:12:38 -0800140 inline unsigned int getSignatureMetadata() const {
141 return mSignatureMetadata;
Stephen Hines4ccf75e2011-08-16 18:21:01 -0700142 }
143
Stephen Hinesc17e1982012-02-22 12:30:45 -0800144 inline bool isDummyRoot() const {
145 return mDummyRoot;
146 }
147
Stephen Hines593a8942011-05-10 15:29:50 -0700148 typedef RSExportRecordType::const_field_iterator const_param_iterator;
149
150 inline const_param_iterator params_begin() const {
151 slangAssert((mParamPacketType != NULL) &&
152 "Get parameter from export foreach having no parameter!");
153 return mParamPacketType->fields_begin();
154 }
Stephen Hinesb5a89fb2011-05-17 14:48:02 -0700155
Stephen Hines593a8942011-05-10 15:29:50 -0700156 inline const_param_iterator params_end() const {
157 slangAssert((mParamPacketType != NULL) &&
158 "Get parameter from export foreach having no parameter!");
159 return mParamPacketType->fields_end();
160 }
161
162 inline static bool isInitRSFunc(const clang::FunctionDecl *FD) {
163 if (!FD) {
164 return false;
165 }
166 const llvm::StringRef Name = FD->getName();
167 static llvm::StringRef FuncInit("init");
168 return Name.equals(FuncInit);
169 }
170
171 inline static bool isRootRSFunc(const clang::FunctionDecl *FD) {
172 if (!FD) {
173 return false;
174 }
175 const llvm::StringRef Name = FD->getName();
176 static llvm::StringRef FuncRoot("root");
177 return Name.equals(FuncRoot);
178 }
179
Stephen Hines688e64b2011-08-23 16:01:25 -0700180 inline static bool isDtorRSFunc(const clang::FunctionDecl *FD) {
181 if (!FD) {
182 return false;
183 }
184 const llvm::StringRef Name = FD->getName();
185 static llvm::StringRef FuncDtor(".rs.dtor");
186 return Name.equals(FuncDtor);
187 }
188
Chris Wailesc9454af2014-06-13 17:25:40 -0700189 static bool isGraphicsRootRSFunc(unsigned int targetAPI,
Stephen Hines9999ec32012-02-10 18:22:14 -0800190 const clang::FunctionDecl *FD);
191
Chris Wailesc9454af2014-06-13 17:25:40 -0700192 static bool isRSForEachFunc(unsigned int targetAPI, slang::RSContext *Context,
Stephen Hines089cde32012-12-07 19:19:10 -0800193 const clang::FunctionDecl *FD);
Stephen Hines593a8942011-05-10 15:29:50 -0700194
Chris Wailesc9454af2014-06-13 17:25:40 -0700195 inline static bool isSpecialRSFunc(unsigned int targetAPI,
Stephen Hines9999ec32012-02-10 18:22:14 -0800196 const clang::FunctionDecl *FD) {
197 return isGraphicsRootRSFunc(targetAPI, FD) || isInitRSFunc(FD) ||
198 isDtorRSFunc(FD);
Stephen Hines593a8942011-05-10 15:29:50 -0700199 }
200
Chris Wailesc9454af2014-06-13 17:25:40 -0700201 static bool validateSpecialFuncDecl(unsigned int targetAPI,
Jean-Luc Brouilletd3f75272014-01-16 18:20:28 -0800202 slang::RSContext *Context,
Stephen Hines593a8942011-05-10 15:29:50 -0700203 const clang::FunctionDecl *FD);
204}; // RSExportForEach
205
206} // namespace slang
207
208#endif // _FRAMEWORKS_COMPILE_SLANG_SLANG_RS_EXPORT_FOREACH_H_ NOLINT