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 | |
Steven Moreland | 9c38761 | 2016-09-07 09:54:26 -0700 | [diff] [blame] | 27 | #include <android-base/logging.h> |
Steven Moreland | ac816ff | 2019-12-09 15:29:13 -0800 | [diff] [blame] | 28 | #include <android-base/strings.h> |
| 29 | #include <hidl-util/Formatter.h> |
| 30 | #include <algorithm> |
| 31 | #include <set> |
Steven Moreland | 9c38761 | 2016-09-07 09:54:26 -0700 | [diff] [blame] | 32 | #include <string> |
| 33 | #include <vector> |
Steven Moreland | 9c38761 | 2016-09-07 09:54:26 -0700 | [diff] [blame] | 34 | |
Steven Moreland | 9c38761 | 2016-09-07 09:54:26 -0700 | [diff] [blame] | 35 | namespace android { |
| 36 | |
Steven Moreland | 9c38761 | 2016-09-07 09:54:26 -0700 | [diff] [blame] | 37 | void AST::generateFetchSymbol(Formatter &out, const std::string& ifaceName) const { |
| 38 | out << "HIDL_FETCH_" << ifaceName; |
| 39 | } |
| 40 | |
Steven Moreland | 6ec9eb9 | 2018-02-16 14:21:49 -0800 | [diff] [blame] | 41 | void AST::generateStubImplMethod(Formatter& out, const std::string& className, |
| 42 | const Method* method) const { |
Yifan Hong | 1254b55 | 2016-10-27 15:03:29 -0700 | [diff] [blame] | 43 | // ignore HIDL reserved methods -- implemented in IFoo already. |
| 44 | if (method->isHidlReserved()) { |
Steven Moreland | 6ec9eb9 | 2018-02-16 14:21:49 -0800 | [diff] [blame] | 45 | return; |
Yifan Hong | 1254b55 | 2016-10-27 15:03:29 -0700 | [diff] [blame] | 46 | } |
| 47 | |
Yifan Hong | 068c552 | 2016-10-31 14:07:25 -0700 | [diff] [blame] | 48 | method->generateCppSignature(out, className, false /* specifyNamespaces */); |
Steven Moreland | 9c38761 | 2016-09-07 09:54:26 -0700 | [diff] [blame] | 49 | |
| 50 | out << " {\n"; |
| 51 | |
| 52 | out.indent(); |
| 53 | out << "// TODO implement\n"; |
| 54 | |
Timur Iskhakov | 7fa79f6 | 2017-08-09 11:04:54 -0700 | [diff] [blame] | 55 | const NamedReference<Type>* elidedReturn = method->canElideCallback(); |
Steven Moreland | 9c38761 | 2016-09-07 09:54:26 -0700 | [diff] [blame] | 56 | |
| 57 | if (elidedReturn == nullptr) { |
| 58 | out << "return Void();\n"; |
| 59 | } else { |
Steven Moreland | 9c38761 | 2016-09-07 09:54:26 -0700 | [diff] [blame] | 60 | out << "return " |
Yifan Hong | 3b320f8 | 2016-11-01 15:15:54 -0700 | [diff] [blame] | 61 | << elidedReturn->type().getCppResultType() |
Steven Moreland | 9c38761 | 2016-09-07 09:54:26 -0700 | [diff] [blame] | 62 | << " {};\n"; |
| 63 | } |
| 64 | |
| 65 | out.unindent(); |
| 66 | |
| 67 | out << "}\n\n"; |
| 68 | |
Steven Moreland | 6ec9eb9 | 2018-02-16 14:21:49 -0800 | [diff] [blame] | 69 | return; |
Steven Moreland | 9c38761 | 2016-09-07 09:54:26 -0700 | [diff] [blame] | 70 | } |
| 71 | |
Steven Moreland | ac816ff | 2019-12-09 15:29:13 -0800 | [diff] [blame] | 72 | static std::string getImplNamespace(const FQName& fqName) { |
| 73 | std::vector<std::string> components = fqName.getPackageComponents(); |
| 74 | components.push_back("implementation"); |
| 75 | return base::Join(components, "::"); |
| 76 | } |
| 77 | |
Steven Moreland | 6ec9eb9 | 2018-02-16 14:21:49 -0800 | [diff] [blame] | 78 | void AST::generateCppImplHeader(Formatter& out) const { |
Steven Moreland | 19f11b5 | 2017-05-12 18:22:21 -0700 | [diff] [blame] | 79 | if (!AST::isInterface()) { |
Steven Moreland | 9c38761 | 2016-09-07 09:54:26 -0700 | [diff] [blame] | 80 | // types.hal does not get a stub header. |
Steven Moreland | 6ec9eb9 | 2018-02-16 14:21:49 -0800 | [diff] [blame] | 81 | return; |
Steven Moreland | 9c38761 | 2016-09-07 09:54:26 -0700 | [diff] [blame] | 82 | } |
| 83 | |
Timur Iskhakov | cb0ba52 | 2017-07-17 20:01:37 -0700 | [diff] [blame] | 84 | const Interface* iface = mRootScope.getInterface(); |
Jayant Chowdhary | 3f32c1f | 2016-09-15 16:53:56 -0700 | [diff] [blame] | 85 | const std::string baseName = iface->getBaseName(); |
Steven Moreland | 9c38761 | 2016-09-07 09:54:26 -0700 | [diff] [blame] | 86 | |
Steven Moreland | 7fa8426 | 2018-09-26 17:12:14 -0700 | [diff] [blame] | 87 | out << "// FIXME: your file license if you have one\n\n"; |
Steven Moreland | e3e219f | 2018-11-08 13:19:29 -0800 | [diff] [blame] | 88 | out << "#pragma once\n\n"; |
Steven Moreland | 9c38761 | 2016-09-07 09:54:26 -0700 | [diff] [blame] | 89 | |
Neel Mehta | 9200af0 | 2019-07-19 13:24:57 -0700 | [diff] [blame] | 90 | generateCppPackageInclude(out, mPackage, iface->definedName()); |
Steven Moreland | 9c38761 | 2016-09-07 09:54:26 -0700 | [diff] [blame] | 91 | |
Andreas Huber | 4bcf97d | 2016-08-30 11:27:49 -0700 | [diff] [blame] | 92 | out << "#include <hidl/MQDescriptor.h>\n"; |
Steven Moreland | 41c6d2e | 2016-11-07 12:26:54 -0800 | [diff] [blame] | 93 | out << "#include <hidl/Status.h>\n\n"; |
Steven Moreland | 9c38761 | 2016-09-07 09:54:26 -0700 | [diff] [blame] | 94 | |
Steven Moreland | ac816ff | 2019-12-09 15:29:13 -0800 | [diff] [blame] | 95 | const std::string nspace = getImplNamespace(mPackage); |
| 96 | out << "namespace " << nspace << " {\n\n"; |
Steven Moreland | 9c38761 | 2016-09-07 09:54:26 -0700 | [diff] [blame] | 97 | |
Steven Moreland | e429a26 | 2016-11-15 09:54:32 -0800 | [diff] [blame] | 98 | out << "using ::android::hardware::hidl_array;\n"; |
Martijn Coenen | 99e6beb | 2016-12-01 15:48:42 +0100 | [diff] [blame] | 99 | out << "using ::android::hardware::hidl_memory;\n"; |
Steven Moreland | e429a26 | 2016-11-15 09:54:32 -0800 | [diff] [blame] | 100 | out << "using ::android::hardware::hidl_string;\n"; |
| 101 | out << "using ::android::hardware::hidl_vec;\n"; |
Steven Moreland | 9c38761 | 2016-09-07 09:54:26 -0700 | [diff] [blame] | 102 | out << "using ::android::hardware::Return;\n"; |
| 103 | out << "using ::android::hardware::Void;\n"; |
Steven Moreland | 9c38761 | 2016-09-07 09:54:26 -0700 | [diff] [blame] | 104 | out << "using ::android::sp;\n"; |
| 105 | |
| 106 | out << "\n"; |
| 107 | |
Steven Moreland | ac816ff | 2019-12-09 15:29:13 -0800 | [diff] [blame] | 108 | out << "struct " << baseName << " : public " << iface->fqName().sanitizedVersion() |
| 109 | << "::" << iface->definedName() << " {\n"; |
Steven Moreland | 9c38761 | 2016-09-07 09:54:26 -0700 | [diff] [blame] | 110 | |
| 111 | out.indent(); |
| 112 | |
Steven Moreland | 6ec9eb9 | 2018-02-16 14:21:49 -0800 | [diff] [blame] | 113 | generateMethods(out, [&](const Method* method, const Interface*) { |
Yifan Hong | 068c552 | 2016-10-31 14:07:25 -0700 | [diff] [blame] | 114 | // ignore HIDL reserved methods -- implemented in IFoo already. |
| 115 | if (method->isHidlReserved()) { |
Steven Moreland | 6ec9eb9 | 2018-02-16 14:21:49 -0800 | [diff] [blame] | 116 | return; |
Yifan Hong | 068c552 | 2016-10-31 14:07:25 -0700 | [diff] [blame] | 117 | } |
| 118 | method->generateCppSignature(out, "" /* className */, |
| 119 | false /* specifyNamespaces */); |
| 120 | out << " override;\n"; |
Yifan Hong | 068c552 | 2016-10-31 14:07:25 -0700 | [diff] [blame] | 121 | }); |
Steven Moreland | 9c38761 | 2016-09-07 09:54:26 -0700 | [diff] [blame] | 122 | |
Steven Moreland | 9c38761 | 2016-09-07 09:54:26 -0700 | [diff] [blame] | 123 | out.unindent(); |
| 124 | |
| 125 | out << "};\n\n"; |
| 126 | |
Steven Moreland | 3918fcc | 2017-04-21 10:54:27 -0700 | [diff] [blame] | 127 | out << "// FIXME: most likely delete, this is only for passthrough implementations\n" |
Neel Mehta | 9200af0 | 2019-07-19 13:24:57 -0700 | [diff] [blame] | 128 | << "// extern \"C\" " << iface->definedName() << "* "; |
| 129 | generateFetchSymbol(out, iface->definedName()); |
Steven Moreland | 9c38761 | 2016-09-07 09:54:26 -0700 | [diff] [blame] | 130 | out << "(const char* name);\n\n"; |
| 131 | |
Steven Moreland | ac816ff | 2019-12-09 15:29:13 -0800 | [diff] [blame] | 132 | out << "} // namespace " << nspace << "\n"; |
Steven Moreland | 9c38761 | 2016-09-07 09:54:26 -0700 | [diff] [blame] | 133 | } |
| 134 | |
Steven Moreland | 6ec9eb9 | 2018-02-16 14:21:49 -0800 | [diff] [blame] | 135 | void AST::generateCppImplSource(Formatter& out) const { |
Steven Moreland | 19f11b5 | 2017-05-12 18:22:21 -0700 | [diff] [blame] | 136 | if (!AST::isInterface()) { |
Steven Moreland | 9c38761 | 2016-09-07 09:54:26 -0700 | [diff] [blame] | 137 | // types.hal does not get a stub header. |
Steven Moreland | 6ec9eb9 | 2018-02-16 14:21:49 -0800 | [diff] [blame] | 138 | return; |
Steven Moreland | 9c38761 | 2016-09-07 09:54:26 -0700 | [diff] [blame] | 139 | } |
| 140 | |
Timur Iskhakov | cb0ba52 | 2017-07-17 20:01:37 -0700 | [diff] [blame] | 141 | const Interface* iface = mRootScope.getInterface(); |
Jayant Chowdhary | 3f32c1f | 2016-09-15 16:53:56 -0700 | [diff] [blame] | 142 | const std::string baseName = iface->getBaseName(); |
Steven Moreland | 9c38761 | 2016-09-07 09:54:26 -0700 | [diff] [blame] | 143 | |
Steven Moreland | 7fa8426 | 2018-09-26 17:12:14 -0700 | [diff] [blame] | 144 | out << "// FIXME: your file license if you have one\n\n"; |
Steven Moreland | 9c38761 | 2016-09-07 09:54:26 -0700 | [diff] [blame] | 145 | out << "#include \"" << baseName << ".h\"\n\n"; |
| 146 | |
Steven Moreland | ac816ff | 2019-12-09 15:29:13 -0800 | [diff] [blame] | 147 | const std::string nspace = getImplNamespace(mPackage); |
| 148 | out << "namespace " << nspace << " {\n\n"; |
Steven Moreland | 9c38761 | 2016-09-07 09:54:26 -0700 | [diff] [blame] | 149 | |
Steven Moreland | 6ec9eb9 | 2018-02-16 14:21:49 -0800 | [diff] [blame] | 150 | generateMethods(out, [&](const Method* method, const Interface*) { |
| 151 | generateStubImplMethod(out, baseName, method); |
Yifan Hong | 068c552 | 2016-10-31 14:07:25 -0700 | [diff] [blame] | 152 | }); |
Steven Moreland | 9c38761 | 2016-09-07 09:54:26 -0700 | [diff] [blame] | 153 | |
Neel Mehta | abe1e7d | 2019-08-21 12:18:06 -0700 | [diff] [blame] | 154 | out.pushLinePrefix("//"); |
Neel Mehta | 9200af0 | 2019-07-19 13:24:57 -0700 | [diff] [blame] | 155 | out << iface->definedName() << "* "; |
| 156 | generateFetchSymbol(out, iface->definedName()); |
Steven Moreland | 9c38761 | 2016-09-07 09:54:26 -0700 | [diff] [blame] | 157 | out << "(const char* /* name */) {\n"; |
| 158 | out.indent(); |
| 159 | out << "return new " << baseName << "();\n"; |
| 160 | out.unindent(); |
| 161 | out << "}\n\n"; |
Neel Mehta | abe1e7d | 2019-08-21 12:18:06 -0700 | [diff] [blame] | 162 | out.popLinePrefix(); |
Steven Moreland | 9c38761 | 2016-09-07 09:54:26 -0700 | [diff] [blame] | 163 | |
Steven Moreland | ac816ff | 2019-12-09 15:29:13 -0800 | [diff] [blame] | 164 | out << "} // namespace " << nspace << "\n"; |
Steven Moreland | 9c38761 | 2016-09-07 09:54:26 -0700 | [diff] [blame] | 165 | } |
| 166 | |
| 167 | } // namespace android |