blob: 9d1adf8a6171259a7925448d540f4464d886f2e6 [file] [log] [blame]
Andreas Huberc9410c72016-07-28 12:18:40 -07001#include "Interface.h"
2
Zhuoyao Zhangba7e6e92016-08-10 12:19:02 -07003#include "Annotation.h"
Andreas Huberc9410c72016-07-28 12:18:40 -07004#include "Formatter.h"
5#include "Method.h"
6
7namespace android {
8
Zhuoyao Zhangba7e6e92016-08-10 12:19:02 -07009Interface::Interface(
10 Interface *super,
Zhuoyao Zhang5158db42016-08-10 10:25:20 -070011 AnnotationVector *annotations)
Zhuoyao Zhangba7e6e92016-08-10 12:19:02 -070012 : mSuperType(super),
13 mAnnotationsByName(annotations) {
Andreas Huberc9410c72016-07-28 12:18:40 -070014}
15
16void Interface::addMethod(Method *method) {
17 mMethods.push_back(method);
18}
19
Andreas Huber6cb08cf2016-08-03 15:44:51 -070020const Interface *Interface::superType() const {
Andreas Huberc9410c72016-07-28 12:18:40 -070021 return mSuperType;
22}
23
Andreas Hubera2723d22016-07-29 15:36:07 -070024bool Interface::isInterface() const {
25 return true;
26}
27
Andreas Huber295ad302016-08-16 11:35:00 -070028bool Interface::isBinder() const {
29 return true;
30}
31
Andreas Huber881227d2016-08-02 14:20:21 -070032const std::vector<Method *> &Interface::methods() const {
33 return mMethods;
34}
35
Zhuoyao Zhang5158db42016-08-10 10:25:20 -070036const AnnotationVector &Interface::annotations() const {
Zhuoyao Zhangba7e6e92016-08-10 12:19:02 -070037 return *mAnnotationsByName;
38}
39
Andreas Huber881227d2016-08-02 14:20:21 -070040std::string Interface::getCppType(StorageMode mode, std::string *extra) const {
41 extra->clear();
Andreas Huber31629bc2016-08-03 09:06:40 -070042 const std::string base = "::android::sp<" + fullName() + ">";
Andreas Huber881227d2016-08-02 14:20:21 -070043
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 Huber2831d512016-08-15 09:33:47 -070054std::string Interface::getJavaType() const {
55 return fullJavaName();
56}
57
Andreas Huber881227d2016-08-02 14:20:21 -070058void 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 Malchev549e2592016-08-10 08:59:12 -070069 const std::string binderName = "_hidl_" + name + "_binder";
Andreas Huber881227d2016-08-02 14:20:21 -070070
Andreas Huber8a82ff72016-08-04 10:29:39 -070071 out << "::android::sp<::android::hardware::IBinder> "
Andreas Huber881227d2016-08-02 14:20:21 -070072 << binderName << ";\n";
73
Iliyan Malchev549e2592016-08-10 08:59:12 -070074 out << "_hidl_err = ";
Andreas Huber881227d2016-08-02 14:20:21 -070075 out << parcelObjDeref
76 << "readNullableStrongBinder(&"
77 << binderName
78 << ");\n";
79
80 handleError(out, mode);
81
82 out << name
83 << " = "
Andreas Huber0e00de42016-08-03 09:56:02 -070084 << fullName()
Andreas Huber881227d2016-08-02 14:20:21 -070085 << "::asInterface("
86 << binderName
87 << ");\n";
88 } else {
Iliyan Malchev549e2592016-08-10 08:59:12 -070089 out << "_hidl_err = ";
Andreas Huber881227d2016-08-02 14:20:21 -070090 out << parcelObjDeref
91 << "writeStrongBinder("
Andreas Huber0e00de42016-08-03 09:56:02 -070092 << fullName()
Andreas Huber881227d2016-08-02 14:20:21 -070093 << "::asBinder("
94 << name
95 << "));\n";
96
97 handleError(out, mode);
98 }
99}
100
Andreas Huber2831d512016-08-15 09:33:47 -0700101void 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 Zhang5158db42016-08-10 10:25:20 -0700121status_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 Huber70a59e12016-08-16 12:57:01 -0700129bool 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 Huberc9410c72016-07-28 12:18:40 -0700143} // namespace android
144