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