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