Christopher Wiley | 89eaab5 | 2015-09-15 14:46:46 -0700 | [diff] [blame] | 1 | /* |
| 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 Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 16 | |
Christopher Wiley | f690be5 | 2015-09-14 15:19:10 -0700 | [diff] [blame] | 17 | #include "aidl.h" |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 18 | |
Christopher Wiley | f690be5 | 2015-09-14 15:19:10 -0700 | [diff] [blame] | 19 | #include <fcntl.h> |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 20 | #include <stdio.h> |
| 21 | #include <stdlib.h> |
| 22 | #include <string.h> |
Christopher Wiley | f690be5 | 2015-09-14 15:19:10 -0700 | [diff] [blame] | 23 | #include <sys/param.h> |
| 24 | #include <sys/stat.h> |
| 25 | #include <unistd.h> |
Jiyong Park | 02da742 | 2018-07-16 16:00:26 +0900 | [diff] [blame] | 26 | #include <algorithm> |
Jiyong Park | 1deecc3 | 2018-07-17 01:14:41 +0900 | [diff] [blame] | 27 | #include <iostream> |
| 28 | #include <map> |
| 29 | #include <memory> |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 30 | |
Elliott Hughes | 549b6e2 | 2015-08-17 12:41:46 -0700 | [diff] [blame] | 31 | #ifdef _WIN32 |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 32 | #include <io.h> |
Andrew Hsieh | 15ce994 | 2014-05-07 20:14:30 +0800 | [diff] [blame] | 33 | #include <direct.h> |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 34 | #include <sys/stat.h> |
| 35 | #endif |
| 36 | |
Elliott Hughes | 0a62067 | 2015-12-04 13:53:18 -0800 | [diff] [blame] | 37 | #include <android-base/strings.h> |
Christopher Wiley | f690be5 | 2015-09-14 15:19:10 -0700 | [diff] [blame] | 38 | |
Steven Moreland | 3981d9e | 2020-03-31 14:11:44 -0700 | [diff] [blame] | 39 | #include "aidl_checkapi.h" |
Christopher Wiley | f690be5 | 2015-09-14 15:19:10 -0700 | [diff] [blame] | 40 | #include "aidl_language.h" |
Jiyong Park | e05195e | 2018-10-08 18:24:23 +0900 | [diff] [blame] | 41 | #include "aidl_typenames.h" |
Andrei Onea | 8714b02 | 2019-02-01 18:55:54 +0000 | [diff] [blame] | 42 | #include "generate_aidl_mappings.h" |
Christopher Wiley | eb1acc1 | 2015-09-16 11:25:13 -0700 | [diff] [blame] | 43 | #include "generate_cpp.h" |
Christopher Wiley | f690be5 | 2015-09-14 15:19:10 -0700 | [diff] [blame] | 44 | #include "generate_java.h" |
Steven Moreland | c26d814 | 2018-09-17 14:25:33 -0700 | [diff] [blame] | 45 | #include "generate_ndk.h" |
Andrei Homescu | b62afd9 | 2020-05-11 19:24:59 -0700 | [diff] [blame] | 46 | #include "generate_rust.h" |
Christopher Wiley | 72877ac | 2015-10-06 14:41:42 -0700 | [diff] [blame] | 47 | #include "import_resolver.h" |
Christopher Wiley | f690be5 | 2015-09-14 15:19:10 -0700 | [diff] [blame] | 48 | #include "logging.h" |
| 49 | #include "options.h" |
| 50 | #include "os.h" |
Jiyong Park | e5c4529 | 2020-05-26 19:06:24 +0900 | [diff] [blame] | 51 | #include "parser.h" |
Christopher Wiley | f690be5 | 2015-09-14 15:19:10 -0700 | [diff] [blame] | 52 | |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 53 | #ifndef O_BINARY |
| 54 | # define O_BINARY 0 |
| 55 | #endif |
| 56 | |
Christopher Wiley | 3a9911c | 2016-01-19 12:59:09 -0800 | [diff] [blame] | 57 | using android::base::Join; |
Christopher Wiley | d76067c | 2015-10-19 17:00:13 -0700 | [diff] [blame] | 58 | using android::base::Split; |
Christopher Wiley | c16e5e7 | 2015-09-16 10:49:40 -0700 | [diff] [blame] | 59 | using std::cerr; |
| 60 | using std::endl; |
Christopher Wiley | 9f4c7ae | 2015-08-24 14:07:32 -0700 | [diff] [blame] | 61 | using std::set; |
| 62 | using std::string; |
Christopher Wiley | 84c1eac | 2015-09-23 13:29:28 -0700 | [diff] [blame] | 63 | using std::unique_ptr; |
Christopher Wiley | 9f4c7ae | 2015-08-24 14:07:32 -0700 | [diff] [blame] | 64 | using std::vector; |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 65 | |
Christopher Wiley | f690be5 | 2015-09-14 15:19:10 -0700 | [diff] [blame] | 66 | namespace android { |
| 67 | namespace aidl { |
| 68 | namespace { |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 69 | |
Jiyong Park | 965c5b9 | 2018-11-21 13:37:15 +0900 | [diff] [blame] | 70 | // Copied from android.is.IBinder.[FIRST|LAST]_CALL_TRANSACTION |
| 71 | const int kFirstCallTransaction = 1; |
| 72 | const int kLastCallTransaction = 0x00ffffff; |
| 73 | |
| 74 | // Following IDs are all offsets from kFirstCallTransaction |
Jiyong Park | 309668e | 2018-07-28 16:55:44 +0900 | [diff] [blame] | 75 | |
| 76 | // IDs for meta transactions. Most of the meta transactions are implemented in |
| 77 | // the framework side (Binder.java or Binder.cpp). But these are the ones that |
| 78 | // are auto-implemented by the AIDL compiler. |
Jiyong Park | 965c5b9 | 2018-11-21 13:37:15 +0900 | [diff] [blame] | 79 | const int kFirstMetaMethodId = kLastCallTransaction - kFirstCallTransaction; |
| 80 | const int kGetInterfaceVersionId = kFirstMetaMethodId; |
Paul Trautrim | b77048c | 2020-01-21 16:39:32 +0900 | [diff] [blame] | 81 | const int kGetInterfaceHashId = kFirstMetaMethodId - 1; |
Jiyong Park | 965c5b9 | 2018-11-21 13:37:15 +0900 | [diff] [blame] | 82 | // Additional meta transactions implemented by AIDL should use |
| 83 | // kFirstMetaMethodId -1, -2, ...and so on. |
| 84 | |
| 85 | // Reserve 100 IDs for meta methods, which is more than enough. If we don't reserve, |
| 86 | // in the future, a newly added meta transaction ID will have a chance to |
| 87 | // collide with the user-defined methods that were added in the past. So, |
| 88 | // let's prevent users from using IDs in this range from the beginning. |
| 89 | const int kLastMetaMethodId = kFirstMetaMethodId - 99; |
| 90 | |
| 91 | // Range of IDs that is allowed for user-defined methods. |
| 92 | const int kMinUserSetMethodId = 0; |
| 93 | const int kMaxUserSetMethodId = kLastMetaMethodId - 1; |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 94 | |
Steven Moreland | 92c55f1 | 2018-07-31 14:08:37 -0700 | [diff] [blame] | 95 | bool check_filename(const std::string& filename, const AidlDefinedType& defined_type) { |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 96 | const char* p; |
| 97 | string expected; |
| 98 | string fn; |
| 99 | size_t len; |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 100 | bool valid = false; |
| 101 | |
Christopher Wiley | bc2df69 | 2016-06-02 16:27:26 -0700 | [diff] [blame] | 102 | if (!IoDelegate::GetAbsolutePath(filename, &fn)) { |
| 103 | return false; |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 104 | } |
| 105 | |
Steven Moreland | 92c55f1 | 2018-07-31 14:08:37 -0700 | [diff] [blame] | 106 | const std::string package = defined_type.GetPackage(); |
Casey Dahlin | fb7da2e | 2015-10-08 17:26:09 -0700 | [diff] [blame] | 107 | if (!package.empty()) { |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 108 | expected = package; |
| 109 | expected += '.'; |
| 110 | } |
| 111 | |
| 112 | len = expected.length(); |
| 113 | for (size_t i=0; i<len; i++) { |
| 114 | if (expected[i] == '.') { |
| 115 | expected[i] = OS_PATH_SEPARATOR; |
| 116 | } |
| 117 | } |
| 118 | |
Steven Moreland | 92c55f1 | 2018-07-31 14:08:37 -0700 | [diff] [blame] | 119 | const std::string name = defined_type.GetName(); |
Casey Dahlin | fb7da2e | 2015-10-08 17:26:09 -0700 | [diff] [blame] | 120 | expected.append(name, 0, name.find('.')); |
Christopher Wiley | 8f8cc9b | 2015-09-14 13:47:40 -0700 | [diff] [blame] | 121 | |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 122 | expected += ".aidl"; |
| 123 | |
| 124 | len = fn.length(); |
| 125 | valid = (len >= expected.length()); |
| 126 | |
| 127 | if (valid) { |
| 128 | p = fn.c_str() + (len - expected.length()); |
| 129 | |
Elliott Hughes | ce310da | 2015-07-29 08:44:17 -0700 | [diff] [blame] | 130 | #ifdef _WIN32 |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 131 | if (OS_PATH_SEPARATOR != '/') { |
| 132 | // Input filename under cygwin most likely has / separators |
| 133 | // whereas the expected string uses \\ separators. Adjust |
| 134 | // them accordingly. |
| 135 | for (char *c = const_cast<char *>(p); *c; ++c) { |
| 136 | if (*c == '/') *c = OS_PATH_SEPARATOR; |
| 137 | } |
| 138 | } |
| 139 | #endif |
| 140 | |
Yabin Cui | 482eefb | 2014-11-10 15:01:43 -0800 | [diff] [blame] | 141 | // aidl assumes case-insensitivity on Mac Os and Windows. |
| 142 | #if defined(__linux__) |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 143 | valid = (expected == p); |
| 144 | #else |
| 145 | valid = !strcasecmp(expected.c_str(), p); |
| 146 | #endif |
| 147 | } |
| 148 | |
| 149 | if (!valid) { |
Steven Moreland | 92c55f1 | 2018-07-31 14:08:37 -0700 | [diff] [blame] | 150 | AIDL_ERROR(defined_type) << name << " should be declared in a file called " << expected; |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 151 | } |
| 152 | |
Casey Dahlin | 42727f8 | 2015-10-12 19:23:40 -0700 | [diff] [blame] | 153 | return valid; |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 154 | } |
| 155 | |
Jiyong Park | 74595c1 | 2018-07-23 15:22:50 +0900 | [diff] [blame] | 156 | bool write_dep_file(const Options& options, const AidlDefinedType& defined_type, |
Jiyong Park | b034bf0 | 2018-07-30 17:44:33 +0900 | [diff] [blame] | 157 | const vector<string>& imports, const IoDelegate& io_delegate, |
Jiyong Park | 74595c1 | 2018-07-23 15:22:50 +0900 | [diff] [blame] | 158 | const string& input_file, const string& output_file) { |
| 159 | string dep_file_name = options.DependencyFile(); |
| 160 | if (dep_file_name.empty() && options.AutoDepFile()) { |
| 161 | dep_file_name = output_file + ".d"; |
| 162 | } |
| 163 | |
| 164 | if (dep_file_name.empty()) { |
| 165 | return true; // nothing to do |
| 166 | } |
Jiyong Park | 0546373 | 2018-08-09 16:03:02 +0900 | [diff] [blame] | 167 | |
Jiyong Park | 74595c1 | 2018-07-23 15:22:50 +0900 | [diff] [blame] | 168 | CodeWriterPtr writer = io_delegate.GetCodeWriter(dep_file_name); |
| 169 | if (!writer) { |
| 170 | LOG(ERROR) << "Could not open dependency file: " << dep_file_name; |
| 171 | return false; |
| 172 | } |
| 173 | |
| 174 | vector<string> source_aidl = {input_file}; |
| 175 | for (const auto& import : imports) { |
Jiyong Park | b034bf0 | 2018-07-30 17:44:33 +0900 | [diff] [blame] | 176 | source_aidl.push_back(import); |
Jiyong Park | 74595c1 | 2018-07-23 15:22:50 +0900 | [diff] [blame] | 177 | } |
| 178 | |
Christopher Wiley | 3a9911c | 2016-01-19 12:59:09 -0800 | [diff] [blame] | 179 | // Encode that the output file depends on aidl input files. |
Jiyong Park | df20212 | 2019-09-30 20:48:35 +0900 | [diff] [blame] | 180 | if (defined_type.AsUnstructuredParcelable() != nullptr && |
| 181 | options.TargetLanguage() == Options::Language::JAVA) { |
| 182 | // Legacy behavior. For parcelable declarations in Java, don't emit output file as |
| 183 | // the dependency target. b/141372861 |
| 184 | writer->Write(" : \\\n"); |
| 185 | } else { |
| 186 | writer->Write("%s : \\\n", output_file.c_str()); |
| 187 | } |
Jiyong Park | 74595c1 | 2018-07-23 15:22:50 +0900 | [diff] [blame] | 188 | writer->Write(" %s", Join(source_aidl, " \\\n ").c_str()); |
Dan Willemsen | 93298ee | 2016-11-10 23:55:55 -0800 | [diff] [blame] | 189 | writer->Write("\n"); |
Christopher Wiley | a30a45e | 2015-10-17 10:56:59 -0700 | [diff] [blame] | 190 | |
Jiyong Park | 74595c1 | 2018-07-23 15:22:50 +0900 | [diff] [blame] | 191 | if (!options.DependencyFileNinja()) { |
Dan Willemsen | 93298ee | 2016-11-10 23:55:55 -0800 | [diff] [blame] | 192 | writer->Write("\n"); |
| 193 | // Output "<input_aidl_file>: " so make won't fail if the input .aidl file |
| 194 | // has been deleted, moved or renamed in incremental build. |
Jiyong Park | 74595c1 | 2018-07-23 15:22:50 +0900 | [diff] [blame] | 195 | for (const auto& src : source_aidl) { |
Dan Willemsen | 93298ee | 2016-11-10 23:55:55 -0800 | [diff] [blame] | 196 | writer->Write("%s :\n", src.c_str()); |
| 197 | } |
Christopher Wiley | 3a9911c | 2016-01-19 12:59:09 -0800 | [diff] [blame] | 198 | } |
Christopher Wiley | a30a45e | 2015-10-17 10:56:59 -0700 | [diff] [blame] | 199 | |
Steven Moreland | c26d814 | 2018-09-17 14:25:33 -0700 | [diff] [blame] | 200 | if (options.IsCppOutput()) { |
Jiyong Park | 74595c1 | 2018-07-23 15:22:50 +0900 | [diff] [blame] | 201 | if (!options.DependencyFileNinja()) { |
| 202 | using ::android::aidl::cpp::ClassNames; |
| 203 | using ::android::aidl::cpp::HeaderFile; |
| 204 | vector<string> headers; |
Jiyong Park | 5b7e532 | 2019-04-03 20:05:01 +0900 | [diff] [blame] | 205 | for (ClassNames c : {ClassNames::CLIENT, ClassNames::SERVER, ClassNames::RAW}) { |
Jiyong Park | 0546373 | 2018-08-09 16:03:02 +0900 | [diff] [blame] | 206 | headers.push_back(options.OutputHeaderDir() + |
Jiyong Park | 74595c1 | 2018-07-23 15:22:50 +0900 | [diff] [blame] | 207 | HeaderFile(defined_type, c, false /* use_os_sep */)); |
| 208 | } |
Christopher Wiley | 3a9911c | 2016-01-19 12:59:09 -0800 | [diff] [blame] | 209 | |
Jiyong Park | 74595c1 | 2018-07-23 15:22:50 +0900 | [diff] [blame] | 210 | writer->Write("\n"); |
| 211 | |
| 212 | // Generated headers also depend on the source aidl files. |
| 213 | writer->Write("%s : \\\n %s\n", Join(headers, " \\\n ").c_str(), |
| 214 | Join(source_aidl, " \\\n ").c_str()); |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 215 | } |
Christopher Wiley | a30a45e | 2015-10-17 10:56:59 -0700 | [diff] [blame] | 216 | } |
Christopher Wiley | 3a9911c | 2016-01-19 12:59:09 -0800 | [diff] [blame] | 217 | |
Christopher Wiley | 3a9911c | 2016-01-19 12:59:09 -0800 | [diff] [blame] | 218 | return true; |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 219 | } |
| 220 | |
Jiyong Park | 74595c1 | 2018-07-23 15:22:50 +0900 | [diff] [blame] | 221 | string generate_outputFileName(const Options& options, const AidlDefinedType& defined_type) { |
Steven Moreland | 5557f1c | 2018-07-02 13:50:23 -0700 | [diff] [blame] | 222 | // create the path to the destination folder based on the |
| 223 | // defined_type package name |
Jiyong Park | 74595c1 | 2018-07-23 15:22:50 +0900 | [diff] [blame] | 224 | string result = options.OutputDir(); |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 225 | |
Jiyong Park | 74595c1 | 2018-07-23 15:22:50 +0900 | [diff] [blame] | 226 | string package = defined_type.GetPackage(); |
| 227 | size_t len = package.length(); |
Steven Moreland | 5557f1c | 2018-07-02 13:50:23 -0700 | [diff] [blame] | 228 | for (size_t i = 0; i < len; i++) { |
Jiyong Park | 74595c1 | 2018-07-23 15:22:50 +0900 | [diff] [blame] | 229 | if (package[i] == '.') { |
| 230 | package[i] = OS_PATH_SEPARATOR; |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 231 | } |
Steven Moreland | 5557f1c | 2018-07-02 13:50:23 -0700 | [diff] [blame] | 232 | } |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 233 | |
Jiyong Park | 74595c1 | 2018-07-23 15:22:50 +0900 | [diff] [blame] | 234 | result += package; |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 235 | |
Steven Moreland | 5557f1c | 2018-07-02 13:50:23 -0700 | [diff] [blame] | 236 | // add the filename by replacing the .aidl extension to .java |
Jiyong Park | 74595c1 | 2018-07-23 15:22:50 +0900 | [diff] [blame] | 237 | const string& name = defined_type.GetName(); |
Steven Moreland | 5557f1c | 2018-07-02 13:50:23 -0700 | [diff] [blame] | 238 | result += OS_PATH_SEPARATOR; |
| 239 | result.append(name, 0, name.find('.')); |
Jiyong Park | b03551f | 2018-08-06 19:20:51 +0900 | [diff] [blame] | 240 | if (options.TargetLanguage() == Options::Language::JAVA) { |
| 241 | result += ".java"; |
Steven Moreland | c26d814 | 2018-09-17 14:25:33 -0700 | [diff] [blame] | 242 | } else if (options.IsCppOutput()) { |
Jiyong Park | b03551f | 2018-08-06 19:20:51 +0900 | [diff] [blame] | 243 | result += ".cpp"; |
Andrei Homescu | b62afd9 | 2020-05-11 19:24:59 -0700 | [diff] [blame] | 244 | } else if (options.TargetLanguage() == Options::Language::RUST) { |
| 245 | result += ".rs"; |
Jiyong Park | b03551f | 2018-08-06 19:20:51 +0900 | [diff] [blame] | 246 | } else { |
| 247 | LOG(FATAL) << "Should not reach here" << endl; |
| 248 | return ""; |
| 249 | } |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 250 | |
Steven Moreland | 5557f1c | 2018-07-02 13:50:23 -0700 | [diff] [blame] | 251 | return result; |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 252 | } |
| 253 | |
Jiyong Park | b034bf0 | 2018-07-30 17:44:33 +0900 | [diff] [blame] | 254 | bool check_and_assign_method_ids(const std::vector<std::unique_ptr<AidlMethod>>& items) { |
Jiyong Park | 309668e | 2018-07-28 16:55:44 +0900 | [diff] [blame] | 255 | // Check whether there are any methods with manually assigned id's and any |
| 256 | // that are not. Either all method id's must be manually assigned or all of |
| 257 | // them must not. Also, check for uplicates of user set ID's and that the |
| 258 | // ID's are within the proper bounds. |
| 259 | set<int> usedIds; |
| 260 | bool hasUnassignedIds = false; |
| 261 | bool hasAssignedIds = false; |
Steven Moreland | ec6f469 | 2019-08-13 10:03:24 -0700 | [diff] [blame] | 262 | int newId = kMinUserSetMethodId; |
Jiyong Park | 309668e | 2018-07-28 16:55:44 +0900 | [diff] [blame] | 263 | for (const auto& item : items) { |
| 264 | // However, meta transactions that are added by the AIDL compiler are |
| 265 | // exceptions. They have fixed IDs but allowed to be with user-defined |
| 266 | // methods having auto-assigned IDs. This is because the Ids of the meta |
| 267 | // transactions must be stable during the entire lifetime of an interface. |
| 268 | // In other words, their IDs must be the same even when new user-defined |
| 269 | // methods are added. |
Jiyong Park | 3633b72 | 2019-04-11 15:38:26 +0900 | [diff] [blame] | 270 | if (!item->IsUserDefined()) { |
| 271 | continue; |
| 272 | } |
| 273 | if (item->HasId()) { |
Jiyong Park | 309668e | 2018-07-28 16:55:44 +0900 | [diff] [blame] | 274 | hasAssignedIds = true; |
Jiyong Park | 309668e | 2018-07-28 16:55:44 +0900 | [diff] [blame] | 275 | } else { |
Steven Moreland | ec6f469 | 2019-08-13 10:03:24 -0700 | [diff] [blame] | 276 | item->SetId(newId++); |
Jiyong Park | 309668e | 2018-07-28 16:55:44 +0900 | [diff] [blame] | 277 | hasUnassignedIds = true; |
| 278 | } |
Steven Moreland | ec6f469 | 2019-08-13 10:03:24 -0700 | [diff] [blame] | 279 | |
Jiyong Park | 309668e | 2018-07-28 16:55:44 +0900 | [diff] [blame] | 280 | if (hasAssignedIds && hasUnassignedIds) { |
Jiyong Park | b034bf0 | 2018-07-30 17:44:33 +0900 | [diff] [blame] | 281 | AIDL_ERROR(item) << "You must either assign id's to all methods or to none of them."; |
Jiyong Park | 309668e | 2018-07-28 16:55:44 +0900 | [diff] [blame] | 282 | return false; |
| 283 | } |
Jiyong Park | 309668e | 2018-07-28 16:55:44 +0900 | [diff] [blame] | 284 | |
Steven Moreland | ec6f469 | 2019-08-13 10:03:24 -0700 | [diff] [blame] | 285 | // Ensure that the user set id is not duplicated. |
| 286 | if (usedIds.find(item->GetId()) != usedIds.end()) { |
| 287 | // We found a duplicate id, so throw an error. |
| 288 | AIDL_ERROR(item) << "Found duplicate method id (" << item->GetId() << ") for method " |
| 289 | << item->GetName(); |
| 290 | return false; |
| 291 | } |
| 292 | usedIds.insert(item->GetId()); |
| 293 | |
| 294 | // Ensure that the user set id is within the appropriate limits |
| 295 | if (item->GetId() < kMinUserSetMethodId || item->GetId() > kMaxUserSetMethodId) { |
| 296 | AIDL_ERROR(item) << "Found out of bounds id (" << item->GetId() << ") for method " |
| 297 | << item->GetName() << ". Value for id must be between " |
| 298 | << kMinUserSetMethodId << " and " << kMaxUserSetMethodId << " inclusive."; |
| 299 | return false; |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 300 | } |
Jiyong Park | 309668e | 2018-07-28 16:55:44 +0900 | [diff] [blame] | 301 | } |
Steven Moreland | ec6f469 | 2019-08-13 10:03:24 -0700 | [diff] [blame] | 302 | |
Jiyong Park | 309668e | 2018-07-28 16:55:44 +0900 | [diff] [blame] | 303 | return true; |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 304 | } |
| 305 | |
Christopher Wiley | ef14093 | 2015-11-03 09:29:19 -0800 | [diff] [blame] | 306 | // TODO: Remove this in favor of using the YACC parser b/25479378 |
Jiyong Park | 1813218 | 2020-06-08 20:24:40 +0900 | [diff] [blame] | 307 | bool ParsePreprocessedLine(const string& line, string* decl, std::string* package, |
| 308 | string* class_name) { |
Christopher Wiley | ef14093 | 2015-11-03 09:29:19 -0800 | [diff] [blame] | 309 | // erase all trailing whitespace and semicolons |
| 310 | const size_t end = line.find_last_not_of(" ;\t"); |
| 311 | if (end == string::npos) { |
| 312 | return false; |
| 313 | } |
| 314 | if (line.rfind(';', end) != string::npos) { |
| 315 | return false; |
| 316 | } |
| 317 | |
| 318 | decl->clear(); |
| 319 | string type; |
| 320 | vector<string> pieces = Split(line.substr(0, end + 1), " \t"); |
| 321 | for (const string& piece : pieces) { |
| 322 | if (piece.empty()) { |
| 323 | continue; |
| 324 | } |
| 325 | if (decl->empty()) { |
| 326 | *decl = std::move(piece); |
| 327 | } else if (type.empty()) { |
| 328 | type = std::move(piece); |
| 329 | } else { |
| 330 | return false; |
| 331 | } |
| 332 | } |
| 333 | |
| 334 | // Note that this logic is absolutely wrong. Given a parcelable |
| 335 | // org.some.Foo.Bar, the class name is Foo.Bar, but this code will claim that |
| 336 | // the class is just Bar. However, this was the way it was done in the past. |
| 337 | // |
| 338 | // See b/17415692 |
| 339 | size_t dot_pos = type.rfind('.'); |
| 340 | if (dot_pos != string::npos) { |
| 341 | *class_name = type.substr(dot_pos + 1); |
Jiyong Park | 1813218 | 2020-06-08 20:24:40 +0900 | [diff] [blame] | 342 | *package = type.substr(0, dot_pos); |
Christopher Wiley | ef14093 | 2015-11-03 09:29:19 -0800 | [diff] [blame] | 343 | } else { |
| 344 | *class_name = type; |
| 345 | package->clear(); |
| 346 | } |
| 347 | |
| 348 | return true; |
| 349 | } |
| 350 | |
Christopher Wiley | 4a2884b | 2015-10-07 11:27:45 -0700 | [diff] [blame] | 351 | } // namespace |
| 352 | |
| 353 | namespace internals { |
| 354 | |
Jiyong Park | 1deecc3 | 2018-07-17 01:14:41 +0900 | [diff] [blame] | 355 | bool parse_preprocessed_file(const IoDelegate& io_delegate, const string& filename, |
Jeongik Cha | 047c5ee | 2019-08-07 23:16:49 +0900 | [diff] [blame] | 356 | AidlTypenames* typenames) { |
Christopher Wiley | ef14093 | 2015-11-03 09:29:19 -0800 | [diff] [blame] | 357 | bool success = true; |
| 358 | unique_ptr<LineReader> line_reader = io_delegate.GetLineReader(filename); |
| 359 | if (!line_reader) { |
| 360 | LOG(ERROR) << "cannot open preprocessed file: " << filename; |
| 361 | success = false; |
| 362 | return success; |
| 363 | } |
| 364 | |
| 365 | string line; |
Dan Willemsen | 609ba6d | 2019-12-30 10:44:00 -0800 | [diff] [blame] | 366 | int lineno = 1; |
Christopher Wiley | ef14093 | 2015-11-03 09:29:19 -0800 | [diff] [blame] | 367 | for ( ; line_reader->ReadLine(&line); ++lineno) { |
| 368 | if (line.empty() || line.compare(0, 2, "//") == 0) { |
| 369 | // skip comments and empty lines |
| 370 | continue; |
| 371 | } |
| 372 | |
| 373 | string decl; |
Jiyong Park | 1813218 | 2020-06-08 20:24:40 +0900 | [diff] [blame] | 374 | std::string package; |
Christopher Wiley | ef14093 | 2015-11-03 09:29:19 -0800 | [diff] [blame] | 375 | string class_name; |
| 376 | if (!ParsePreprocessedLine(line, &decl, &package, &class_name)) { |
| 377 | success = false; |
| 378 | break; |
| 379 | } |
| 380 | |
Steven Moreland | 46e9da8 | 2018-07-27 15:45:29 -0700 | [diff] [blame] | 381 | AidlLocation::Point point = {.line = lineno, .column = 0 /*column*/}; |
Devin Moore | df93ebb | 2020-03-25 14:03:35 -0700 | [diff] [blame] | 382 | AidlLocation location = AidlLocation(filename, point, point, AidlLocation::Source::EXTERNAL); |
Steven Moreland | 46e9da8 | 2018-07-27 15:45:29 -0700 | [diff] [blame] | 383 | |
Christopher Wiley | ef14093 | 2015-11-03 09:29:19 -0800 | [diff] [blame] | 384 | if (decl == "parcelable") { |
Jeongik Cha | ce58bc6 | 2019-04-22 13:30:39 +0900 | [diff] [blame] | 385 | // ParcelFileDescriptor is treated as a built-in type, but it's also in the framework.aidl. |
| 386 | // So aidl should ignore built-in types in framework.aidl to prevent duplication. |
| 387 | // (b/130899491) |
| 388 | if (AidlTypenames::IsBuiltinTypename(class_name)) { |
| 389 | continue; |
| 390 | } |
Jiyong Park | 1813218 | 2020-06-08 20:24:40 +0900 | [diff] [blame] | 391 | AidlParcelable* doc = new AidlParcelable(location, class_name, package, "" /* comments */); |
Jeongik Cha | 047c5ee | 2019-08-07 23:16:49 +0900 | [diff] [blame] | 392 | typenames->AddPreprocessedType(unique_ptr<AidlParcelable>(doc)); |
Steven Moreland | ed83a28 | 2018-07-17 13:27:29 -0700 | [diff] [blame] | 393 | } else if (decl == "structured_parcelable") { |
| 394 | auto temp = new std::vector<std::unique_ptr<AidlVariableDeclaration>>(); |
Devin Moore | 53fc99c | 2020-08-12 08:07:52 -0700 | [diff] [blame] | 395 | AidlStructuredParcelable* doc = new AidlStructuredParcelable( |
| 396 | location, class_name, package, "" /* comments */, temp, nullptr); |
Jeongik Cha | 047c5ee | 2019-08-07 23:16:49 +0900 | [diff] [blame] | 397 | typenames->AddPreprocessedType(unique_ptr<AidlStructuredParcelable>(doc)); |
Christopher Wiley | ef14093 | 2015-11-03 09:29:19 -0800 | [diff] [blame] | 398 | } else if (decl == "interface") { |
Casey Dahlin | d40e2fe | 2015-11-24 14:06:52 -0800 | [diff] [blame] | 399 | auto temp = new std::vector<std::unique_ptr<AidlMember>>(); |
Steven Moreland | 46e9da8 | 2018-07-27 15:45:29 -0700 | [diff] [blame] | 400 | AidlInterface* doc = new AidlInterface(location, class_name, "", false, temp, package); |
Jeongik Cha | 047c5ee | 2019-08-07 23:16:49 +0900 | [diff] [blame] | 401 | typenames->AddPreprocessedType(unique_ptr<AidlInterface>(doc)); |
Christopher Wiley | ef14093 | 2015-11-03 09:29:19 -0800 | [diff] [blame] | 402 | } else { |
| 403 | success = false; |
| 404 | break; |
| 405 | } |
| 406 | } |
| 407 | if (!success) { |
| 408 | LOG(ERROR) << filename << ':' << lineno |
| 409 | << " malformed preprocessed file line: '" << line << "'"; |
| 410 | } |
| 411 | |
| 412 | return success; |
| 413 | } |
| 414 | |
Jiyong Park | fbbfa93 | 2018-07-30 21:44:10 +0900 | [diff] [blame] | 415 | AidlError load_and_validate_aidl(const std::string& input_file_name, const Options& options, |
Jeongik Cha | 047c5ee | 2019-08-07 23:16:49 +0900 | [diff] [blame] | 416 | const IoDelegate& io_delegate, AidlTypenames* typenames, |
Jiyong Park | b034bf0 | 2018-07-30 17:44:33 +0900 | [diff] [blame] | 417 | vector<string>* imported_files) { |
Christopher Wiley | 632801d | 2015-11-05 14:15:49 -0800 | [diff] [blame] | 418 | AidlError err = AidlError::OK; |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 419 | |
Jiyong Park | b034bf0 | 2018-07-30 17:44:33 +0900 | [diff] [blame] | 420 | ////////////////////////////////////////////////////////////////////////// |
| 421 | // Loading phase |
| 422 | ////////////////////////////////////////////////////////////////////////// |
Jiyong Park | fbbfa93 | 2018-07-30 21:44:10 +0900 | [diff] [blame] | 423 | |
Jiyong Park | b034bf0 | 2018-07-30 17:44:33 +0900 | [diff] [blame] | 424 | // Parse the main input file |
Jeongik Cha | 047c5ee | 2019-08-07 23:16:49 +0900 | [diff] [blame] | 425 | std::unique_ptr<Parser> main_parser = Parser::Parse(input_file_name, io_delegate, *typenames); |
Steven Moreland | 64e29be | 2018-08-08 18:52:19 -0700 | [diff] [blame] | 426 | if (main_parser == nullptr) { |
Jiyong Park | b034bf0 | 2018-07-30 17:44:33 +0900 | [diff] [blame] | 427 | return AidlError::PARSE_ERROR; |
| 428 | } |
Jiyong Park | da8c693 | 2019-08-12 19:56:08 +0900 | [diff] [blame] | 429 | int num_interfaces_or_structured_parcelables = 0; |
Jiyong Park | 8e79b7f | 2020-07-20 20:52:38 +0900 | [diff] [blame] | 430 | for (const auto& type : main_parser->ParsedDocument().DefinedTypes()) { |
Jiyong Park | da8c693 | 2019-08-12 19:56:08 +0900 | [diff] [blame] | 431 | if (type->AsInterface() != nullptr || type->AsStructuredParcelable() != nullptr) { |
| 432 | num_interfaces_or_structured_parcelables++; |
Devin Moore | 5de18ed | 2020-04-02 13:52:29 -0700 | [diff] [blame] | 433 | if (num_interfaces_or_structured_parcelables > 1) { |
| 434 | AIDL_ERROR(*type) << "You must declare only one type per file."; |
| 435 | return AidlError::BAD_TYPE; |
| 436 | } |
Jiyong Park | da8c693 | 2019-08-12 19:56:08 +0900 | [diff] [blame] | 437 | } |
| 438 | } |
Jiyong Park | b034bf0 | 2018-07-30 17:44:33 +0900 | [diff] [blame] | 439 | |
| 440 | // Import the preprocessed file |
Jiyong Park | fbbfa93 | 2018-07-30 21:44:10 +0900 | [diff] [blame] | 441 | for (const string& s : options.PreprocessedFiles()) { |
Jeongik Cha | 047c5ee | 2019-08-07 23:16:49 +0900 | [diff] [blame] | 442 | if (!parse_preprocessed_file(io_delegate, s, typenames)) { |
Christopher Wiley | 632801d | 2015-11-05 14:15:49 -0800 | [diff] [blame] | 443 | err = AidlError::BAD_PRE_PROCESSED_FILE; |
Christopher Wiley | ef14093 | 2015-11-03 09:29:19 -0800 | [diff] [blame] | 444 | } |
Christopher Wiley | 3a9d158 | 2015-09-16 12:42:14 -0700 | [diff] [blame] | 445 | } |
Christopher Wiley | 632801d | 2015-11-05 14:15:49 -0800 | [diff] [blame] | 446 | if (err != AidlError::OK) { |
Christopher Wiley | 3a9d158 | 2015-09-16 12:42:14 -0700 | [diff] [blame] | 447 | return err; |
| 448 | } |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 449 | |
Jiyong Park | b034bf0 | 2018-07-30 17:44:33 +0900 | [diff] [blame] | 450 | // Find files to import and parse them |
Jiyong Park | e59c368 | 2018-09-11 23:10:25 +0900 | [diff] [blame] | 451 | vector<string> import_paths; |
Jiyong Park | 8c38053 | 2018-08-30 14:55:26 +0900 | [diff] [blame] | 452 | ImportResolver import_resolver{io_delegate, input_file_name, options.ImportDirs(), |
| 453 | options.InputFiles()}; |
Jiyong Park | e59c368 | 2018-09-11 23:10:25 +0900 | [diff] [blame] | 454 | |
Jiyong Park | 8f6ec46 | 2020-01-19 20:52:47 +0900 | [diff] [blame] | 455 | vector<string> type_from_import_statements; |
Jiyong Park | 8e79b7f | 2020-07-20 20:52:38 +0900 | [diff] [blame] | 456 | for (const auto& import : main_parser->ParsedDocument().Imports()) { |
Jiyong Park | e05195e | 2018-10-08 18:24:23 +0900 | [diff] [blame] | 457 | if (!AidlTypenames::IsBuiltinTypename(import->GetNeededClass())) { |
Jiyong Park | 8f6ec46 | 2020-01-19 20:52:47 +0900 | [diff] [blame] | 458 | type_from_import_statements.emplace_back(import->GetNeededClass()); |
Jiyong Park | e05195e | 2018-10-08 18:24:23 +0900 | [diff] [blame] | 459 | } |
Jiyong Park | e59c368 | 2018-09-11 23:10:25 +0900 | [diff] [blame] | 460 | } |
| 461 | |
| 462 | // When referencing a type using fully qualified name it should be imported |
| 463 | // without the import statement. To support that, add all unresolved |
| 464 | // typespecs encountered during the parsing to the import_candidates list. |
| 465 | // Note that there is no guarantee that the typespecs are all fully qualified. |
| 466 | // It will be determined by calling FindImportFile(). |
| 467 | set<string> unresolved_types; |
| 468 | for (const auto type : main_parser->GetUnresolvedTypespecs()) { |
| 469 | if (!AidlTypenames::IsBuiltinTypename(type->GetName())) { |
| 470 | unresolved_types.emplace(type->GetName()); |
| 471 | } |
| 472 | } |
Jiyong Park | 8f6ec46 | 2020-01-19 20:52:47 +0900 | [diff] [blame] | 473 | vector<string> import_candidates(type_from_import_statements); |
| 474 | import_candidates.insert(import_candidates.end(), unresolved_types.begin(), |
| 475 | unresolved_types.end()); |
Jiyong Park | e59c368 | 2018-09-11 23:10:25 +0900 | [diff] [blame] | 476 | for (const auto& import : import_candidates) { |
Jeongik Cha | 047c5ee | 2019-08-07 23:16:49 +0900 | [diff] [blame] | 477 | if (typenames->IsIgnorableImport(import)) { |
Christopher Wiley | fb4b22d | 2015-09-25 15:16:13 -0700 | [diff] [blame] | 478 | // There are places in the Android tree where an import doesn't resolve, |
| 479 | // but we'll pick the type up through the preprocessed types. |
| 480 | // This seems like an error, but legacy support demands we support it... |
Christopher Wiley | c16e5e7 | 2015-09-16 10:49:40 -0700 | [diff] [blame] | 481 | continue; |
| 482 | } |
Jiyong Park | e59c368 | 2018-09-11 23:10:25 +0900 | [diff] [blame] | 483 | string import_path = import_resolver.FindImportFile(import); |
Christopher Wiley | 72877ac | 2015-10-06 14:41:42 -0700 | [diff] [blame] | 484 | if (import_path.empty()) { |
Steven Moreland | cb1bcd7 | 2020-04-29 16:30:35 -0700 | [diff] [blame] | 485 | if (typenames->ResolveTypename(import).is_resolved) { |
Jiyong Park | 8f6ec46 | 2020-01-19 20:52:47 +0900 | [diff] [blame] | 486 | // Couldn't find the *.aidl file for the type from the include paths, but we |
| 487 | // have the type already resolved. This could happen when the type is |
| 488 | // from the preprocessed aidl file. In that case, use the type from the |
| 489 | // preprocessed aidl file as a last resort. |
| 490 | continue; |
| 491 | } |
| 492 | |
| 493 | if (std::find(type_from_import_statements.begin(), type_from_import_statements.end(), |
| 494 | import) != type_from_import_statements.end()) { |
Jiyong Park | e59c368 | 2018-09-11 23:10:25 +0900 | [diff] [blame] | 495 | // Complain only when the import from the import statement has failed. |
Devin Moore | 5de18ed | 2020-04-02 13:52:29 -0700 | [diff] [blame] | 496 | AIDL_ERROR(input_file_name) << "Couldn't find import for class " << import; |
Jiyong Park | e59c368 | 2018-09-11 23:10:25 +0900 | [diff] [blame] | 497 | err = AidlError::BAD_IMPORT; |
| 498 | } |
Christopher Wiley | c16e5e7 | 2015-09-16 10:49:40 -0700 | [diff] [blame] | 499 | continue; |
| 500 | } |
Casey Dahlin | 2cc9316 | 2015-10-02 16:14:17 -0700 | [diff] [blame] | 501 | |
Jiyong Park | e59c368 | 2018-09-11 23:10:25 +0900 | [diff] [blame] | 502 | import_paths.emplace_back(import_path); |
Jiyong Park | b034bf0 | 2018-07-30 17:44:33 +0900 | [diff] [blame] | 503 | |
Jeongik Cha | 047c5ee | 2019-08-07 23:16:49 +0900 | [diff] [blame] | 504 | std::unique_ptr<Parser> import_parser = Parser::Parse(import_path, io_delegate, *typenames); |
Steven Moreland | 64e29be | 2018-08-08 18:52:19 -0700 | [diff] [blame] | 505 | if (import_parser == nullptr) { |
Jiyong Park | e59c368 | 2018-09-11 23:10:25 +0900 | [diff] [blame] | 506 | cerr << "error while importing " << import_path << " for " << import << endl; |
Christopher Wiley | 632801d | 2015-11-05 14:15:49 -0800 | [diff] [blame] | 507 | err = AidlError::BAD_IMPORT; |
Christopher Wiley | c16e5e7 | 2015-09-16 10:49:40 -0700 | [diff] [blame] | 508 | continue; |
| 509 | } |
Christopher Wiley | c16e5e7 | 2015-09-16 10:49:40 -0700 | [diff] [blame] | 510 | } |
Christopher Wiley | 632801d | 2015-11-05 14:15:49 -0800 | [diff] [blame] | 511 | if (err != AidlError::OK) { |
Christopher Wiley | c16e5e7 | 2015-09-16 10:49:40 -0700 | [diff] [blame] | 512 | return err; |
Christopher Wiley | 3a9d158 | 2015-09-16 12:42:14 -0700 | [diff] [blame] | 513 | } |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 514 | |
Jiyong Park | 3c35e39 | 2018-08-30 13:10:30 +0900 | [diff] [blame] | 515 | for (const auto& imported_file : options.ImportFiles()) { |
Jiyong Park | e59c368 | 2018-09-11 23:10:25 +0900 | [diff] [blame] | 516 | import_paths.emplace_back(imported_file); |
Jiyong Park | 3c35e39 | 2018-08-30 13:10:30 +0900 | [diff] [blame] | 517 | |
Jeongik Cha | 047c5ee | 2019-08-07 23:16:49 +0900 | [diff] [blame] | 518 | std::unique_ptr<Parser> import_parser = Parser::Parse(imported_file, io_delegate, *typenames); |
Jiyong Park | 3c35e39 | 2018-08-30 13:10:30 +0900 | [diff] [blame] | 519 | if (import_parser == nullptr) { |
| 520 | AIDL_ERROR(imported_file) << "error while importing " << imported_file; |
| 521 | err = AidlError::BAD_IMPORT; |
| 522 | continue; |
| 523 | } |
Jiyong Park | 3c35e39 | 2018-08-30 13:10:30 +0900 | [diff] [blame] | 524 | } |
| 525 | if (err != AidlError::OK) { |
| 526 | return err; |
| 527 | } |
Jiyong Park | 96c16a9 | 2018-08-16 16:37:09 +0900 | [diff] [blame] | 528 | const bool is_check_api = options.GetTask() == Options::Task::CHECK_API; |
Jiyong Park | b034bf0 | 2018-07-30 17:44:33 +0900 | [diff] [blame] | 529 | |
| 530 | // Resolve the unresolved type references found from the input file |
Jiyong Park | 96c16a9 | 2018-08-16 16:37:09 +0900 | [diff] [blame] | 531 | if (!is_check_api && !main_parser->Resolve()) { |
| 532 | // Resolution is not need for check api because all typespecs are |
| 533 | // using fully qualified names. |
Jiyong Park | 1deecc3 | 2018-07-17 01:14:41 +0900 | [diff] [blame] | 534 | return AidlError::BAD_TYPE; |
| 535 | } |
Daniel Norman | 85aed54 | 2019-08-21 12:01:14 -0700 | [diff] [blame] | 536 | |
| 537 | typenames->IterateTypes([&](const AidlDefinedType& type) { |
| 538 | AidlEnumDeclaration* enum_decl = const_cast<AidlEnumDeclaration*>(type.AsEnumDeclaration()); |
| 539 | if (enum_decl != nullptr) { |
| 540 | // BackingType is filled in for all known enums, including imported enums, |
| 541 | // because other types that may use enums, such as Interface or |
| 542 | // StructuredParcelable, need to know the enum BackingType when |
| 543 | // generating code. |
Daniel Norman | 716d311 | 2019-09-10 13:11:56 -0700 | [diff] [blame] | 544 | if (auto backing_type = enum_decl->BackingType(*typenames); backing_type != nullptr) { |
Daniel Norman | 85aed54 | 2019-08-21 12:01:14 -0700 | [diff] [blame] | 545 | enum_decl->SetBackingType(std::unique_ptr<const AidlTypeSpecifier>(backing_type)); |
| 546 | } else { |
| 547 | // Default to byte type for enums. |
Daniel Norman | 716d311 | 2019-09-10 13:11:56 -0700 | [diff] [blame] | 548 | auto byte_type = |
| 549 | std::make_unique<AidlTypeSpecifier>(AIDL_LOCATION_HERE, "byte", false, nullptr, ""); |
| 550 | byte_type->Resolve(*typenames); |
| 551 | enum_decl->SetBackingType(std::move(byte_type)); |
Daniel Norman | 85aed54 | 2019-08-21 12:01:14 -0700 | [diff] [blame] | 552 | } |
| 553 | |
Steven Moreland | 59e53e4 | 2019-11-26 20:38:08 -0800 | [diff] [blame] | 554 | if (!enum_decl->Autofill()) { |
| 555 | err = AidlError::BAD_TYPE; |
| 556 | } |
Daniel Norman | 85aed54 | 2019-08-21 12:01:14 -0700 | [diff] [blame] | 557 | } |
| 558 | }); |
Steven Moreland | 59e53e4 | 2019-11-26 20:38:08 -0800 | [diff] [blame] | 559 | if (err != AidlError::OK) { |
| 560 | return err; |
| 561 | } |
Daniel Norman | 85aed54 | 2019-08-21 12:01:14 -0700 | [diff] [blame] | 562 | |
Jeongik Cha | db0f59e | 2018-11-01 18:11:21 +0900 | [diff] [blame] | 563 | ////////////////////////////////////////////////////////////////////////// |
| 564 | // Validation phase |
| 565 | ////////////////////////////////////////////////////////////////////////// |
| 566 | |
Steven Moreland | e2c64b4 | 2018-09-18 15:06:37 -0700 | [diff] [blame] | 567 | // For legacy reasons, by default, compiling an unstructured parcelable (which contains no output) |
| 568 | // is allowed. This must not be returned as an error until the very end of this procedure since |
| 569 | // this may be considered a success, and we should first check that there are not other, more |
| 570 | // serious failures. |
| 571 | bool contains_unstructured_parcelable = false; |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 572 | |
Jiyong Park | 8e79b7f | 2020-07-20 20:52:38 +0900 | [diff] [blame] | 573 | const auto& types = main_parser->ParsedDocument().DefinedTypes(); |
Jiyong Park | 6251551 | 2020-06-08 15:57:11 +0900 | [diff] [blame] | 574 | const int num_defined_types = types.size(); |
Jiyong Park | 8e79b7f | 2020-07-20 20:52:38 +0900 | [diff] [blame] | 575 | for (const auto& defined_type : types) { |
Jeongik Cha | db0f59e | 2018-11-01 18:11:21 +0900 | [diff] [blame] | 576 | CHECK(defined_type != nullptr); |
Jeongik Cha | 047c5ee | 2019-08-07 23:16:49 +0900 | [diff] [blame] | 577 | |
| 578 | // Language specific validation |
Steven Moreland | d59e317 | 2020-05-11 16:42:09 -0700 | [diff] [blame] | 579 | if (!defined_type->LanguageSpecificCheckValid(*typenames, options.TargetLanguage())) { |
Jeongik Cha | 047c5ee | 2019-08-07 23:16:49 +0900 | [diff] [blame] | 580 | return AidlError::BAD_TYPE; |
| 581 | } |
| 582 | |
Steven Moreland | e2c64b4 | 2018-09-18 15:06:37 -0700 | [diff] [blame] | 583 | AidlParcelable* unstructuredParcelable = defined_type->AsUnstructuredParcelable(); |
| 584 | if (unstructuredParcelable != nullptr) { |
Jeongik Cha | 047c5ee | 2019-08-07 23:16:49 +0900 | [diff] [blame] | 585 | if (!unstructuredParcelable->CheckValid(*typenames)) { |
Jeongik Cha | 82317dd | 2019-02-27 20:26:11 +0900 | [diff] [blame] | 586 | return AidlError::BAD_TYPE; |
| 587 | } |
Jeongik Cha | 88f95a8 | 2020-01-15 13:02:16 +0900 | [diff] [blame] | 588 | bool isStable = unstructuredParcelable->IsStableApiParcelable(options.TargetLanguage()); |
Jeongik Cha | 82317dd | 2019-02-27 20:26:11 +0900 | [diff] [blame] | 589 | if (options.IsStructured() && !isStable) { |
Steven Moreland | 2a9a7d6 | 2019-02-05 16:11:54 -0800 | [diff] [blame] | 590 | AIDL_ERROR(unstructuredParcelable) |
| 591 | << "Cannot declared parcelable in a --structured interface. Parcelable must be defined " |
| 592 | "in AIDL directly."; |
| 593 | return AidlError::NOT_STRUCTURED; |
| 594 | } |
| 595 | if (options.FailOnParcelable()) { |
| 596 | AIDL_ERROR(unstructuredParcelable) |
| 597 | << "Refusing to generate code with unstructured parcelables. Declared parcelables " |
| 598 | "should be in their own file and/or cannot be used with --structured interfaces."; |
| 599 | // Continue parsing for more errors |
| 600 | } |
| 601 | |
Steven Moreland | e2c64b4 | 2018-09-18 15:06:37 -0700 | [diff] [blame] | 602 | contains_unstructured_parcelable = true; |
| 603 | continue; |
| 604 | } |
| 605 | |
Devin Moore | 0d0e3f6 | 2020-03-30 17:45:39 -0700 | [diff] [blame] | 606 | if (defined_type->IsVintfStability()) { |
| 607 | bool success = true; |
| 608 | if (options.GetStability() != Options::Stability::VINTF) { |
| 609 | AIDL_ERROR(defined_type) |
| 610 | << "Must compile @VintfStability type w/ aidl_interface 'stability: \"vintf\"'"; |
| 611 | success = false; |
| 612 | } |
| 613 | if (!options.IsStructured()) { |
| 614 | AIDL_ERROR(defined_type) |
| 615 | << "Must compile @VintfStability type w/ aidl_interface --structured"; |
| 616 | success = false; |
| 617 | } |
| 618 | if (!success) return AidlError::NOT_STRUCTURED; |
Steven Moreland | a57d0a6 | 2019-07-30 09:41:14 -0700 | [diff] [blame] | 619 | } |
| 620 | |
Daniel Norman | 85aed54 | 2019-08-21 12:01:14 -0700 | [diff] [blame] | 621 | // Ensure that a type is either an interface, structured parcelable, or |
| 622 | // enum. |
Jiyong Park | b034bf0 | 2018-07-30 17:44:33 +0900 | [diff] [blame] | 623 | AidlInterface* interface = defined_type->AsInterface(); |
| 624 | AidlStructuredParcelable* parcelable = defined_type->AsStructuredParcelable(); |
Daniel Norman | 85aed54 | 2019-08-21 12:01:14 -0700 | [diff] [blame] | 625 | AidlEnumDeclaration* enum_decl = defined_type->AsEnumDeclaration(); |
| 626 | CHECK(!!interface + !!parcelable + !!enum_decl == 1); |
Jiyong Park | b034bf0 | 2018-07-30 17:44:33 +0900 | [diff] [blame] | 627 | |
| 628 | // Ensure that foo.bar.IFoo is defined in <some_path>/foo/bar/IFoo.aidl |
Jiyong Park | e59c368 | 2018-09-11 23:10:25 +0900 | [diff] [blame] | 629 | if (num_defined_types == 1 && !check_filename(input_file_name, *defined_type)) { |
Jiyong Park | b034bf0 | 2018-07-30 17:44:33 +0900 | [diff] [blame] | 630 | return AidlError::BAD_PACKAGE; |
| 631 | } |
| 632 | |
Jiyong Park | b034bf0 | 2018-07-30 17:44:33 +0900 | [diff] [blame] | 633 | // Check the referenced types in parsed_doc to make sure we've imported them |
Jiyong Park | 96c16a9 | 2018-08-16 16:37:09 +0900 | [diff] [blame] | 634 | if (!is_check_api) { |
| 635 | // No need to do this for check api because all typespecs are already |
| 636 | // using fully qualified name and we don't import in AIDL files. |
Jeongik Cha | 047c5ee | 2019-08-07 23:16:49 +0900 | [diff] [blame] | 637 | if (!defined_type->CheckValid(*typenames)) { |
Jiyong Park | 96c16a9 | 2018-08-16 16:37:09 +0900 | [diff] [blame] | 638 | return AidlError::BAD_TYPE; |
| 639 | } |
Jiyong Park | b034bf0 | 2018-07-30 17:44:33 +0900 | [diff] [blame] | 640 | } |
| 641 | |
| 642 | if (interface != nullptr) { |
| 643 | // add the meta-method 'int getInterfaceVersion()' if version is specified. |
| 644 | if (options.Version() > 0) { |
| 645 | AidlTypeSpecifier* ret = |
Steven Moreland | 02e012e | 2018-08-02 14:58:10 -0700 | [diff] [blame] | 646 | new AidlTypeSpecifier(AIDL_LOCATION_HERE, "int", false, nullptr, ""); |
Jeongik Cha | 047c5ee | 2019-08-07 23:16:49 +0900 | [diff] [blame] | 647 | ret->Resolve(*typenames); |
Jiyong Park | b034bf0 | 2018-07-30 17:44:33 +0900 | [diff] [blame] | 648 | vector<unique_ptr<AidlArgument>>* args = new vector<unique_ptr<AidlArgument>>(); |
| 649 | AidlMethod* method = |
Steven Moreland | 02e012e | 2018-08-02 14:58:10 -0700 | [diff] [blame] | 650 | new AidlMethod(AIDL_LOCATION_HERE, false, ret, "getInterfaceVersion", args, "", |
Jiyong Park | b034bf0 | 2018-07-30 17:44:33 +0900 | [diff] [blame] | 651 | kGetInterfaceVersionId, false /* is_user_defined */); |
| 652 | interface->GetMutableMethods().emplace_back(method); |
| 653 | } |
Paul Trautrim | b77048c | 2020-01-21 16:39:32 +0900 | [diff] [blame] | 654 | // add the meta-method 'string getInterfaceHash()' if hash is specified. |
| 655 | if (!options.Hash().empty()) { |
| 656 | AidlTypeSpecifier* ret = |
| 657 | new AidlTypeSpecifier(AIDL_LOCATION_HERE, "String", false, nullptr, ""); |
| 658 | ret->Resolve(*typenames); |
| 659 | vector<unique_ptr<AidlArgument>>* args = new vector<unique_ptr<AidlArgument>>(); |
| 660 | AidlMethod* method = new AidlMethod(AIDL_LOCATION_HERE, false, ret, kGetInterfaceHash, args, |
| 661 | "", kGetInterfaceHashId, false /* is_user_defined */); |
| 662 | interface->GetMutableMethods().emplace_back(method); |
| 663 | } |
Jiyong Park | b034bf0 | 2018-07-30 17:44:33 +0900 | [diff] [blame] | 664 | if (!check_and_assign_method_ids(interface->GetMethods())) { |
| 665 | return AidlError::BAD_METHOD_ID; |
| 666 | } |
Will McVicker | d7d18df | 2019-09-12 13:40:50 -0700 | [diff] [blame] | 667 | |
| 668 | // Verify and resolve the constant declarations |
| 669 | for (const auto& constant : interface->GetConstantDeclarations()) { |
| 670 | switch (constant->GetValue().GetType()) { |
| 671 | case AidlConstantValue::Type::STRING: // fall-through |
| 672 | case AidlConstantValue::Type::INT8: // fall-through |
| 673 | case AidlConstantValue::Type::INT32: // fall-through |
| 674 | case AidlConstantValue::Type::INT64: // fall-through |
| 675 | case AidlConstantValue::Type::FLOATING: // fall-through |
| 676 | case AidlConstantValue::Type::UNARY: // fall-through |
| 677 | case AidlConstantValue::Type::BINARY: { |
| 678 | bool success = constant->CheckValid(*typenames); |
| 679 | if (!success) { |
| 680 | return AidlError::BAD_TYPE; |
| 681 | } |
| 682 | if (constant->ValueString(cpp::ConstantValueDecorator).empty()) { |
| 683 | return AidlError::BAD_TYPE; |
| 684 | } |
| 685 | break; |
| 686 | } |
| 687 | default: |
| 688 | LOG(FATAL) << "Unrecognized constant type: " |
| 689 | << static_cast<int>(constant->GetValue().GetType()); |
| 690 | break; |
| 691 | } |
| 692 | } |
Jiyong Park | b034bf0 | 2018-07-30 17:44:33 +0900 | [diff] [blame] | 693 | } |
Christopher Wiley | 632801d | 2015-11-05 14:15:49 -0800 | [diff] [blame] | 694 | } |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 695 | |
Jeongik Cha | 047c5ee | 2019-08-07 23:16:49 +0900 | [diff] [blame] | 696 | typenames->IterateTypes([&](const AidlDefinedType& type) { |
Steven Moreland | a57d0a6 | 2019-07-30 09:41:14 -0700 | [diff] [blame] | 697 | if (options.IsStructured() && type.AsUnstructuredParcelable() != nullptr && |
Jeongik Cha | 88f95a8 | 2020-01-15 13:02:16 +0900 | [diff] [blame] | 698 | !type.AsUnstructuredParcelable()->IsStableApiParcelable(options.TargetLanguage())) { |
Steven Moreland | a57d0a6 | 2019-07-30 09:41:14 -0700 | [diff] [blame] | 699 | err = AidlError::NOT_STRUCTURED; |
Devin Moore | 097a3ab | 2020-03-11 16:08:44 -0700 | [diff] [blame] | 700 | AIDL_ERROR(type) << type.GetCanonicalName() |
| 701 | << " is not structured, but this is a structured interface."; |
Steven Moreland | a57d0a6 | 2019-07-30 09:41:14 -0700 | [diff] [blame] | 702 | } |
| 703 | if (options.GetStability() == Options::Stability::VINTF && !type.IsVintfStability()) { |
| 704 | err = AidlError::NOT_STRUCTURED; |
Devin Moore | 097a3ab | 2020-03-11 16:08:44 -0700 | [diff] [blame] | 705 | AIDL_ERROR(type) << type.GetCanonicalName() |
| 706 | << " does not have VINTF level stability, but this interface requires it."; |
Steven Moreland | a57d0a6 | 2019-07-30 09:41:14 -0700 | [diff] [blame] | 707 | } |
Jiyong Park | f8d5361 | 2020-05-04 14:06:13 +0900 | [diff] [blame] | 708 | |
| 709 | // Ensure that untyped List/Map is not used in stable AIDL. |
| 710 | if (options.IsStructured()) { |
| 711 | const AidlInterface* iface = type.AsInterface(); |
| 712 | const AidlStructuredParcelable* parcelable = type.AsStructuredParcelable(); |
| 713 | |
| 714 | auto check = [&err](const AidlTypeSpecifier& type, const AidlNode* node) { |
| 715 | if (!type.IsGeneric() && (type.GetName() == "List" || type.GetName() == "Map")) { |
| 716 | err = AidlError::BAD_TYPE; |
| 717 | AIDL_ERROR(node) |
| 718 | << "Encountered an untyped List or Map. The use of untyped List/Map is prohibited " |
| 719 | << "because it is not guaranteed that the objects in the list are recognizable in " |
| 720 | << "the receiving side. Consider switching to an array or a generic List/Map."; |
| 721 | } |
| 722 | }; |
| 723 | |
| 724 | if (iface != nullptr) { |
| 725 | for (const auto& method : iface->GetMethods()) { |
| 726 | check(method->GetType(), method.get()); |
| 727 | for (const auto& arg : method->GetArguments()) { |
| 728 | check(arg->GetType(), method.get()); |
| 729 | } |
| 730 | } |
| 731 | } else if (parcelable != nullptr) { |
| 732 | for (const auto& field : parcelable->GetFields()) { |
| 733 | check(field->GetType(), field.get()); |
| 734 | } |
| 735 | } |
| 736 | } |
Steven Moreland | a57d0a6 | 2019-07-30 09:41:14 -0700 | [diff] [blame] | 737 | }); |
| 738 | |
Steven Moreland | 6cee348 | 2018-07-18 14:39:58 -0700 | [diff] [blame] | 739 | if (err != AidlError::OK) { |
| 740 | return err; |
| 741 | } |
| 742 | |
Jiyong Park | b034bf0 | 2018-07-30 17:44:33 +0900 | [diff] [blame] | 743 | if (imported_files != nullptr) { |
Jiyong Park | e59c368 | 2018-09-11 23:10:25 +0900 | [diff] [blame] | 744 | *imported_files = import_paths; |
Christopher Wiley | 3a9d158 | 2015-09-16 12:42:14 -0700 | [diff] [blame] | 745 | } |
Casey Dahlin | 0edf342 | 2015-10-07 12:34:59 -0700 | [diff] [blame] | 746 | |
Steven Moreland | e2c64b4 | 2018-09-18 15:06:37 -0700 | [diff] [blame] | 747 | if (contains_unstructured_parcelable) { |
| 748 | // Considered a success for the legacy case, so this must be returned last. |
| 749 | return AidlError::FOUND_PARCELABLE; |
| 750 | } |
| 751 | |
Christopher Wiley | 632801d | 2015-11-05 14:15:49 -0800 | [diff] [blame] | 752 | return AidlError::OK; |
Christopher Wiley | eb1acc1 | 2015-09-16 11:25:13 -0700 | [diff] [blame] | 753 | } |
| 754 | |
Casey Dahlin | 2cc9316 | 2015-10-02 16:14:17 -0700 | [diff] [blame] | 755 | } // namespace internals |
| 756 | |
Jiyong Park | b034bf0 | 2018-07-30 17:44:33 +0900 | [diff] [blame] | 757 | int compile_aidl(const Options& options, const IoDelegate& io_delegate) { |
| 758 | const Options::Language lang = options.TargetLanguage(); |
Jiyong Park | 74595c1 | 2018-07-23 15:22:50 +0900 | [diff] [blame] | 759 | for (const string& input_file : options.InputFiles()) { |
Jeongik Cha | 047c5ee | 2019-08-07 23:16:49 +0900 | [diff] [blame] | 760 | AidlTypenames typenames; |
Jiyong Park | 74595c1 | 2018-07-23 15:22:50 +0900 | [diff] [blame] | 761 | |
Jiyong Park | b034bf0 | 2018-07-30 17:44:33 +0900 | [diff] [blame] | 762 | vector<string> imported_files; |
Jiyong Park | 74595c1 | 2018-07-23 15:22:50 +0900 | [diff] [blame] | 763 | |
Jiyong Park | 8e79b7f | 2020-07-20 20:52:38 +0900 | [diff] [blame] | 764 | AidlError aidl_err = internals::load_and_validate_aidl(input_file, options, io_delegate, |
| 765 | &typenames, &imported_files); |
Steven Moreland | 2a9a7d6 | 2019-02-05 16:11:54 -0800 | [diff] [blame] | 766 | bool allowError = aidl_err == AidlError::FOUND_PARCELABLE && !options.FailOnParcelable(); |
| 767 | if (aidl_err != AidlError::OK && !allowError) { |
Jiyong Park | 74595c1 | 2018-07-23 15:22:50 +0900 | [diff] [blame] | 768 | return 1; |
| 769 | } |
Christopher Wiley | eb1acc1 | 2015-09-16 11:25:13 -0700 | [diff] [blame] | 770 | |
Jiyong Park | 8e79b7f | 2020-07-20 20:52:38 +0900 | [diff] [blame] | 771 | for (const auto& defined_type : typenames.MainDocument().DefinedTypes()) { |
Jiyong Park | b034bf0 | 2018-07-30 17:44:33 +0900 | [diff] [blame] | 772 | CHECK(defined_type != nullptr); |
Steven Moreland | 5557f1c | 2018-07-02 13:50:23 -0700 | [diff] [blame] | 773 | |
Jiyong Park | b034bf0 | 2018-07-30 17:44:33 +0900 | [diff] [blame] | 774 | string output_file_name = options.OutputFile(); |
| 775 | // if needed, generate the output file name from the base folder |
| 776 | if (output_file_name.empty() && !options.OutputDir().empty()) { |
Jiyong Park | b03551f | 2018-08-06 19:20:51 +0900 | [diff] [blame] | 777 | output_file_name = generate_outputFileName(options, *defined_type); |
| 778 | if (output_file_name.empty()) { |
Jiyong Park | b034bf0 | 2018-07-30 17:44:33 +0900 | [diff] [blame] | 779 | return 1; |
| 780 | } |
| 781 | } |
Jiyong Park | 74595c1 | 2018-07-23 15:22:50 +0900 | [diff] [blame] | 782 | |
Jiyong Park | b034bf0 | 2018-07-30 17:44:33 +0900 | [diff] [blame] | 783 | if (!write_dep_file(options, *defined_type, imported_files, io_delegate, input_file, |
| 784 | output_file_name)) { |
| 785 | return 1; |
| 786 | } |
Jiyong Park | 74595c1 | 2018-07-23 15:22:50 +0900 | [diff] [blame] | 787 | |
Jiyong Park | b034bf0 | 2018-07-30 17:44:33 +0900 | [diff] [blame] | 788 | bool success = false; |
| 789 | if (lang == Options::Language::CPP) { |
Jeongik Cha | 047c5ee | 2019-08-07 23:16:49 +0900 | [diff] [blame] | 790 | success = |
| 791 | cpp::GenerateCpp(output_file_name, options, typenames, *defined_type, io_delegate); |
Steven Moreland | c26d814 | 2018-09-17 14:25:33 -0700 | [diff] [blame] | 792 | } else if (lang == Options::Language::NDK) { |
Jeongik Cha | 047c5ee | 2019-08-07 23:16:49 +0900 | [diff] [blame] | 793 | ndk::GenerateNdk(output_file_name, options, typenames, *defined_type, io_delegate); |
Steven Moreland | c26d814 | 2018-09-17 14:25:33 -0700 | [diff] [blame] | 794 | success = true; |
Jiyong Park | b034bf0 | 2018-07-30 17:44:33 +0900 | [diff] [blame] | 795 | } else if (lang == Options::Language::JAVA) { |
Jiyong Park | 9ca5c7e | 2019-10-17 15:01:14 +0900 | [diff] [blame] | 796 | if (defined_type->AsUnstructuredParcelable() != nullptr) { |
| 797 | // Legacy behavior. For parcelable declarations in Java, don't generate output file. |
| 798 | success = true; |
| 799 | } else { |
Jiyong Park | 8e79b7f | 2020-07-20 20:52:38 +0900 | [diff] [blame] | 800 | success = java::generate_java(output_file_name, defined_type.get(), typenames, |
| 801 | io_delegate, options); |
Jiyong Park | 9ca5c7e | 2019-10-17 15:01:14 +0900 | [diff] [blame] | 802 | } |
Andrei Homescu | b62afd9 | 2020-05-11 19:24:59 -0700 | [diff] [blame] | 803 | } else if (lang == Options::Language::RUST) { |
| 804 | success = rust::GenerateRust(output_file_name, defined_type.get(), typenames, io_delegate, |
| 805 | options); |
Jiyong Park | b034bf0 | 2018-07-30 17:44:33 +0900 | [diff] [blame] | 806 | } else { |
| 807 | LOG(FATAL) << "Should not reach here" << endl; |
| 808 | return 1; |
| 809 | } |
| 810 | if (!success) { |
| 811 | return 1; |
| 812 | } |
Jiyong Park | 74595c1 | 2018-07-23 15:22:50 +0900 | [diff] [blame] | 813 | } |
Christopher Wiley | 3a9d158 | 2015-09-16 12:42:14 -0700 | [diff] [blame] | 814 | } |
Jiyong Park | 74595c1 | 2018-07-23 15:22:50 +0900 | [diff] [blame] | 815 | return 0; |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 816 | } |
| 817 | |
Andrei Onea | 8714b02 | 2019-02-01 18:55:54 +0000 | [diff] [blame] | 818 | bool dump_mappings(const Options& options, const IoDelegate& io_delegate) { |
| 819 | android::aidl::mappings::SignatureMap all_mappings; |
| 820 | for (const string& input_file : options.InputFiles()) { |
Jeongik Cha | 047c5ee | 2019-08-07 23:16:49 +0900 | [diff] [blame] | 821 | AidlTypenames typenames; |
Andrei Onea | 8714b02 | 2019-02-01 18:55:54 +0000 | [diff] [blame] | 822 | vector<string> imported_files; |
| 823 | |
Jiyong Park | 8e79b7f | 2020-07-20 20:52:38 +0900 | [diff] [blame] | 824 | AidlError aidl_err = internals::load_and_validate_aidl(input_file, options, io_delegate, |
| 825 | &typenames, &imported_files); |
Andrei Onea | 8714b02 | 2019-02-01 18:55:54 +0000 | [diff] [blame] | 826 | if (aidl_err != AidlError::OK) { |
Jiyong Park | 3a06039 | 2020-04-11 21:02:19 +0900 | [diff] [blame] | 827 | return false; |
Andrei Onea | 8714b02 | 2019-02-01 18:55:54 +0000 | [diff] [blame] | 828 | } |
Jiyong Park | 8e79b7f | 2020-07-20 20:52:38 +0900 | [diff] [blame] | 829 | for (const auto& defined_type : typenames.MainDocument().DefinedTypes()) { |
| 830 | auto mappings = mappings::generate_mappings(defined_type.get(), typenames); |
Andrei Onea | 8714b02 | 2019-02-01 18:55:54 +0000 | [diff] [blame] | 831 | all_mappings.insert(mappings.begin(), mappings.end()); |
| 832 | } |
| 833 | } |
| 834 | std::stringstream mappings_str; |
| 835 | for (const auto& mapping : all_mappings) { |
| 836 | mappings_str << mapping.first << "\n" << mapping.second << "\n"; |
| 837 | } |
| 838 | auto code_writer = io_delegate.GetCodeWriter(options.OutputFile()); |
| 839 | code_writer->Write("%s", mappings_str.str().c_str()); |
| 840 | return true; |
| 841 | } |
| 842 | |
Jiyong Park | 74595c1 | 2018-07-23 15:22:50 +0900 | [diff] [blame] | 843 | bool preprocess_aidl(const Options& options, const IoDelegate& io_delegate) { |
| 844 | unique_ptr<CodeWriter> writer = io_delegate.GetCodeWriter(options.OutputFile()); |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 845 | |
Jiyong Park | 74595c1 | 2018-07-23 15:22:50 +0900 | [diff] [blame] | 846 | for (const auto& file : options.InputFiles()) { |
Jiyong Park | 1deecc3 | 2018-07-17 01:14:41 +0900 | [diff] [blame] | 847 | AidlTypenames typenames; |
Steven Moreland | 64e29be | 2018-08-08 18:52:19 -0700 | [diff] [blame] | 848 | std::unique_ptr<Parser> p = Parser::Parse(file, io_delegate, typenames); |
| 849 | if (p == nullptr) return false; |
Casey Dahlin | 59401da | 2015-10-09 18:16:45 -0700 | [diff] [blame] | 850 | |
Jiyong Park | 8e79b7f | 2020-07-20 20:52:38 +0900 | [diff] [blame] | 851 | for (const auto& defined_type : p->ParsedDocument().DefinedTypes()) { |
Steven Moreland | ed83a28 | 2018-07-17 13:27:29 -0700 | [diff] [blame] | 852 | if (!writer->Write("%s %s;\n", defined_type->GetPreprocessDeclarationName().c_str(), |
Steven Moreland | c258abc | 2018-07-10 14:03:38 -0700 | [diff] [blame] | 853 | defined_type->GetCanonicalName().c_str())) { |
Casey Dahlin | c1f39b4 | 2015-11-24 10:34:34 -0800 | [diff] [blame] | 854 | return false; |
| 855 | } |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 856 | } |
Casey Dahlin | c1f39b4 | 2015-11-24 10:34:34 -0800 | [diff] [blame] | 857 | } |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 858 | |
Casey Dahlin | c1f39b4 | 2015-11-24 10:34:34 -0800 | [diff] [blame] | 859 | return writer->Close(); |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 860 | } |
Christopher Wiley | fdeb0f4 | 2015-09-11 15:38:22 -0700 | [diff] [blame] | 861 | |
Jiyong Park | e59c368 | 2018-09-11 23:10:25 +0900 | [diff] [blame] | 862 | static string GetApiDumpPathFor(const AidlDefinedType& defined_type, const Options& options) { |
| 863 | string package_as_path = Join(Split(defined_type.GetPackage(), "."), OS_PATH_SEPARATOR); |
| 864 | CHECK(!options.OutputDir().empty() && options.OutputDir().back() == '/'); |
| 865 | return options.OutputDir() + package_as_path + OS_PATH_SEPARATOR + defined_type.GetName() + |
| 866 | ".aidl"; |
| 867 | } |
Jiyong Park | b034bf0 | 2018-07-30 17:44:33 +0900 | [diff] [blame] | 868 | |
Jiyong Park | e59c368 | 2018-09-11 23:10:25 +0900 | [diff] [blame] | 869 | bool dump_api(const Options& options, const IoDelegate& io_delegate) { |
Jiyong Park | 74595c1 | 2018-07-23 15:22:50 +0900 | [diff] [blame] | 870 | for (const auto& file : options.InputFiles()) { |
Jeongik Cha | 047c5ee | 2019-08-07 23:16:49 +0900 | [diff] [blame] | 871 | AidlTypenames typenames; |
Jiyong Park | 8e79b7f | 2020-07-20 20:52:38 +0900 | [diff] [blame] | 872 | if (internals::load_and_validate_aidl(file, options, io_delegate, &typenames, nullptr) == |
| 873 | AidlError::OK) { |
| 874 | for (const auto& type : typenames.MainDocument().DefinedTypes()) { |
Jiyong Park | e59c368 | 2018-09-11 23:10:25 +0900 | [diff] [blame] | 875 | unique_ptr<CodeWriter> writer = |
| 876 | io_delegate.GetCodeWriter(GetApiDumpPathFor(*type, options)); |
Steven Moreland | ec0531d | 2018-09-20 11:11:20 -0700 | [diff] [blame] | 877 | if (!type->GetPackage().empty()) { |
Paul Trautrim | b01451d | 2020-02-27 13:10:16 +0900 | [diff] [blame] | 878 | (*writer) << kPreamble << "package " << type->GetPackage() << ";\n"; |
Steven Moreland | ec0531d | 2018-09-20 11:11:20 -0700 | [diff] [blame] | 879 | } |
Jeongik Cha | 997281d | 2020-01-16 15:23:59 +0900 | [diff] [blame] | 880 | type->Dump(writer.get()); |
Jiyong Park | b034bf0 | 2018-07-30 17:44:33 +0900 | [diff] [blame] | 881 | } |
Jiyong Park | 02da742 | 2018-07-16 16:00:26 +0900 | [diff] [blame] | 882 | } else { |
| 883 | return false; |
| 884 | } |
| 885 | } |
Jiyong Park | e59c368 | 2018-09-11 23:10:25 +0900 | [diff] [blame] | 886 | return true; |
Jiyong Park | 02da742 | 2018-07-16 16:00:26 +0900 | [diff] [blame] | 887 | } |
| 888 | |
Steven Moreland | 3981d9e | 2020-03-31 14:11:44 -0700 | [diff] [blame] | 889 | int aidl_entry(const Options& options, const IoDelegate& io_delegate) { |
Steven Moreland | 33efcf6 | 2020-04-10 16:40:43 -0700 | [diff] [blame] | 890 | AidlErrorLog::clearError(); |
| 891 | |
Steven Moreland | 3981d9e | 2020-03-31 14:11:44 -0700 | [diff] [blame] | 892 | int ret = 1; |
| 893 | switch (options.GetTask()) { |
| 894 | case Options::Task::COMPILE: |
| 895 | ret = android::aidl::compile_aidl(options, io_delegate); |
| 896 | break; |
| 897 | case Options::Task::PREPROCESS: |
| 898 | ret = android::aidl::preprocess_aidl(options, io_delegate) ? 0 : 1; |
| 899 | break; |
| 900 | case Options::Task::DUMP_API: |
| 901 | ret = android::aidl::dump_api(options, io_delegate) ? 0 : 1; |
| 902 | break; |
| 903 | case Options::Task::CHECK_API: |
| 904 | ret = android::aidl::check_api(options, io_delegate) ? 0 : 1; |
| 905 | break; |
| 906 | case Options::Task::DUMP_MAPPINGS: |
| 907 | ret = android::aidl::dump_mappings(options, io_delegate) ? 0 : 1; |
| 908 | break; |
| 909 | default: |
| 910 | AIDL_FATAL(AIDL_LOCATION_HERE) |
| 911 | << "Unrecognized task: " << static_cast<size_t>(options.GetTask()); |
| 912 | } |
| 913 | |
| 914 | // compiler invariants |
| 915 | |
| 916 | // once AIDL_ERROR/AIDL_FATAL are used everywhere instead of std::cerr/LOG, we |
| 917 | // can make this assertion in both directions. |
| 918 | if (ret == 0) { |
| 919 | AIDL_FATAL_IF(AidlErrorLog::hadError(), "Compiler success, but error emitted"); |
| 920 | } |
| 921 | |
| 922 | return ret; |
| 923 | } |
| 924 | |
Christopher Wiley | fdeb0f4 | 2015-09-11 15:38:22 -0700 | [diff] [blame] | 925 | } // namespace aidl |
Steven Moreland | f4c64df | 2019-07-29 19:54:04 -0700 | [diff] [blame] | 926 | } // namespace android |