blob: c45626d8d9f38633972d1c383b8eae50ee293069 [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 Han829ec7c2020-12-02 12:07:36 +0900234static bool are_compatible_parcelables(const AidlDefinedType& older, const AidlTypenames&,
235 const AidlDefinedType& newer,
Jooyung Han636fd2f2020-10-22 11:33:45 +0900236 const AidlTypenames& new_types) {
Jiyong Park3656c3c2018-08-01 20:02:01 +0900237 const auto& old_fields = older.GetFields();
238 const auto& new_fields = newer.GetFields();
239 if (old_fields.size() > new_fields.size()) {
240 // you can add new fields only at the end
241 AIDL_ERROR(newer) << "Number of fields in " << older.GetCanonicalName() << " is reduced from "
242 << old_fields.size() << " to " << new_fields.size() << ".";
243 return false;
244 }
Devin Moorec7e47a32020-08-07 10:55:25 -0700245 if (newer.IsFixedSize() && old_fields.size() != new_fields.size()) {
246 AIDL_ERROR(newer) << "Number of fields in " << older.GetCanonicalName() << " is changed from "
247 << old_fields.size() << " to " << new_fields.size()
248 << ". This is an incompatible change for FixedSize types.";
249 return false;
250 }
Jiyong Park3656c3c2018-08-01 20:02:01 +0900251
252 bool compatible = true;
253 for (size_t i = 0; i < old_fields.size(); i++) {
Jiyong Parka468e2a2018-08-29 21:25:18 +0900254 const auto& old_field = old_fields.at(i);
255 const auto& new_field = new_fields.at(i);
256 compatible &= are_compatible_types(old_field->GetType(), new_field->GetType());
Jiyong Park3656c3c2018-08-01 20:02:01 +0900257
Jooyung Hanfdaae1d2020-12-14 13:16:15 +0900258 string old_value = old_field->GetDefaultValue() ? old_field->GetDefaultValue()->Literal() : "";
259 string new_value = new_field->GetDefaultValue() ? new_field->GetDefaultValue()->Literal() : "";
Jiyong Parka468e2a2018-08-29 21:25:18 +0900260 if (old_value != new_value) {
Steven Moreland370ed342020-04-28 18:14:39 -0700261 AIDL_ERROR(new_field) << "Changed default value: " << old_value << " to " << new_value << ".";
262 compatible = false;
263 }
264 }
265
Jiyong Parkb07d9932020-05-15 12:56:54 +0900266 // Reordering of fields is an incompatible change.
267 for (size_t i = 0; i < new_fields.size(); i++) {
268 const auto& new_field = new_fields.at(i);
269 auto found = std::find_if(old_fields.begin(), old_fields.end(), [&new_field](const auto& f) {
270 return new_field->GetName() == f->GetName();
271 });
272 if (found != old_fields.end()) {
273 size_t old_index = std::distance(old_fields.begin(), found);
274 if (old_index != i) {
275 AIDL_ERROR(new_field) << "Reordered " << new_field->GetName() << " from " << old_index
276 << " to " << i << ".";
277 compatible = false;
278 }
279 }
280 }
281
Steven Moreland370ed342020-04-28 18:14:39 -0700282 for (size_t i = old_fields.size(); i < new_fields.size(); i++) {
283 const auto& new_field = new_fields.at(i);
Jooyung Han53fb4242020-12-17 16:03:49 +0900284 if (new_field->HasUsefulDefaultValue()) {
Jooyung Han636fd2f2020-10-22 11:33:45 +0900285 continue;
286 }
287
288 // enum can't be nullable, but it's okay if it has 0 as a valid enumerator.
289 if (const auto& enum_decl = new_types.GetEnumDeclaration(new_field->GetType());
290 enum_decl != nullptr) {
291 if (HasZeroEnumerator(*enum_decl)) {
292 continue;
293 }
294
295 // TODO(b/142893595): Rephrase the message: "provide a default value or make sure ..."
296 AIDL_ERROR(new_field) << "Field '" << new_field->GetName() << "' of enum '"
297 << enum_decl->GetName()
298 << "' can't be initialized as '0'. Please make sure '"
299 << enum_decl->GetName() << "' has '0' as a valid value.";
300 compatible = false;
301 continue;
302 }
303
304 // Old API versions may suffer from the issue presented here. There is
305 // only a finite number in Android, which we must allow indefinitely.
306 struct HistoricalException {
307 std::string canonical;
308 std::string field;
309 };
310 static std::vector<HistoricalException> exceptions = {
311 {"android.net.DhcpResultsParcelable", "serverHostName"},
312 {"android.net.ResolverParamsParcel", "resolverOptions"},
313 };
314 bool excepted = false;
315 for (const HistoricalException& exception : exceptions) {
316 if (older.GetCanonicalName() == exception.canonical &&
317 new_field->GetName() == exception.field) {
318 excepted = true;
319 break;
320 }
321 }
322 if (excepted) continue;
323
324 AIDL_ERROR(new_field)
325 << "Field '" << new_field->GetName()
326 << "' does not have a useful default in some backends. Please either provide a default "
327 "value for this field or mark the field as @nullable. This value or a null value will "
328 "be used automatically when an old version of this parcelable is sent to a process "
329 "which understands a new version of this parcelable. In order to make sure your code "
330 "continues to be backwards compatible, make sure the default or null value does not "
331 "cause a semantic change to this parcelable.";
332 compatible = false;
Jiyong Park3656c3c2018-08-01 20:02:01 +0900333 }
Jooyung Han3f347ca2020-12-01 12:41:50 +0900334
335 compatible = are_compatible_constants(older, newer) && compatible;
336
Jiyong Park3656c3c2018-08-01 20:02:01 +0900337 return compatible;
338}
339
Daniel Norman85aed542019-08-21 12:01:14 -0700340static bool are_compatible_enums(const AidlEnumDeclaration& older,
341 const AidlEnumDeclaration& newer) {
342 if (!are_compatible_types(older.GetBackingType(), newer.GetBackingType())) {
343 AIDL_ERROR(newer) << "Changed backing types.";
344 return false;
345 }
346
347 std::map<std::string, const AidlConstantValue*> old_enum_map;
348 for (const auto& enumerator : older.GetEnumerators()) {
349 old_enum_map[enumerator->GetName()] = enumerator->GetValue();
350 }
351 std::map<std::string, const AidlConstantValue*> new_enum_map;
352 for (const auto& enumerator : newer.GetEnumerators()) {
353 new_enum_map[enumerator->GetName()] = enumerator->GetValue();
354 }
355
356 bool compatible = true;
357 for (const auto& [name, value] : old_enum_map) {
358 if (new_enum_map.find(name) == new_enum_map.end()) {
359 AIDL_ERROR(newer) << "Removed enumerator from " << older.GetCanonicalName() << ": " << name;
360 compatible = false;
361 continue;
362 }
Jooyung Hanfdaae1d2020-12-14 13:16:15 +0900363 const string old_value = old_enum_map[name]->Literal();
364 const string new_value = new_enum_map[name]->Literal();
Daniel Norman85aed542019-08-21 12:01:14 -0700365 if (old_value != new_value) {
366 AIDL_ERROR(newer) << "Changed enumerator value: " << older.GetCanonicalName() << "::" << name
367 << " from " << old_value << " to " << new_value << ".";
368 compatible = false;
369 }
370 }
371 return compatible;
372}
373
Jiyong Park0cf03b12020-07-22 19:36:34 +0900374static Result<AidlTypenames> load_from_dir(const Options& options, const IoDelegate& io_delegate,
375 const std::string& dir) {
Steven Moreland6a945f32021-02-18 00:25:36 +0000376 Result<std::vector<std::string>> dir_files = io_delegate.ListFiles(dir);
377 if (!dir_files.ok()) {
378 AIDL_ERROR(dir) << dir_files.error();
379 return Error();
380 }
381
Jiyong Park0cf03b12020-07-22 19:36:34 +0900382 AidlTypenames typenames;
Steven Moreland6a945f32021-02-18 00:25:36 +0000383 for (const auto& file : *dir_files) {
Jiyong Park0cf03b12020-07-22 19:36:34 +0900384 if (!android::base::EndsWith(file, ".aidl")) continue;
385 if (internals::load_and_validate_aidl(file, options, io_delegate, &typenames,
386 nullptr /* imported_files */) != AidlError::OK) {
387 AIDL_ERROR(file) << "Failed to read.";
388 return Error();
389 }
390 }
Steven Moreland6a945f32021-02-18 00:25:36 +0000391
Jiyong Park0cf03b12020-07-22 19:36:34 +0900392 return typenames;
393}
394
Jiyong Park3656c3c2018-08-01 20:02:01 +0900395bool check_api(const Options& options, const IoDelegate& io_delegate) {
Steven Moreland21780812020-09-11 01:29:45 +0000396 AIDL_FATAL_IF(!options.IsStructured(), AIDL_LOCATION_HERE);
397 AIDL_FATAL_IF(options.InputFiles().size() != 2, AIDL_LOCATION_HERE)
398 << "--checkapi requires two inputs "
399 << "but got " << options.InputFiles().size();
Jiyong Park0cf03b12020-07-22 19:36:34 +0900400 auto old_tns = load_from_dir(options, io_delegate, options.InputFiles().at(0));
401 if (!old_tns.ok()) {
Jiyong Park3656c3c2018-08-01 20:02:01 +0900402 return false;
403 }
Jiyong Park0cf03b12020-07-22 19:36:34 +0900404 auto new_tns = load_from_dir(options, io_delegate, options.InputFiles().at(1));
405 if (!new_tns.ok()) {
Jiyong Park3656c3c2018-08-01 20:02:01 +0900406 return false;
407 }
Jiyong Parkf7534672019-08-12 22:06:22 +0900408
Jooyung Hanb8a97772021-01-19 01:27:38 +0900409 const Options::CheckApiLevel level = options.GetCheckApiLevel();
410
Jiyong Park0cf03b12020-07-22 19:36:34 +0900411 std::vector<AidlDefinedType*> old_types = old_tns->AllDefinedTypes();
412 std::vector<AidlDefinedType*> new_types = new_tns->AllDefinedTypes();
Jiyong Park3656c3c2018-08-01 20:02:01 +0900413
Jooyung Hanb8a97772021-01-19 01:27:38 +0900414 bool compatible = true;
415
416 if (level == Options::CheckApiLevel::EQUAL) {
417 std::set<string> old_type_names;
418 for (const auto t : old_types) {
419 old_type_names.insert(t->GetCanonicalName());
420 }
421 for (const auto new_type : new_types) {
422 const auto found = old_type_names.find(new_type->GetCanonicalName());
423 if (found == old_type_names.end()) {
424 AIDL_ERROR(new_type) << "Added type: " << new_type->GetCanonicalName();
425 compatible = false;
426 continue;
427 }
428 }
429 }
430
Jiyong Park3656c3c2018-08-01 20:02:01 +0900431 map<string, AidlDefinedType*> new_map;
432 for (const auto t : new_types) {
433 new_map.emplace(t->GetCanonicalName(), t);
434 }
435
Jiyong Park3656c3c2018-08-01 20:02:01 +0900436 for (const auto old_type : old_types) {
437 const auto found = new_map.find(old_type->GetCanonicalName());
438 if (found == new_map.end()) {
439 AIDL_ERROR(old_type) << "Removed type: " << old_type->GetCanonicalName();
440 compatible = false;
441 continue;
442 }
443 const auto new_type = found->second;
444
Jooyung Hanb8a97772021-01-19 01:27:38 +0900445 if (level == Options::CheckApiLevel::EQUAL) {
446 if (!CheckEquality(*old_type, *new_type)) {
447 compatible = false;
448 }
449 continue;
450 }
451
Devin Mooredb7ac512020-08-07 11:17:36 -0700452 if (!have_compatible_annotations(*old_type, *new_type)) {
453 compatible = false;
454 }
Daniel Norman85aed542019-08-21 12:01:14 -0700455 if (old_type->AsInterface() != nullptr) {
456 if (new_type->AsInterface() == nullptr) {
457 AIDL_ERROR(new_type) << "Type mismatch: " << old_type->GetCanonicalName()
458 << " is changed from " << old_type->GetPreprocessDeclarationName()
459 << " to " << new_type->GetPreprocessDeclarationName();
460 compatible = false;
461 continue;
462 }
Jiyong Park3656c3c2018-08-01 20:02:01 +0900463 compatible &=
464 are_compatible_interfaces(*(old_type->AsInterface()), *(new_type->AsInterface()));
Daniel Norman85aed542019-08-21 12:01:14 -0700465 } else if (old_type->AsStructuredParcelable() != nullptr) {
466 if (new_type->AsStructuredParcelable() == nullptr) {
467 AIDL_ERROR(new_type) << "Parcelable" << new_type->GetCanonicalName()
468 << " is not structured. ";
469 compatible = false;
470 continue;
471 }
Jooyung Han636fd2f2020-10-22 11:33:45 +0900472 compatible &= are_compatible_parcelables(*(old_type->AsStructuredParcelable()), *old_tns,
473 *(new_type->AsStructuredParcelable()), *new_tns);
Jooyung Han2946afc2020-10-05 20:29:16 +0900474 } else if (old_type->AsUnionDeclaration() != nullptr) {
475 if (new_type->AsUnionDeclaration() == nullptr) {
476 AIDL_ERROR(new_type) << "Type mismatch: " << old_type->GetCanonicalName()
477 << " is changed from " << old_type->GetPreprocessDeclarationName()
478 << " to " << new_type->GetPreprocessDeclarationName();
479 compatible = false;
480 continue;
481 }
Jooyung Han636fd2f2020-10-22 11:33:45 +0900482 compatible &= are_compatible_parcelables(*(old_type->AsUnionDeclaration()), *old_tns,
483 *(new_type->AsUnionDeclaration()), *new_tns);
Daniel Norman85aed542019-08-21 12:01:14 -0700484 } else if (old_type->AsEnumDeclaration() != nullptr) {
485 if (new_type->AsEnumDeclaration() == nullptr) {
486 AIDL_ERROR(new_type) << "Type mismatch: " << old_type->GetCanonicalName()
487 << " is changed from " << old_type->GetPreprocessDeclarationName()
488 << " to " << new_type->GetPreprocessDeclarationName();
489 compatible = false;
490 continue;
491 }
492 compatible &=
493 are_compatible_enums(*(old_type->AsEnumDeclaration()), *(new_type->AsEnumDeclaration()));
494 } else {
495 AIDL_ERROR(old_type) << "Unsupported type " << old_type->GetPreprocessDeclarationName()
496 << " for " << old_type->GetCanonicalName();
497 compatible = false;
Jiyong Park3656c3c2018-08-01 20:02:01 +0900498 }
499 }
500
501 return compatible;
502}
503
504} // namespace aidl
505} // namespace android