blob: afc0237ee64bac1dc7a010df5d8055cb0ea6a080 [file] [log] [blame]
Christopher Wiley89eaab52015-09-15 14:46:46 -07001/*
2 * Copyright (C) 2015, 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 */
Adam Lesinskiffa16862014-01-23 18:17:42 -080016
Christopher Wileyf690be52015-09-14 15:19:10 -070017#include "aidl.h"
Adam Lesinskiffa16862014-01-23 18:17:42 -080018
Christopher Wileyf690be52015-09-14 15:19:10 -070019#include <fcntl.h>
Adam Lesinskiffa16862014-01-23 18:17:42 -080020#include <stdio.h>
21#include <stdlib.h>
22#include <string.h>
Christopher Wileyf690be52015-09-14 15:19:10 -070023#include <sys/param.h>
24#include <sys/stat.h>
25#include <unistd.h>
Jiyong Park1deecc32018-07-17 01:14:41 +090026#include <iostream>
27#include <map>
28#include <memory>
Adam Lesinskiffa16862014-01-23 18:17:42 -080029
Elliott Hughes549b6e22015-08-17 12:41:46 -070030#ifdef _WIN32
Adam Lesinskiffa16862014-01-23 18:17:42 -080031#include <io.h>
Andrew Hsieh15ce9942014-05-07 20:14:30 +080032#include <direct.h>
Adam Lesinskiffa16862014-01-23 18:17:42 -080033#include <sys/stat.h>
34#endif
35
Elliott Hughes0a620672015-12-04 13:53:18 -080036#include <android-base/strings.h>
Christopher Wileyf690be52015-09-14 15:19:10 -070037
Christopher Wileyf690be52015-09-14 15:19:10 -070038#include "aidl_language.h"
Christopher Wileyeb1acc12015-09-16 11:25:13 -070039#include "generate_cpp.h"
Christopher Wileyf690be52015-09-14 15:19:10 -070040#include "generate_java.h"
Christopher Wiley72877ac2015-10-06 14:41:42 -070041#include "import_resolver.h"
Christopher Wileyf690be52015-09-14 15:19:10 -070042#include "logging.h"
43#include "options.h"
44#include "os.h"
Christopher Wileye3550c62015-09-29 13:26:10 -070045#include "type_cpp.h"
Christopher Wiley775fa1f2015-09-22 15:00:12 -070046#include "type_java.h"
Christopher Wiley84c1eac2015-09-23 13:29:28 -070047#include "type_namespace.h"
Christopher Wileyf690be52015-09-14 15:19:10 -070048
Adam Lesinskiffa16862014-01-23 18:17:42 -080049#ifndef O_BINARY
50# define O_BINARY 0
51#endif
52
Christopher Wiley3a9911c2016-01-19 12:59:09 -080053using android::base::Join;
Christopher Wileyd76067c2015-10-19 17:00:13 -070054using android::base::Split;
Christopher Wileyc16e5e72015-09-16 10:49:40 -070055using std::cerr;
56using std::endl;
Christopher Wiley9f4c7ae2015-08-24 14:07:32 -070057using std::map;
58using std::set;
59using std::string;
Christopher Wiley84c1eac2015-09-23 13:29:28 -070060using std::unique_ptr;
Christopher Wiley9f4c7ae2015-08-24 14:07:32 -070061using std::vector;
Adam Lesinskiffa16862014-01-23 18:17:42 -080062
Christopher Wileyf690be52015-09-14 15:19:10 -070063namespace android {
64namespace aidl {
65namespace {
Adam Lesinskiffa16862014-01-23 18:17:42 -080066
Christopher Wileyf690be52015-09-14 15:19:10 -070067// The following are gotten as the offset from the allowable id's between
68// android.os.IBinder.FIRST_CALL_TRANSACTION=1 and
69// android.os.IBinder.LAST_CALL_TRANSACTION=16777215
70const int kMinUserSetMethodId = 0;
71const int kMaxUserSetMethodId = 16777214;
Adam Lesinskiffa16862014-01-23 18:17:42 -080072
Casey Dahlin42727f82015-10-12 19:23:40 -070073bool check_filename(const std::string& filename,
74 const std::string& package,
75 const std::string& name,
76 unsigned line) {
Adam Lesinskiffa16862014-01-23 18:17:42 -080077 const char* p;
78 string expected;
79 string fn;
80 size_t len;
Adam Lesinskiffa16862014-01-23 18:17:42 -080081 bool valid = false;
82
Christopher Wileybc2df692016-06-02 16:27:26 -070083 if (!IoDelegate::GetAbsolutePath(filename, &fn)) {
84 return false;
Adam Lesinskiffa16862014-01-23 18:17:42 -080085 }
86
Casey Dahlinfb7da2e2015-10-08 17:26:09 -070087 if (!package.empty()) {
Adam Lesinskiffa16862014-01-23 18:17:42 -080088 expected = package;
89 expected += '.';
90 }
91
92 len = expected.length();
93 for (size_t i=0; i<len; i++) {
94 if (expected[i] == '.') {
95 expected[i] = OS_PATH_SEPARATOR;
96 }
97 }
98
Casey Dahlinfb7da2e2015-10-08 17:26:09 -070099 expected.append(name, 0, name.find('.'));
Christopher Wiley8f8cc9b2015-09-14 13:47:40 -0700100
Adam Lesinskiffa16862014-01-23 18:17:42 -0800101 expected += ".aidl";
102
103 len = fn.length();
104 valid = (len >= expected.length());
105
106 if (valid) {
107 p = fn.c_str() + (len - expected.length());
108
Elliott Hughesce310da2015-07-29 08:44:17 -0700109#ifdef _WIN32
Adam Lesinskiffa16862014-01-23 18:17:42 -0800110 if (OS_PATH_SEPARATOR != '/') {
111 // Input filename under cygwin most likely has / separators
112 // whereas the expected string uses \\ separators. Adjust
113 // them accordingly.
114 for (char *c = const_cast<char *>(p); *c; ++c) {
115 if (*c == '/') *c = OS_PATH_SEPARATOR;
116 }
117 }
118#endif
119
Yabin Cui482eefb2014-11-10 15:01:43 -0800120 // aidl assumes case-insensitivity on Mac Os and Windows.
121#if defined(__linux__)
Adam Lesinskiffa16862014-01-23 18:17:42 -0800122 valid = (expected == p);
123#else
124 valid = !strcasecmp(expected.c_str(), p);
125#endif
126 }
127
128 if (!valid) {
Steven Moreland6ddc37f2018-07-02 13:05:50 -0700129 fprintf(stderr, "%s:%d %s should be declared in a file called %s.\n",
Casey Dahlinfb7da2e2015-10-08 17:26:09 -0700130 filename.c_str(), line, name.c_str(), expected.c_str());
Adam Lesinskiffa16862014-01-23 18:17:42 -0800131 }
132
Casey Dahlin42727f82015-10-12 19:23:40 -0700133 return valid;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800134}
135
Steven Morelandc258abc2018-07-10 14:03:38 -0700136bool check_filenames(const std::string& filename, const AidlDocument& doc) {
Casey Dahlin42727f82015-10-12 19:23:40 -0700137 bool success = true;
138
Steven Morelandc258abc2018-07-10 14:03:38 -0700139 for (const auto& item : doc.GetDefinedTypes()) {
Casey Dahlinc1f39b42015-11-24 10:34:34 -0800140 success &= check_filename(filename, item->GetPackage(), item->GetName(),
141 item->GetLine());
142 }
Casey Dahlin42727f82015-10-12 19:23:40 -0700143
144 return success;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800145}
146
Steven Morelandc258abc2018-07-10 14:03:38 -0700147bool gather_types(const std::string& filename, const AidlDocument& doc, TypeNamespace* types) {
Christopher Wiley84c1eac2015-09-23 13:29:28 -0700148 bool success = true;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800149
Steven Morelandc258abc2018-07-10 14:03:38 -0700150 for (const auto& item : doc.GetDefinedTypes()) {
151 const AidlInterface* interface = item->AsInterface();
152 if (interface != nullptr) {
153 success &= types->AddBinderType(*interface, filename);
154 continue;
155 }
Casey Dahlin42727f82015-10-12 19:23:40 -0700156
Steven Morelandc258abc2018-07-10 14:03:38 -0700157 const AidlParcelable* parcelable = item->AsParcelable();
158 if (parcelable != nullptr) {
159 success &= types->AddParcelableType(*parcelable, filename);
160 continue;
161 }
Casey Dahlin42727f82015-10-12 19:23:40 -0700162
Steven Morelandc258abc2018-07-10 14:03:38 -0700163 CHECK(false) << "aidl internal error: unrecognized type";
Christopher Wiley84c1eac2015-09-23 13:29:28 -0700164 }
Casey Dahlin42727f82015-10-12 19:23:40 -0700165
Christopher Wiley84c1eac2015-09-23 13:29:28 -0700166 return success;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800167}
168
Steven Moreland5557f1c2018-07-02 13:50:23 -0700169int check_types(const string& filename, const AidlStructuredParcelable* parcel,
170 TypeNamespace* types) {
171 int err = 0;
172 for (const auto& v : parcel->GetFields()) {
173 if (!types->MaybeAddContainerType(v->GetType())) {
174 err = 1; // return type is invalid
175 }
176
177 const ValidatableType* type = types->GetReturnType(v->GetType(), filename, *parcel);
178 if (!type) {
179 err = 1;
180 }
181
182 v->GetMutableType()->SetLanguageType(type);
183 }
184
185 return err;
186}
187
Christopher Wileyfb4b22d2015-09-25 15:16:13 -0700188int check_types(const string& filename,
Casey Dahlin98a544b2015-10-14 14:22:55 -0700189 const AidlInterface* c,
Christopher Wileyfb4b22d2015-09-25 15:16:13 -0700190 TypeNamespace* types) {
191 int err = 0;
Casey Dahlinf4a93112015-10-05 16:58:09 -0700192
Casey Dahlinc5afb402016-03-01 13:54:05 -0800193 if (c->IsUtf8() && c->IsUtf8InCpp()) {
194 cerr << filename << ":" << c->GetLine()
195 << "Interface cannot be marked as both @utf8 and @utf8InCpp";
196 err = 1;
197 }
198
Casey Dahlinf4a93112015-10-05 16:58:09 -0700199 // Has to be a pointer due to deleting copy constructor. No idea why.
200 map<string, const AidlMethod*> method_names;
Casey Dahlinfb7da2e2015-10-08 17:26:09 -0700201 for (const auto& m : c->GetMethods()) {
Casey Dahlin0c6fcec2015-10-20 13:32:21 -0700202 bool oneway = m->IsOneway() || c->IsOneway();
203
Christopher Wiley934a82d2016-01-27 13:02:24 -0800204 if (!types->MaybeAddContainerType(m->GetType())) {
Christopher Wileyfb4b22d2015-09-25 15:16:13 -0700205 err = 1; // return type is invalid
Adam Lesinskiffa16862014-01-23 18:17:42 -0800206 }
207
Casey Dahlina2f77c42015-12-01 18:26:02 -0800208 const ValidatableType* return_type =
Casey Dahlinc5afb402016-03-01 13:54:05 -0800209 types->GetReturnType(m->GetType(), filename, *c);
Casey Dahlina2f77c42015-12-01 18:26:02 -0800210
Casey Dahlin57dbe242015-12-04 11:44:02 -0800211 if (!return_type) {
212 err = 1;
Casey Dahlina2f77c42015-12-01 18:26:02 -0800213 }
214
215 m->GetMutableType()->SetLanguageType(return_type);
216
Casey Dahlin0c6fcec2015-10-20 13:32:21 -0700217 if (oneway && m->GetType().GetName() != "void") {
218 cerr << filename << ":" << m->GetLine()
Christopher Wiley45db9ee2015-10-26 18:53:53 -0700219 << " oneway method '" << m->GetName() << "' cannot return a value"
220 << endl;
Casey Dahlin0c6fcec2015-10-20 13:32:21 -0700221 err = 1;
222 }
223
Adam Lesinskiffa16862014-01-23 18:17:42 -0800224 int index = 1;
Casey Dahlinf4a93112015-10-05 16:58:09 -0700225 for (const auto& arg : m->GetArguments()) {
Christopher Wiley934a82d2016-01-27 13:02:24 -0800226 if (!types->MaybeAddContainerType(arg->GetType())) {
Christopher Wileyfb4b22d2015-09-25 15:16:13 -0700227 err = 1;
228 }
Casey Dahlin0c6fcec2015-10-20 13:32:21 -0700229
Casey Dahlina2f77c42015-12-01 18:26:02 -0800230 const ValidatableType* arg_type =
Casey Dahlinc5afb402016-03-01 13:54:05 -0800231 types->GetArgType(*arg, index, filename, *c);
Casey Dahlina2f77c42015-12-01 18:26:02 -0800232
Casey Dahlin57dbe242015-12-04 11:44:02 -0800233 if (!arg_type) {
234 err = 1;
Casey Dahlina2f77c42015-12-01 18:26:02 -0800235 }
236
237 arg->GetMutableType()->SetLanguageType(arg_type);
238
Casey Dahlin0c6fcec2015-10-20 13:32:21 -0700239 if (oneway && arg->IsOut()) {
240 cerr << filename << ":" << m->GetLine()
Christopher Wiley45db9ee2015-10-26 18:53:53 -0700241 << " oneway method '" << m->GetName()
242 << "' cannot have out parameters" << endl;
Casey Dahlin0c6fcec2015-10-20 13:32:21 -0700243 err = 1;
244 }
Adam Lesinskiffa16862014-01-23 18:17:42 -0800245 }
246
Casey Dahlinf4a93112015-10-05 16:58:09 -0700247 auto it = method_names.find(m->GetName());
Christopher Wileyfb4b22d2015-09-25 15:16:13 -0700248 // prevent duplicate methods
Casey Dahlinf4a93112015-10-05 16:58:09 -0700249 if (it == method_names.end()) {
250 method_names[m->GetName()] = m.get();
Christopher Wileyfb4b22d2015-09-25 15:16:13 -0700251 } else {
Casey Dahlinf4a93112015-10-05 16:58:09 -0700252 cerr << filename << ":" << m->GetLine()
253 << " attempt to redefine method " << m->GetName() << "," << endl
254 << filename << ":" << it->second->GetLine()
Christopher Wileyfb4b22d2015-09-25 15:16:13 -0700255 << " previously defined here." << endl;
256 err = 1;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800257 }
Christopher Wileyfb4b22d2015-09-25 15:16:13 -0700258 }
259 return err;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800260}
261
Christopher Wiley3a9911c2016-01-19 12:59:09 -0800262void write_common_dep_file(const string& output_file,
263 const vector<string>& aidl_sources,
Dan Willemsen93298ee2016-11-10 23:55:55 -0800264 CodeWriter* writer,
265 const bool ninja) {
Christopher Wiley3a9911c2016-01-19 12:59:09 -0800266 // Encode that the output file depends on aidl input files.
267 writer->Write("%s : \\\n", output_file.c_str());
268 writer->Write(" %s", Join(aidl_sources, " \\\n ").c_str());
Dan Willemsen93298ee2016-11-10 23:55:55 -0800269 writer->Write("\n");
Christopher Wileya30a45e2015-10-17 10:56:59 -0700270
Dan Willemsen93298ee2016-11-10 23:55:55 -0800271 if (!ninja) {
272 writer->Write("\n");
273 // Output "<input_aidl_file>: " so make won't fail if the input .aidl file
274 // has been deleted, moved or renamed in incremental build.
275 for (const auto& src : aidl_sources) {
276 writer->Write("%s :\n", src.c_str());
277 }
Christopher Wiley3a9911c2016-01-19 12:59:09 -0800278 }
279}
Christopher Wileya30a45e2015-10-17 10:56:59 -0700280
Christopher Wiley3a9911c2016-01-19 12:59:09 -0800281bool write_java_dep_file(const JavaOptions& options,
282 const vector<unique_ptr<AidlImport>>& imports,
Christopher Wileyf8136192016-04-12 14:19:35 -0700283 const IoDelegate& io_delegate,
284 const string& output_file_name) {
Christopher Wiley3a9911c2016-01-19 12:59:09 -0800285 string dep_file_name = options.DependencyFilePath();
286 if (dep_file_name.empty()) {
287 return true; // nothing to do
288 }
289 CodeWriterPtr writer = io_delegate.GetCodeWriter(dep_file_name);
290 if (!writer) {
291 LOG(ERROR) << "Could not open dependency file: " << dep_file_name;
292 return false;
293 }
294
295 vector<string> source_aidl = {options.input_file_name_};
Christopher Wileya30a45e2015-10-17 10:56:59 -0700296 for (const auto& import : imports) {
Christopher Wiley3a9911c2016-01-19 12:59:09 -0800297 if (!import->GetFilename().empty()) {
298 source_aidl.push_back(import->GetFilename());
Adam Lesinskiffa16862014-01-23 18:17:42 -0800299 }
Christopher Wileya30a45e2015-10-17 10:56:59 -0700300 }
Christopher Wiley3a9911c2016-01-19 12:59:09 -0800301
Dan Willemsen93298ee2016-11-10 23:55:55 -0800302 write_common_dep_file(output_file_name, source_aidl, writer.get(),
303 options.DependencyFileNinja());
Christopher Wiley3a9911c2016-01-19 12:59:09 -0800304
305 return true;
306}
307
Steven Moreland5557f1c2018-07-02 13:50:23 -0700308bool write_cpp_dep_file(const CppOptions& options, const AidlDefinedType& defined_type,
Christopher Wiley3a9911c2016-01-19 12:59:09 -0800309 const vector<unique_ptr<AidlImport>>& imports,
310 const IoDelegate& io_delegate) {
311 using ::android::aidl::cpp::HeaderFile;
312 using ::android::aidl::cpp::ClassNames;
313
314 string dep_file_name = options.DependencyFilePath();
315 if (dep_file_name.empty()) {
316 return true; // nothing to do
317 }
318 CodeWriterPtr writer = io_delegate.GetCodeWriter(dep_file_name);
319 if (!writer) {
320 LOG(ERROR) << "Could not open dependency file: " << dep_file_name;
321 return false;
322 }
323
324 vector<string> source_aidl = {options.InputFileName()};
325 for (const auto& import : imports) {
326 if (!import->GetFilename().empty()) {
327 source_aidl.push_back(import->GetFilename());
328 }
329 }
330
Dan Willemsen93298ee2016-11-10 23:55:55 -0800331 write_common_dep_file(options.OutputCppFilePath(), source_aidl, writer.get(),
332 options.DependencyFileNinja());
333
334 if (!options.DependencyFileNinja()) {
335 vector<string> headers;
336 for (ClassNames c : {ClassNames::CLIENT,
337 ClassNames::SERVER,
338 ClassNames::INTERFACE}) {
339 headers.push_back(options.OutputHeaderDir() + '/' +
Steven Moreland5557f1c2018-07-02 13:50:23 -0700340 HeaderFile(defined_type, c, false /* use_os_sep */));
Dan Willemsen93298ee2016-11-10 23:55:55 -0800341 }
342
343 writer->Write("\n");
344
345 // Generated headers also depend on the source aidl files.
346 writer->Write("%s : \\\n %s\n", Join(headers, " \\\n ").c_str(),
347 Join(source_aidl, " \\\n ").c_str());
Christopher Wiley3a9911c2016-01-19 12:59:09 -0800348 }
349
Christopher Wiley3a9911c2016-01-19 12:59:09 -0800350 return true;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800351}
352
Steven Moreland5557f1c2018-07-02 13:50:23 -0700353string generate_outputFileName(const JavaOptions& options, const AidlDefinedType& defined_type) {
354 const string& name = defined_type.GetName();
355 string package = defined_type.GetPackage();
356 string result;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800357
Steven Moreland5557f1c2018-07-02 13:50:23 -0700358 // create the path to the destination folder based on the
359 // defined_type package name
360 result = options.output_base_folder_;
361 result += OS_PATH_SEPARATOR;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800362
Steven Moreland5557f1c2018-07-02 13:50:23 -0700363 string packageStr = package;
364 size_t len = packageStr.length();
365 for (size_t i = 0; i < len; i++) {
366 if (packageStr[i] == '.') {
367 packageStr[i] = OS_PATH_SEPARATOR;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800368 }
Steven Moreland5557f1c2018-07-02 13:50:23 -0700369 }
Adam Lesinskiffa16862014-01-23 18:17:42 -0800370
Steven Moreland5557f1c2018-07-02 13:50:23 -0700371 result += packageStr;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800372
Steven Moreland5557f1c2018-07-02 13:50:23 -0700373 // add the filename by replacing the .aidl extension to .java
374 result += OS_PATH_SEPARATOR;
375 result.append(name, 0, name.find('.'));
376 result += ".java";
Adam Lesinskiffa16862014-01-23 18:17:42 -0800377
Steven Moreland5557f1c2018-07-02 13:50:23 -0700378 return result;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800379}
380
Christopher Wileyf690be52015-09-14 15:19:10 -0700381int check_and_assign_method_ids(const char * filename,
Casey Dahlin5c69deb2015-10-01 14:44:12 -0700382 const std::vector<std::unique_ptr<AidlMethod>>& items) {
Adam Lesinskiffa16862014-01-23 18:17:42 -0800383 // Check whether there are any methods with manually assigned id's and any that are not.
384 // Either all method id's must be manually assigned or all of them must not.
385 // Also, check for duplicates of user set id's and that the id's are within the proper bounds.
386 set<int> usedIds;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800387 bool hasUnassignedIds = false;
388 bool hasAssignedIds = false;
Casey Dahlin5c69deb2015-10-01 14:44:12 -0700389 for (const auto& item : items) {
Casey Dahlinf4a93112015-10-05 16:58:09 -0700390 if (item->HasId()) {
Casey Dahlindff80e52015-09-29 13:57:06 -0700391 hasAssignedIds = true;
Casey Dahlindff80e52015-09-29 13:57:06 -0700392 // Ensure that the user set id is not duplicated.
Casey Dahlinf4a93112015-10-05 16:58:09 -0700393 if (usedIds.find(item->GetId()) != usedIds.end()) {
Casey Dahlindff80e52015-09-29 13:57:06 -0700394 // We found a duplicate id, so throw an error.
Adam Lesinskiffa16862014-01-23 18:17:42 -0800395 fprintf(stderr,
Casey Dahlindff80e52015-09-29 13:57:06 -0700396 "%s:%d Found duplicate method id (%d) for method: %s\n",
Casey Dahlinf4a93112015-10-05 16:58:09 -0700397 filename, item->GetLine(),
398 item->GetId(), item->GetName().c_str());
Adam Lesinskiffa16862014-01-23 18:17:42 -0800399 return 1;
400 }
Casey Dahlindff80e52015-09-29 13:57:06 -0700401 // Ensure that the user set id is within the appropriate limits
Casey Dahlinf4a93112015-10-05 16:58:09 -0700402 if (item->GetId() < kMinUserSetMethodId ||
403 item->GetId() > kMaxUserSetMethodId) {
Casey Dahlindff80e52015-09-29 13:57:06 -0700404 fprintf(stderr, "%s:%d Found out of bounds id (%d) for method: %s\n",
Casey Dahlinf4a93112015-10-05 16:58:09 -0700405 filename, item->GetLine(),
406 item->GetId(), item->GetName().c_str());
Casey Dahlindff80e52015-09-29 13:57:06 -0700407 fprintf(stderr, " Value for id must be between %d and %d inclusive.\n",
408 kMinUserSetMethodId, kMaxUserSetMethodId);
409 return 1;
410 }
Casey Dahlinf4a93112015-10-05 16:58:09 -0700411 usedIds.insert(item->GetId());
Casey Dahlindff80e52015-09-29 13:57:06 -0700412 } else {
413 hasUnassignedIds = true;
414 }
415 if (hasAssignedIds && hasUnassignedIds) {
416 fprintf(stderr,
417 "%s: You must either assign id's to all methods or to none of them.\n",
418 filename);
419 return 1;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800420 }
Adam Lesinskiffa16862014-01-23 18:17:42 -0800421 }
422
423 // In the case that all methods have unassigned id's, set a unique id for them.
424 if (hasUnassignedIds) {
425 int newId = 0;
Casey Dahlin5c69deb2015-10-01 14:44:12 -0700426 for (const auto& item : items) {
Casey Dahlinf4a93112015-10-05 16:58:09 -0700427 item->SetId(newId++);
Adam Lesinskiffa16862014-01-23 18:17:42 -0800428 }
429 }
430
431 // success
432 return 0;
433}
434
Christopher Wiley69b44cf2016-05-03 13:43:33 -0700435bool validate_constants(const AidlInterface& interface) {
436 bool success = true;
437 set<string> names;
438 for (const std::unique_ptr<AidlIntConstant>& int_constant :
439 interface.GetIntConstants()) {
440 if (names.count(int_constant->GetName()) > 0) {
441 LOG(ERROR) << "Found duplicate constant name '" << int_constant->GetName()
442 << "'";
443 success = false;
444 }
445 names.insert(int_constant->GetName());
Roshan Pius3b2203d2016-07-22 16:13:20 -0700446 // We've logged an error message for this on object construction.
447 success = success && int_constant->IsValid();
Christopher Wiley69b44cf2016-05-03 13:43:33 -0700448 }
449 for (const std::unique_ptr<AidlStringConstant>& string_constant :
450 interface.GetStringConstants()) {
451 if (names.count(string_constant->GetName()) > 0) {
452 LOG(ERROR) << "Found duplicate constant name '" << string_constant->GetName()
453 << "'";
454 success = false;
455 }
456 names.insert(string_constant->GetName());
457 // We've logged an error message for this on object construction.
458 success = success && string_constant->IsValid();
459 }
460 return success;
461}
462
Christopher Wileyef140932015-11-03 09:29:19 -0800463// TODO: Remove this in favor of using the YACC parser b/25479378
464bool ParsePreprocessedLine(const string& line, string* decl,
465 vector<string>* package, string* class_name) {
466 // erase all trailing whitespace and semicolons
467 const size_t end = line.find_last_not_of(" ;\t");
468 if (end == string::npos) {
469 return false;
470 }
471 if (line.rfind(';', end) != string::npos) {
472 return false;
473 }
474
475 decl->clear();
476 string type;
477 vector<string> pieces = Split(line.substr(0, end + 1), " \t");
478 for (const string& piece : pieces) {
479 if (piece.empty()) {
480 continue;
481 }
482 if (decl->empty()) {
483 *decl = std::move(piece);
484 } else if (type.empty()) {
485 type = std::move(piece);
486 } else {
487 return false;
488 }
489 }
490
491 // Note that this logic is absolutely wrong. Given a parcelable
492 // org.some.Foo.Bar, the class name is Foo.Bar, but this code will claim that
493 // the class is just Bar. However, this was the way it was done in the past.
494 //
495 // See b/17415692
496 size_t dot_pos = type.rfind('.');
497 if (dot_pos != string::npos) {
498 *class_name = type.substr(dot_pos + 1);
499 *package = Split(type.substr(0, dot_pos), ".");
500 } else {
501 *class_name = type;
502 package->clear();
503 }
504
505 return true;
506}
507
Christopher Wiley4a2884b2015-10-07 11:27:45 -0700508} // namespace
509
510namespace internals {
511
Jiyong Park1deecc32018-07-17 01:14:41 +0900512bool parse_preprocessed_file(const IoDelegate& io_delegate, const string& filename,
513 TypeNamespace* types, AidlTypenames& typenames) {
Christopher Wileyef140932015-11-03 09:29:19 -0800514 bool success = true;
515 unique_ptr<LineReader> line_reader = io_delegate.GetLineReader(filename);
516 if (!line_reader) {
517 LOG(ERROR) << "cannot open preprocessed file: " << filename;
518 success = false;
519 return success;
520 }
521
522 string line;
523 unsigned lineno = 1;
524 for ( ; line_reader->ReadLine(&line); ++lineno) {
525 if (line.empty() || line.compare(0, 2, "//") == 0) {
526 // skip comments and empty lines
527 continue;
528 }
529
530 string decl;
531 vector<string> package;
532 string class_name;
533 if (!ParsePreprocessedLine(line, &decl, &package, &class_name)) {
534 success = false;
535 break;
536 }
537
538 if (decl == "parcelable") {
Jiyong Park1deecc32018-07-17 01:14:41 +0900539 AidlParcelable* doc =
540 new AidlParcelable(new AidlQualifiedName(class_name, ""), lineno, package);
541 types->AddParcelableType(*doc, filename);
542 typenames.AddPreprocessedType(unique_ptr<AidlParcelable>(doc));
Steven Morelanded83a282018-07-17 13:27:29 -0700543 } else if (decl == "structured_parcelable") {
544 auto temp = new std::vector<std::unique_ptr<AidlVariableDeclaration>>();
Jiyong Park1deecc32018-07-17 01:14:41 +0900545 AidlStructuredParcelable* doc = new AidlStructuredParcelable(
546 new AidlQualifiedName(class_name, ""), lineno, package, temp);
547 types->AddParcelableType(*doc, filename);
548 typenames.AddPreprocessedType(unique_ptr<AidlStructuredParcelable>(doc));
Christopher Wileyef140932015-11-03 09:29:19 -0800549 } else if (decl == "interface") {
Casey Dahlind40e2fe2015-11-24 14:06:52 -0800550 auto temp = new std::vector<std::unique_ptr<AidlMember>>();
Jiyong Park1deecc32018-07-17 01:14:41 +0900551 AidlInterface* doc = new AidlInterface(class_name, lineno, "", false, temp, package);
552 types->AddBinderType(*doc, filename);
553 typenames.AddPreprocessedType(unique_ptr<AidlInterface>(doc));
Christopher Wileyef140932015-11-03 09:29:19 -0800554 } else {
555 success = false;
556 break;
557 }
558 }
559 if (!success) {
560 LOG(ERROR) << filename << ':' << lineno
561 << " malformed preprocessed file line: '" << line << "'";
562 }
563
564 return success;
565}
566
Steven Moreland5557f1c2018-07-02 13:50:23 -0700567AidlError load_and_validate_aidl(const std::vector<std::string>& preprocessed_files,
568 const std::vector<std::string>& import_paths,
569 const std::string& input_file_name, const bool generate_traces,
570 const IoDelegate& io_delegate, TypeNamespace* types,
571 std::unique_ptr<AidlDefinedType>* returned_type,
572 std::vector<std::unique_ptr<AidlImport>>* returned_imports) {
Christopher Wiley632801d2015-11-05 14:15:49 -0800573 AidlError err = AidlError::OK;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800574
Casey Dahlinc1f39b42015-11-24 10:34:34 -0800575 std::map<AidlImport*,std::unique_ptr<AidlDocument>> docs;
Casey Dahlin624358c2015-10-12 19:29:51 -0700576
Jiyong Park1deecc32018-07-17 01:14:41 +0900577 AidlTypenames typenames;
578
Christopher Wiley3a9d1582015-09-16 12:42:14 -0700579 // import the preprocessed file
Christopher Wileyeb1acc12015-09-16 11:25:13 -0700580 for (const string& s : preprocessed_files) {
Jiyong Park1deecc32018-07-17 01:14:41 +0900581 if (!parse_preprocessed_file(io_delegate, s, types, typenames)) {
Christopher Wiley632801d2015-11-05 14:15:49 -0800582 err = AidlError::BAD_PRE_PROCESSED_FILE;
Christopher Wileyef140932015-11-03 09:29:19 -0800583 }
Christopher Wiley3a9d1582015-09-16 12:42:14 -0700584 }
Christopher Wiley632801d2015-11-05 14:15:49 -0800585 if (err != AidlError::OK) {
Christopher Wiley3a9d1582015-09-16 12:42:14 -0700586 return err;
587 }
Adam Lesinskiffa16862014-01-23 18:17:42 -0800588
Christopher Wileyc16e5e72015-09-16 10:49:40 -0700589 // parse the input file
Jiyong Park1deecc32018-07-17 01:14:41 +0900590 Parser p{io_delegate, &typenames};
Christopher Wiley4a2884b2015-10-07 11:27:45 -0700591 if (!p.ParseFile(input_file_name)) {
Christopher Wiley632801d2015-11-05 14:15:49 -0800592 return AidlError::PARSE_ERROR;
Christopher Wiley4a2884b2015-10-07 11:27:45 -0700593 }
Casey Dahlin2cc93162015-10-02 16:14:17 -0700594
Casey Dahlinc1f39b42015-11-24 10:34:34 -0800595 AidlDocument* parsed_doc = p.GetDocument();
596
Steven Morelandc258abc2018-07-10 14:03:38 -0700597 if (parsed_doc->GetDefinedTypes().empty()) {
598 LOG(ERROR) << "Cannot generate file without any definitions.";
599 return AidlError::BAD_TYPE;
600 }
Casey Dahlinc1f39b42015-11-24 10:34:34 -0800601
Steven Morelandc258abc2018-07-10 14:03:38 -0700602 bool has_only_unstructured_parcelables = true;
603 for (const auto& defined_type : parsed_doc->GetDefinedTypes()) {
604 if (defined_type->AsStructuredParcelable() != nullptr ||
605 defined_type->AsInterface() != nullptr) {
606 has_only_unstructured_parcelables = false;
607 break;
608 }
609 }
610 if (has_only_unstructured_parcelables) {
611 LOG(ERROR) << "Refusing to generate code with unstructured parcelables.";
Christopher Wiley632801d2015-11-05 14:15:49 -0800612 return AidlError::FOUND_PARCELABLE;
613 }
614
Steven Morelandc258abc2018-07-10 14:03:38 -0700615 if (parsed_doc->GetDefinedTypes().size() > 1) {
616 LOG(ERROR) << "Exactly one structured type is required to be defined.";
617 return AidlError::BAD_TYPE;
618 }
619
620 unique_ptr<AidlDefinedType> defined_type(parsed_doc->ReleaseDefinedType());
621 AidlInterface* interface = defined_type->AsInterface();
622 AidlStructuredParcelable* parcelable = defined_type->AsStructuredParcelable();
623
624 CHECK(interface != nullptr || parcelable != nullptr);
625
Steven Moreland5557f1c2018-07-02 13:50:23 -0700626 if (!check_filename(input_file_name.c_str(), defined_type->GetPackage(), defined_type->GetName(),
627 defined_type->GetLine()) ||
628 !types->IsValidPackage(defined_type->GetPackage())) {
629 LOG(ERROR) << "Invalid package declaration '" << defined_type->GetPackage() << "'";
Christopher Wiley632801d2015-11-05 14:15:49 -0800630 return AidlError::BAD_PACKAGE;
631 }
Adam Lesinskiffa16862014-01-23 18:17:42 -0800632
Christopher Wileyc16e5e72015-09-16 10:49:40 -0700633 // parse the imports of the input file
Christopher Wiley72877ac2015-10-06 14:41:42 -0700634 ImportResolver import_resolver{io_delegate, import_paths};
Casey Dahlin0edf3422015-10-07 12:34:59 -0700635 for (auto& import : p.GetImports()) {
Christopher Wiley934a82d2016-01-27 13:02:24 -0800636 if (types->HasImportType(*import)) {
Christopher Wileyfb4b22d2015-09-25 15:16:13 -0700637 // There are places in the Android tree where an import doesn't resolve,
638 // but we'll pick the type up through the preprocessed types.
639 // This seems like an error, but legacy support demands we support it...
Christopher Wileyc16e5e72015-09-16 10:49:40 -0700640 continue;
641 }
Casey Dahlin0edf3422015-10-07 12:34:59 -0700642 string import_path = import_resolver.FindImportFile(import->GetNeededClass());
Christopher Wiley72877ac2015-10-06 14:41:42 -0700643 if (import_path.empty()) {
Casey Dahlin0edf3422015-10-07 12:34:59 -0700644 cerr << import->GetFileFrom() << ":" << import->GetLine()
Christopher Wileyc16e5e72015-09-16 10:49:40 -0700645 << ": couldn't find import for class "
Casey Dahlin0edf3422015-10-07 12:34:59 -0700646 << import->GetNeededClass() << endl;
Christopher Wiley632801d2015-11-05 14:15:49 -0800647 err = AidlError::BAD_IMPORT;
Christopher Wileyc16e5e72015-09-16 10:49:40 -0700648 continue;
649 }
Casey Dahlin0edf3422015-10-07 12:34:59 -0700650 import->SetFilename(import_path);
Casey Dahlin2cc93162015-10-02 16:14:17 -0700651
Jiyong Park1deecc32018-07-17 01:14:41 +0900652 Parser p{io_delegate, &typenames};
Casey Dahlin0edf3422015-10-07 12:34:59 -0700653 if (!p.ParseFile(import->GetFilename())) {
Christopher Wileyc16e5e72015-09-16 10:49:40 -0700654 cerr << "error while parsing import for class "
Casey Dahlin0edf3422015-10-07 12:34:59 -0700655 << import->GetNeededClass() << endl;
Christopher Wiley632801d2015-11-05 14:15:49 -0800656 err = AidlError::BAD_IMPORT;
Christopher Wileyc16e5e72015-09-16 10:49:40 -0700657 continue;
658 }
Christopher Wiley4a2884b2015-10-07 11:27:45 -0700659
Casey Dahlinc1f39b42015-11-24 10:34:34 -0800660 std::unique_ptr<AidlDocument> document(p.ReleaseDocument());
Steven Morelandc258abc2018-07-10 14:03:38 -0700661 if (!check_filenames(import->GetFilename(), *document)) err = AidlError::BAD_IMPORT;
Casey Dahlinc1f39b42015-11-24 10:34:34 -0800662 docs[import.get()] = std::move(document);
Christopher Wileyc16e5e72015-09-16 10:49:40 -0700663 }
Christopher Wiley632801d2015-11-05 14:15:49 -0800664 if (err != AidlError::OK) {
Christopher Wileyc16e5e72015-09-16 10:49:40 -0700665 return err;
Christopher Wiley3a9d1582015-09-16 12:42:14 -0700666 }
Adam Lesinskiffa16862014-01-23 18:17:42 -0800667
Jiyong Park1deecc32018-07-17 01:14:41 +0900668 if (!p.Resolve()) {
669 return AidlError::BAD_TYPE;
670 }
671
Steven Moreland5557f1c2018-07-02 13:50:23 -0700672 if (interface) {
673 // gather the types that have been declared
Steven Morelandc258abc2018-07-10 14:03:38 -0700674 if (!types->AddBinderType(*interface, input_file_name)) {
Steven Moreland5557f1c2018-07-02 13:50:23 -0700675 err = AidlError::BAD_TYPE;
676 }
677
678 interface->SetGenerateTraces(generate_traces);
Christopher Wiley84c1eac2015-09-23 13:29:28 -0700679 }
Casey Dahlinc1f39b42015-11-24 10:34:34 -0800680
Steven Moreland5557f1c2018-07-02 13:50:23 -0700681 if (parcelable) {
Steven Morelandc258abc2018-07-10 14:03:38 -0700682 if (!types->AddParcelableType(*parcelable, input_file_name)) {
Steven Moreland5557f1c2018-07-02 13:50:23 -0700683 err = AidlError::BAD_TYPE;
684 }
685 }
Casey Dahlina2f77c42015-12-01 18:26:02 -0800686
Steven Moreland5557f1c2018-07-02 13:50:23 -0700687 defined_type->SetLanguageType(types->GetDefinedType(*defined_type));
Martijn Coenenf1b50782018-02-21 21:06:23 +0100688
Casey Dahlin0edf3422015-10-07 12:34:59 -0700689 for (const auto& import : p.GetImports()) {
Casey Dahlinc1f39b42015-11-24 10:34:34 -0800690 // If we skipped an unresolved import above (see comment there) we'll have
691 // an empty bucket here.
692 const auto import_itr = docs.find(import.get());
693 if (import_itr == docs.cend()) {
694 continue;
695 }
696
Steven Morelandc258abc2018-07-10 14:03:38 -0700697 if (!gather_types(import->GetFilename(), *import_itr->second, types)) {
Christopher Wiley632801d2015-11-05 14:15:49 -0800698 err = AidlError::BAD_TYPE;
Christopher Wiley84c1eac2015-09-23 13:29:28 -0700699 }
Christopher Wiley3a9d1582015-09-16 12:42:14 -0700700 }
Adam Lesinskiffa16862014-01-23 18:17:42 -0800701
Christopher Wileyc16e5e72015-09-16 10:49:40 -0700702 // check the referenced types in parsed_doc to make sure we've imported them
Steven Morelandc258abc2018-07-10 14:03:38 -0700703 if (interface && check_types(input_file_name, interface, types) != 0) {
Steven Moreland5557f1c2018-07-02 13:50:23 -0700704 err = AidlError::BAD_TYPE;
705 }
Steven Morelandc258abc2018-07-10 14:03:38 -0700706 if (parcelable && check_types(input_file_name, parcelable, types) != 0) {
Christopher Wiley632801d2015-11-05 14:15:49 -0800707 err = AidlError::BAD_TYPE;
708 }
709 if (err != AidlError::OK) {
710 return err;
711 }
Adam Lesinskiffa16862014-01-23 18:17:42 -0800712
Christopher Wileyc16e5e72015-09-16 10:49:40 -0700713 // assign method ids and validate.
Steven Moreland5557f1c2018-07-02 13:50:23 -0700714 if (interface &&
715 check_and_assign_method_ids(input_file_name.c_str(), interface->GetMethods()) != 0) {
Christopher Wiley632801d2015-11-05 14:15:49 -0800716 return AidlError::BAD_METHOD_ID;
Christopher Wiley3a9d1582015-09-16 12:42:14 -0700717 }
Steven Moreland5557f1c2018-07-02 13:50:23 -0700718 if (interface && !validate_constants(*interface)) {
Christopher Wiley69b44cf2016-05-03 13:43:33 -0700719 return AidlError::BAD_CONSTANTS;
720 }
Adam Lesinskiffa16862014-01-23 18:17:42 -0800721
Steven Morelandc258abc2018-07-10 14:03:38 -0700722 *returned_type = std::move(defined_type);
Casey Dahlin0edf3422015-10-07 12:34:59 -0700723
724 if (returned_imports)
725 p.ReleaseImports(returned_imports);
726
Christopher Wiley632801d2015-11-05 14:15:49 -0800727 return AidlError::OK;
Christopher Wileyeb1acc12015-09-16 11:25:13 -0700728}
729
Casey Dahlin2cc93162015-10-02 16:14:17 -0700730} // namespace internals
731
Christopher Wiley4a2884b2015-10-07 11:27:45 -0700732int compile_aidl_to_cpp(const CppOptions& options,
733 const IoDelegate& io_delegate) {
Steven Moreland5557f1c2018-07-02 13:50:23 -0700734 unique_ptr<AidlDefinedType> defined_type;
Casey Dahlin0edf3422015-10-07 12:34:59 -0700735 std::vector<std::unique_ptr<AidlImport>> imports;
Christopher Wileye3550c62015-09-29 13:26:10 -0700736 unique_ptr<cpp::TypeNamespace> types(new cpp::TypeNamespace());
Christopher Wiley56799522015-10-31 10:17:04 -0700737 types->Init();
Christopher Wiley632801d2015-11-05 14:15:49 -0800738 AidlError err = internals::load_and_validate_aidl(
Steven Moreland4b37e262018-07-17 12:49:38 -0700739 options.preprocessed_files_, options.ImportPaths(), options.InputFileName(),
740 options.ShouldGenTraces(), io_delegate, types.get(), &defined_type, &imports);
Christopher Wiley632801d2015-11-05 14:15:49 -0800741 if (err != AidlError::OK) {
742 return 1;
Christopher Wileyeb1acc12015-09-16 11:25:13 -0700743 }
744
Steven Moreland5557f1c2018-07-02 13:50:23 -0700745 CHECK(defined_type != nullptr);
746
747 if (!write_cpp_dep_file(options, *defined_type, imports, io_delegate)) {
Christopher Wiley3a9911c2016-01-19 12:59:09 -0800748 return 1;
Christopher Wiley19059cb2015-11-05 16:11:56 -0800749 }
Christopher Wileyeb1acc12015-09-16 11:25:13 -0700750
Steven Moreland5557f1c2018-07-02 13:50:23 -0700751 return (cpp::GenerateCpp(options, *types, *defined_type, io_delegate)) ? 0 : 1;
Christopher Wileyeb1acc12015-09-16 11:25:13 -0700752}
753
Christopher Wiley4a2884b2015-10-07 11:27:45 -0700754int compile_aidl_to_java(const JavaOptions& options,
755 const IoDelegate& io_delegate) {
Steven Moreland5557f1c2018-07-02 13:50:23 -0700756 unique_ptr<AidlDefinedType> defined_type;
Casey Dahlin0edf3422015-10-07 12:34:59 -0700757 std::vector<std::unique_ptr<AidlImport>> imports;
Christopher Wileydb154a52015-09-28 16:32:25 -0700758 unique_ptr<java::JavaTypeNamespace> types(new java::JavaTypeNamespace());
Christopher Wiley56799522015-10-31 10:17:04 -0700759 types->Init();
Christopher Wiley632801d2015-11-05 14:15:49 -0800760 AidlError aidl_err = internals::load_and_validate_aidl(
Steven Moreland5557f1c2018-07-02 13:50:23 -0700761 options.preprocessed_files_, options.import_paths_, options.input_file_name_,
762 options.gen_traces_, io_delegate, types.get(), &defined_type, &imports);
Christopher Wiley632801d2015-11-05 14:15:49 -0800763 if (aidl_err == AidlError::FOUND_PARCELABLE && !options.fail_on_parcelable_) {
764 // We aborted code generation because this file contains parcelables.
765 // However, we were not told to complain if we find parcelables.
Christopher Wileyb1bbdf82016-04-21 11:43:45 -0700766 // Just generate a dep file and exit quietly. The dep file is for a legacy
767 // use case by the SDK.
768 write_java_dep_file(options, imports, io_delegate, "");
Christopher Wiley632801d2015-11-05 14:15:49 -0800769 return 0;
770 }
771 if (aidl_err != AidlError::OK) {
772 return 1;
Christopher Wileyeb1acc12015-09-16 11:25:13 -0700773 }
Christopher Wileyeb1acc12015-09-16 11:25:13 -0700774
Steven Moreland5557f1c2018-07-02 13:50:23 -0700775 CHECK(defined_type != nullptr);
776
Christopher Wiley3a9d1582015-09-16 12:42:14 -0700777 string output_file_name = options.output_file_name_;
778 // if needed, generate the output file name from the base folder
Christopher Wiley632801d2015-11-05 14:15:49 -0800779 if (output_file_name.empty() && !options.output_base_folder_.empty()) {
Steven Moreland5557f1c2018-07-02 13:50:23 -0700780 output_file_name = generate_outputFileName(options, *defined_type);
Christopher Wiley3a9d1582015-09-16 12:42:14 -0700781 }
Adam Lesinskiffa16862014-01-23 18:17:42 -0800782
Casey Dahlin64533512015-10-23 17:11:21 -0700783 // make sure the folders of the output file all exists
784 if (!io_delegate.CreatePathForFile(output_file_name)) {
785 return 1;
786 }
787
Christopher Wileyf8136192016-04-12 14:19:35 -0700788 if (!write_java_dep_file(options, imports, io_delegate, output_file_name)) {
Christopher Wiley3a9911c2016-01-19 12:59:09 -0800789 return 1;
Christopher Wiley3a9d1582015-09-16 12:42:14 -0700790 }
Adam Lesinskiffa16862014-01-23 18:17:42 -0800791
Steven Moreland5557f1c2018-07-02 13:50:23 -0700792 return generate_java(output_file_name, options.input_file_name_.c_str(), defined_type.get(),
793 types.get(), io_delegate, options);
Adam Lesinskiffa16862014-01-23 18:17:42 -0800794}
795
Casey Dahlinc1f39b42015-11-24 10:34:34 -0800796bool preprocess_aidl(const JavaOptions& options,
797 const IoDelegate& io_delegate) {
798 unique_ptr<CodeWriter> writer =
799 io_delegate.GetCodeWriter(options.output_file_name_);
Adam Lesinskiffa16862014-01-23 18:17:42 -0800800
Casey Dahlinc1f39b42015-11-24 10:34:34 -0800801 for (const auto& file : options.files_to_preprocess_) {
Jiyong Park1deecc32018-07-17 01:14:41 +0900802 AidlTypenames typenames;
803 Parser p{io_delegate, &typenames};
Casey Dahlinc1f39b42015-11-24 10:34:34 -0800804 if (!p.ParseFile(file))
805 return false;
806 AidlDocument* doc = p.GetDocument();
807 string line;
Casey Dahlin59401da2015-10-09 18:16:45 -0700808
Steven Morelandc258abc2018-07-10 14:03:38 -0700809 for (const auto& defined_type : doc->GetDefinedTypes()) {
Steven Morelanded83a282018-07-17 13:27:29 -0700810 if (!writer->Write("%s %s;\n", defined_type->GetPreprocessDeclarationName().c_str(),
Steven Morelandc258abc2018-07-10 14:03:38 -0700811 defined_type->GetCanonicalName().c_str())) {
Casey Dahlinc1f39b42015-11-24 10:34:34 -0800812 return false;
813 }
Adam Lesinskiffa16862014-01-23 18:17:42 -0800814 }
Casey Dahlinc1f39b42015-11-24 10:34:34 -0800815 }
Adam Lesinskiffa16862014-01-23 18:17:42 -0800816
Casey Dahlinc1f39b42015-11-24 10:34:34 -0800817 return writer->Close();
Adam Lesinskiffa16862014-01-23 18:17:42 -0800818}
Christopher Wileyfdeb0f42015-09-11 15:38:22 -0700819
820} // namespace android
821} // namespace aidl