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