• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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     const std::string guard = makeHeaderGuard(baseName, false /* indicateGenerated */);
81 
82     out << "#ifndef " << guard << "\n";
83     out << "#define " << guard << "\n\n";
84 
85     generateCppPackageInclude(out, mPackage, iface->localName());
86 
87     out << "#include <hidl/MQDescriptor.h>\n";
88     out << "#include <hidl/Status.h>\n\n";
89 
90     enterLeaveNamespace(out, true /* enter */);
91     out << "namespace implementation {\n\n";
92 
93     out << "using ::android::hardware::hidl_array;\n";
94     out << "using ::android::hardware::hidl_memory;\n";
95     out << "using ::android::hardware::hidl_string;\n";
96     out << "using ::android::hardware::hidl_vec;\n";
97     out << "using ::android::hardware::Return;\n";
98     out << "using ::android::hardware::Void;\n";
99     out << "using ::android::sp;\n";
100 
101     out << "\n";
102 
103     out << "struct "
104         << baseName
105         << " : public "
106         << iface->localName()
107         << " {\n";
108 
109     out.indent();
110 
111     generateMethods(out, [&](const Method* method, const Interface*) {
112         // ignore HIDL reserved methods -- implemented in IFoo already.
113         if (method->isHidlReserved()) {
114             return;
115         }
116         method->generateCppSignature(out, "" /* className */,
117                 false /* specifyNamespaces */);
118         out << " override;\n";
119     });
120 
121     out.unindent();
122 
123     out << "};\n\n";
124 
125     out << "// FIXME: most likely delete, this is only for passthrough implementations\n"
126         << "// extern \"C\" "
127         << iface->localName()
128         << "* ";
129     generateFetchSymbol(out, iface->localName());
130     out << "(const char* name);\n\n";
131 
132     out << "}  // namespace implementation\n";
133     enterLeaveNamespace(out, false /* leave */);
134 
135     out << "\n#endif  // " << guard << "\n";
136 }
137 
generateCppImplSource(Formatter & out) const138 void AST::generateCppImplSource(Formatter& out) const {
139     if (!AST::isInterface()) {
140         // types.hal does not get a stub header.
141         return;
142     }
143 
144     const Interface* iface = mRootScope.getInterface();
145     const std::string baseName = iface->getBaseName();
146 
147     out << "#include \"" << baseName << ".h\"\n\n";
148 
149     enterLeaveNamespace(out, true /* enter */);
150     out << "namespace implementation {\n\n";
151 
152     generateMethods(out, [&](const Method* method, const Interface*) {
153         generateStubImplMethod(out, baseName, method);
154     });
155 
156     out.setLinePrefix("//");
157     out << iface->localName()
158         << "* ";
159     generateFetchSymbol(out, iface->localName());
160     out << "(const char* /* name */) {\n";
161     out.indent();
162     out << "return new " << baseName << "();\n";
163     out.unindent();
164     out << "}\n\n";
165     out.unsetLinePrefix();
166 
167     out << "}  // namespace implementation\n";
168     enterLeaveNamespace(out, false /* leave */);
169 }
170 
171 }  // namespace android
172