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