Steven Moreland | 9c38761 | 2016-09-07 09:54:26 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2016 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #include "AST.h" |
| 18 | |
| 19 | #include "Coordinator.h" |
| 20 | #include "EnumType.h" |
Steven Moreland | 9c38761 | 2016-09-07 09:54:26 -0700 | [diff] [blame] | 21 | #include "Interface.h" |
| 22 | #include "Method.h" |
Timur Iskhakov | 7fa79f6 | 2017-08-09 11:04:54 -0700 | [diff] [blame] | 23 | #include "Reference.h" |
Steven Moreland | 9c38761 | 2016-09-07 09:54:26 -0700 | [diff] [blame] | 24 | #include "ScalarType.h" |
| 25 | #include "Scope.h" |
| 26 | |
| 27 | #include <algorithm> |
Iliyan Malchev | a72e0d2 | 2016-09-09 11:03:08 -0700 | [diff] [blame] | 28 | #include <hidl-util/Formatter.h> |
Steven Moreland | 9c38761 | 2016-09-07 09:54:26 -0700 | [diff] [blame] | 29 | #include <android-base/logging.h> |
| 30 | #include <string> |
| 31 | #include <vector> |
| 32 | #include <set> |
| 33 | |
Steven Moreland | 9c38761 | 2016-09-07 09:54:26 -0700 | [diff] [blame] | 34 | namespace android { |
| 35 | |
Steven Moreland | 9c38761 | 2016-09-07 09:54:26 -0700 | [diff] [blame] | 36 | void AST::generateFetchSymbol(Formatter &out, const std::string& ifaceName) const { |
| 37 | out << "HIDL_FETCH_" << ifaceName; |
| 38 | } |
| 39 | |
Steven Moreland | 6ec9eb9 | 2018-02-16 14:21:49 -0800 | [diff] [blame] | 40 | void AST::generateStubImplMethod(Formatter& out, const std::string& className, |
| 41 | const Method* method) const { |
Yifan Hong | 1254b55 | 2016-10-27 15:03:29 -0700 | [diff] [blame] | 42 | // ignore HIDL reserved methods -- implemented in IFoo already. |
| 43 | if (method->isHidlReserved()) { |
Steven Moreland | 6ec9eb9 | 2018-02-16 14:21:49 -0800 | [diff] [blame] | 44 | return; |
Yifan Hong | 1254b55 | 2016-10-27 15:03:29 -0700 | [diff] [blame] | 45 | } |
| 46 | |
Yifan Hong | 068c552 | 2016-10-31 14:07:25 -0700 | [diff] [blame] | 47 | method->generateCppSignature(out, className, false /* specifyNamespaces */); |
Steven Moreland | 9c38761 | 2016-09-07 09:54:26 -0700 | [diff] [blame] | 48 | |
| 49 | out << " {\n"; |
| 50 | |
| 51 | out.indent(); |
| 52 | out << "// TODO implement\n"; |
| 53 | |
Timur Iskhakov | 7fa79f6 | 2017-08-09 11:04:54 -0700 | [diff] [blame] | 54 | const NamedReference<Type>* elidedReturn = method->canElideCallback(); |
Steven Moreland | 9c38761 | 2016-09-07 09:54:26 -0700 | [diff] [blame] | 55 | |
| 56 | if (elidedReturn == nullptr) { |
| 57 | out << "return Void();\n"; |
| 58 | } else { |
Steven Moreland | 9c38761 | 2016-09-07 09:54:26 -0700 | [diff] [blame] | 59 | out << "return " |
Yifan Hong | 3b320f8 | 2016-11-01 15:15:54 -0700 | [diff] [blame] | 60 | << elidedReturn->type().getCppResultType() |
Steven Moreland | 9c38761 | 2016-09-07 09:54:26 -0700 | [diff] [blame] | 61 | << " {};\n"; |
| 62 | } |
| 63 | |
| 64 | out.unindent(); |
| 65 | |
| 66 | out << "}\n\n"; |
| 67 | |
Steven Moreland | 6ec9eb9 | 2018-02-16 14:21:49 -0800 | [diff] [blame] | 68 | return; |
Steven Moreland | 9c38761 | 2016-09-07 09:54:26 -0700 | [diff] [blame] | 69 | } |
| 70 | |
Steven Moreland | 6ec9eb9 | 2018-02-16 14:21:49 -0800 | [diff] [blame] | 71 | void AST::generateCppImplHeader(Formatter& out) const { |
Steven Moreland | 19f11b5 | 2017-05-12 18:22:21 -0700 | [diff] [blame] | 72 | if (!AST::isInterface()) { |
Steven Moreland | 9c38761 | 2016-09-07 09:54:26 -0700 | [diff] [blame] | 73 | // types.hal does not get a stub header. |
Steven Moreland | 6ec9eb9 | 2018-02-16 14:21:49 -0800 | [diff] [blame] | 74 | return; |
Steven Moreland | 9c38761 | 2016-09-07 09:54:26 -0700 | [diff] [blame] | 75 | } |
| 76 | |
Timur Iskhakov | cb0ba52 | 2017-07-17 20:01:37 -0700 | [diff] [blame] | 77 | const Interface* iface = mRootScope.getInterface(); |
Jayant Chowdhary | 3f32c1f | 2016-09-15 16:53:56 -0700 | [diff] [blame] | 78 | const std::string baseName = iface->getBaseName(); |
Steven Moreland | 9c38761 | 2016-09-07 09:54:26 -0700 | [diff] [blame] | 79 | |
Steven Moreland | 5708edf | 2016-11-04 15:33:31 +0000 | [diff] [blame] | 80 | const std::string guard = makeHeaderGuard(baseName, false /* indicateGenerated */); |
Steven Moreland | 9c38761 | 2016-09-07 09:54:26 -0700 | [diff] [blame] | 81 | |
| 82 | out << "#ifndef " << guard << "\n"; |
| 83 | out << "#define " << guard << "\n\n"; |
| 84 | |
Yifan Hong | eefe4f2 | 2017-01-04 15:32:42 -0800 | [diff] [blame] | 85 | generateCppPackageInclude(out, mPackage, iface->localName()); |
Steven Moreland | 9c38761 | 2016-09-07 09:54:26 -0700 | [diff] [blame] | 86 | |
Andreas Huber | 4bcf97d | 2016-08-30 11:27:49 -0700 | [diff] [blame] | 87 | out << "#include <hidl/MQDescriptor.h>\n"; |
Steven Moreland | 41c6d2e | 2016-11-07 12:26:54 -0800 | [diff] [blame] | 88 | out << "#include <hidl/Status.h>\n\n"; |
Steven Moreland | 9c38761 | 2016-09-07 09:54:26 -0700 | [diff] [blame] | 89 | |
| 90 | enterLeaveNamespace(out, true /* enter */); |
| 91 | out << "namespace implementation {\n\n"; |
| 92 | |
Steven Moreland | e429a26 | 2016-11-15 09:54:32 -0800 | [diff] [blame] | 93 | out << "using ::android::hardware::hidl_array;\n"; |
Martijn Coenen | 99e6beb | 2016-12-01 15:48:42 +0100 | [diff] [blame] | 94 | out << "using ::android::hardware::hidl_memory;\n"; |
Steven Moreland | e429a26 | 2016-11-15 09:54:32 -0800 | [diff] [blame] | 95 | out << "using ::android::hardware::hidl_string;\n"; |
| 96 | out << "using ::android::hardware::hidl_vec;\n"; |
Steven Moreland | 9c38761 | 2016-09-07 09:54:26 -0700 | [diff] [blame] | 97 | out << "using ::android::hardware::Return;\n"; |
| 98 | out << "using ::android::hardware::Void;\n"; |
Steven Moreland | 9c38761 | 2016-09-07 09:54:26 -0700 | [diff] [blame] | 99 | out << "using ::android::sp;\n"; |
| 100 | |
| 101 | out << "\n"; |
| 102 | |
| 103 | out << "struct " |
| 104 | << baseName |
| 105 | << " : public " |
Steven Moreland | 19f11b5 | 2017-05-12 18:22:21 -0700 | [diff] [blame] | 106 | << iface->localName() |
Steven Moreland | 9c38761 | 2016-09-07 09:54:26 -0700 | [diff] [blame] | 107 | << " {\n"; |
| 108 | |
| 109 | out.indent(); |
| 110 | |
Steven Moreland | 6ec9eb9 | 2018-02-16 14:21:49 -0800 | [diff] [blame] | 111 | generateMethods(out, [&](const Method* method, const Interface*) { |
Yifan Hong | 068c552 | 2016-10-31 14:07:25 -0700 | [diff] [blame] | 112 | // ignore HIDL reserved methods -- implemented in IFoo already. |
| 113 | if (method->isHidlReserved()) { |
Steven Moreland | 6ec9eb9 | 2018-02-16 14:21:49 -0800 | [diff] [blame] | 114 | return; |
Yifan Hong | 068c552 | 2016-10-31 14:07:25 -0700 | [diff] [blame] | 115 | } |
| 116 | method->generateCppSignature(out, "" /* className */, |
| 117 | false /* specifyNamespaces */); |
| 118 | out << " override;\n"; |
Yifan Hong | 068c552 | 2016-10-31 14:07:25 -0700 | [diff] [blame] | 119 | }); |
Steven Moreland | 9c38761 | 2016-09-07 09:54:26 -0700 | [diff] [blame] | 120 | |
Steven Moreland | 9c38761 | 2016-09-07 09:54:26 -0700 | [diff] [blame] | 121 | out.unindent(); |
| 122 | |
| 123 | out << "};\n\n"; |
| 124 | |
Steven Moreland | 3918fcc | 2017-04-21 10:54:27 -0700 | [diff] [blame] | 125 | out << "// FIXME: most likely delete, this is only for passthrough implementations\n" |
| 126 | << "// extern \"C\" " |
Steven Moreland | 19f11b5 | 2017-05-12 18:22:21 -0700 | [diff] [blame] | 127 | << iface->localName() |
Steven Moreland | 9c38761 | 2016-09-07 09:54:26 -0700 | [diff] [blame] | 128 | << "* "; |
Steven Moreland | 19f11b5 | 2017-05-12 18:22:21 -0700 | [diff] [blame] | 129 | generateFetchSymbol(out, iface->localName()); |
Steven Moreland | 9c38761 | 2016-09-07 09:54:26 -0700 | [diff] [blame] | 130 | out << "(const char* name);\n\n"; |
| 131 | |
| 132 | out << "} // namespace implementation\n"; |
| 133 | enterLeaveNamespace(out, false /* leave */); |
| 134 | |
| 135 | out << "\n#endif // " << guard << "\n"; |
Steven Moreland | 9c38761 | 2016-09-07 09:54:26 -0700 | [diff] [blame] | 136 | } |
| 137 | |
Steven Moreland | 6ec9eb9 | 2018-02-16 14:21:49 -0800 | [diff] [blame] | 138 | void AST::generateCppImplSource(Formatter& out) const { |
Steven Moreland | 19f11b5 | 2017-05-12 18:22:21 -0700 | [diff] [blame] | 139 | if (!AST::isInterface()) { |
Steven Moreland | 9c38761 | 2016-09-07 09:54:26 -0700 | [diff] [blame] | 140 | // types.hal does not get a stub header. |
Steven Moreland | 6ec9eb9 | 2018-02-16 14:21:49 -0800 | [diff] [blame] | 141 | return; |
Steven Moreland | 9c38761 | 2016-09-07 09:54:26 -0700 | [diff] [blame] | 142 | } |
| 143 | |
Timur Iskhakov | cb0ba52 | 2017-07-17 20:01:37 -0700 | [diff] [blame] | 144 | const Interface* iface = mRootScope.getInterface(); |
Jayant Chowdhary | 3f32c1f | 2016-09-15 16:53:56 -0700 | [diff] [blame] | 145 | const std::string baseName = iface->getBaseName(); |
Steven Moreland | 9c38761 | 2016-09-07 09:54:26 -0700 | [diff] [blame] | 146 | |
Steven Moreland | 9c38761 | 2016-09-07 09:54:26 -0700 | [diff] [blame] | 147 | out << "#include \"" << baseName << ".h\"\n\n"; |
| 148 | |
| 149 | enterLeaveNamespace(out, true /* enter */); |
| 150 | out << "namespace implementation {\n\n"; |
| 151 | |
Steven Moreland | 6ec9eb9 | 2018-02-16 14:21:49 -0800 | [diff] [blame] | 152 | generateMethods(out, [&](const Method* method, const Interface*) { |
| 153 | generateStubImplMethod(out, baseName, method); |
Yifan Hong | 068c552 | 2016-10-31 14:07:25 -0700 | [diff] [blame] | 154 | }); |
Steven Moreland | 9c38761 | 2016-09-07 09:54:26 -0700 | [diff] [blame] | 155 | |
Steven Moreland | 3918fcc | 2017-04-21 10:54:27 -0700 | [diff] [blame] | 156 | out.setLinePrefix("//"); |
Steven Moreland | 19f11b5 | 2017-05-12 18:22:21 -0700 | [diff] [blame] | 157 | out << iface->localName() |
Steven Moreland | 9c38761 | 2016-09-07 09:54:26 -0700 | [diff] [blame] | 158 | << "* "; |
Steven Moreland | 19f11b5 | 2017-05-12 18:22:21 -0700 | [diff] [blame] | 159 | generateFetchSymbol(out, iface->localName()); |
Steven Moreland | 9c38761 | 2016-09-07 09:54:26 -0700 | [diff] [blame] | 160 | out << "(const char* /* name */) {\n"; |
| 161 | out.indent(); |
| 162 | out << "return new " << baseName << "();\n"; |
| 163 | out.unindent(); |
| 164 | out << "}\n\n"; |
Steven Moreland | 3918fcc | 2017-04-21 10:54:27 -0700 | [diff] [blame] | 165 | out.unsetLinePrefix(); |
Steven Moreland | 9c38761 | 2016-09-07 09:54:26 -0700 | [diff] [blame] | 166 | |
Yifan Hong | dfc4b9c | 2016-12-02 16:31:31 -0800 | [diff] [blame] | 167 | out << "} // namespace implementation\n"; |
Steven Moreland | 9c38761 | 2016-09-07 09:54:26 -0700 | [diff] [blame] | 168 | enterLeaveNamespace(out, false /* leave */); |
Steven Moreland | 9c38761 | 2016-09-07 09:54:26 -0700 | [diff] [blame] | 169 | } |
| 170 | |
| 171 | } // namespace android |