slang_rs_export_func.h revision f2174cfd6a556b51aadf2b8765e50df080e8f18e
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
17#ifndef _FRAMEWORKS_COMPILE_SLANG_SLANG_RS_EXPORT_FUNC_H_  // NOLINT
18#define _FRAMEWORKS_COMPILE_SLANG_SLANG_RS_EXPORT_FUNC_H_
19
20#include <list>
21#include <string>
22
23#include "llvm/ADT/StringRef.h"
24
25#include "slang_assert.h"
26#include "slang_rs_export_type.h"
27#include "slang_rs_exportable.h"
28
29namespace llvm {
30  class StructType;
31}
32
33namespace clang {
34  class FunctionDecl;
35}   // namespace clang
36
37namespace slang {
38
39class RSContext;
40
41class RSExportFunc : public RSExportable {
42  friend class RSContext;
43
44 private:
45  std::string mName;
46  std::string mMangledName;
47  bool mShouldMangle;
48  RSExportRecordType *mParamPacketType;
49
50  RSExportFunc(RSContext *Context, const llvm::StringRef &Name,
51               const clang::FunctionDecl *FD)
52    : RSExportable(Context, RSExportable::EX_FUNC),
53      mName(Name.data(), Name.size()),
54      mMangledName(),
55      mShouldMangle(false),
56      mParamPacketType(NULL) {
57
58    mShouldMangle = Context->getMangleContext().shouldMangleDeclName(FD);
59
60    if (mShouldMangle) {
61      llvm::SmallString<256> Buffer;
62      Context->getMangleContext().mangleName(FD, Buffer);
63
64      mMangledName = Buffer.str();
65    }
66
67    return;
68  }
69
70 public:
71  static RSExportFunc *Create(RSContext *Context,
72                              const clang::FunctionDecl *FD);
73
74  typedef RSExportRecordType::const_field_iterator const_param_iterator;
75
76  inline const_param_iterator params_begin() const {
77    slangAssert((mParamPacketType != NULL) &&
78                "Get parameter from export function having no parameter!");
79    return mParamPacketType->fields_begin();
80  }
81  inline const_param_iterator params_end() const {
82    slangAssert((mParamPacketType != NULL) &&
83                "Get parameter from export function having no parameter!");
84    return mParamPacketType->fields_end();
85  }
86
87  inline const std::string &getName(bool mangle = true) const {
88    return (mShouldMangle && mangle) ? mMangledName : mName;
89  }
90
91  inline bool hasParam() const
92    { return (mParamPacketType && !mParamPacketType->getFields().empty()); }
93  inline size_t getNumParameters() const
94    { return ((mParamPacketType) ? mParamPacketType->getFields().size() : 0); }
95
96  inline const RSExportRecordType *getParamPacketType() const
97    { return mParamPacketType; }
98
99  // Check whether the given ParamsPacket type (in LLVM type) is "size
100  // equivalent" to the one obtained from getParamPacketType(). If the @Params
101  // is NULL, means there must be no any parameters.
102  bool checkParameterPacketType(const llvm::StructType *ParamTy) const;
103};  // RSExportFunc
104
105
106}   // namespace slang
107
108#endif  // _FRAMEWORKS_COMPILE_SLANG_SLANG_RS_EXPORT_FUNC_H_  NOLINT
109