blob: 3c0804d50a641f7b4b6638577996088ae3316469 [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 clang {
zonr6315f762010-10-05 15:35:14 +080037 class FunctionDecl;
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070038} // namespace clang
Shih-wei Liao462aefd2010-06-04 15:32:04 -070039
40namespace slang {
41
Zonr Chang0da0a7d2010-10-05 21:26:37 +080042class RSContext;
Shih-wei Liao462aefd2010-06-04 15:32:04 -070043
Zonr Changa41ce1d2010-10-06 02:23:12 +080044class RSExportFunc : public RSExportable {
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070045 friend class RSContext;
Shih-wei Liao462aefd2010-06-04 15:32:04 -070046
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070047 private:
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070048 std::string mName;
Shih-wei Liao3fa286b2011-02-10 11:04:44 -080049 std::string mMangledName;
50 bool mShouldMangle;
Zonr Chang0da0a7d2010-10-05 21:26:37 +080051 RSExportRecordType *mParamPacketType;
Shih-wei Liao462aefd2010-06-04 15:32:04 -070052
Shih-wei Liao3fa286b2011-02-10 11:04:44 -080053 RSExportFunc(RSContext *Context, const llvm::StringRef &Name,
54 const clang::FunctionDecl *FD)
Zonr Changa41ce1d2010-10-06 02:23:12 +080055 : RSExportable(Context, RSExportable::EX_FUNC),
Zonr Chang0da0a7d2010-10-05 21:26:37 +080056 mName(Name.data(), Name.size()),
Shih-wei Liao3fa286b2011-02-10 11:04:44 -080057 mMangledName(),
58 mShouldMangle(false),
Chris Wailes5abbe0e2014-08-12 15:58:29 -070059 mParamPacketType(nullptr) {
Shih-wei Liao3fa286b2011-02-10 11:04:44 -080060
61 mShouldMangle = Context->getMangleContext().shouldMangleDeclName(FD);
62
63 if (mShouldMangle) {
Loganbe274822011-02-16 22:02:54 +080064 llvm::raw_string_ostream BufStm(mMangledName);
65 Context->getMangleContext().mangleName(FD, BufStm);
66 BufStm.flush();
Shih-wei Liao3fa286b2011-02-10 11:04:44 -080067 }
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070068 }
Shih-wei Liao462aefd2010-06-04 15:32:04 -070069
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070070 public:
71 static RSExportFunc *Create(RSContext *Context,
72 const clang::FunctionDecl *FD);
Shih-wei Liao462aefd2010-06-04 15:32:04 -070073
Zonr Chang0da0a7d2010-10-05 21:26:37 +080074 typedef RSExportRecordType::const_field_iterator const_param_iterator;
Shih-wei Liao462aefd2010-06-04 15:32:04 -070075
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070076 inline const_param_iterator params_begin() const {
Chris Wailes5abbe0e2014-08-12 15:58:29 -070077 slangAssert((mParamPacketType != nullptr) &&
Stephen Hines6e6578a2011-02-07 18:05:48 -080078 "Get parameter from export function having no parameter!");
Zonr Chang0da0a7d2010-10-05 21:26:37 +080079 return mParamPacketType->fields_begin();
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070080 }
81 inline const_param_iterator params_end() const {
Chris Wailes5abbe0e2014-08-12 15:58:29 -070082 slangAssert((mParamPacketType != nullptr) &&
Stephen Hines6e6578a2011-02-07 18:05:48 -080083 "Get parameter from export function having no parameter!");
Zonr Chang0da0a7d2010-10-05 21:26:37 +080084 return mParamPacketType->fields_end();
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070085 }
Shih-wei Liao462aefd2010-06-04 15:32:04 -070086
Stephen Hinesf2174cf2011-02-09 23:21:37 -080087 inline const std::string &getName(bool mangle = true) const {
Shih-wei Liao3fa286b2011-02-10 11:04:44 -080088 return (mShouldMangle && mangle) ? mMangledName : mName;
89 }
Shih-wei Liao462aefd2010-06-04 15:32:04 -070090
Zonr Chang0da0a7d2010-10-05 21:26:37 +080091 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 Liao9ef2f782010-10-01 12:31:37 -070095
Zonr Chang0da0a7d2010-10-05 21:26:37 +080096 inline const RSExportRecordType *getParamPacketType() const
97 { return mParamPacketType; }
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070098
Zonr Chang0da0a7d2010-10-05 21:26:37 +080099 // Check whether the given ParamsPacket type (in LLVM type) is "size
100 // equivalent" to the one obtained from getParamPacketType(). If the @Params
Chris Wailes5abbe0e2014-08-12 15:58:29 -0700101 // is nullptr, means there must be no any parameters.
Logan Chienab992e52011-07-20 22:06:52 +0800102 bool checkParameterPacketType(llvm::StructType *ParamTy) const;
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700103}; // RSExportFunc
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700104
Zonr Chang0da0a7d2010-10-05 21:26:37 +0800105
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700106} // namespace slang
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700107
Stephen Hinese639eb52010-11-08 19:27:20 -0800108#endif // _FRAMEWORKS_COMPILE_SLANG_SLANG_RS_EXPORT_FUNC_H_ NOLINT