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"
21 #include "Interface.h"
22 #include "Method.h"
23 #include "Reference.h"
24 #include "ScalarType.h"
25 #include "Scope.h"
26
27 #include <algorithm>
28 #include <hidl-util/Formatter.h>
29 #include <android-base/logging.h>
30 #include <string>
31 #include <vector>
32 #include <set>
33
34 namespace android {
35
generateFetchSymbol(Formatter & out,const std::string & ifaceName) const36 void AST::generateFetchSymbol(Formatter &out, const std::string& ifaceName) const {
37 out << "HIDL_FETCH_" << ifaceName;
38 }
39
generateStubImplMethod(Formatter & out,const std::string & className,const Method * method) const40 void AST::generateStubImplMethod(Formatter& out, const std::string& className,
41 const Method* method) const {
42 // ignore HIDL reserved methods -- implemented in IFoo already.
43 if (method->isHidlReserved()) {
44 return;
45 }
46
47 method->generateCppSignature(out, className, false /* specifyNamespaces */);
48
49 out << " {\n";
50
51 out.indent();
52 out << "// TODO implement\n";
53
54 const NamedReference<Type>* elidedReturn = method->canElideCallback();
55
56 if (elidedReturn == nullptr) {
57 out << "return Void();\n";
58 } else {
59 out << "return "
60 << elidedReturn->type().getCppResultType()
61 << " {};\n";
62 }
63
64 out.unindent();
65
66 out << "}\n\n";
67
68 return;
69 }
70
generateCppImplHeader(Formatter & out) const71 void AST::generateCppImplHeader(Formatter& out) const {
72 if (!AST::isInterface()) {
73 // types.hal does not get a stub header.
74 return;
75 }
76
77 const Interface* iface = mRootScope.getInterface();
78 const std::string baseName = iface->getBaseName();
79
80 out << "// FIXME: your file license if you have one\n\n";
81 out << "#pragma once\n\n";
82
83 generateCppPackageInclude(out, mPackage, iface->localName());
84
85 out << "#include <hidl/MQDescriptor.h>\n";
86 out << "#include <hidl/Status.h>\n\n";
87
88 enterLeaveNamespace(out, true /* enter */);
89 out << "namespace implementation {\n\n";
90
91 out << "using ::android::hardware::hidl_array;\n";
92 out << "using ::android::hardware::hidl_memory;\n";
93 out << "using ::android::hardware::hidl_string;\n";
94 out << "using ::android::hardware::hidl_vec;\n";
95 out << "using ::android::hardware::Return;\n";
96 out << "using ::android::hardware::Void;\n";
97 out << "using ::android::sp;\n";
98
99 out << "\n";
100
101 out << "struct "
102 << baseName
103 << " : public "
104 << iface->localName()
105 << " {\n";
106
107 out.indent();
108
109 generateMethods(out, [&](const Method* method, const Interface*) {
110 // ignore HIDL reserved methods -- implemented in IFoo already.
111 if (method->isHidlReserved()) {
112 return;
113 }
114 method->generateCppSignature(out, "" /* className */,
115 false /* specifyNamespaces */);
116 out << " override;\n";
117 });
118
119 out.unindent();
120
121 out << "};\n\n";
122
123 out << "// FIXME: most likely delete, this is only for passthrough implementations\n"
124 << "// extern \"C\" "
125 << iface->localName()
126 << "* ";
127 generateFetchSymbol(out, iface->localName());
128 out << "(const char* name);\n\n";
129
130 out << "} // namespace implementation\n";
131 enterLeaveNamespace(out, false /* leave */);
132 }
133
generateCppImplSource(Formatter & out) const134 void AST::generateCppImplSource(Formatter& out) const {
135 if (!AST::isInterface()) {
136 // types.hal does not get a stub header.
137 return;
138 }
139
140 const Interface* iface = mRootScope.getInterface();
141 const std::string baseName = iface->getBaseName();
142
143 out << "// FIXME: your file license if you have one\n\n";
144 out << "#include \"" << baseName << ".h\"\n\n";
145
146 enterLeaveNamespace(out, true /* enter */);
147 out << "namespace implementation {\n\n";
148
149 generateMethods(out, [&](const Method* method, const Interface*) {
150 generateStubImplMethod(out, baseName, method);
151 });
152
153 out.setLinePrefix("//");
154 out << iface->localName()
155 << "* ";
156 generateFetchSymbol(out, iface->localName());
157 out << "(const char* /* name */) {\n";
158 out.indent();
159 out << "return new " << baseName << "();\n";
160 out.unindent();
161 out << "}\n\n";
162 out.unsetLinePrefix();
163
164 out << "} // namespace implementation\n";
165 enterLeaveNamespace(out, false /* leave */);
166 }
167
168 } // namespace android
169