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