slang_rs_export_foreach.h revision 796e7b1400d3f3f7c07496d88bb48129ea925bb9
1/* 2 * Copyright 2011-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_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 mSignatureMetadata; 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 54 clang::QualType mResultType; // return type (if present). 55 bool mHasReturnType; // does this kernel have a return type? 56 bool mIsKernelStyle; // is this a pass-by-value kernel? 57 58 bool mDummyRoot; 59 60 // TODO(all): Add support for LOD/face when we have them 61 RSExportForEach(RSContext *Context, const llvm::StringRef &Name) 62 : RSExportable(Context, RSExportable::EX_FOREACH), 63 mName(Name.data(), Name.size()), mParamPacketType(NULL), mInType(NULL), 64 mOutType(NULL), numParams(0), mSignatureMetadata(0), 65 mIn(NULL), mOut(NULL), mUsrData(NULL), mX(NULL), mY(NULL), 66 mResultType(clang::QualType()), mHasReturnType(false), 67 mIsKernelStyle(false), mDummyRoot(false) { 68 } 69 70 bool validateAndConstructParams(RSContext *Context, 71 const clang::FunctionDecl *FD); 72 73 bool validateAndConstructOldStyleParams(RSContext *Context, 74 const clang::FunctionDecl *FD); 75 76 bool validateAndConstructKernelParams(RSContext *Context, 77 const clang::FunctionDecl *FD); 78 79 bool validateIterationParameters(RSContext *Context, 80 const clang::FunctionDecl *FD, 81 size_t *IndexOfFirstIterator); 82 83 bool setSignatureMetadata(RSContext *Context, 84 const clang::FunctionDecl *FD); 85 public: 86 static RSExportForEach *Create(RSContext *Context, 87 const clang::FunctionDecl *FD); 88 89 static RSExportForEach *CreateDummyRoot(RSContext *Context); 90 91 inline const std::string &getName() const { 92 return mName; 93 } 94 95 inline size_t getNumParameters() const { 96 return numParams; 97 } 98 99 inline bool hasIn() const { 100 return (mIn != NULL); 101 } 102 103 inline bool hasOut() const { 104 return (mOut != NULL); 105 } 106 107 inline bool hasUsrData() const { 108 return (mUsrData != NULL); 109 } 110 111 inline bool hasReturn() const { 112 return mHasReturnType; 113 } 114 115 inline const RSExportType *getInType() const { 116 return mInType; 117 } 118 119 inline const RSExportType *getOutType() const { 120 return mOutType; 121 } 122 123 inline const RSExportRecordType *getParamPacketType() const { 124 return mParamPacketType; 125 } 126 127 inline unsigned int getSignatureMetadata() const { 128 return mSignatureMetadata; 129 } 130 131 inline bool isDummyRoot() const { 132 return mDummyRoot; 133 } 134 135 typedef RSExportRecordType::const_field_iterator const_param_iterator; 136 137 inline const_param_iterator params_begin() const { 138 slangAssert((mParamPacketType != NULL) && 139 "Get parameter from export foreach having no parameter!"); 140 return mParamPacketType->fields_begin(); 141 } 142 143 inline const_param_iterator params_end() const { 144 slangAssert((mParamPacketType != NULL) && 145 "Get parameter from export foreach having no parameter!"); 146 return mParamPacketType->fields_end(); 147 } 148 149 inline static bool isInitRSFunc(const clang::FunctionDecl *FD) { 150 if (!FD) { 151 return false; 152 } 153 const llvm::StringRef Name = FD->getName(); 154 static llvm::StringRef FuncInit("init"); 155 return Name.equals(FuncInit); 156 } 157 158 inline static bool isRootRSFunc(const clang::FunctionDecl *FD) { 159 if (!FD) { 160 return false; 161 } 162 const llvm::StringRef Name = FD->getName(); 163 static llvm::StringRef FuncRoot("root"); 164 return Name.equals(FuncRoot); 165 } 166 167 inline static bool isDtorRSFunc(const clang::FunctionDecl *FD) { 168 if (!FD) { 169 return false; 170 } 171 const llvm::StringRef Name = FD->getName(); 172 static llvm::StringRef FuncDtor(".rs.dtor"); 173 return Name.equals(FuncDtor); 174 } 175 176 static bool isGraphicsRootRSFunc(int targetAPI, 177 const clang::FunctionDecl *FD); 178 179 static bool isRSForEachFunc(int targetAPI, slang::RSContext *Context, 180 const clang::FunctionDecl *FD); 181 182 inline static bool isSpecialRSFunc(int targetAPI, 183 const clang::FunctionDecl *FD) { 184 return isGraphicsRootRSFunc(targetAPI, FD) || isInitRSFunc(FD) || 185 isDtorRSFunc(FD); 186 } 187 188 static bool validateSpecialFuncDecl(int targetAPI, 189 slang::RSContext *Context, 190 const clang::FunctionDecl *FD); 191}; // RSExportForEach 192 193} // namespace slang 194 195#endif // _FRAMEWORKS_COMPILE_SLANG_SLANG_RS_EXPORT_FOREACH_H_ NOLINT 196