blob: b6c4f01cd0219908a23cde34a83806a5448270e8 [file] [log] [blame]
Zonr Changc383a502010-10-12 01:52:08 +08001/*
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 Hinese639eb52010-11-08 19:27:20 -080017#ifndef _FRAMEWORKS_COMPILE_SLANG_SLANG_RS_EXPORT_FUNC_H_ // NOLINT
18#define _FRAMEWORKS_COMPILE_SLANG_SLANG_RS_EXPORT_FUNC_H_
Shih-wei Liao462aefd2010-06-04 15:32:04 -070019
20#include <list>
21#include <string>
22
zonr6315f762010-10-05 15:35:14 +080023#include "llvm/ADT/StringRef.h"
Loganbe274822011-02-16 22:02:54 +080024#include "llvm/Support/raw_ostream.h"
25
26#include "clang/AST/Decl.h"
zonr6315f762010-10-05 15:35:14 +080027
Stephen Hines6e6578a2011-02-07 18:05:48 -080028#include "slang_assert.h"
Zonr Chang0da0a7d2010-10-05 21:26:37 +080029#include "slang_rs_export_type.h"
Stephen Hinese639eb52010-11-08 19:27:20 -080030#include "slang_rs_exportable.h"
Zonr Chang0da0a7d2010-10-05 21:26:37 +080031
32namespace llvm {
33 class StructType;
34}
35
Shih-wei Liao462aefd2010-06-04 15:32:04 -070036namespace slang {
37
Zonr Chang0da0a7d2010-10-05 21:26:37 +080038class RSContext;
Shih-wei Liao462aefd2010-06-04 15:32:04 -070039
Zonr Changa41ce1d2010-10-06 02:23:12 +080040class RSExportFunc : public RSExportable {
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070041 friend class RSContext;
Shih-wei Liao462aefd2010-06-04 15:32:04 -070042
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070043 private:
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070044 std::string mName;
Shih-wei Liao3fa286b2011-02-10 11:04:44 -080045 std::string mMangledName;
46 bool mShouldMangle;
Zonr Chang0da0a7d2010-10-05 21:26:37 +080047 RSExportRecordType *mParamPacketType;
Shih-wei Liao462aefd2010-06-04 15:32:04 -070048
Shih-wei Liao3fa286b2011-02-10 11:04:44 -080049 RSExportFunc(RSContext *Context, const llvm::StringRef &Name,
50 const clang::FunctionDecl *FD)
David Gross63e61082017-07-13 14:17:29 -070051 : RSExportable(Context, RSExportable::EX_FUNC, FD->getLocation()),
Zonr Chang0da0a7d2010-10-05 21:26:37 +080052 mName(Name.data(), Name.size()),
Shih-wei Liao3fa286b2011-02-10 11:04:44 -080053 mMangledName(),
54 mShouldMangle(false),
Chris Wailes5abbe0e2014-08-12 15:58:29 -070055 mParamPacketType(nullptr) {
Shih-wei Liao3fa286b2011-02-10 11:04:44 -080056
57 mShouldMangle = Context->getMangleContext().shouldMangleDeclName(FD);
58
59 if (mShouldMangle) {
Loganbe274822011-02-16 22:02:54 +080060 llvm::raw_string_ostream BufStm(mMangledName);
61 Context->getMangleContext().mangleName(FD, BufStm);
62 BufStm.flush();
Shih-wei Liao3fa286b2011-02-10 11:04:44 -080063 }
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070064 }
Shih-wei Liao462aefd2010-06-04 15:32:04 -070065
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070066 public:
67 static RSExportFunc *Create(RSContext *Context,
68 const clang::FunctionDecl *FD);
Shih-wei Liao462aefd2010-06-04 15:32:04 -070069
Zonr Chang0da0a7d2010-10-05 21:26:37 +080070 typedef RSExportRecordType::const_field_iterator const_param_iterator;
Shih-wei Liao462aefd2010-06-04 15:32:04 -070071
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070072 inline const_param_iterator params_begin() const {
Chris Wailes5abbe0e2014-08-12 15:58:29 -070073 slangAssert((mParamPacketType != nullptr) &&
Stephen Hines6e6578a2011-02-07 18:05:48 -080074 "Get parameter from export function having no parameter!");
Zonr Chang0da0a7d2010-10-05 21:26:37 +080075 return mParamPacketType->fields_begin();
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070076 }
77 inline const_param_iterator params_end() const {
Chris Wailes5abbe0e2014-08-12 15:58:29 -070078 slangAssert((mParamPacketType != nullptr) &&
Stephen Hines6e6578a2011-02-07 18:05:48 -080079 "Get parameter from export function having no parameter!");
Zonr Chang0da0a7d2010-10-05 21:26:37 +080080 return mParamPacketType->fields_end();
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070081 }
Shih-wei Liao462aefd2010-06-04 15:32:04 -070082
Stephen Hinesf2174cf2011-02-09 23:21:37 -080083 inline const std::string &getName(bool mangle = true) const {
Shih-wei Liao3fa286b2011-02-10 11:04:44 -080084 return (mShouldMangle && mangle) ? mMangledName : mName;
85 }
Shih-wei Liao462aefd2010-06-04 15:32:04 -070086
Zonr Chang0da0a7d2010-10-05 21:26:37 +080087 inline bool hasParam() const
88 { return (mParamPacketType && !mParamPacketType->getFields().empty()); }
89 inline size_t getNumParameters() const
90 { return ((mParamPacketType) ? mParamPacketType->getFields().size() : 0); }
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070091
Zonr Chang0da0a7d2010-10-05 21:26:37 +080092 inline const RSExportRecordType *getParamPacketType() const
93 { return mParamPacketType; }
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070094
Zonr Chang0da0a7d2010-10-05 21:26:37 +080095 // Check whether the given ParamsPacket type (in LLVM type) is "size
96 // equivalent" to the one obtained from getParamPacketType(). If the @Params
Chris Wailes5abbe0e2014-08-12 15:58:29 -070097 // is nullptr, means there must be no any parameters.
Logan Chienab992e52011-07-20 22:06:52 +080098 bool checkParameterPacketType(llvm::StructType *ParamTy) const;
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070099}; // RSExportFunc
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700100
Zonr Chang0da0a7d2010-10-05 21:26:37 +0800101
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700102} // namespace slang
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700103
Stephen Hinese639eb52010-11-08 19:27:20 -0800104#endif // _FRAMEWORKS_COMPILE_SLANG_SLANG_RS_EXPORT_FUNC_H_ NOLINT