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