Zonr Chang | c383a50 | 2010-10-12 01:52:08 +0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2010, 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 | |
Stephen Hines | e639eb5 | 2010-11-08 19:27:20 -0800 | [diff] [blame] | 17 | #ifndef _FRAMEWORKS_COMPILE_SLANG_SLANG_RS_EXPORT_FUNC_H_ // NOLINT |
| 18 | #define _FRAMEWORKS_COMPILE_SLANG_SLANG_RS_EXPORT_FUNC_H_ |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 19 | |
| 20 | #include <list> |
| 21 | #include <string> |
| 22 | |
zonr | 6315f76 | 2010-10-05 15:35:14 +0800 | [diff] [blame] | 23 | #include "llvm/ADT/StringRef.h" |
Logan | be27482 | 2011-02-16 22:02:54 +0800 | [diff] [blame] | 24 | #include "llvm/Support/raw_ostream.h" |
| 25 | |
| 26 | #include "clang/AST/Decl.h" |
zonr | 6315f76 | 2010-10-05 15:35:14 +0800 | [diff] [blame] | 27 | |
Stephen Hines | 6e6578a | 2011-02-07 18:05:48 -0800 | [diff] [blame] | 28 | #include "slang_assert.h" |
Zonr Chang | 0da0a7d | 2010-10-05 21:26:37 +0800 | [diff] [blame] | 29 | #include "slang_rs_export_type.h" |
Stephen Hines | e639eb5 | 2010-11-08 19:27:20 -0800 | [diff] [blame] | 30 | #include "slang_rs_exportable.h" |
Zonr Chang | 0da0a7d | 2010-10-05 21:26:37 +0800 | [diff] [blame] | 31 | |
| 32 | namespace llvm { |
| 33 | class StructType; |
| 34 | } |
| 35 | |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 36 | namespace clang { |
zonr | 6315f76 | 2010-10-05 15:35:14 +0800 | [diff] [blame] | 37 | class FunctionDecl; |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 38 | } // namespace clang |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 39 | |
| 40 | namespace slang { |
| 41 | |
Zonr Chang | 0da0a7d | 2010-10-05 21:26:37 +0800 | [diff] [blame] | 42 | class RSContext; |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 43 | |
Zonr Chang | a41ce1d | 2010-10-06 02:23:12 +0800 | [diff] [blame] | 44 | class RSExportFunc : public RSExportable { |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 45 | friend class RSContext; |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 46 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 47 | private: |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 48 | std::string mName; |
Shih-wei Liao | 3fa286b | 2011-02-10 11:04:44 -0800 | [diff] [blame] | 49 | std::string mMangledName; |
| 50 | bool mShouldMangle; |
Zonr Chang | 0da0a7d | 2010-10-05 21:26:37 +0800 | [diff] [blame] | 51 | RSExportRecordType *mParamPacketType; |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 52 | |
Shih-wei Liao | 3fa286b | 2011-02-10 11:04:44 -0800 | [diff] [blame] | 53 | RSExportFunc(RSContext *Context, const llvm::StringRef &Name, |
| 54 | const clang::FunctionDecl *FD) |
Zonr Chang | a41ce1d | 2010-10-06 02:23:12 +0800 | [diff] [blame] | 55 | : RSExportable(Context, RSExportable::EX_FUNC), |
Zonr Chang | 0da0a7d | 2010-10-05 21:26:37 +0800 | [diff] [blame] | 56 | mName(Name.data(), Name.size()), |
Shih-wei Liao | 3fa286b | 2011-02-10 11:04:44 -0800 | [diff] [blame] | 57 | mMangledName(), |
| 58 | mShouldMangle(false), |
Chris Wailes | 5abbe0e | 2014-08-12 15:58:29 -0700 | [diff] [blame] | 59 | mParamPacketType(nullptr) { |
Shih-wei Liao | 3fa286b | 2011-02-10 11:04:44 -0800 | [diff] [blame] | 60 | |
| 61 | mShouldMangle = Context->getMangleContext().shouldMangleDeclName(FD); |
| 62 | |
| 63 | if (mShouldMangle) { |
Logan | be27482 | 2011-02-16 22:02:54 +0800 | [diff] [blame] | 64 | llvm::raw_string_ostream BufStm(mMangledName); |
| 65 | Context->getMangleContext().mangleName(FD, BufStm); |
| 66 | BufStm.flush(); |
Shih-wei Liao | 3fa286b | 2011-02-10 11:04:44 -0800 | [diff] [blame] | 67 | } |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 68 | } |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 69 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 70 | public: |
| 71 | static RSExportFunc *Create(RSContext *Context, |
| 72 | const clang::FunctionDecl *FD); |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 73 | |
Zonr Chang | 0da0a7d | 2010-10-05 21:26:37 +0800 | [diff] [blame] | 74 | typedef RSExportRecordType::const_field_iterator const_param_iterator; |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 75 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 76 | inline const_param_iterator params_begin() const { |
Chris Wailes | 5abbe0e | 2014-08-12 15:58:29 -0700 | [diff] [blame] | 77 | slangAssert((mParamPacketType != nullptr) && |
Stephen Hines | 6e6578a | 2011-02-07 18:05:48 -0800 | [diff] [blame] | 78 | "Get parameter from export function having no parameter!"); |
Zonr Chang | 0da0a7d | 2010-10-05 21:26:37 +0800 | [diff] [blame] | 79 | return mParamPacketType->fields_begin(); |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 80 | } |
| 81 | inline const_param_iterator params_end() const { |
Chris Wailes | 5abbe0e | 2014-08-12 15:58:29 -0700 | [diff] [blame] | 82 | slangAssert((mParamPacketType != nullptr) && |
Stephen Hines | 6e6578a | 2011-02-07 18:05:48 -0800 | [diff] [blame] | 83 | "Get parameter from export function having no parameter!"); |
Zonr Chang | 0da0a7d | 2010-10-05 21:26:37 +0800 | [diff] [blame] | 84 | return mParamPacketType->fields_end(); |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 85 | } |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 86 | |
Stephen Hines | f2174cf | 2011-02-09 23:21:37 -0800 | [diff] [blame] | 87 | inline const std::string &getName(bool mangle = true) const { |
Shih-wei Liao | 3fa286b | 2011-02-10 11:04:44 -0800 | [diff] [blame] | 88 | return (mShouldMangle && mangle) ? mMangledName : mName; |
| 89 | } |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 90 | |
Zonr Chang | 0da0a7d | 2010-10-05 21:26:37 +0800 | [diff] [blame] | 91 | inline bool hasParam() const |
| 92 | { return (mParamPacketType && !mParamPacketType->getFields().empty()); } |
| 93 | inline size_t getNumParameters() const |
| 94 | { return ((mParamPacketType) ? mParamPacketType->getFields().size() : 0); } |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 95 | |
Zonr Chang | 0da0a7d | 2010-10-05 21:26:37 +0800 | [diff] [blame] | 96 | inline const RSExportRecordType *getParamPacketType() const |
| 97 | { return mParamPacketType; } |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 98 | |
Zonr Chang | 0da0a7d | 2010-10-05 21:26:37 +0800 | [diff] [blame] | 99 | // Check whether the given ParamsPacket type (in LLVM type) is "size |
| 100 | // equivalent" to the one obtained from getParamPacketType(). If the @Params |
Chris Wailes | 5abbe0e | 2014-08-12 15:58:29 -0700 | [diff] [blame] | 101 | // is nullptr, means there must be no any parameters. |
Logan Chien | ab992e5 | 2011-07-20 22:06:52 +0800 | [diff] [blame] | 102 | bool checkParameterPacketType(llvm::StructType *ParamTy) const; |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 103 | }; // RSExportFunc |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 104 | |
Zonr Chang | 0da0a7d | 2010-10-05 21:26:37 +0800 | [diff] [blame] | 105 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 106 | } // namespace slang |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 107 | |
Stephen Hines | e639eb5 | 2010-11-08 19:27:20 -0800 | [diff] [blame] | 108 | #endif // _FRAMEWORKS_COMPILE_SLANG_SLANG_RS_EXPORT_FUNC_H_ NOLINT |