blob: a85735cd04c0dcaba8652c1f7e31b010aabeb332 [file] [log] [blame]
Andreas Huber1aec3972016-08-26 09:26:32 -07001/*
2 * Copyright (C) 2016 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
Andreas Huberc9410c72016-07-28 12:18:40 -070017#include "Method.h"
18
Andreas Huber3599d922016-08-09 10:42:57 -070019#include "Annotation.h"
Andreas Huberc9410c72016-07-28 12:18:40 -070020#include "Formatter.h"
Iliyan Malchev40d474a2016-08-16 06:20:17 -070021#include "ScalarType.h"
Andreas Huberc9410c72016-07-28 12:18:40 -070022#include "Type.h"
23
24namespace android {
25
Andreas Huber3599d922016-08-09 10:42:57 -070026Method::Method(const char *name,
27 std::vector<TypedVar *> *args,
28 std::vector<TypedVar *> *results,
Iliyan Malchev639bff82016-08-13 14:24:11 -070029 bool oneway,
Zhuoyao Zhang5158db42016-08-10 10:25:20 -070030 AnnotationVector *annotations)
Andreas Huberc9410c72016-07-28 12:18:40 -070031 : mName(name),
32 mArgs(args),
Andreas Huber3599d922016-08-09 10:42:57 -070033 mResults(results),
Iliyan Malchev639bff82016-08-13 14:24:11 -070034 mOneway(oneway),
Andreas Huber3599d922016-08-09 10:42:57 -070035 mAnnotationsByName(annotations) {
Andreas Huberc9410c72016-07-28 12:18:40 -070036}
37
Andreas Huber881227d2016-08-02 14:20:21 -070038std::string Method::name() const {
39 return mName;
40}
41
42const std::vector<TypedVar *> &Method::args() const {
43 return *mArgs;
44}
45
46const std::vector<TypedVar *> &Method::results() const {
47 return *mResults;
48}
49
Zhuoyao Zhang5158db42016-08-10 10:25:20 -070050const AnnotationVector &Method::annotations() const {
Andreas Huber3599d922016-08-09 10:42:57 -070051 return *mAnnotationsByName;
52}
53
Andreas Huber881227d2016-08-02 14:20:21 -070054// static
55std::string Method::GetSignature(const std::vector<TypedVar *> &args) {
56 bool first = true;
57 std::string out;
58 for (const auto &arg : args) {
59 if (!first) {
60 out += ", ";
61 }
62
63 std::string extra;
64 out += arg->type().getCppArgumentType(&extra);
65 out += " ";
66 out += arg->name();
67 out += extra;
68
69 first = false;
70 }
71
72 return out;
73}
74
Andreas Huber2831d512016-08-15 09:33:47 -070075// static
76std::string Method::GetJavaSignature(const std::vector<TypedVar *> &args) {
77 bool first = true;
78 std::string out;
79 for (const auto &arg : args) {
80 if (!first) {
81 out += ", ";
82 }
83
84 std::string extra;
85 out += arg->type().getJavaType();
86 out += " ";
87 out += arg->name();
88 out += extra;
89
90 first = false;
91 }
92
93 return out;
94}
95
Andreas Huber3599d922016-08-09 10:42:57 -070096void Method::dumpAnnotations(Formatter &out) const {
97 if (mAnnotationsByName->size() == 0) {
98 return;
99 }
100
101 out << "// ";
102 for (size_t i = 0; i < mAnnotationsByName->size(); ++i) {
103 if (i > 0) {
104 out << " ";
105 }
106 mAnnotationsByName->valueAt(i)->dump(out);
107 }
108 out << "\n";
109}
110
Andreas Huber70a59e12016-08-16 12:57:01 -0700111bool Method::isJavaCompatible() const {
112 for (const auto &arg : *mArgs) {
113 if (!arg->isJavaCompatible()) {
114 return false;
115 }
116 }
117
118 for (const auto &result : *mResults) {
119 if (!result->isJavaCompatible()) {
120 return false;
121 }
122 }
123
124 return true;
125}
126
Iliyan Malchev40d474a2016-08-16 06:20:17 -0700127const TypedVar* Method::canElideCallback() const {
128 auto &res = results();
129
130 // Can't elide callback for void or tuple-returning methods
131 if (res.size() != 1) {
132 return nullptr;
133 }
134
135 const TypedVar *typedVar = res.at(0);
136
137 // We only elide callbacks for methods returning a single scalar.
138 if (typedVar->type().resolveToScalarType() != nullptr) {
139 return typedVar;
140 }
141
142 return nullptr;
143}
144
Andreas Huber31629bc2016-08-03 09:06:40 -0700145////////////////////////////////////////////////////////////////////////////////
146
147TypedVar::TypedVar(const char *name, Type *type)
148 : mName(name),
149 mType(type) {
150}
151
152std::string TypedVar::name() const {
153 return mName;
154}
155
156const Type &TypedVar::type() const {
157 return *mType;
158}
159
Andreas Huber70a59e12016-08-16 12:57:01 -0700160bool TypedVar::isJavaCompatible() const {
161 return mType->isJavaCompatible();
162}
163
Andreas Huberc9410c72016-07-28 12:18:40 -0700164} // namespace android
165