Stephen Hines | 593a894 | 2011-05-10 15:29:50 -0700 | [diff] [blame] | 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 | |
| 30 | namespace clang { |
| 31 | class FunctionDecl; |
| 32 | } // namespace clang |
| 33 | |
| 34 | namespace slang { |
| 35 | |
| 36 | // Base class for reflecting control-side forEach (currently for root() |
| 37 | // functions that fit appropriate criteria) |
| 38 | class RSExportForEach : public RSExportable { |
| 39 | private: |
| 40 | std::string mName; |
| 41 | RSExportRecordType *mParamPacketType; |
Stephen Hines | b5a89fb | 2011-05-17 14:48:02 -0700 | [diff] [blame] | 42 | RSExportType *mInType; |
| 43 | RSExportType *mOutType; |
Stephen Hines | 593a894 | 2011-05-10 15:29:50 -0700 | [diff] [blame] | 44 | size_t numParams; |
| 45 | |
Stephen Hines | 4ccf75e | 2011-08-16 18:21:01 -0700 | [diff] [blame] | 46 | unsigned int mMetadataEncoding; |
| 47 | |
Stephen Hines | b5a89fb | 2011-05-17 14:48:02 -0700 | [diff] [blame] | 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 |
Stephen Hines | 593a894 | 2011-05-10 15:29:50 -0700 | [diff] [blame] | 57 | RSExportForEach(RSContext *Context, const llvm::StringRef &Name, |
| 58 | const clang::FunctionDecl *FD) |
| 59 | : RSExportable(Context, RSExportable::EX_FOREACH), |
Stephen Hines | b5a89fb | 2011-05-17 14:48:02 -0700 | [diff] [blame] | 60 | mName(Name.data(), Name.size()), mParamPacketType(NULL), mInType(NULL), |
Stephen Hines | 4ccf75e | 2011-08-16 18:21:01 -0700 | [diff] [blame] | 61 | mOutType(NULL), numParams(0), mMetadataEncoding(0), |
| 62 | mIn(NULL), mOut(NULL), mUsrData(NULL), |
Stephen Hines | b5a89fb | 2011-05-17 14:48:02 -0700 | [diff] [blame] | 63 | mX(NULL), mY(NULL), mZ(NULL), mAr(NULL) { |
Stephen Hines | 593a894 | 2011-05-10 15:29:50 -0700 | [diff] [blame] | 64 | return; |
| 65 | } |
| 66 | |
Stephen Hines | b5a89fb | 2011-05-17 14:48:02 -0700 | [diff] [blame] | 67 | bool validateAndConstructParams(RSContext *Context, |
| 68 | const clang::FunctionDecl *FD); |
| 69 | |
Stephen Hines | 593a894 | 2011-05-10 15:29:50 -0700 | [diff] [blame] | 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 | |
Stephen Hines | b5a89fb | 2011-05-17 14:48:02 -0700 | [diff] [blame] | 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 | } |
Stephen Hines | 593a894 | 2011-05-10 15:29:50 -0700 | [diff] [blame] | 105 | |
Stephen Hines | 4ccf75e | 2011-08-16 18:21:01 -0700 | [diff] [blame] | 106 | inline unsigned int getMetadataEncoding() const { |
| 107 | return mMetadataEncoding; |
| 108 | } |
| 109 | |
Stephen Hines | 593a894 | 2011-05-10 15:29:50 -0700 | [diff] [blame] | 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 | } |
Stephen Hines | b5a89fb | 2011-05-17 14:48:02 -0700 | [diff] [blame] | 117 | |
Stephen Hines | 593a894 | 2011-05-10 15:29:50 -0700 | [diff] [blame] | 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 | |
Stephen Hines | 688e64b | 2011-08-23 16:01:25 -0700 | [diff] [blame] | 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 | |
Stephen Hines | f736d5a | 2011-10-26 16:18:14 -0700 | [diff] [blame^] | 151 | static bool isRSForEachFunc(int targetAPI, const clang::FunctionDecl *FD); |
Stephen Hines | 593a894 | 2011-05-10 15:29:50 -0700 | [diff] [blame] | 152 | |
| 153 | inline static bool isSpecialRSFunc(const clang::FunctionDecl *FD) { |
Stephen Hines | 688e64b | 2011-08-23 16:01:25 -0700 | [diff] [blame] | 154 | return isRootRSFunc(FD) || isInitRSFunc(FD) || isDtorRSFunc(FD); |
Stephen Hines | 593a894 | 2011-05-10 15:29:50 -0700 | [diff] [blame] | 155 | } |
| 156 | |
Stephen Hines | f736d5a | 2011-10-26 16:18:14 -0700 | [diff] [blame^] | 157 | static bool validateSpecialFuncDecl(int targetAPI, |
| 158 | clang::Diagnostic *Diags, |
Stephen Hines | 593a894 | 2011-05-10 15:29:50 -0700 | [diff] [blame] | 159 | const clang::FunctionDecl *FD); |
| 160 | }; // RSExportForEach |
| 161 | |
| 162 | } // namespace slang |
| 163 | |
| 164 | #endif // _FRAMEWORKS_COMPILE_SLANG_SLANG_RS_EXPORT_FOREACH_H_ NOLINT |