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