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" |
| 23 | #include "ScalarType.h" |
| 24 | #include "Scope.h" |
| 25 | |
| 26 | #include <algorithm> |
Iliyan Malchev | a72e0d2 | 2016-09-09 11:03:08 -0700 | [diff] [blame] | 27 | #include <hidl-util/Formatter.h> |
Steven Moreland | 9c38761 | 2016-09-07 09:54:26 -0700 | [diff] [blame] | 28 | #include <android-base/logging.h> |
| 29 | #include <string> |
| 30 | #include <vector> |
| 31 | #include <set> |
| 32 | |
Steven Moreland | 9c38761 | 2016-09-07 09:54:26 -0700 | [diff] [blame] | 33 | namespace android { |
| 34 | |
Steven Moreland | 9c38761 | 2016-09-07 09:54:26 -0700 | [diff] [blame] | 35 | status_t AST::generateCppImpl(const std::string &outputPath) const { |
| 36 | status_t err = generateStubImplHeader(outputPath); |
| 37 | |
| 38 | if (err == OK) { |
| 39 | err = generateStubImplSource(outputPath); |
| 40 | } |
| 41 | |
| 42 | return err; |
| 43 | } |
| 44 | |
| 45 | void AST::generateFetchSymbol(Formatter &out, const std::string& ifaceName) const { |
| 46 | out << "HIDL_FETCH_" << ifaceName; |
| 47 | } |
| 48 | |
| 49 | status_t AST::generateStubImplMethod(Formatter &out, |
| 50 | const std::string &className, |
| 51 | const Method *method, |
| 52 | bool specifyNamespaces) const { |
| 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 | |
Steven Moreland | 9c38761 | 2016-09-07 09:54:26 -0700 | [diff] [blame] | 59 | method->generateCppSignature(out, className, specifyNamespaces); |
| 60 | |
| 61 | out << " {\n"; |
| 62 | |
| 63 | out.indent(); |
| 64 | out << "// TODO implement\n"; |
| 65 | |
| 66 | const TypedVar *elidedReturn = method->canElideCallback(); |
| 67 | |
| 68 | if (elidedReturn == nullptr) { |
| 69 | out << "return Void();\n"; |
| 70 | } else { |
| 71 | std::string extra; |
| 72 | out << "return " |
| 73 | << elidedReturn->type().getCppResultType(&extra) |
| 74 | << " {};\n"; |
| 75 | } |
| 76 | |
| 77 | out.unindent(); |
| 78 | |
| 79 | out << "}\n\n"; |
| 80 | |
| 81 | return OK; |
| 82 | } |
| 83 | |
| 84 | status_t AST::generateStubImplDeclaration(Formatter &out, |
| 85 | const std::string &className, |
| 86 | const Method *method, |
| 87 | bool specifyNamespaces) const { |
| 88 | |
Yifan Hong | 1254b55 | 2016-10-27 15:03:29 -0700 | [diff] [blame^] | 89 | // ignore HIDL reserved methods -- implemented in IFoo already. |
| 90 | if (method->isHidlReserved()) { |
| 91 | return OK; |
| 92 | } |
| 93 | |
Steven Moreland | 9c38761 | 2016-09-07 09:54:26 -0700 | [diff] [blame] | 94 | method->generateCppSignature(out, |
| 95 | className, |
| 96 | specifyNamespaces); |
| 97 | out << " override;\n"; |
| 98 | |
| 99 | return OK; |
| 100 | } |
| 101 | |
| 102 | status_t AST::generateStubImplHeader(const std::string &outputPath) const { |
| 103 | std::string ifaceName; |
| 104 | if (!AST::isInterface(&ifaceName)) { |
| 105 | // types.hal does not get a stub header. |
| 106 | return OK; |
| 107 | } |
| 108 | |
| 109 | const Interface *iface = mRootScope->getInterface(); |
| 110 | |
Jayant Chowdhary | 3f32c1f | 2016-09-15 16:53:56 -0700 | [diff] [blame] | 111 | const std::string baseName = iface->getBaseName(); |
Steven Moreland | 9c38761 | 2016-09-07 09:54:26 -0700 | [diff] [blame] | 112 | |
| 113 | std::string path = outputPath; |
| 114 | path.append(baseName); |
| 115 | path.append(".h"); |
| 116 | |
| 117 | CHECK(Coordinator::MakeParentHierarchy(path)); |
| 118 | FILE *file = fopen(path.c_str(), "w"); |
| 119 | |
| 120 | if (file == NULL) { |
| 121 | return -errno; |
| 122 | } |
| 123 | |
| 124 | Formatter out(file); |
| 125 | |
| 126 | const std::string guard = makeHeaderGuard(baseName); |
| 127 | |
| 128 | out << "#ifndef " << guard << "\n"; |
| 129 | out << "#define " << guard << "\n\n"; |
| 130 | |
| 131 | std::vector<std::string> packageComponents; |
| 132 | getPackageAndVersionComponents( |
| 133 | &packageComponents, false /* cpp_compatible */); |
| 134 | |
| 135 | out << "#include <"; |
| 136 | for (const auto &component : packageComponents) { |
| 137 | out << component << "/"; |
| 138 | } |
| 139 | out << "I" << baseName << ".h>\n"; |
| 140 | |
| 141 | out << "#include <hidl/Status.h>\n\n"; |
Andreas Huber | 4bcf97d | 2016-08-30 11:27:49 -0700 | [diff] [blame] | 142 | out << "#include <hidl/MQDescriptor.h>\n"; |
Steven Moreland | 9c38761 | 2016-09-07 09:54:26 -0700 | [diff] [blame] | 143 | |
| 144 | enterLeaveNamespace(out, true /* enter */); |
| 145 | out << "namespace implementation {\n\n"; |
| 146 | |
| 147 | // this is namespace aware code and doesn't require post-processing |
| 148 | out.setNamespace(""); |
| 149 | |
Yifan Hong | 10fe0b5 | 2016-10-19 14:20:17 -0700 | [diff] [blame] | 150 | std::vector<const Interface *> chain = iface->typeChain(); |
Steven Moreland | 9c38761 | 2016-09-07 09:54:26 -0700 | [diff] [blame] | 151 | |
| 152 | std::set<const FQName> usedTypes{}; |
| 153 | |
| 154 | for (auto it = chain.rbegin(); it != chain.rend(); ++it) { |
| 155 | const Interface *superInterface = *it; |
Steven Moreland | 9c38761 | 2016-09-07 09:54:26 -0700 | [diff] [blame] | 156 | superInterface->addNamedTypesToSet(usedTypes); |
Yifan Hong | 10fe0b5 | 2016-10-19 14:20:17 -0700 | [diff] [blame] | 157 | } |
Steven Moreland | 9c38761 | 2016-09-07 09:54:26 -0700 | [diff] [blame] | 158 | |
Yifan Hong | 10fe0b5 | 2016-10-19 14:20:17 -0700 | [diff] [blame] | 159 | for (const auto &tuple : iface->allMethodsFromRoot()) { |
| 160 | const Method *method = tuple.method(); |
| 161 | for(const auto & arg : method->args()) { |
| 162 | arg->type().addNamedTypesToSet(usedTypes); |
| 163 | } |
| 164 | for(const auto & results : method->results()) { |
| 165 | results->type().addNamedTypesToSet(usedTypes); |
Steven Moreland | 9c38761 | 2016-09-07 09:54:26 -0700 | [diff] [blame] | 166 | } |
| 167 | } |
| 168 | |
| 169 | std::set<const FQName> topLevelTypes{}; |
| 170 | |
| 171 | for (const auto &name : usedTypes) { |
| 172 | topLevelTypes.insert(name.getTopLevelType()); |
| 173 | } |
| 174 | |
| 175 | for (const FQName &name : topLevelTypes) { |
| 176 | out << "using " << name.cppName() << ";\n"; |
| 177 | } |
| 178 | |
| 179 | out << "using ::android::hardware::Return;\n"; |
| 180 | out << "using ::android::hardware::Void;\n"; |
| 181 | out << "using ::android::hardware::hidl_vec;\n"; |
| 182 | out << "using ::android::hardware::hidl_string;\n"; |
| 183 | out << "using ::android::sp;\n"; |
| 184 | |
| 185 | out << "\n"; |
| 186 | |
| 187 | out << "struct " |
| 188 | << baseName |
| 189 | << " : public " |
| 190 | << ifaceName |
| 191 | << " {\n"; |
| 192 | |
| 193 | out.indent(); |
| 194 | |
| 195 | status_t err = generateMethods(out, |
| 196 | "", /* class name */ |
| 197 | MethodLocation::IMPL_HEADER, |
| 198 | false /* specify namespaces */); |
| 199 | |
| 200 | if (err != OK) { |
| 201 | return err; |
| 202 | } |
| 203 | |
| 204 | out.unindent(); |
| 205 | |
| 206 | out << "};\n\n"; |
| 207 | |
| 208 | out << "extern \"C\" " |
| 209 | << ifaceName |
| 210 | << "* "; |
| 211 | generateFetchSymbol(out, ifaceName); |
| 212 | out << "(const char* name);\n\n"; |
| 213 | |
| 214 | out << "} // namespace implementation\n"; |
| 215 | enterLeaveNamespace(out, false /* leave */); |
| 216 | |
| 217 | out << "\n#endif // " << guard << "\n"; |
| 218 | |
| 219 | return OK; |
| 220 | } |
| 221 | |
| 222 | status_t AST::generateStubImplSource(const std::string &outputPath) const { |
| 223 | std::string ifaceName; |
| 224 | if (!AST::isInterface(&ifaceName)) { |
| 225 | // types.hal does not get a stub header. |
| 226 | return OK; |
| 227 | } |
| 228 | |
Jayant Chowdhary | 3f32c1f | 2016-09-15 16:53:56 -0700 | [diff] [blame] | 229 | const Interface *iface = mRootScope->getInterface(); |
| 230 | const std::string baseName = iface->getBaseName(); |
Steven Moreland | 9c38761 | 2016-09-07 09:54:26 -0700 | [diff] [blame] | 231 | |
| 232 | std::string path = outputPath; |
| 233 | path.append(baseName); |
| 234 | path.append(".cpp"); |
| 235 | |
| 236 | CHECK(Coordinator::MakeParentHierarchy(path)); |
| 237 | FILE *file = fopen(path.c_str(), "w"); |
| 238 | |
| 239 | if (file == NULL) { |
| 240 | return -errno; |
| 241 | } |
| 242 | |
| 243 | Formatter out(file); |
| 244 | |
| 245 | out << "#include \"" << baseName << ".h\"\n\n"; |
| 246 | |
| 247 | enterLeaveNamespace(out, true /* enter */); |
| 248 | out << "namespace implementation {\n\n"; |
| 249 | |
| 250 | // this is namespace aware code and doesn't require post-processing |
| 251 | out.setNamespace(""); |
| 252 | |
| 253 | generateMethods(out, |
| 254 | baseName, |
| 255 | MethodLocation::IMPL_SOURCE, |
| 256 | false /* specify namespaces */); |
| 257 | |
Steven Moreland | 9c38761 | 2016-09-07 09:54:26 -0700 | [diff] [blame] | 258 | |
| 259 | out << ifaceName |
| 260 | << "* "; |
| 261 | generateFetchSymbol(out, ifaceName); |
| 262 | out << "(const char* /* name */) {\n"; |
| 263 | out.indent(); |
| 264 | out << "return new " << baseName << "();\n"; |
| 265 | out.unindent(); |
| 266 | out << "}\n\n"; |
| 267 | |
| 268 | out << "} // namespace implementation\n"; |
| 269 | enterLeaveNamespace(out, false /* leave */); |
| 270 | |
| 271 | return OK; |
| 272 | } |
| 273 | |
| 274 | } // namespace android |