blob: 148511966f14fc2db980da8b522848bde17a7b67 [file] [log] [blame]
Steven Moreland9c387612016-09-07 09:54:26 -07001/*
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 Moreland9c387612016-09-07 09:54:26 -070021#include "Interface.h"
22#include "Method.h"
Timur Iskhakov7fa79f62017-08-09 11:04:54 -070023#include "Reference.h"
Steven Moreland9c387612016-09-07 09:54:26 -070024#include "ScalarType.h"
25#include "Scope.h"
26
27#include <algorithm>
Iliyan Malcheva72e0d22016-09-09 11:03:08 -070028#include <hidl-util/Formatter.h>
Steven Moreland9c387612016-09-07 09:54:26 -070029#include <android-base/logging.h>
30#include <string>
31#include <vector>
32#include <set>
33
Steven Moreland9c387612016-09-07 09:54:26 -070034namespace android {
35
Steven Moreland9c387612016-09-07 09:54:26 -070036void AST::generateFetchSymbol(Formatter &out, const std::string& ifaceName) const {
37 out << "HIDL_FETCH_" << ifaceName;
38}
39
Steven Moreland6ec9eb92018-02-16 14:21:49 -080040void AST::generateStubImplMethod(Formatter& out, const std::string& className,
41 const Method* method) const {
Yifan Hong1254b552016-10-27 15:03:29 -070042 // ignore HIDL reserved methods -- implemented in IFoo already.
43 if (method->isHidlReserved()) {
Steven Moreland6ec9eb92018-02-16 14:21:49 -080044 return;
Yifan Hong1254b552016-10-27 15:03:29 -070045 }
46
Yifan Hong068c5522016-10-31 14:07:25 -070047 method->generateCppSignature(out, className, false /* specifyNamespaces */);
Steven Moreland9c387612016-09-07 09:54:26 -070048
49 out << " {\n";
50
51 out.indent();
52 out << "// TODO implement\n";
53
Timur Iskhakov7fa79f62017-08-09 11:04:54 -070054 const NamedReference<Type>* elidedReturn = method->canElideCallback();
Steven Moreland9c387612016-09-07 09:54:26 -070055
56 if (elidedReturn == nullptr) {
57 out << "return Void();\n";
58 } else {
Steven Moreland9c387612016-09-07 09:54:26 -070059 out << "return "
Yifan Hong3b320f82016-11-01 15:15:54 -070060 << elidedReturn->type().getCppResultType()
Steven Moreland9c387612016-09-07 09:54:26 -070061 << " {};\n";
62 }
63
64 out.unindent();
65
66 out << "}\n\n";
67
Steven Moreland6ec9eb92018-02-16 14:21:49 -080068 return;
Steven Moreland9c387612016-09-07 09:54:26 -070069}
70
Steven Moreland6ec9eb92018-02-16 14:21:49 -080071void AST::generateCppImplHeader(Formatter& out) const {
Steven Moreland19f11b52017-05-12 18:22:21 -070072 if (!AST::isInterface()) {
Steven Moreland9c387612016-09-07 09:54:26 -070073 // types.hal does not get a stub header.
Steven Moreland6ec9eb92018-02-16 14:21:49 -080074 return;
Steven Moreland9c387612016-09-07 09:54:26 -070075 }
76
Timur Iskhakovcb0ba522017-07-17 20:01:37 -070077 const Interface* iface = mRootScope.getInterface();
Jayant Chowdhary3f32c1f2016-09-15 16:53:56 -070078 const std::string baseName = iface->getBaseName();
Steven Moreland9c387612016-09-07 09:54:26 -070079
Steven Moreland7fa84262018-09-26 17:12:14 -070080 out << "// FIXME: your file license if you have one\n\n";
Steven Morelande3e219f2018-11-08 13:19:29 -080081 out << "#pragma once\n\n";
Steven Moreland9c387612016-09-07 09:54:26 -070082
Yifan Hongeefe4f22017-01-04 15:32:42 -080083 generateCppPackageInclude(out, mPackage, iface->localName());
Steven Moreland9c387612016-09-07 09:54:26 -070084
Andreas Huber4bcf97d2016-08-30 11:27:49 -070085 out << "#include <hidl/MQDescriptor.h>\n";
Steven Moreland41c6d2e2016-11-07 12:26:54 -080086 out << "#include <hidl/Status.h>\n\n";
Steven Moreland9c387612016-09-07 09:54:26 -070087
88 enterLeaveNamespace(out, true /* enter */);
89 out << "namespace implementation {\n\n";
90
Steven Morelande429a262016-11-15 09:54:32 -080091 out << "using ::android::hardware::hidl_array;\n";
Martijn Coenen99e6beb2016-12-01 15:48:42 +010092 out << "using ::android::hardware::hidl_memory;\n";
Steven Morelande429a262016-11-15 09:54:32 -080093 out << "using ::android::hardware::hidl_string;\n";
94 out << "using ::android::hardware::hidl_vec;\n";
Steven Moreland9c387612016-09-07 09:54:26 -070095 out << "using ::android::hardware::Return;\n";
96 out << "using ::android::hardware::Void;\n";
Steven Moreland9c387612016-09-07 09:54:26 -070097 out << "using ::android::sp;\n";
98
99 out << "\n";
100
101 out << "struct "
102 << baseName
103 << " : public "
Steven Moreland19f11b52017-05-12 18:22:21 -0700104 << iface->localName()
Steven Moreland9c387612016-09-07 09:54:26 -0700105 << " {\n";
106
107 out.indent();
108
Steven Moreland6ec9eb92018-02-16 14:21:49 -0800109 generateMethods(out, [&](const Method* method, const Interface*) {
Yifan Hong068c5522016-10-31 14:07:25 -0700110 // ignore HIDL reserved methods -- implemented in IFoo already.
111 if (method->isHidlReserved()) {
Steven Moreland6ec9eb92018-02-16 14:21:49 -0800112 return;
Yifan Hong068c5522016-10-31 14:07:25 -0700113 }
114 method->generateCppSignature(out, "" /* className */,
115 false /* specifyNamespaces */);
116 out << " override;\n";
Yifan Hong068c5522016-10-31 14:07:25 -0700117 });
Steven Moreland9c387612016-09-07 09:54:26 -0700118
Steven Moreland9c387612016-09-07 09:54:26 -0700119 out.unindent();
120
121 out << "};\n\n";
122
Steven Moreland3918fcc2017-04-21 10:54:27 -0700123 out << "// FIXME: most likely delete, this is only for passthrough implementations\n"
124 << "// extern \"C\" "
Steven Moreland19f11b52017-05-12 18:22:21 -0700125 << iface->localName()
Steven Moreland9c387612016-09-07 09:54:26 -0700126 << "* ";
Steven Moreland19f11b52017-05-12 18:22:21 -0700127 generateFetchSymbol(out, iface->localName());
Steven Moreland9c387612016-09-07 09:54:26 -0700128 out << "(const char* name);\n\n";
129
130 out << "} // namespace implementation\n";
131 enterLeaveNamespace(out, false /* leave */);
Steven Moreland9c387612016-09-07 09:54:26 -0700132}
133
Steven Moreland6ec9eb92018-02-16 14:21:49 -0800134void AST::generateCppImplSource(Formatter& out) const {
Steven Moreland19f11b52017-05-12 18:22:21 -0700135 if (!AST::isInterface()) {
Steven Moreland9c387612016-09-07 09:54:26 -0700136 // types.hal does not get a stub header.
Steven Moreland6ec9eb92018-02-16 14:21:49 -0800137 return;
Steven Moreland9c387612016-09-07 09:54:26 -0700138 }
139
Timur Iskhakovcb0ba522017-07-17 20:01:37 -0700140 const Interface* iface = mRootScope.getInterface();
Jayant Chowdhary3f32c1f2016-09-15 16:53:56 -0700141 const std::string baseName = iface->getBaseName();
Steven Moreland9c387612016-09-07 09:54:26 -0700142
Steven Moreland7fa84262018-09-26 17:12:14 -0700143 out << "// FIXME: your file license if you have one\n\n";
Steven Moreland9c387612016-09-07 09:54:26 -0700144 out << "#include \"" << baseName << ".h\"\n\n";
145
146 enterLeaveNamespace(out, true /* enter */);
147 out << "namespace implementation {\n\n";
148
Steven Moreland6ec9eb92018-02-16 14:21:49 -0800149 generateMethods(out, [&](const Method* method, const Interface*) {
150 generateStubImplMethod(out, baseName, method);
Yifan Hong068c5522016-10-31 14:07:25 -0700151 });
Steven Moreland9c387612016-09-07 09:54:26 -0700152
Steven Moreland3918fcc2017-04-21 10:54:27 -0700153 out.setLinePrefix("//");
Steven Moreland19f11b52017-05-12 18:22:21 -0700154 out << iface->localName()
Steven Moreland9c387612016-09-07 09:54:26 -0700155 << "* ";
Steven Moreland19f11b52017-05-12 18:22:21 -0700156 generateFetchSymbol(out, iface->localName());
Steven Moreland9c387612016-09-07 09:54:26 -0700157 out << "(const char* /* name */) {\n";
158 out.indent();
159 out << "return new " << baseName << "();\n";
160 out.unindent();
161 out << "}\n\n";
Steven Moreland3918fcc2017-04-21 10:54:27 -0700162 out.unsetLinePrefix();
Steven Moreland9c387612016-09-07 09:54:26 -0700163
Yifan Hongdfc4b9c2016-12-02 16:31:31 -0800164 out << "} // namespace implementation\n";
Steven Moreland9c387612016-09-07 09:54:26 -0700165 enterLeaveNamespace(out, false /* leave */);
Steven Moreland9c387612016-09-07 09:54:26 -0700166}
167
168} // namespace android