blob: 08d1798e3527e66453f61125eaac28e799143556 [file] [log] [blame]
Jiyong Park3656c3c2018-08-01 20:02:01 +09001/*
Will McVickerd7d18df2019-09-12 13:40:50 -07002 * Copyright (C) 2018, The Android Open Source Project
3 *
Jiyong Park3656c3c2018-08-01 20:02:01 +09004 * 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 "aidl.h"
Jiyong Park3656c3c2018-08-01 20:02:01 +090018
19#include <map>
20#include <string>
21#include <vector>
22
Jiyong Park0cf03b12020-07-22 19:36:34 +090023#include <android-base/result.h>
Jiyong Parkf7534672019-08-12 22:06:22 +090024#include <android-base/strings.h>
Jooyung Hanb8a97772021-01-19 01:27:38 +090025#include <gtest/gtest.h>
Jiyong Parkf7534672019-08-12 22:06:22 +090026
Jooyung Han1f56b702021-02-11 13:16:15 +090027#include "aidl_dumpapi.h"
28#include "aidl_language.h"
29#include "import_resolver.h"
30#include "logging.h"
31#include "options.h"
32
Jiyong Park3656c3c2018-08-01 20:02:01 +090033namespace android {
34namespace aidl {
35
Jiyong Park0cf03b12020-07-22 19:36:34 +090036using android::base::Error;
37using android::base::Result;
Jooyung Han00073272020-11-27 14:20:20 +090038using android::base::StartsWith;
Jiyong Park3656c3c2018-08-01 20:02:01 +090039using std::map;
40using std::set;
41using std::string;
42using std::vector;
43
Jooyung Han02a11be2021-02-11 13:18:06 +090044struct DumpForEqualityVisitor : DumpVisitor {
45 DumpForEqualityVisitor(CodeWriter& out) : DumpVisitor(out) {}
46
47 void DumpConstantValue(const AidlTypeSpecifier&, const AidlConstantValue& c) {
48 out << c.Literal();
49 }
50};
51
Jooyung Hanb8a97772021-01-19 01:27:38 +090052static std::string Dump(const AidlDefinedType& type) {
Jooyung Han1f56b702021-02-11 13:16:15 +090053 string code;
54 CodeWriterPtr out = CodeWriter::ForString(&code);
Jooyung Han02a11be2021-02-11 13:18:06 +090055 DumpForEqualityVisitor visitor(*out);
Jooyung Han1f56b702021-02-11 13:16:15 +090056 type.DispatchVisit(visitor);
57 out->Close();
58 return code;
Jooyung Hanb8a97772021-01-19 01:27:38 +090059}
60
61// Uses each type's Dump() and GTest utility(EqHelper).
62static bool CheckEquality(const AidlDefinedType& older, const AidlDefinedType& newer) {
63 using testing::internal::EqHelper;
64 auto older_file = older.GetLocation().GetFile();
65 auto newer_file = newer.GetLocation().GetFile();
66 auto result = EqHelper::Compare(older_file.data(), newer_file.data(), Dump(older), Dump(newer));
67 if (!result) {
68 AIDL_ERROR(newer) << result.failure_message();
69 }
70 return result;
71}
72
Jooyung Han69ea4ba2020-10-29 15:33:37 +090073static vector<string> get_strict_annotations(const AidlAnnotatable& node) {
Steven Moreland7b6a7d92020-04-20 22:00:33 -070074 // This must be symmetrical (if you can add something, you must be able to
75 // remove it). The reason is that we have no way of knowing which interface a
76 // server serves and which interface a client serves (e.g. a callback
77 // interface). Note that this is being overly lenient. It makes sense for
78 // newer code to start accepting nullable things. However, here, we don't know
79 // if the client of an interface or the server of an interface is newer.
80 //
81 // Here are two examples to demonstrate this:
82 // - a new implementation might change so that it no longer returns null
83 // values (remove @nullable)
84 // - a new implementation might start accepting null values (add @nullable)
85 static const set<AidlAnnotation::Type> kIgnoreAnnotations{
86 AidlAnnotation::Type::NULLABLE,
Jooyung Han69ea4ba2020-10-29 15:33:37 +090087 // @JavaDerive doesn't affect read/write
Jooyung Han90345002020-10-23 15:28:53 +090088 AidlAnnotation::Type::JAVA_DERIVE,
Jeongik Chad0a10272020-08-06 16:33:36 +090089 AidlAnnotation::Type::JAVA_ONLY_IMMUTABLE,
Jooyung Han69ea4ba2020-10-29 15:33:37 +090090 // @Backing for a enum type is checked by the enum checker
91 AidlAnnotation::Type::BACKING,
92 // @RustDerive doesn't affect read/write
93 AidlAnnotation::Type::RUST_DERIVE,
Jooyung Hanf8dbbcc2020-12-26 03:05:55 +090094 AidlAnnotation::Type::SUPPRESS_WARNINGS,
Steven Moreland7b6a7d92020-04-20 22:00:33 -070095 };
Jooyung Han69ea4ba2020-10-29 15:33:37 +090096 vector<string> annotations;
Steven Moreland7b6a7d92020-04-20 22:00:33 -070097 for (const AidlAnnotation& annotation : node.GetAnnotations()) {
Jooyung Han00073272020-11-27 14:20:20 +090098 if (kIgnoreAnnotations.find(annotation.GetType()) != kIgnoreAnnotations.end()) {
99 continue;
Steven Moreland7b6a7d92020-04-20 22:00:33 -0700100 }
Jooyung Han00073272020-11-27 14:20:20 +0900101 auto annotation_string = annotation.ToString();
102 // adding @Deprecated (with optional args) is okay
103 if (StartsWith(annotation_string, "@JavaPassthrough(annotation=\"@Deprecated")) {
104 continue;
105 }
106 annotations.push_back(annotation_string);
Steven Moreland7b6a7d92020-04-20 22:00:33 -0700107 }
108 return annotations;
109}
110
Jiyong Park3656c3c2018-08-01 20:02:01 +0900111static bool have_compatible_annotations(const AidlAnnotatable& older,
112 const AidlAnnotatable& newer) {
Jooyung Han69ea4ba2020-10-29 15:33:37 +0900113 vector<string> olderAnnotations = get_strict_annotations(older);
114 vector<string> newerAnnotations = get_strict_annotations(newer);
115 sort(olderAnnotations.begin(), olderAnnotations.end());
116 sort(newerAnnotations.begin(), newerAnnotations.end());
Jeongik Cha3271ffa2018-12-04 15:19:20 +0900117 if (olderAnnotations != newerAnnotations) {
Jiyong Park3656c3c2018-08-01 20:02:01 +0900118 const string from = older.ToString().empty() ? "(empty)" : older.ToString();
119 const string to = newer.ToString().empty() ? "(empty)" : newer.ToString();
120 AIDL_ERROR(newer) << "Changed annotations: " << from << " to " << to;
121 return false;
122 }
123 return true;
124}
125
126static bool are_compatible_types(const AidlTypeSpecifier& older, const AidlTypeSpecifier& newer) {
127 bool compatible = true;
Jooyung Han965e31d2020-11-27 12:30:16 +0900128 if (older.Signature() != newer.Signature()) {
129 AIDL_ERROR(newer) << "Type changed: " << older.Signature() << " to " << newer.Signature()
130 << ".";
Jiyong Park3656c3c2018-08-01 20:02:01 +0900131 compatible = false;
132 }
133 compatible &= have_compatible_annotations(older, newer);
134 return compatible;
135}
136
Jooyung Han829ec7c2020-12-02 12:07:36 +0900137static bool are_compatible_constants(const AidlDefinedType& older, const AidlDefinedType& newer) {
Jooyung Han3f347ca2020-12-01 12:41:50 +0900138 bool compatible = true;
139
140 map<string, AidlConstantDeclaration*> new_constdecls;
141 for (const auto& c : newer.GetConstantDeclarations()) {
142 new_constdecls[c->GetName()] = &*c;
143 }
144
145 for (const auto& old_c : older.GetConstantDeclarations()) {
146 const auto found = new_constdecls.find(old_c->GetName());
147 if (found == new_constdecls.end()) {
148 AIDL_ERROR(old_c) << "Removed constant declaration: " << older.GetCanonicalName() << "."
149 << old_c->GetName();
150 compatible = false;
151 continue;
152 }
153
154 const auto new_c = found->second;
155 compatible &= are_compatible_types(old_c->GetType(), new_c->GetType());
156
Jooyung Hanfdaae1d2020-12-14 13:16:15 +0900157 const string old_value = old_c->GetValue().Literal();
158 const string new_value = new_c->GetValue().Literal();
Jooyung Han3f347ca2020-12-01 12:41:50 +0900159 if (old_value != new_value) {
160 AIDL_ERROR(newer) << "Changed constant value: " << older.GetCanonicalName() << "."
161 << old_c->GetName() << " from " << old_value << " to " << new_value << ".";
162 compatible = false;
163 }
164 }
165 return compatible;
166}
167
Jiyong Park3656c3c2018-08-01 20:02:01 +0900168static bool are_compatible_interfaces(const AidlInterface& older, const AidlInterface& newer) {
169 bool compatible = true;
Jiyong Park3656c3c2018-08-01 20:02:01 +0900170
171 map<string, AidlMethod*> new_methods;
172 for (const auto& m : newer.AsInterface()->GetMethods()) {
173 new_methods.emplace(m->Signature(), m.get());
174 }
175
176 for (const auto& old_m : older.AsInterface()->GetMethods()) {
177 const auto found = new_methods.find(old_m->Signature());
178 if (found == new_methods.end()) {
Steven Moreland4ee68632018-12-14 15:52:46 -0800179 AIDL_ERROR(old_m) << "Removed or changed method: " << older.GetCanonicalName() << "."
Jiyong Park3656c3c2018-08-01 20:02:01 +0900180 << old_m->Signature();
181 compatible = false;
182 continue;
183 }
184
185 // Compare IDs to detect method reordering. IDs are assigned by their
186 // textual order, so if there is an ID mismatch, that means reordering
187 // has happened.
188 const auto new_m = found->second;
Steven Moreland4ee68632018-12-14 15:52:46 -0800189
190 if (old_m->IsOneway() != new_m->IsOneway()) {
191 AIDL_ERROR(new_m) << "Oneway attribute " << (old_m->IsOneway() ? "removed" : "added") << ": "
192 << older.GetCanonicalName() << "." << old_m->Signature();
193 compatible = false;
194 }
195
Jiyong Park3656c3c2018-08-01 20:02:01 +0900196 if (old_m->GetId() != new_m->GetId()) {
197 AIDL_ERROR(new_m) << "Transaction ID changed: " << older.GetCanonicalName() << "."
198 << old_m->Signature() << " is changed from " << old_m->GetId() << " to "
199 << new_m->GetId() << ".";
200 compatible = false;
201 }
202
203 compatible &= are_compatible_types(old_m->GetType(), new_m->GetType());
204
205 const auto& old_args = old_m->GetArguments();
206 const auto& new_args = new_m->GetArguments();
207 // this is guaranteed because arguments are part of AidlMethod::Signature()
Steven Moreland21780812020-09-11 01:29:45 +0000208 AIDL_FATAL_IF(old_args.size() != new_args.size(), old_m);
Jiyong Park3656c3c2018-08-01 20:02:01 +0900209 for (size_t i = 0; i < old_args.size(); i++) {
210 const AidlArgument& old_a = *(old_args.at(i));
211 const AidlArgument& new_a = *(new_args.at(i));
212 compatible &= are_compatible_types(old_a.GetType(), new_a.GetType());
213
214 if (old_a.GetDirection() != new_a.GetDirection()) {
215 AIDL_ERROR(new_m) << "Direction changed: " << old_a.GetDirectionSpecifier() << " to "
216 << new_a.GetDirectionSpecifier() << ".";
217 compatible = false;
218 }
219 }
220 }
Jiyong Parka428d212018-08-29 22:26:30 +0900221
Jooyung Han3f347ca2020-12-01 12:41:50 +0900222 compatible = are_compatible_constants(older, newer) && compatible;
Jiyong Parka428d212018-08-29 22:26:30 +0900223
Jiyong Park3656c3c2018-08-01 20:02:01 +0900224 return compatible;
225}
226
Jooyung Han636fd2f2020-10-22 11:33:45 +0900227static bool HasZeroEnumerator(const AidlEnumDeclaration& enum_decl) {
228 return std::any_of(enum_decl.GetEnumerators().begin(), enum_decl.GetEnumerators().end(),
229 [&](const unique_ptr<AidlEnumerator>& enumerator) {
Jooyung Hanfdaae1d2020-12-14 13:16:15 +0900230 return enumerator->GetValue()->Literal() == "0";
Jooyung Han636fd2f2020-10-22 11:33:45 +0900231 });
232}
233
Jooyung Han308bbe02021-04-27 12:09:59 +0900234static bool EvaluatesToZero(const AidlEnumDeclaration& enum_decl, const std::string& value) {
235 if (value == "") return true;
236 // Because --check_api runs with "valid" AIDL definitions, we can safely assume that
237 // the value is formatted as <scope>.<enumerator>.
238 auto enumerator_name = value.substr(value.find_last_of('.') + 1);
239 for (const auto& enumerator : enum_decl.GetEnumerators()) {
240 if (enumerator->GetName() == enumerator_name) {
241 return enumerator->GetValue()->Literal() == "0";
242 }
243 }
244 AIDL_FATAL(enum_decl) << "Can't find " << enumerator_name << " in " << enum_decl.GetName();
245}
246
Jooyung Han829ec7c2020-12-02 12:07:36 +0900247static bool are_compatible_parcelables(const AidlDefinedType& older, const AidlTypenames&,
248 const AidlDefinedType& newer,
Jooyung Han636fd2f2020-10-22 11:33:45 +0900249 const AidlTypenames& new_types) {
Jiyong Park3656c3c2018-08-01 20:02:01 +0900250 const auto& old_fields = older.GetFields();
251 const auto& new_fields = newer.GetFields();
252 if (old_fields.size() > new_fields.size()) {
253 // you can add new fields only at the end
254 AIDL_ERROR(newer) << "Number of fields in " << older.GetCanonicalName() << " is reduced from "
255 << old_fields.size() << " to " << new_fields.size() << ".";
256 return false;
257 }
Devin Moorec7e47a32020-08-07 10:55:25 -0700258 if (newer.IsFixedSize() && old_fields.size() != new_fields.size()) {
259 AIDL_ERROR(newer) << "Number of fields in " << older.GetCanonicalName() << " is changed from "
260 << old_fields.size() << " to " << new_fields.size()
261 << ". This is an incompatible change for FixedSize types.";
262 return false;
263 }
Jiyong Park3656c3c2018-08-01 20:02:01 +0900264
265 bool compatible = true;
266 for (size_t i = 0; i < old_fields.size(); i++) {
Jiyong Parka468e2a2018-08-29 21:25:18 +0900267 const auto& old_field = old_fields.at(i);
268 const auto& new_field = new_fields.at(i);
269 compatible &= are_compatible_types(old_field->GetType(), new_field->GetType());
Jiyong Park3656c3c2018-08-01 20:02:01 +0900270
Jooyung Hanfdaae1d2020-12-14 13:16:15 +0900271 string old_value = old_field->GetDefaultValue() ? old_field->GetDefaultValue()->Literal() : "";
272 string new_value = new_field->GetDefaultValue() ? new_field->GetDefaultValue()->Literal() : "";
Jooyung Han308bbe02021-04-27 12:09:59 +0900273
274 if (old_value == new_value) {
275 continue;
Steven Moreland370ed342020-04-28 18:14:39 -0700276 }
Jooyung Han308bbe02021-04-27 12:09:59 +0900277 // For enum type fields, we accept setting explicit default value which is "zero"
278 auto enum_decl = new_types.GetEnumDeclaration(new_field->GetType());
279 if (old_value == "" && enum_decl && EvaluatesToZero(*enum_decl, new_value)) {
280 continue;
281 }
282
283 AIDL_ERROR(new_field) << "Changed default value: " << old_value << " to " << new_value << ".";
284 compatible = false;
Steven Moreland370ed342020-04-28 18:14:39 -0700285 }
286
Jiyong Parkb07d9932020-05-15 12:56:54 +0900287 // Reordering of fields is an incompatible change.
288 for (size_t i = 0; i < new_fields.size(); i++) {
289 const auto& new_field = new_fields.at(i);
290 auto found = std::find_if(old_fields.begin(), old_fields.end(), [&new_field](const auto& f) {
291 return new_field->GetName() == f->GetName();
292 });
293 if (found != old_fields.end()) {
294 size_t old_index = std::distance(old_fields.begin(), found);
295 if (old_index != i) {
296 AIDL_ERROR(new_field) << "Reordered " << new_field->GetName() << " from " << old_index
297 << " to " << i << ".";
298 compatible = false;
299 }
300 }
301 }
302
Steven Moreland370ed342020-04-28 18:14:39 -0700303 for (size_t i = old_fields.size(); i < new_fields.size(); i++) {
304 const auto& new_field = new_fields.at(i);
Jooyung Han53fb4242020-12-17 16:03:49 +0900305 if (new_field->HasUsefulDefaultValue()) {
Jooyung Han636fd2f2020-10-22 11:33:45 +0900306 continue;
307 }
308
309 // enum can't be nullable, but it's okay if it has 0 as a valid enumerator.
310 if (const auto& enum_decl = new_types.GetEnumDeclaration(new_field->GetType());
311 enum_decl != nullptr) {
312 if (HasZeroEnumerator(*enum_decl)) {
313 continue;
314 }
315
316 // TODO(b/142893595): Rephrase the message: "provide a default value or make sure ..."
317 AIDL_ERROR(new_field) << "Field '" << new_field->GetName() << "' of enum '"
318 << enum_decl->GetName()
319 << "' can't be initialized as '0'. Please make sure '"
320 << enum_decl->GetName() << "' has '0' as a valid value.";
321 compatible = false;
322 continue;
323 }
324
325 // Old API versions may suffer from the issue presented here. There is
326 // only a finite number in Android, which we must allow indefinitely.
327 struct HistoricalException {
328 std::string canonical;
329 std::string field;
330 };
331 static std::vector<HistoricalException> exceptions = {
332 {"android.net.DhcpResultsParcelable", "serverHostName"},
333 {"android.net.ResolverParamsParcel", "resolverOptions"},
334 };
335 bool excepted = false;
336 for (const HistoricalException& exception : exceptions) {
337 if (older.GetCanonicalName() == exception.canonical &&
338 new_field->GetName() == exception.field) {
339 excepted = true;
340 break;
341 }
342 }
343 if (excepted) continue;
344
345 AIDL_ERROR(new_field)
346 << "Field '" << new_field->GetName()
347 << "' does not have a useful default in some backends. Please either provide a default "
348 "value for this field or mark the field as @nullable. This value or a null value will "
349 "be used automatically when an old version of this parcelable is sent to a process "
350 "which understands a new version of this parcelable. In order to make sure your code "
351 "continues to be backwards compatible, make sure the default or null value does not "
352 "cause a semantic change to this parcelable.";
353 compatible = false;
Jiyong Park3656c3c2018-08-01 20:02:01 +0900354 }
Jooyung Han3f347ca2020-12-01 12:41:50 +0900355
356 compatible = are_compatible_constants(older, newer) && compatible;
357
Jiyong Park3656c3c2018-08-01 20:02:01 +0900358 return compatible;
359}
360
Daniel Norman85aed542019-08-21 12:01:14 -0700361static bool are_compatible_enums(const AidlEnumDeclaration& older,
362 const AidlEnumDeclaration& newer) {
363 if (!are_compatible_types(older.GetBackingType(), newer.GetBackingType())) {
364 AIDL_ERROR(newer) << "Changed backing types.";
365 return false;
366 }
367
368 std::map<std::string, const AidlConstantValue*> old_enum_map;
369 for (const auto& enumerator : older.GetEnumerators()) {
370 old_enum_map[enumerator->GetName()] = enumerator->GetValue();
371 }
372 std::map<std::string, const AidlConstantValue*> new_enum_map;
373 for (const auto& enumerator : newer.GetEnumerators()) {
374 new_enum_map[enumerator->GetName()] = enumerator->GetValue();
375 }
376
377 bool compatible = true;
378 for (const auto& [name, value] : old_enum_map) {
379 if (new_enum_map.find(name) == new_enum_map.end()) {
380 AIDL_ERROR(newer) << "Removed enumerator from " << older.GetCanonicalName() << ": " << name;
381 compatible = false;
382 continue;
383 }
Jooyung Hanfdaae1d2020-12-14 13:16:15 +0900384 const string old_value = old_enum_map[name]->Literal();
385 const string new_value = new_enum_map[name]->Literal();
Daniel Norman85aed542019-08-21 12:01:14 -0700386 if (old_value != new_value) {
387 AIDL_ERROR(newer) << "Changed enumerator value: " << older.GetCanonicalName() << "::" << name
388 << " from " << old_value << " to " << new_value << ".";
389 compatible = false;
390 }
391 }
392 return compatible;
393}
394
Jiyong Park0cf03b12020-07-22 19:36:34 +0900395static Result<AidlTypenames> load_from_dir(const Options& options, const IoDelegate& io_delegate,
396 const std::string& dir) {
Steven Moreland6a945f32021-02-18 00:25:36 +0000397 Result<std::vector<std::string>> dir_files = io_delegate.ListFiles(dir);
398 if (!dir_files.ok()) {
399 AIDL_ERROR(dir) << dir_files.error();
400 return Error();
401 }
402
Jiyong Park0cf03b12020-07-22 19:36:34 +0900403 AidlTypenames typenames;
Steven Moreland6a945f32021-02-18 00:25:36 +0000404 for (const auto& file : *dir_files) {
Jiyong Park0cf03b12020-07-22 19:36:34 +0900405 if (!android::base::EndsWith(file, ".aidl")) continue;
406 if (internals::load_and_validate_aidl(file, options, io_delegate, &typenames,
407 nullptr /* imported_files */) != AidlError::OK) {
408 AIDL_ERROR(file) << "Failed to read.";
409 return Error();
410 }
411 }
Steven Moreland6a945f32021-02-18 00:25:36 +0000412
Jiyong Park0cf03b12020-07-22 19:36:34 +0900413 return typenames;
414}
415
Jiyong Park3656c3c2018-08-01 20:02:01 +0900416bool check_api(const Options& options, const IoDelegate& io_delegate) {
Steven Moreland21780812020-09-11 01:29:45 +0000417 AIDL_FATAL_IF(!options.IsStructured(), AIDL_LOCATION_HERE);
418 AIDL_FATAL_IF(options.InputFiles().size() != 2, AIDL_LOCATION_HERE)
419 << "--checkapi requires two inputs "
420 << "but got " << options.InputFiles().size();
Jiyong Park0cf03b12020-07-22 19:36:34 +0900421 auto old_tns = load_from_dir(options, io_delegate, options.InputFiles().at(0));
422 if (!old_tns.ok()) {
Jiyong Park3656c3c2018-08-01 20:02:01 +0900423 return false;
424 }
Jiyong Park0cf03b12020-07-22 19:36:34 +0900425 auto new_tns = load_from_dir(options, io_delegate, options.InputFiles().at(1));
426 if (!new_tns.ok()) {
Jiyong Park3656c3c2018-08-01 20:02:01 +0900427 return false;
428 }
Jiyong Parkf7534672019-08-12 22:06:22 +0900429
Jooyung Hanb8a97772021-01-19 01:27:38 +0900430 const Options::CheckApiLevel level = options.GetCheckApiLevel();
431
Jiyong Park0cf03b12020-07-22 19:36:34 +0900432 std::vector<AidlDefinedType*> old_types = old_tns->AllDefinedTypes();
433 std::vector<AidlDefinedType*> new_types = new_tns->AllDefinedTypes();
Jiyong Park3656c3c2018-08-01 20:02:01 +0900434
Jooyung Hanb8a97772021-01-19 01:27:38 +0900435 bool compatible = true;
436
437 if (level == Options::CheckApiLevel::EQUAL) {
438 std::set<string> old_type_names;
439 for (const auto t : old_types) {
440 old_type_names.insert(t->GetCanonicalName());
441 }
442 for (const auto new_type : new_types) {
443 const auto found = old_type_names.find(new_type->GetCanonicalName());
444 if (found == old_type_names.end()) {
445 AIDL_ERROR(new_type) << "Added type: " << new_type->GetCanonicalName();
446 compatible = false;
447 continue;
448 }
449 }
450 }
451
Jiyong Park3656c3c2018-08-01 20:02:01 +0900452 map<string, AidlDefinedType*> new_map;
453 for (const auto t : new_types) {
454 new_map.emplace(t->GetCanonicalName(), t);
455 }
456
Jiyong Park3656c3c2018-08-01 20:02:01 +0900457 for (const auto old_type : old_types) {
458 const auto found = new_map.find(old_type->GetCanonicalName());
459 if (found == new_map.end()) {
460 AIDL_ERROR(old_type) << "Removed type: " << old_type->GetCanonicalName();
461 compatible = false;
462 continue;
463 }
464 const auto new_type = found->second;
465
Jooyung Hanb8a97772021-01-19 01:27:38 +0900466 if (level == Options::CheckApiLevel::EQUAL) {
467 if (!CheckEquality(*old_type, *new_type)) {
468 compatible = false;
469 }
470 continue;
471 }
472
Devin Mooredb7ac512020-08-07 11:17:36 -0700473 if (!have_compatible_annotations(*old_type, *new_type)) {
474 compatible = false;
475 }
Daniel Norman85aed542019-08-21 12:01:14 -0700476 if (old_type->AsInterface() != nullptr) {
477 if (new_type->AsInterface() == nullptr) {
478 AIDL_ERROR(new_type) << "Type mismatch: " << old_type->GetCanonicalName()
479 << " is changed from " << old_type->GetPreprocessDeclarationName()
480 << " to " << new_type->GetPreprocessDeclarationName();
481 compatible = false;
482 continue;
483 }
Jiyong Park3656c3c2018-08-01 20:02:01 +0900484 compatible &=
485 are_compatible_interfaces(*(old_type->AsInterface()), *(new_type->AsInterface()));
Daniel Norman85aed542019-08-21 12:01:14 -0700486 } else if (old_type->AsStructuredParcelable() != nullptr) {
487 if (new_type->AsStructuredParcelable() == nullptr) {
488 AIDL_ERROR(new_type) << "Parcelable" << new_type->GetCanonicalName()
489 << " is not structured. ";
490 compatible = false;
491 continue;
492 }
Jooyung Han636fd2f2020-10-22 11:33:45 +0900493 compatible &= are_compatible_parcelables(*(old_type->AsStructuredParcelable()), *old_tns,
494 *(new_type->AsStructuredParcelable()), *new_tns);
Jooyung Han2946afc2020-10-05 20:29:16 +0900495 } else if (old_type->AsUnionDeclaration() != nullptr) {
496 if (new_type->AsUnionDeclaration() == nullptr) {
497 AIDL_ERROR(new_type) << "Type mismatch: " << old_type->GetCanonicalName()
498 << " is changed from " << old_type->GetPreprocessDeclarationName()
499 << " to " << new_type->GetPreprocessDeclarationName();
500 compatible = false;
501 continue;
502 }
Jooyung Han636fd2f2020-10-22 11:33:45 +0900503 compatible &= are_compatible_parcelables(*(old_type->AsUnionDeclaration()), *old_tns,
504 *(new_type->AsUnionDeclaration()), *new_tns);
Daniel Norman85aed542019-08-21 12:01:14 -0700505 } else if (old_type->AsEnumDeclaration() != nullptr) {
506 if (new_type->AsEnumDeclaration() == nullptr) {
507 AIDL_ERROR(new_type) << "Type mismatch: " << old_type->GetCanonicalName()
508 << " is changed from " << old_type->GetPreprocessDeclarationName()
509 << " to " << new_type->GetPreprocessDeclarationName();
510 compatible = false;
511 continue;
512 }
513 compatible &=
514 are_compatible_enums(*(old_type->AsEnumDeclaration()), *(new_type->AsEnumDeclaration()));
515 } else {
516 AIDL_ERROR(old_type) << "Unsupported type " << old_type->GetPreprocessDeclarationName()
517 << " for " << old_type->GetCanonicalName();
518 compatible = false;
Jiyong Park3656c3c2018-08-01 20:02:01 +0900519 }
520 }
521
522 return compatible;
523}
524
525} // namespace aidl
526} // namespace android