Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 1 | #include "Interface.h" |
| 2 | |
Zhuoyao Zhang | ba7e6e9 | 2016-08-10 12:19:02 -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 "Method.h" |
| 6 | |
| 7 | namespace android { |
| 8 | |
Zhuoyao Zhang | ba7e6e9 | 2016-08-10 12:19:02 -0700 | [diff] [blame] | 9 | Interface::Interface( |
| 10 | Interface *super, |
Zhuoyao Zhang | 5158db4 | 2016-08-10 10:25:20 -0700 | [diff] [blame] | 11 | AnnotationVector *annotations) |
Zhuoyao Zhang | ba7e6e9 | 2016-08-10 12:19:02 -0700 | [diff] [blame] | 12 | : mSuperType(super), |
| 13 | mAnnotationsByName(annotations) { |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 14 | } |
| 15 | |
| 16 | void Interface::addMethod(Method *method) { |
| 17 | mMethods.push_back(method); |
| 18 | } |
| 19 | |
Andreas Huber | 6cb08cf | 2016-08-03 15:44:51 -0700 | [diff] [blame] | 20 | const Interface *Interface::superType() const { |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 21 | return mSuperType; |
| 22 | } |
| 23 | |
Andreas Huber | a2723d2 | 2016-07-29 15:36:07 -0700 | [diff] [blame] | 24 | bool Interface::isInterface() const { |
| 25 | return true; |
| 26 | } |
| 27 | |
Andreas Huber | 295ad30 | 2016-08-16 11:35:00 -0700 | [diff] [blame^] | 28 | bool Interface::isBinder() const { |
| 29 | return true; |
| 30 | } |
| 31 | |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 32 | const std::vector<Method *> &Interface::methods() const { |
| 33 | return mMethods; |
| 34 | } |
| 35 | |
Zhuoyao Zhang | 5158db4 | 2016-08-10 10:25:20 -0700 | [diff] [blame] | 36 | const AnnotationVector &Interface::annotations() const { |
Zhuoyao Zhang | ba7e6e9 | 2016-08-10 12:19:02 -0700 | [diff] [blame] | 37 | return *mAnnotationsByName; |
| 38 | } |
| 39 | |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 40 | std::string Interface::getCppType(StorageMode mode, std::string *extra) const { |
| 41 | extra->clear(); |
Andreas Huber | 31629bc | 2016-08-03 09:06:40 -0700 | [diff] [blame] | 42 | const std::string base = "::android::sp<" + fullName() + ">"; |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 43 | |
| 44 | switch (mode) { |
| 45 | case StorageMode_Stack: |
| 46 | case StorageMode_Result: |
| 47 | return base; |
| 48 | |
| 49 | case StorageMode_Argument: |
| 50 | return "const " + base + "&"; |
| 51 | } |
| 52 | } |
| 53 | |
Andreas Huber | 2831d51 | 2016-08-15 09:33:47 -0700 | [diff] [blame] | 54 | std::string Interface::getJavaType() const { |
| 55 | return fullJavaName(); |
| 56 | } |
| 57 | |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 58 | void Interface::emitReaderWriter( |
| 59 | Formatter &out, |
| 60 | const std::string &name, |
| 61 | const std::string &parcelObj, |
| 62 | bool parcelObjIsPointer, |
| 63 | bool isReader, |
| 64 | ErrorMode mode) const { |
| 65 | const std::string parcelObjDeref = |
| 66 | parcelObj + (parcelObjIsPointer ? "->" : "."); |
| 67 | |
| 68 | if (isReader) { |
Iliyan Malchev | 549e259 | 2016-08-10 08:59:12 -0700 | [diff] [blame] | 69 | const std::string binderName = "_hidl_" + name + "_binder"; |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 70 | |
Andreas Huber | 8a82ff7 | 2016-08-04 10:29:39 -0700 | [diff] [blame] | 71 | out << "::android::sp<::android::hardware::IBinder> " |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 72 | << binderName << ";\n"; |
| 73 | |
Iliyan Malchev | 549e259 | 2016-08-10 08:59:12 -0700 | [diff] [blame] | 74 | out << "_hidl_err = "; |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 75 | out << parcelObjDeref |
| 76 | << "readNullableStrongBinder(&" |
| 77 | << binderName |
| 78 | << ");\n"; |
| 79 | |
| 80 | handleError(out, mode); |
| 81 | |
| 82 | out << name |
| 83 | << " = " |
Andreas Huber | 0e00de4 | 2016-08-03 09:56:02 -0700 | [diff] [blame] | 84 | << fullName() |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 85 | << "::asInterface(" |
| 86 | << binderName |
| 87 | << ");\n"; |
| 88 | } else { |
Iliyan Malchev | 549e259 | 2016-08-10 08:59:12 -0700 | [diff] [blame] | 89 | out << "_hidl_err = "; |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 90 | out << parcelObjDeref |
| 91 | << "writeStrongBinder(" |
Andreas Huber | 0e00de4 | 2016-08-03 09:56:02 -0700 | [diff] [blame] | 92 | << fullName() |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 93 | << "::asBinder(" |
| 94 | << name |
| 95 | << "));\n"; |
| 96 | |
| 97 | handleError(out, mode); |
| 98 | } |
| 99 | } |
| 100 | |
Andreas Huber | 2831d51 | 2016-08-15 09:33:47 -0700 | [diff] [blame] | 101 | void Interface::emitJavaReaderWriter( |
| 102 | Formatter &out, |
| 103 | const std::string &parcelObj, |
| 104 | const std::string &argName, |
| 105 | bool isReader) const { |
| 106 | if (isReader) { |
| 107 | out << fullJavaName() |
| 108 | << ".asInterface(" |
| 109 | << parcelObj |
| 110 | << ".readStrongBinder());\n"; |
| 111 | } else { |
| 112 | out << parcelObj |
| 113 | << ".writeStrongBinder(" |
| 114 | << argName |
| 115 | << " == null ? null : " |
| 116 | << argName |
| 117 | << ".asBinder());\n"; |
| 118 | } |
| 119 | } |
| 120 | |
Zhuoyao Zhang | 5158db4 | 2016-08-10 10:25:20 -0700 | [diff] [blame] | 121 | status_t Interface::emitVtsArgumentType(Formatter &out) const { |
| 122 | out << "type: TYPE_HIDL_CALLBACK\n" |
| 123 | << "predefined_type: \"" |
| 124 | << localName() |
| 125 | << "\"\n"; |
| 126 | return OK; |
| 127 | } |
| 128 | |
Andreas Huber | 70a59e1 | 2016-08-16 12:57:01 -0700 | [diff] [blame] | 129 | bool Interface::isJavaCompatible() const { |
| 130 | if (!Scope::isJavaCompatible()) { |
| 131 | return false; |
| 132 | } |
| 133 | |
| 134 | for (const auto &method : mMethods) { |
| 135 | if (!method->isJavaCompatible()) { |
| 136 | return false; |
| 137 | } |
| 138 | } |
| 139 | |
| 140 | return true; |
| 141 | } |
| 142 | |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 143 | } // namespace android |
| 144 | |