Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 1 | #include "Method.h" |
| 2 | |
Andreas Huber | 3599d92 | 2016-08-09 10:42:57 -0700 | [diff] [blame] | 3 | #include "Annotation.h" |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 4 | #include "Formatter.h" |
| 5 | #include "Type.h" |
| 6 | |
| 7 | namespace android { |
| 8 | |
Andreas Huber | 3599d92 | 2016-08-09 10:42:57 -0700 | [diff] [blame] | 9 | Method::Method(const char *name, |
| 10 | std::vector<TypedVar *> *args, |
| 11 | std::vector<TypedVar *> *results, |
Iliyan Malchev | 639bff8 | 2016-08-13 14:24:11 -0700 | [diff] [blame] | 12 | bool oneway, |
Zhuoyao Zhang | 5158db4 | 2016-08-10 10:25:20 -0700 | [diff] [blame] | 13 | AnnotationVector *annotations) |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 14 | : mName(name), |
| 15 | mArgs(args), |
Andreas Huber | 3599d92 | 2016-08-09 10:42:57 -0700 | [diff] [blame] | 16 | mResults(results), |
Iliyan Malchev | 639bff8 | 2016-08-13 14:24:11 -0700 | [diff] [blame] | 17 | mOneway(oneway), |
Andreas Huber | 3599d92 | 2016-08-09 10:42:57 -0700 | [diff] [blame] | 18 | mAnnotationsByName(annotations) { |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 19 | } |
| 20 | |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 21 | std::string Method::name() const { |
| 22 | return mName; |
| 23 | } |
| 24 | |
| 25 | const std::vector<TypedVar *> &Method::args() const { |
| 26 | return *mArgs; |
| 27 | } |
| 28 | |
| 29 | const std::vector<TypedVar *> &Method::results() const { |
| 30 | return *mResults; |
| 31 | } |
| 32 | |
Zhuoyao Zhang | 5158db4 | 2016-08-10 10:25:20 -0700 | [diff] [blame] | 33 | const AnnotationVector &Method::annotations() const { |
Andreas Huber | 3599d92 | 2016-08-09 10:42:57 -0700 | [diff] [blame] | 34 | return *mAnnotationsByName; |
| 35 | } |
| 36 | |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 37 | // static |
| 38 | std::string Method::GetSignature(const std::vector<TypedVar *> &args) { |
| 39 | bool first = true; |
| 40 | std::string out; |
| 41 | for (const auto &arg : args) { |
| 42 | if (!first) { |
| 43 | out += ", "; |
| 44 | } |
| 45 | |
| 46 | std::string extra; |
| 47 | out += arg->type().getCppArgumentType(&extra); |
| 48 | out += " "; |
| 49 | out += arg->name(); |
| 50 | out += extra; |
| 51 | |
| 52 | first = false; |
| 53 | } |
| 54 | |
| 55 | return out; |
| 56 | } |
| 57 | |
Andreas Huber | 2831d51 | 2016-08-15 09:33:47 -0700 | [diff] [blame] | 58 | // static |
| 59 | std::string Method::GetJavaSignature(const std::vector<TypedVar *> &args) { |
| 60 | bool first = true; |
| 61 | std::string out; |
| 62 | for (const auto &arg : args) { |
| 63 | if (!first) { |
| 64 | out += ", "; |
| 65 | } |
| 66 | |
| 67 | std::string extra; |
| 68 | out += arg->type().getJavaType(); |
| 69 | out += " "; |
| 70 | out += arg->name(); |
| 71 | out += extra; |
| 72 | |
| 73 | first = false; |
| 74 | } |
| 75 | |
| 76 | return out; |
| 77 | } |
| 78 | |
Andreas Huber | 3599d92 | 2016-08-09 10:42:57 -0700 | [diff] [blame] | 79 | void Method::dumpAnnotations(Formatter &out) const { |
| 80 | if (mAnnotationsByName->size() == 0) { |
| 81 | return; |
| 82 | } |
| 83 | |
| 84 | out << "// "; |
| 85 | for (size_t i = 0; i < mAnnotationsByName->size(); ++i) { |
| 86 | if (i > 0) { |
| 87 | out << " "; |
| 88 | } |
| 89 | mAnnotationsByName->valueAt(i)->dump(out); |
| 90 | } |
| 91 | out << "\n"; |
| 92 | } |
| 93 | |
Andreas Huber | 70a59e1 | 2016-08-16 12:57:01 -0700 | [diff] [blame] | 94 | bool Method::isJavaCompatible() const { |
| 95 | for (const auto &arg : *mArgs) { |
| 96 | if (!arg->isJavaCompatible()) { |
| 97 | return false; |
| 98 | } |
| 99 | } |
| 100 | |
| 101 | for (const auto &result : *mResults) { |
| 102 | if (!result->isJavaCompatible()) { |
| 103 | return false; |
| 104 | } |
| 105 | } |
| 106 | |
| 107 | return true; |
| 108 | } |
| 109 | |
Andreas Huber | 31629bc | 2016-08-03 09:06:40 -0700 | [diff] [blame] | 110 | //////////////////////////////////////////////////////////////////////////////// |
| 111 | |
| 112 | TypedVar::TypedVar(const char *name, Type *type) |
| 113 | : mName(name), |
| 114 | mType(type) { |
| 115 | } |
| 116 | |
| 117 | std::string TypedVar::name() const { |
| 118 | return mName; |
| 119 | } |
| 120 | |
| 121 | const Type &TypedVar::type() const { |
| 122 | return *mType; |
| 123 | } |
| 124 | |
Andreas Huber | 70a59e1 | 2016-08-16 12:57:01 -0700 | [diff] [blame] | 125 | bool TypedVar::isJavaCompatible() const { |
| 126 | return mType->isJavaCompatible(); |
| 127 | } |
| 128 | |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 129 | } // namespace android |
| 130 | |