1 /*
2  * Copyright (C) 2019 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 <android-base/file.h>
18 #include <android-base/logging.h>
19 #include <android-base/strings.h>
20 #include <hidl-util/FQName.h>
21 #include <hidl-util/Formatter.h>
22 #include <hidl-util/StringHelper.h>
23 #include <iostream>
24 #include <set>
25 #include <string>
26 #include <vector>
27 
28 #include "AidlHelper.h"
29 #include "ArrayType.h"
30 #include "CompoundType.h"
31 #include "Coordinator.h"
32 #include "Interface.h"
33 #include "Method.h"
34 #include "NamedType.h"
35 #include "Reference.h"
36 #include "Scope.h"
37 
38 namespace android {
39 
40 Formatter* AidlHelper::notesFormatter = nullptr;
41 std::string AidlHelper::fileHeader = "";
42 bool AidlHelper::expandExtended = false;
43 
notes()44 Formatter& AidlHelper::notes() {
45     CHECK(notesFormatter != nullptr);
46     return *notesFormatter;
47 }
48 
setNotes(Formatter * formatter)49 void AidlHelper::setNotes(Formatter* formatter) {
50     CHECK(formatter != nullptr);
51     notesFormatter = formatter;
52 }
53 
getAidlName(const FQName & fqName,AidlBackend backend)54 std::string AidlHelper::getAidlName(const FQName& fqName, AidlBackend backend) {
55     std::vector<std::string> names;
56     for (const std::string& name : fqName.names()) {
57         names.push_back(StringHelper::Capitalize(name));
58     }
59     switch (backend) {
60         case AidlBackend::CPP:
61             /* fall through */
62         case AidlBackend::NDK:
63             return StringHelper::JoinStrings(names, "::");
64         case AidlBackend::JAVA:
65             return StringHelper::JoinStrings(names, ".");
66         case AidlBackend::UNKNOWN:
67             /* fall through */
68         default:
69             return StringHelper::Capitalize(names.back());
70     }
71 }
72 
getAidlPackage(const FQName & fqName)73 std::string AidlHelper::getAidlPackage(const FQName& fqName) {
74     std::string aidlPackage = fqName.package();
75     if (fqName.getPackageMajorVersion() != 1) {
76         aidlPackage += std::to_string(fqName.getPackageMajorVersion());
77     }
78 
79     return aidlPackage;
80 }
81 
getAidlPackagePath(const FQName & fqName)82 std::string AidlHelper::getAidlPackagePath(const FQName& fqName) {
83     return base::Join(base::Split(AidlHelper::getAidlPackage(fqName), "."), "/");
84 }
85 
getAidlFQName(const FQName & fqName)86 std::optional<std::string> AidlHelper::getAidlFQName(const FQName& fqName) {
87     std::optional<const ReplacedTypeInfo> type = getAidlReplacedType(fqName);
88     if (type) {
89         return type.value().aidlReplacedFQName;
90     }
91     return getAidlPackage(fqName) + "." + getAidlName(fqName);
92 }
93 
getTopLevelType(const NamedType * type)94 const NamedType* AidlHelper::getTopLevelType(const NamedType* type) {
95     if (type->parent() && type->parent()->fqName().hasVersion()) {
96         auto base_type = type->parent();
97         while (base_type->parent() && base_type->parent()->fqName().hasVersion()) {
98             base_type = base_type->parent();
99         }
100         return base_type;
101     } else {
102         return type;
103     }
104 }
105 
importLocallyReferencedType(const Type & scope,const Type & type,std::set<FQName> * imports)106 static void importLocallyReferencedType(const Type& scope, const Type& type,
107                                         std::set<FQName>* imports) {
108     if (type.isArray()) {
109         return importLocallyReferencedType(
110                 scope, *static_cast<const ArrayType*>(&type)->getElementType(), imports);
111     }
112     if (type.isTemplatedType()) {
113         return importLocallyReferencedType(
114                 scope, *static_cast<const TemplatedType*>(&type)->getElementType(), imports);
115     }
116 
117     if (!type.isNamedType()) return;
118     const NamedType& namedType = *static_cast<const NamedType*>(&type);
119     // If this type has the same top level type as the scope, then it is defined
120     // in the same file and does not need to be imported.
121     if (scope.isNamedType()) {
122         const auto& scopeTopLevel =
123                 AidlHelper::getTopLevelType(static_cast<const NamedType*>(&scope));
124         const auto& thisTopLevel = AidlHelper::getTopLevelType(&namedType);
125         // The fqName might not be equal because of differing HIDL versions for the
126         // top level type. Generated AIDL does not have these differences in
127         // versions, so we can test the equality of the name.
128         if (scopeTopLevel->fqName().name() == thisTopLevel->fqName().name()) return;
129     }
130     imports->insert(namedType.fqName());
131 }
132 
133 // This tries iterating over the HIDL AST which is a bit messy because
134 // it has to encode the logic in the rest of hidl2aidl. It would be better
135 // if we could iterate over the AIDL structure which has already been
136 // processed.
emitFileHeader(Formatter & out,const NamedType & type,const std::map<const NamedType *,const ProcessedCompoundType> & processedTypes)137 void AidlHelper::emitFileHeader(
138         Formatter& out, const NamedType& type,
139         const std::map<const NamedType*, const ProcessedCompoundType>& processedTypes) {
140     AidlHelper::emitFileHeader(out);
141     out << "package " << getAidlPackage(type.fqName()) << ";\n\n";
142 
143     std::set<FQName> imports;
144 
145     // Import all the referenced types
146     if (type.isInterface()) {
147         // This is a separate case because getReferences doesn't traverse all the superTypes and
148         // sometimes includes references to types that would not exist on AIDL
149         const std::vector<const Method*>& methods =
150                 getUserDefinedMethods(out, static_cast<const Interface&>(type));
151         for (const Method* method : methods) {
152             for (const Reference<Type>* ref : method->getReferences()) {
153                 importLocallyReferencedType(type, *ref->get(), &imports);
154             }
155         }
156     } else if (type.isCompoundType()) {
157         // Get all of the imports for the flattened compound type that may
158         // include additional fields and subtypes from older versions
159         const auto& it = processedTypes.find(&type);
160         CHECK(it != processedTypes.end()) << "Failed to find " << type.fullName();
161         const ProcessedCompoundType& processedType = it->second;
162 
163         for (const auto& field : processedType.fields) {
164             importLocallyReferencedType(type, *field.field->get(), &imports);
165         }
166     } else {
167         for (const Reference<Type>* ref : type.getReferences()) {
168             if (ref->get()->definedName() == type.fqName().name()) {
169                 // Don't import the referenced type if this is referencing itself
170                 continue;
171             }
172             importLocallyReferencedType(type, *ref->get(), &imports);
173         }
174     }
175 
176     const FQName& relativeTo = type.fqName();
177     for (const auto& fqName : imports) {
178         // Import all the defined types since they will now be in a different file.
179         // No need to import types from different packages because they're referenced with FQName.
180         // See AidlHelper::getAidlType()
181         if (getAidlPackage(relativeTo) != getAidlPackage(fqName)) continue;
182 
183         std::optional<std::string> import = AidlHelper::getAidlFQName(fqName);
184         if (import) {
185             out << "import " << import.value() << ";\n";
186         }
187     }
188 
189     if (imports.size() > 0) {
190         out << "\n";
191     }
192 }
193 
getFileWithHeader(const NamedType & namedType,const Coordinator & coordinator,const std::map<const NamedType *,const ProcessedCompoundType> & processedTypes)194 Formatter AidlHelper::getFileWithHeader(
195         const NamedType& namedType, const Coordinator& coordinator,
196         const std::map<const NamedType*, const ProcessedCompoundType>& processedTypes) {
197     Formatter out =
198             coordinator.getFormatter(namedType.fqName(), Coordinator::Location::DIRECT,
199                                      AidlHelper::getAidlPackagePath(namedType.fqName()) + "/" +
200                                              getAidlName(namedType.fqName()) + ".aidl");
201     emitFileHeader(out, namedType, processedTypes);
202     return out;
203 }
204 
processCompoundType(const CompoundType & compoundType,ProcessedCompoundType * processedType,const std::string & fieldNamePrefix)205 void AidlHelper::processCompoundType(const CompoundType& compoundType,
206                                      ProcessedCompoundType* processedType,
207                                      const std::string& fieldNamePrefix) {
208     // Gather all of the subtypes defined in this type
209     for (const NamedType* subType : compoundType.getSubTypes()) {
210         processedType->subTypes.insert(subType);
211     }
212     std::pair<size_t, size_t> version = compoundType.fqName().hasVersion()
213                                                 ? compoundType.fqName().getVersion()
214                                                 : std::pair<size_t, size_t>{0, 0};
215     for (const NamedReference<Type>* field : compoundType.getFields()) {
216         // Check for references to another version of itself
217         if (field->get()->typeName() == compoundType.typeName()) {
218             if (AidlHelper::shouldBeExpanded(
219                         static_cast<const CompoundType&>(*field->get()).fqName(),
220                         compoundType.fqName())) {
221                 processCompoundType(static_cast<const CompoundType&>(*field->get()), processedType,
222                                     fieldNamePrefix + field->name() + ".");
223             } else {
224                 // Keep this field as is
225                 processedType->fields.push_back({field, fieldNamePrefix + field->name(), version});
226             }
227         } else {
228             // Handle duplicate field names. Keep only the most recent definitions.
229             auto it = std::find_if(processedType->fields.begin(), processedType->fields.end(),
230                                    [field](auto& processedField) {
231                                        return processedField.field->name() == field->name();
232                                    });
233             if (it != processedType->fields.end()) {
234                 AidlHelper::notes()
235                         << "Found conflicting field name \"" << field->name()
236                         << "\" in different versions of " << compoundType.fqName().name() << ". ";
237 
238                 if (version.first > it->version.first ||
239                     (version.first == it->version.first && version.second > it->version.second)) {
240                     AidlHelper::notes()
241                             << "Keeping " << field->get()->typeName() << " from " << version.first
242                             << "." << version.second << " and discarding "
243                             << (it->field)->get()->typeName() << " from " << it->version.first
244                             << "." << it->version.second << ".\n";
245                     it->fullName = fieldNamePrefix + field->name();
246                     it->field = field;
247                     it->version = version;
248                 } else {
249                     AidlHelper::notes()
250                             << "Keeping " << (it->field)->get()->typeName() << " from "
251                             << it->version.first << "." << it->version.second << " and discarding "
252                             << field->get()->typeName() << " from " << version.first << "."
253                             << version.second << ".\n";
254                 }
255             } else {
256                 processedType->fields.push_back({field, fieldNamePrefix + field->name(), version});
257             }
258         }
259     }
260 }
261 
setFileHeader(const std::string & file)262 void AidlHelper::setFileHeader(const std::string& file) {
263     if (!file.empty()) {
264         if (!android::base::ReadFileToString(file, &fileHeader)) {
265             std::cerr << "ERROR: Failed to find license file: " << file << "\n";
266             exit(1);
267         }
268     }
269 }
270 
emitFileHeader(Formatter & out)271 void AidlHelper::emitFileHeader(Formatter& out) {
272     if (fileHeader.empty()) {
273         out << "// FIXME: license file, or use the -l option to generate the files with the "
274                "header.\n\n";
275     } else {
276         out << fileHeader << "\n";
277     }
278 }
279 
shouldBeExpanded(const FQName & a,const FQName & b)280 bool AidlHelper::shouldBeExpanded(const FQName& a, const FQName& b) {
281     return a.package() == b.package() || expandExtended;
282 }
283 
284 }  // namespace android
285