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