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 | status_t AST::generateCppImpl(const std::string &outputPath) const { |
| 37 | status_t err = generateStubImplHeader(outputPath); |
| 38 | |
| 39 | if (err == OK) { |
| 40 | err = generateStubImplSource(outputPath); |
| 41 | } |
| 42 | |
| 43 | return err; |
| 44 | } |
| 45 | |
| 46 | void AST::generateFetchSymbol(Formatter &out, const std::string& ifaceName) const { |
| 47 | out << "HIDL_FETCH_" << ifaceName; |
| 48 | } |
| 49 | |
| 50 | status_t AST::generateStubImplMethod(Formatter &out, |
| 51 | const std::string &className, |
Yifan Hong | 068c552 | 2016-10-31 14:07:25 -0700 | [diff] [blame] | 52 | const Method *method) const { |
Steven Moreland | 9c38761 | 2016-09-07 09:54:26 -0700 | [diff] [blame] | 53 | |
Yifan Hong | 1254b55 | 2016-10-27 15:03:29 -0700 | [diff] [blame] | 54 | // ignore HIDL reserved methods -- implemented in IFoo already. |
| 55 | if (method->isHidlReserved()) { |
| 56 | return OK; |
| 57 | } |
| 58 | |
Yifan Hong | 068c552 | 2016-10-31 14:07:25 -0700 | [diff] [blame] | 59 | method->generateCppSignature(out, className, false /* specifyNamespaces */); |
Steven Moreland | 9c38761 | 2016-09-07 09:54:26 -0700 | [diff] [blame] | 60 | |
| 61 | out << " {\n"; |
| 62 | |
| 63 | out.indent(); |
| 64 | out << "// TODO implement\n"; |
| 65 | |
Timur Iskhakov | 7fa79f6 | 2017-08-09 11:04:54 -0700 | [diff] [blame] | 66 | const NamedReference<Type>* elidedReturn = method->canElideCallback(); |
Steven Moreland | 9c38761 | 2016-09-07 09:54:26 -0700 | [diff] [blame] | 67 | |
| 68 | if (elidedReturn == nullptr) { |
| 69 | out << "return Void();\n"; |
| 70 | } else { |
Steven Moreland | 9c38761 | 2016-09-07 09:54:26 -0700 | [diff] [blame] | 71 | out << "return " |
Yifan Hong | 3b320f8 | 2016-11-01 15:15:54 -0700 | [diff] [blame] | 72 | << elidedReturn->type().getCppResultType() |
Steven Moreland | 9c38761 | 2016-09-07 09:54:26 -0700 | [diff] [blame] | 73 | << " {};\n"; |
| 74 | } |
| 75 | |
| 76 | out.unindent(); |
| 77 | |
| 78 | out << "}\n\n"; |
| 79 | |
| 80 | return OK; |
| 81 | } |
| 82 | |
Steven Moreland | 9c38761 | 2016-09-07 09:54:26 -0700 | [diff] [blame] | 83 | status_t AST::generateStubImplHeader(const std::string &outputPath) const { |
Steven Moreland | 19f11b5 | 2017-05-12 18:22:21 -0700 | [diff] [blame] | 84 | if (!AST::isInterface()) { |
Steven Moreland | 9c38761 | 2016-09-07 09:54:26 -0700 | [diff] [blame] | 85 | // types.hal does not get a stub header. |
| 86 | return OK; |
| 87 | } |
| 88 | |
Timur Iskhakov | cb0ba52 | 2017-07-17 20:01:37 -0700 | [diff] [blame] | 89 | const Interface* iface = mRootScope.getInterface(); |
Jayant Chowdhary | 3f32c1f | 2016-09-15 16:53:56 -0700 | [diff] [blame] | 90 | const std::string baseName = iface->getBaseName(); |
Steven Moreland | 9c38761 | 2016-09-07 09:54:26 -0700 | [diff] [blame] | 91 | |
| 92 | std::string path = outputPath; |
| 93 | path.append(baseName); |
| 94 | path.append(".h"); |
| 95 | |
| 96 | CHECK(Coordinator::MakeParentHierarchy(path)); |
| 97 | FILE *file = fopen(path.c_str(), "w"); |
| 98 | |
| 99 | if (file == NULL) { |
| 100 | return -errno; |
| 101 | } |
| 102 | |
| 103 | Formatter out(file); |
| 104 | |
Steven Moreland | 5708edf | 2016-11-04 15:33:31 +0000 | [diff] [blame] | 105 | const std::string guard = makeHeaderGuard(baseName, false /* indicateGenerated */); |
Steven Moreland | 9c38761 | 2016-09-07 09:54:26 -0700 | [diff] [blame] | 106 | |
| 107 | out << "#ifndef " << guard << "\n"; |
| 108 | out << "#define " << guard << "\n\n"; |
| 109 | |
Yifan Hong | eefe4f2 | 2017-01-04 15:32:42 -0800 | [diff] [blame] | 110 | generateCppPackageInclude(out, mPackage, iface->localName()); |
Steven Moreland | 9c38761 | 2016-09-07 09:54:26 -0700 | [diff] [blame] | 111 | |
Andreas Huber | 4bcf97d | 2016-08-30 11:27:49 -0700 | [diff] [blame] | 112 | out << "#include <hidl/MQDescriptor.h>\n"; |
Steven Moreland | 41c6d2e | 2016-11-07 12:26:54 -0800 | [diff] [blame] | 113 | out << "#include <hidl/Status.h>\n\n"; |
Steven Moreland | 9c38761 | 2016-09-07 09:54:26 -0700 | [diff] [blame] | 114 | |
| 115 | enterLeaveNamespace(out, true /* enter */); |
| 116 | out << "namespace implementation {\n\n"; |
| 117 | |
Steven Moreland | e429a26 | 2016-11-15 09:54:32 -0800 | [diff] [blame] | 118 | out << "using ::android::hardware::hidl_array;\n"; |
Martijn Coenen | 99e6beb | 2016-12-01 15:48:42 +0100 | [diff] [blame] | 119 | out << "using ::android::hardware::hidl_memory;\n"; |
Steven Moreland | e429a26 | 2016-11-15 09:54:32 -0800 | [diff] [blame] | 120 | out << "using ::android::hardware::hidl_string;\n"; |
| 121 | out << "using ::android::hardware::hidl_vec;\n"; |
Steven Moreland | 9c38761 | 2016-09-07 09:54:26 -0700 | [diff] [blame] | 122 | out << "using ::android::hardware::Return;\n"; |
| 123 | out << "using ::android::hardware::Void;\n"; |
Steven Moreland | 9c38761 | 2016-09-07 09:54:26 -0700 | [diff] [blame] | 124 | out << "using ::android::sp;\n"; |
| 125 | |
| 126 | out << "\n"; |
| 127 | |
| 128 | out << "struct " |
| 129 | << baseName |
| 130 | << " : public " |
Steven Moreland | 19f11b5 | 2017-05-12 18:22:21 -0700 | [diff] [blame] | 131 | << iface->localName() |
Steven Moreland | 9c38761 | 2016-09-07 09:54:26 -0700 | [diff] [blame] | 132 | << " {\n"; |
| 133 | |
| 134 | out.indent(); |
| 135 | |
Yifan Hong | 068c552 | 2016-10-31 14:07:25 -0700 | [diff] [blame] | 136 | status_t err = generateMethods(out, [&](const Method *method, const Interface *) { |
| 137 | // ignore HIDL reserved methods -- implemented in IFoo already. |
| 138 | if (method->isHidlReserved()) { |
| 139 | return OK; |
| 140 | } |
| 141 | method->generateCppSignature(out, "" /* className */, |
| 142 | false /* specifyNamespaces */); |
| 143 | out << " override;\n"; |
| 144 | return OK; |
| 145 | }); |
Steven Moreland | 9c38761 | 2016-09-07 09:54:26 -0700 | [diff] [blame] | 146 | |
| 147 | if (err != OK) { |
| 148 | return err; |
| 149 | } |
| 150 | |
| 151 | out.unindent(); |
| 152 | |
| 153 | out << "};\n\n"; |
| 154 | |
Steven Moreland | 3918fcc | 2017-04-21 10:54:27 -0700 | [diff] [blame] | 155 | out << "// FIXME: most likely delete, this is only for passthrough implementations\n" |
| 156 | << "// extern \"C\" " |
Steven Moreland | 19f11b5 | 2017-05-12 18:22:21 -0700 | [diff] [blame] | 157 | << 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\n"; |
| 161 | |
| 162 | out << "} // namespace implementation\n"; |
| 163 | enterLeaveNamespace(out, false /* leave */); |
| 164 | |
| 165 | out << "\n#endif // " << guard << "\n"; |
| 166 | |
| 167 | return OK; |
| 168 | } |
| 169 | |
| 170 | status_t AST::generateStubImplSource(const std::string &outputPath) const { |
Steven Moreland | 19f11b5 | 2017-05-12 18:22:21 -0700 | [diff] [blame] | 171 | if (!AST::isInterface()) { |
Steven Moreland | 9c38761 | 2016-09-07 09:54:26 -0700 | [diff] [blame] | 172 | // types.hal does not get a stub header. |
| 173 | return OK; |
| 174 | } |
| 175 | |
Timur Iskhakov | cb0ba52 | 2017-07-17 20:01:37 -0700 | [diff] [blame] | 176 | const Interface* iface = mRootScope.getInterface(); |
Jayant Chowdhary | 3f32c1f | 2016-09-15 16:53:56 -0700 | [diff] [blame] | 177 | const std::string baseName = iface->getBaseName(); |
Steven Moreland | 9c38761 | 2016-09-07 09:54:26 -0700 | [diff] [blame] | 178 | |
| 179 | std::string path = outputPath; |
| 180 | path.append(baseName); |
| 181 | path.append(".cpp"); |
| 182 | |
| 183 | CHECK(Coordinator::MakeParentHierarchy(path)); |
| 184 | FILE *file = fopen(path.c_str(), "w"); |
| 185 | |
| 186 | if (file == NULL) { |
| 187 | return -errno; |
| 188 | } |
| 189 | |
| 190 | Formatter out(file); |
| 191 | |
| 192 | out << "#include \"" << baseName << ".h\"\n\n"; |
| 193 | |
| 194 | enterLeaveNamespace(out, true /* enter */); |
| 195 | out << "namespace implementation {\n\n"; |
| 196 | |
Yifan Hong | 068c552 | 2016-10-31 14:07:25 -0700 | [diff] [blame] | 197 | status_t err = generateMethods(out, [&](const Method *method, const Interface *) { |
| 198 | return generateStubImplMethod(out, baseName, method); |
| 199 | }); |
Steven Moreland | 9c38761 | 2016-09-07 09:54:26 -0700 | [diff] [blame] | 200 | |
Steven Moreland | 6081863 | 2017-02-04 00:33:42 -0800 | [diff] [blame] | 201 | if (err != OK) { |
| 202 | return err; |
| 203 | } |
| 204 | |
Steven Moreland | 3918fcc | 2017-04-21 10:54:27 -0700 | [diff] [blame] | 205 | out.setLinePrefix("//"); |
Steven Moreland | 19f11b5 | 2017-05-12 18:22:21 -0700 | [diff] [blame] | 206 | out << iface->localName() |
Steven Moreland | 9c38761 | 2016-09-07 09:54:26 -0700 | [diff] [blame] | 207 | << "* "; |
Steven Moreland | 19f11b5 | 2017-05-12 18:22:21 -0700 | [diff] [blame] | 208 | generateFetchSymbol(out, iface->localName()); |
Steven Moreland | 9c38761 | 2016-09-07 09:54:26 -0700 | [diff] [blame] | 209 | out << "(const char* /* name */) {\n"; |
| 210 | out.indent(); |
| 211 | out << "return new " << baseName << "();\n"; |
| 212 | out.unindent(); |
| 213 | out << "}\n\n"; |
Steven Moreland | 3918fcc | 2017-04-21 10:54:27 -0700 | [diff] [blame] | 214 | out.unsetLinePrefix(); |
Steven Moreland | 9c38761 | 2016-09-07 09:54:26 -0700 | [diff] [blame] | 215 | |
Yifan Hong | dfc4b9c | 2016-12-02 16:31:31 -0800 | [diff] [blame] | 216 | out << "} // namespace implementation\n"; |
Steven Moreland | 9c38761 | 2016-09-07 09:54:26 -0700 | [diff] [blame] | 217 | enterLeaveNamespace(out, false /* leave */); |
| 218 | |
| 219 | return OK; |
| 220 | } |
| 221 | |
| 222 | } // namespace android |