blob: 7f335cc76ada07778c64a3838f3cef14b5cbe98f [file] [log] [blame]
Andreas Huberc9410c72016-07-28 12:18:40 -07001#include "Method.h"
2
Andreas Huber3599d922016-08-09 10:42:57 -07003#include "Annotation.h"
Andreas Huberc9410c72016-07-28 12:18:40 -07004#include "Formatter.h"
Iliyan Malchev40d474a2016-08-16 06:20:17 -07005#include "ScalarType.h"
Andreas Huberc9410c72016-07-28 12:18:40 -07006#include "Type.h"
7
8namespace android {
9
Andreas Huber3599d922016-08-09 10:42:57 -070010Method::Method(const char *name,
11 std::vector<TypedVar *> *args,
12 std::vector<TypedVar *> *results,
Iliyan Malchev639bff82016-08-13 14:24:11 -070013 bool oneway,
Zhuoyao Zhang5158db42016-08-10 10:25:20 -070014 AnnotationVector *annotations)
Andreas Huberc9410c72016-07-28 12:18:40 -070015 : mName(name),
16 mArgs(args),
Andreas Huber3599d922016-08-09 10:42:57 -070017 mResults(results),
Iliyan Malchev639bff82016-08-13 14:24:11 -070018 mOneway(oneway),
Andreas Huber3599d922016-08-09 10:42:57 -070019 mAnnotationsByName(annotations) {
Andreas Huberc9410c72016-07-28 12:18:40 -070020}
21
Andreas Huber881227d2016-08-02 14:20:21 -070022std::string Method::name() const {
23 return mName;
24}
25
26const std::vector<TypedVar *> &Method::args() const {
27 return *mArgs;
28}
29
30const std::vector<TypedVar *> &Method::results() const {
31 return *mResults;
32}
33
Zhuoyao Zhang5158db42016-08-10 10:25:20 -070034const AnnotationVector &Method::annotations() const {
Andreas Huber3599d922016-08-09 10:42:57 -070035 return *mAnnotationsByName;
36}
37
Andreas Huber881227d2016-08-02 14:20:21 -070038// static
39std::string Method::GetSignature(const std::vector<TypedVar *> &args) {
40 bool first = true;
41 std::string out;
42 for (const auto &arg : args) {
43 if (!first) {
44 out += ", ";
45 }
46
47 std::string extra;
48 out += arg->type().getCppArgumentType(&extra);
49 out += " ";
50 out += arg->name();
51 out += extra;
52
53 first = false;
54 }
55
56 return out;
57}
58
Andreas Huber2831d512016-08-15 09:33:47 -070059// static
60std::string Method::GetJavaSignature(const std::vector<TypedVar *> &args) {
61 bool first = true;
62 std::string out;
63 for (const auto &arg : args) {
64 if (!first) {
65 out += ", ";
66 }
67
68 std::string extra;
69 out += arg->type().getJavaType();
70 out += " ";
71 out += arg->name();
72 out += extra;
73
74 first = false;
75 }
76
77 return out;
78}
79
Andreas Huber3599d922016-08-09 10:42:57 -070080void Method::dumpAnnotations(Formatter &out) const {
81 if (mAnnotationsByName->size() == 0) {
82 return;
83 }
84
85 out << "// ";
86 for (size_t i = 0; i < mAnnotationsByName->size(); ++i) {
87 if (i > 0) {
88 out << " ";
89 }
90 mAnnotationsByName->valueAt(i)->dump(out);
91 }
92 out << "\n";
93}
94
Andreas Huber70a59e12016-08-16 12:57:01 -070095bool Method::isJavaCompatible() const {
96 for (const auto &arg : *mArgs) {
97 if (!arg->isJavaCompatible()) {
98 return false;
99 }
100 }
101
102 for (const auto &result : *mResults) {
103 if (!result->isJavaCompatible()) {
104 return false;
105 }
106 }
107
108 return true;
109}
110
Iliyan Malchev40d474a2016-08-16 06:20:17 -0700111const TypedVar* Method::canElideCallback() const {
112 auto &res = results();
113
114 // Can't elide callback for void or tuple-returning methods
115 if (res.size() != 1) {
116 return nullptr;
117 }
118
119 const TypedVar *typedVar = res.at(0);
120
121 // We only elide callbacks for methods returning a single scalar.
122 if (typedVar->type().resolveToScalarType() != nullptr) {
123 return typedVar;
124 }
125
126 return nullptr;
127}
128
Andreas Huber31629bc2016-08-03 09:06:40 -0700129////////////////////////////////////////////////////////////////////////////////
130
131TypedVar::TypedVar(const char *name, Type *type)
132 : mName(name),
133 mType(type) {
134}
135
136std::string TypedVar::name() const {
137 return mName;
138}
139
140const Type &TypedVar::type() const {
141 return *mType;
142}
143
Andreas Huber70a59e12016-08-16 12:57:01 -0700144bool TypedVar::isJavaCompatible() const {
145 return mType->isJavaCompatible();
146}
147
Andreas Huberc9410c72016-07-28 12:18:40 -0700148} // namespace android
149