| 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 | 1deecc3 | 2018-07-17 01:14:41 +0900 | [diff] [blame] | 26 | #include <iostream> |
| 27 | #include <map> |
| 28 | #include <memory> |
| Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 29 | |
| Elliott Hughes | 549b6e2 | 2015-08-17 12:41:46 -0700 | [diff] [blame] | 30 | #ifdef _WIN32 |
| Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 31 | #include <io.h> |
| Andrew Hsieh | 15ce994 | 2014-05-07 20:14:30 +0800 | [diff] [blame] | 32 | #include <direct.h> |
| Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 33 | #include <sys/stat.h> |
| 34 | #endif |
| 35 | |
| Elliott Hughes | 0a62067 | 2015-12-04 13:53:18 -0800 | [diff] [blame] | 36 | #include <android-base/strings.h> |
| Christopher Wiley | f690be5 | 2015-09-14 15:19:10 -0700 | [diff] [blame] | 37 | |
| Christopher Wiley | f690be5 | 2015-09-14 15:19:10 -0700 | [diff] [blame] | 38 | #include "aidl_language.h" |
| Christopher Wiley | eb1acc1 | 2015-09-16 11:25:13 -0700 | [diff] [blame] | 39 | #include "generate_cpp.h" |
| Christopher Wiley | f690be5 | 2015-09-14 15:19:10 -0700 | [diff] [blame] | 40 | #include "generate_java.h" |
| Christopher Wiley | 72877ac | 2015-10-06 14:41:42 -0700 | [diff] [blame] | 41 | #include "import_resolver.h" |
| Christopher Wiley | f690be5 | 2015-09-14 15:19:10 -0700 | [diff] [blame] | 42 | #include "logging.h" |
| 43 | #include "options.h" |
| 44 | #include "os.h" |
| Christopher Wiley | e3550c6 | 2015-09-29 13:26:10 -0700 | [diff] [blame] | 45 | #include "type_cpp.h" |
| Christopher Wiley | 775fa1f | 2015-09-22 15:00:12 -0700 | [diff] [blame] | 46 | #include "type_java.h" |
| Christopher Wiley | 84c1eac | 2015-09-23 13:29:28 -0700 | [diff] [blame] | 47 | #include "type_namespace.h" |
| Christopher Wiley | f690be5 | 2015-09-14 15:19:10 -0700 | [diff] [blame] | 48 | |
| Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 49 | #ifndef O_BINARY |
| 50 | # define O_BINARY 0 |
| 51 | #endif |
| 52 | |
| Christopher Wiley | 3a9911c | 2016-01-19 12:59:09 -0800 | [diff] [blame] | 53 | using android::base::Join; |
| Christopher Wiley | d76067c | 2015-10-19 17:00:13 -0700 | [diff] [blame] | 54 | using android::base::Split; |
| Christopher Wiley | c16e5e7 | 2015-09-16 10:49:40 -0700 | [diff] [blame] | 55 | using std::cerr; |
| 56 | using std::endl; |
| Christopher Wiley | 9f4c7ae | 2015-08-24 14:07:32 -0700 | [diff] [blame] | 57 | using std::map; |
| 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 | |
| Christopher Wiley | f690be5 | 2015-09-14 15:19:10 -0700 | [diff] [blame] | 67 | // The following are gotten as the offset from the allowable id's between |
| 68 | // android.os.IBinder.FIRST_CALL_TRANSACTION=1 and |
| 69 | // android.os.IBinder.LAST_CALL_TRANSACTION=16777215 |
| 70 | const int kMinUserSetMethodId = 0; |
| 71 | const int kMaxUserSetMethodId = 16777214; |
| Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 72 | |
| Casey Dahlin | 42727f8 | 2015-10-12 19:23:40 -0700 | [diff] [blame] | 73 | bool check_filename(const std::string& filename, |
| 74 | const std::string& package, |
| 75 | const std::string& name, |
| 76 | unsigned line) { |
| Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 77 | const char* p; |
| 78 | string expected; |
| 79 | string fn; |
| 80 | size_t len; |
| Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 81 | bool valid = false; |
| 82 | |
| Christopher Wiley | bc2df69 | 2016-06-02 16:27:26 -0700 | [diff] [blame] | 83 | if (!IoDelegate::GetAbsolutePath(filename, &fn)) { |
| 84 | return false; |
| Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 85 | } |
| 86 | |
| Casey Dahlin | fb7da2e | 2015-10-08 17:26:09 -0700 | [diff] [blame] | 87 | if (!package.empty()) { |
| Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 88 | expected = package; |
| 89 | expected += '.'; |
| 90 | } |
| 91 | |
| 92 | len = expected.length(); |
| 93 | for (size_t i=0; i<len; i++) { |
| 94 | if (expected[i] == '.') { |
| 95 | expected[i] = OS_PATH_SEPARATOR; |
| 96 | } |
| 97 | } |
| 98 | |
| Casey Dahlin | fb7da2e | 2015-10-08 17:26:09 -0700 | [diff] [blame] | 99 | expected.append(name, 0, name.find('.')); |
| Christopher Wiley | 8f8cc9b | 2015-09-14 13:47:40 -0700 | [diff] [blame] | 100 | |
| Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 101 | expected += ".aidl"; |
| 102 | |
| 103 | len = fn.length(); |
| 104 | valid = (len >= expected.length()); |
| 105 | |
| 106 | if (valid) { |
| 107 | p = fn.c_str() + (len - expected.length()); |
| 108 | |
| Elliott Hughes | ce310da | 2015-07-29 08:44:17 -0700 | [diff] [blame] | 109 | #ifdef _WIN32 |
| Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 110 | if (OS_PATH_SEPARATOR != '/') { |
| 111 | // Input filename under cygwin most likely has / separators |
| 112 | // whereas the expected string uses \\ separators. Adjust |
| 113 | // them accordingly. |
| 114 | for (char *c = const_cast<char *>(p); *c; ++c) { |
| 115 | if (*c == '/') *c = OS_PATH_SEPARATOR; |
| 116 | } |
| 117 | } |
| 118 | #endif |
| 119 | |
| Yabin Cui | 482eefb | 2014-11-10 15:01:43 -0800 | [diff] [blame] | 120 | // aidl assumes case-insensitivity on Mac Os and Windows. |
| 121 | #if defined(__linux__) |
| Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 122 | valid = (expected == p); |
| 123 | #else |
| 124 | valid = !strcasecmp(expected.c_str(), p); |
| 125 | #endif |
| 126 | } |
| 127 | |
| 128 | if (!valid) { |
| Steven Moreland | 6ddc37f | 2018-07-02 13:05:50 -0700 | [diff] [blame] | 129 | fprintf(stderr, "%s:%d %s should be declared in a file called %s.\n", |
| Casey Dahlin | fb7da2e | 2015-10-08 17:26:09 -0700 | [diff] [blame] | 130 | filename.c_str(), line, name.c_str(), expected.c_str()); |
| Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 131 | } |
| 132 | |
| Casey Dahlin | 42727f8 | 2015-10-12 19:23:40 -0700 | [diff] [blame] | 133 | return valid; |
| Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 134 | } |
| 135 | |
| Steven Moreland | c258abc | 2018-07-10 14:03:38 -0700 | [diff] [blame] | 136 | bool check_filenames(const std::string& filename, const AidlDocument& doc) { |
| Casey Dahlin | 42727f8 | 2015-10-12 19:23:40 -0700 | [diff] [blame] | 137 | bool success = true; |
| 138 | |
| Steven Moreland | c258abc | 2018-07-10 14:03:38 -0700 | [diff] [blame] | 139 | for (const auto& item : doc.GetDefinedTypes()) { |
| Casey Dahlin | c1f39b4 | 2015-11-24 10:34:34 -0800 | [diff] [blame] | 140 | success &= check_filename(filename, item->GetPackage(), item->GetName(), |
| 141 | item->GetLine()); |
| 142 | } |
| Casey Dahlin | 42727f8 | 2015-10-12 19:23:40 -0700 | [diff] [blame] | 143 | |
| 144 | return success; |
| Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 145 | } |
| 146 | |
| Steven Moreland | c258abc | 2018-07-10 14:03:38 -0700 | [diff] [blame] | 147 | bool gather_types(const std::string& filename, const AidlDocument& doc, TypeNamespace* types) { |
| Christopher Wiley | 84c1eac | 2015-09-23 13:29:28 -0700 | [diff] [blame] | 148 | bool success = true; |
| Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 149 | |
| Steven Moreland | c258abc | 2018-07-10 14:03:38 -0700 | [diff] [blame] | 150 | for (const auto& item : doc.GetDefinedTypes()) { |
| 151 | const AidlInterface* interface = item->AsInterface(); |
| 152 | if (interface != nullptr) { |
| 153 | success &= types->AddBinderType(*interface, filename); |
| 154 | continue; |
| 155 | } |
| Casey Dahlin | 42727f8 | 2015-10-12 19:23:40 -0700 | [diff] [blame] | 156 | |
| Steven Moreland | c258abc | 2018-07-10 14:03:38 -0700 | [diff] [blame] | 157 | const AidlParcelable* parcelable = item->AsParcelable(); |
| 158 | if (parcelable != nullptr) { |
| 159 | success &= types->AddParcelableType(*parcelable, filename); |
| 160 | continue; |
| 161 | } |
| Casey Dahlin | 42727f8 | 2015-10-12 19:23:40 -0700 | [diff] [blame] | 162 | |
| Steven Moreland | c258abc | 2018-07-10 14:03:38 -0700 | [diff] [blame] | 163 | CHECK(false) << "aidl internal error: unrecognized type"; |
| Christopher Wiley | 84c1eac | 2015-09-23 13:29:28 -0700 | [diff] [blame] | 164 | } |
| Casey Dahlin | 42727f8 | 2015-10-12 19:23:40 -0700 | [diff] [blame] | 165 | |
| Christopher Wiley | 84c1eac | 2015-09-23 13:29:28 -0700 | [diff] [blame] | 166 | return success; |
| Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 167 | } |
| 168 | |
| Steven Moreland | 5557f1c | 2018-07-02 13:50:23 -0700 | [diff] [blame] | 169 | int check_types(const string& filename, const AidlStructuredParcelable* parcel, |
| 170 | TypeNamespace* types) { |
| 171 | int err = 0; |
| 172 | for (const auto& v : parcel->GetFields()) { |
| 173 | if (!types->MaybeAddContainerType(v->GetType())) { |
| 174 | err = 1; // return type is invalid |
| 175 | } |
| 176 | |
| 177 | const ValidatableType* type = types->GetReturnType(v->GetType(), filename, *parcel); |
| 178 | if (!type) { |
| 179 | err = 1; |
| 180 | } |
| 181 | |
| 182 | v->GetMutableType()->SetLanguageType(type); |
| 183 | } |
| 184 | |
| 185 | return err; |
| 186 | } |
| 187 | |
| Christopher Wiley | fb4b22d | 2015-09-25 15:16:13 -0700 | [diff] [blame] | 188 | int check_types(const string& filename, |
| Casey Dahlin | 98a544b | 2015-10-14 14:22:55 -0700 | [diff] [blame] | 189 | const AidlInterface* c, |
| Christopher Wiley | fb4b22d | 2015-09-25 15:16:13 -0700 | [diff] [blame] | 190 | TypeNamespace* types) { |
| 191 | int err = 0; |
| Casey Dahlin | f4a9311 | 2015-10-05 16:58:09 -0700 | [diff] [blame] | 192 | |
| Casey Dahlin | c5afb40 | 2016-03-01 13:54:05 -0800 | [diff] [blame] | 193 | if (c->IsUtf8() && c->IsUtf8InCpp()) { |
| 194 | cerr << filename << ":" << c->GetLine() |
| 195 | << "Interface cannot be marked as both @utf8 and @utf8InCpp"; |
| 196 | err = 1; |
| 197 | } |
| 198 | |
| Casey Dahlin | f4a9311 | 2015-10-05 16:58:09 -0700 | [diff] [blame] | 199 | // Has to be a pointer due to deleting copy constructor. No idea why. |
| 200 | map<string, const AidlMethod*> method_names; |
| Casey Dahlin | fb7da2e | 2015-10-08 17:26:09 -0700 | [diff] [blame] | 201 | for (const auto& m : c->GetMethods()) { |
| Casey Dahlin | 0c6fcec | 2015-10-20 13:32:21 -0700 | [diff] [blame] | 202 | bool oneway = m->IsOneway() || c->IsOneway(); |
| 203 | |
| Christopher Wiley | 934a82d | 2016-01-27 13:02:24 -0800 | [diff] [blame] | 204 | if (!types->MaybeAddContainerType(m->GetType())) { |
| Christopher Wiley | fb4b22d | 2015-09-25 15:16:13 -0700 | [diff] [blame] | 205 | err = 1; // return type is invalid |
| Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 206 | } |
| 207 | |
| Casey Dahlin | a2f77c4 | 2015-12-01 18:26:02 -0800 | [diff] [blame] | 208 | const ValidatableType* return_type = |
| Casey Dahlin | c5afb40 | 2016-03-01 13:54:05 -0800 | [diff] [blame] | 209 | types->GetReturnType(m->GetType(), filename, *c); |
| Casey Dahlin | a2f77c4 | 2015-12-01 18:26:02 -0800 | [diff] [blame] | 210 | |
| Casey Dahlin | 57dbe24 | 2015-12-04 11:44:02 -0800 | [diff] [blame] | 211 | if (!return_type) { |
| 212 | err = 1; |
| Casey Dahlin | a2f77c4 | 2015-12-01 18:26:02 -0800 | [diff] [blame] | 213 | } |
| 214 | |
| 215 | m->GetMutableType()->SetLanguageType(return_type); |
| 216 | |
| Casey Dahlin | 0c6fcec | 2015-10-20 13:32:21 -0700 | [diff] [blame] | 217 | if (oneway && m->GetType().GetName() != "void") { |
| 218 | cerr << filename << ":" << m->GetLine() |
| Christopher Wiley | 45db9ee | 2015-10-26 18:53:53 -0700 | [diff] [blame] | 219 | << " oneway method '" << m->GetName() << "' cannot return a value" |
| 220 | << endl; |
| Casey Dahlin | 0c6fcec | 2015-10-20 13:32:21 -0700 | [diff] [blame] | 221 | err = 1; |
| 222 | } |
| 223 | |
| Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 224 | int index = 1; |
| Casey Dahlin | f4a9311 | 2015-10-05 16:58:09 -0700 | [diff] [blame] | 225 | for (const auto& arg : m->GetArguments()) { |
| Christopher Wiley | 934a82d | 2016-01-27 13:02:24 -0800 | [diff] [blame] | 226 | if (!types->MaybeAddContainerType(arg->GetType())) { |
| Christopher Wiley | fb4b22d | 2015-09-25 15:16:13 -0700 | [diff] [blame] | 227 | err = 1; |
| 228 | } |
| Casey Dahlin | 0c6fcec | 2015-10-20 13:32:21 -0700 | [diff] [blame] | 229 | |
| Casey Dahlin | a2f77c4 | 2015-12-01 18:26:02 -0800 | [diff] [blame] | 230 | const ValidatableType* arg_type = |
| Casey Dahlin | c5afb40 | 2016-03-01 13:54:05 -0800 | [diff] [blame] | 231 | types->GetArgType(*arg, index, filename, *c); |
| Casey Dahlin | a2f77c4 | 2015-12-01 18:26:02 -0800 | [diff] [blame] | 232 | |
| Casey Dahlin | 57dbe24 | 2015-12-04 11:44:02 -0800 | [diff] [blame] | 233 | if (!arg_type) { |
| 234 | err = 1; |
| Casey Dahlin | a2f77c4 | 2015-12-01 18:26:02 -0800 | [diff] [blame] | 235 | } |
| 236 | |
| 237 | arg->GetMutableType()->SetLanguageType(arg_type); |
| 238 | |
| Casey Dahlin | 0c6fcec | 2015-10-20 13:32:21 -0700 | [diff] [blame] | 239 | if (oneway && arg->IsOut()) { |
| 240 | cerr << filename << ":" << m->GetLine() |
| Christopher Wiley | 45db9ee | 2015-10-26 18:53:53 -0700 | [diff] [blame] | 241 | << " oneway method '" << m->GetName() |
| 242 | << "' cannot have out parameters" << endl; |
| Casey Dahlin | 0c6fcec | 2015-10-20 13:32:21 -0700 | [diff] [blame] | 243 | err = 1; |
| 244 | } |
| Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 245 | } |
| 246 | |
| Casey Dahlin | f4a9311 | 2015-10-05 16:58:09 -0700 | [diff] [blame] | 247 | auto it = method_names.find(m->GetName()); |
| Christopher Wiley | fb4b22d | 2015-09-25 15:16:13 -0700 | [diff] [blame] | 248 | // prevent duplicate methods |
| Casey Dahlin | f4a9311 | 2015-10-05 16:58:09 -0700 | [diff] [blame] | 249 | if (it == method_names.end()) { |
| 250 | method_names[m->GetName()] = m.get(); |
| Christopher Wiley | fb4b22d | 2015-09-25 15:16:13 -0700 | [diff] [blame] | 251 | } else { |
| Casey Dahlin | f4a9311 | 2015-10-05 16:58:09 -0700 | [diff] [blame] | 252 | cerr << filename << ":" << m->GetLine() |
| 253 | << " attempt to redefine method " << m->GetName() << "," << endl |
| 254 | << filename << ":" << it->second->GetLine() |
| Christopher Wiley | fb4b22d | 2015-09-25 15:16:13 -0700 | [diff] [blame] | 255 | << " previously defined here." << endl; |
| 256 | err = 1; |
| Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 257 | } |
| Christopher Wiley | fb4b22d | 2015-09-25 15:16:13 -0700 | [diff] [blame] | 258 | } |
| 259 | return err; |
| Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 260 | } |
| 261 | |
| Christopher Wiley | 3a9911c | 2016-01-19 12:59:09 -0800 | [diff] [blame] | 262 | void write_common_dep_file(const string& output_file, |
| 263 | const vector<string>& aidl_sources, |
| Dan Willemsen | 93298ee | 2016-11-10 23:55:55 -0800 | [diff] [blame] | 264 | CodeWriter* writer, |
| 265 | const bool ninja) { |
| Christopher Wiley | 3a9911c | 2016-01-19 12:59:09 -0800 | [diff] [blame] | 266 | // Encode that the output file depends on aidl input files. |
| 267 | writer->Write("%s : \\\n", output_file.c_str()); |
| 268 | writer->Write(" %s", Join(aidl_sources, " \\\n ").c_str()); |
| Dan Willemsen | 93298ee | 2016-11-10 23:55:55 -0800 | [diff] [blame] | 269 | writer->Write("\n"); |
| Christopher Wiley | a30a45e | 2015-10-17 10:56:59 -0700 | [diff] [blame] | 270 | |
| Dan Willemsen | 93298ee | 2016-11-10 23:55:55 -0800 | [diff] [blame] | 271 | if (!ninja) { |
| 272 | writer->Write("\n"); |
| 273 | // Output "<input_aidl_file>: " so make won't fail if the input .aidl file |
| 274 | // has been deleted, moved or renamed in incremental build. |
| 275 | for (const auto& src : aidl_sources) { |
| 276 | writer->Write("%s :\n", src.c_str()); |
| 277 | } |
| Christopher Wiley | 3a9911c | 2016-01-19 12:59:09 -0800 | [diff] [blame] | 278 | } |
| 279 | } |
| Christopher Wiley | a30a45e | 2015-10-17 10:56:59 -0700 | [diff] [blame] | 280 | |
| Christopher Wiley | 3a9911c | 2016-01-19 12:59:09 -0800 | [diff] [blame] | 281 | bool write_java_dep_file(const JavaOptions& options, |
| 282 | const vector<unique_ptr<AidlImport>>& imports, |
| Christopher Wiley | f813619 | 2016-04-12 14:19:35 -0700 | [diff] [blame] | 283 | const IoDelegate& io_delegate, |
| 284 | const string& output_file_name) { |
| Christopher Wiley | 3a9911c | 2016-01-19 12:59:09 -0800 | [diff] [blame] | 285 | string dep_file_name = options.DependencyFilePath(); |
| 286 | if (dep_file_name.empty()) { |
| 287 | return true; // nothing to do |
| 288 | } |
| 289 | CodeWriterPtr writer = io_delegate.GetCodeWriter(dep_file_name); |
| 290 | if (!writer) { |
| 291 | LOG(ERROR) << "Could not open dependency file: " << dep_file_name; |
| 292 | return false; |
| 293 | } |
| 294 | |
| 295 | vector<string> source_aidl = {options.input_file_name_}; |
| Christopher Wiley | a30a45e | 2015-10-17 10:56:59 -0700 | [diff] [blame] | 296 | for (const auto& import : imports) { |
| Christopher Wiley | 3a9911c | 2016-01-19 12:59:09 -0800 | [diff] [blame] | 297 | if (!import->GetFilename().empty()) { |
| 298 | source_aidl.push_back(import->GetFilename()); |
| Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 299 | } |
| Christopher Wiley | a30a45e | 2015-10-17 10:56:59 -0700 | [diff] [blame] | 300 | } |
| Christopher Wiley | 3a9911c | 2016-01-19 12:59:09 -0800 | [diff] [blame] | 301 | |
| Dan Willemsen | 93298ee | 2016-11-10 23:55:55 -0800 | [diff] [blame] | 302 | write_common_dep_file(output_file_name, source_aidl, writer.get(), |
| 303 | options.DependencyFileNinja()); |
| Christopher Wiley | 3a9911c | 2016-01-19 12:59:09 -0800 | [diff] [blame] | 304 | |
| 305 | return true; |
| 306 | } |
| 307 | |
| Steven Moreland | 5557f1c | 2018-07-02 13:50:23 -0700 | [diff] [blame] | 308 | bool write_cpp_dep_file(const CppOptions& options, const AidlDefinedType& defined_type, |
| Christopher Wiley | 3a9911c | 2016-01-19 12:59:09 -0800 | [diff] [blame] | 309 | const vector<unique_ptr<AidlImport>>& imports, |
| 310 | const IoDelegate& io_delegate) { |
| 311 | using ::android::aidl::cpp::HeaderFile; |
| 312 | using ::android::aidl::cpp::ClassNames; |
| 313 | |
| 314 | string dep_file_name = options.DependencyFilePath(); |
| 315 | if (dep_file_name.empty()) { |
| 316 | return true; // nothing to do |
| 317 | } |
| 318 | CodeWriterPtr writer = io_delegate.GetCodeWriter(dep_file_name); |
| 319 | if (!writer) { |
| 320 | LOG(ERROR) << "Could not open dependency file: " << dep_file_name; |
| 321 | return false; |
| 322 | } |
| 323 | |
| 324 | vector<string> source_aidl = {options.InputFileName()}; |
| 325 | for (const auto& import : imports) { |
| 326 | if (!import->GetFilename().empty()) { |
| 327 | source_aidl.push_back(import->GetFilename()); |
| 328 | } |
| 329 | } |
| 330 | |
| Dan Willemsen | 93298ee | 2016-11-10 23:55:55 -0800 | [diff] [blame] | 331 | write_common_dep_file(options.OutputCppFilePath(), source_aidl, writer.get(), |
| 332 | options.DependencyFileNinja()); |
| 333 | |
| 334 | if (!options.DependencyFileNinja()) { |
| 335 | vector<string> headers; |
| 336 | for (ClassNames c : {ClassNames::CLIENT, |
| 337 | ClassNames::SERVER, |
| 338 | ClassNames::INTERFACE}) { |
| 339 | headers.push_back(options.OutputHeaderDir() + '/' + |
| Steven Moreland | 5557f1c | 2018-07-02 13:50:23 -0700 | [diff] [blame] | 340 | HeaderFile(defined_type, c, false /* use_os_sep */)); |
| Dan Willemsen | 93298ee | 2016-11-10 23:55:55 -0800 | [diff] [blame] | 341 | } |
| 342 | |
| 343 | writer->Write("\n"); |
| 344 | |
| 345 | // Generated headers also depend on the source aidl files. |
| 346 | writer->Write("%s : \\\n %s\n", Join(headers, " \\\n ").c_str(), |
| 347 | Join(source_aidl, " \\\n ").c_str()); |
| Christopher Wiley | 3a9911c | 2016-01-19 12:59:09 -0800 | [diff] [blame] | 348 | } |
| 349 | |
| Christopher Wiley | 3a9911c | 2016-01-19 12:59:09 -0800 | [diff] [blame] | 350 | return true; |
| Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 351 | } |
| 352 | |
| Steven Moreland | 5557f1c | 2018-07-02 13:50:23 -0700 | [diff] [blame] | 353 | string generate_outputFileName(const JavaOptions& options, const AidlDefinedType& defined_type) { |
| 354 | const string& name = defined_type.GetName(); |
| 355 | string package = defined_type.GetPackage(); |
| 356 | string result; |
| Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 357 | |
| Steven Moreland | 5557f1c | 2018-07-02 13:50:23 -0700 | [diff] [blame] | 358 | // create the path to the destination folder based on the |
| 359 | // defined_type package name |
| 360 | result = options.output_base_folder_; |
| 361 | result += OS_PATH_SEPARATOR; |
| Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 362 | |
| Steven Moreland | 5557f1c | 2018-07-02 13:50:23 -0700 | [diff] [blame] | 363 | string packageStr = package; |
| 364 | size_t len = packageStr.length(); |
| 365 | for (size_t i = 0; i < len; i++) { |
| 366 | if (packageStr[i] == '.') { |
| 367 | packageStr[i] = OS_PATH_SEPARATOR; |
| Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 368 | } |
| Steven Moreland | 5557f1c | 2018-07-02 13:50:23 -0700 | [diff] [blame] | 369 | } |
| Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 370 | |
| Steven Moreland | 5557f1c | 2018-07-02 13:50:23 -0700 | [diff] [blame] | 371 | result += packageStr; |
| Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 372 | |
| Steven Moreland | 5557f1c | 2018-07-02 13:50:23 -0700 | [diff] [blame] | 373 | // add the filename by replacing the .aidl extension to .java |
| 374 | result += OS_PATH_SEPARATOR; |
| 375 | result.append(name, 0, name.find('.')); |
| 376 | result += ".java"; |
| Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 377 | |
| Steven Moreland | 5557f1c | 2018-07-02 13:50:23 -0700 | [diff] [blame] | 378 | return result; |
| Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 379 | } |
| 380 | |
| Christopher Wiley | f690be5 | 2015-09-14 15:19:10 -0700 | [diff] [blame] | 381 | int check_and_assign_method_ids(const char * filename, |
| Casey Dahlin | 5c69deb | 2015-10-01 14:44:12 -0700 | [diff] [blame] | 382 | const std::vector<std::unique_ptr<AidlMethod>>& items) { |
| Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 383 | // Check whether there are any methods with manually assigned id's and any that are not. |
| 384 | // Either all method id's must be manually assigned or all of them must not. |
| 385 | // Also, check for duplicates of user set id's and that the id's are within the proper bounds. |
| 386 | set<int> usedIds; |
| Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 387 | bool hasUnassignedIds = false; |
| 388 | bool hasAssignedIds = false; |
| Casey Dahlin | 5c69deb | 2015-10-01 14:44:12 -0700 | [diff] [blame] | 389 | for (const auto& item : items) { |
| Casey Dahlin | f4a9311 | 2015-10-05 16:58:09 -0700 | [diff] [blame] | 390 | if (item->HasId()) { |
| Casey Dahlin | dff80e5 | 2015-09-29 13:57:06 -0700 | [diff] [blame] | 391 | hasAssignedIds = true; |
| Casey Dahlin | dff80e5 | 2015-09-29 13:57:06 -0700 | [diff] [blame] | 392 | // Ensure that the user set id is not duplicated. |
| Casey Dahlin | f4a9311 | 2015-10-05 16:58:09 -0700 | [diff] [blame] | 393 | if (usedIds.find(item->GetId()) != usedIds.end()) { |
| Casey Dahlin | dff80e5 | 2015-09-29 13:57:06 -0700 | [diff] [blame] | 394 | // We found a duplicate id, so throw an error. |
| Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 395 | fprintf(stderr, |
| Casey Dahlin | dff80e5 | 2015-09-29 13:57:06 -0700 | [diff] [blame] | 396 | "%s:%d Found duplicate method id (%d) for method: %s\n", |
| Casey Dahlin | f4a9311 | 2015-10-05 16:58:09 -0700 | [diff] [blame] | 397 | filename, item->GetLine(), |
| 398 | item->GetId(), item->GetName().c_str()); |
| Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 399 | return 1; |
| 400 | } |
| Casey Dahlin | dff80e5 | 2015-09-29 13:57:06 -0700 | [diff] [blame] | 401 | // Ensure that the user set id is within the appropriate limits |
| Casey Dahlin | f4a9311 | 2015-10-05 16:58:09 -0700 | [diff] [blame] | 402 | if (item->GetId() < kMinUserSetMethodId || |
| 403 | item->GetId() > kMaxUserSetMethodId) { |
| Casey Dahlin | dff80e5 | 2015-09-29 13:57:06 -0700 | [diff] [blame] | 404 | fprintf(stderr, "%s:%d Found out of bounds id (%d) for method: %s\n", |
| Casey Dahlin | f4a9311 | 2015-10-05 16:58:09 -0700 | [diff] [blame] | 405 | filename, item->GetLine(), |
| 406 | item->GetId(), item->GetName().c_str()); |
| Casey Dahlin | dff80e5 | 2015-09-29 13:57:06 -0700 | [diff] [blame] | 407 | fprintf(stderr, " Value for id must be between %d and %d inclusive.\n", |
| 408 | kMinUserSetMethodId, kMaxUserSetMethodId); |
| 409 | return 1; |
| 410 | } |
| Casey Dahlin | f4a9311 | 2015-10-05 16:58:09 -0700 | [diff] [blame] | 411 | usedIds.insert(item->GetId()); |
| Casey Dahlin | dff80e5 | 2015-09-29 13:57:06 -0700 | [diff] [blame] | 412 | } else { |
| 413 | hasUnassignedIds = true; |
| 414 | } |
| 415 | if (hasAssignedIds && hasUnassignedIds) { |
| 416 | fprintf(stderr, |
| 417 | "%s: You must either assign id's to all methods or to none of them.\n", |
| 418 | filename); |
| 419 | return 1; |
| Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 420 | } |
| Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 421 | } |
| 422 | |
| 423 | // In the case that all methods have unassigned id's, set a unique id for them. |
| 424 | if (hasUnassignedIds) { |
| 425 | int newId = 0; |
| Casey Dahlin | 5c69deb | 2015-10-01 14:44:12 -0700 | [diff] [blame] | 426 | for (const auto& item : items) { |
| Casey Dahlin | f4a9311 | 2015-10-05 16:58:09 -0700 | [diff] [blame] | 427 | item->SetId(newId++); |
| Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 428 | } |
| 429 | } |
| 430 | |
| 431 | // success |
| 432 | return 0; |
| 433 | } |
| 434 | |
| Christopher Wiley | 69b44cf | 2016-05-03 13:43:33 -0700 | [diff] [blame] | 435 | bool validate_constants(const AidlInterface& interface) { |
| 436 | bool success = true; |
| 437 | set<string> names; |
| 438 | for (const std::unique_ptr<AidlIntConstant>& int_constant : |
| 439 | interface.GetIntConstants()) { |
| 440 | if (names.count(int_constant->GetName()) > 0) { |
| 441 | LOG(ERROR) << "Found duplicate constant name '" << int_constant->GetName() |
| 442 | << "'"; |
| 443 | success = false; |
| 444 | } |
| 445 | names.insert(int_constant->GetName()); |
| Roshan Pius | 3b2203d | 2016-07-22 16:13:20 -0700 | [diff] [blame] | 446 | // We've logged an error message for this on object construction. |
| 447 | success = success && int_constant->IsValid(); |
| Christopher Wiley | 69b44cf | 2016-05-03 13:43:33 -0700 | [diff] [blame] | 448 | } |
| 449 | for (const std::unique_ptr<AidlStringConstant>& string_constant : |
| 450 | interface.GetStringConstants()) { |
| 451 | if (names.count(string_constant->GetName()) > 0) { |
| 452 | LOG(ERROR) << "Found duplicate constant name '" << string_constant->GetName() |
| 453 | << "'"; |
| 454 | success = false; |
| 455 | } |
| 456 | names.insert(string_constant->GetName()); |
| 457 | // We've logged an error message for this on object construction. |
| 458 | success = success && string_constant->IsValid(); |
| 459 | } |
| 460 | return success; |
| 461 | } |
| 462 | |
| Christopher Wiley | ef14093 | 2015-11-03 09:29:19 -0800 | [diff] [blame] | 463 | // TODO: Remove this in favor of using the YACC parser b/25479378 |
| 464 | bool ParsePreprocessedLine(const string& line, string* decl, |
| 465 | vector<string>* package, string* class_name) { |
| 466 | // erase all trailing whitespace and semicolons |
| 467 | const size_t end = line.find_last_not_of(" ;\t"); |
| 468 | if (end == string::npos) { |
| 469 | return false; |
| 470 | } |
| 471 | if (line.rfind(';', end) != string::npos) { |
| 472 | return false; |
| 473 | } |
| 474 | |
| 475 | decl->clear(); |
| 476 | string type; |
| 477 | vector<string> pieces = Split(line.substr(0, end + 1), " \t"); |
| 478 | for (const string& piece : pieces) { |
| 479 | if (piece.empty()) { |
| 480 | continue; |
| 481 | } |
| 482 | if (decl->empty()) { |
| 483 | *decl = std::move(piece); |
| 484 | } else if (type.empty()) { |
| 485 | type = std::move(piece); |
| 486 | } else { |
| 487 | return false; |
| 488 | } |
| 489 | } |
| 490 | |
| 491 | // Note that this logic is absolutely wrong. Given a parcelable |
| 492 | // org.some.Foo.Bar, the class name is Foo.Bar, but this code will claim that |
| 493 | // the class is just Bar. However, this was the way it was done in the past. |
| 494 | // |
| 495 | // See b/17415692 |
| 496 | size_t dot_pos = type.rfind('.'); |
| 497 | if (dot_pos != string::npos) { |
| 498 | *class_name = type.substr(dot_pos + 1); |
| 499 | *package = Split(type.substr(0, dot_pos), "."); |
| 500 | } else { |
| 501 | *class_name = type; |
| 502 | package->clear(); |
| 503 | } |
| 504 | |
| 505 | return true; |
| 506 | } |
| 507 | |
| Christopher Wiley | 4a2884b | 2015-10-07 11:27:45 -0700 | [diff] [blame] | 508 | } // namespace |
| 509 | |
| 510 | namespace internals { |
| 511 | |
| Jiyong Park | 1deecc3 | 2018-07-17 01:14:41 +0900 | [diff] [blame] | 512 | bool parse_preprocessed_file(const IoDelegate& io_delegate, const string& filename, |
| 513 | TypeNamespace* types, AidlTypenames& typenames) { |
| Christopher Wiley | ef14093 | 2015-11-03 09:29:19 -0800 | [diff] [blame] | 514 | bool success = true; |
| 515 | unique_ptr<LineReader> line_reader = io_delegate.GetLineReader(filename); |
| 516 | if (!line_reader) { |
| 517 | LOG(ERROR) << "cannot open preprocessed file: " << filename; |
| 518 | success = false; |
| 519 | return success; |
| 520 | } |
| 521 | |
| 522 | string line; |
| 523 | unsigned lineno = 1; |
| 524 | for ( ; line_reader->ReadLine(&line); ++lineno) { |
| 525 | if (line.empty() || line.compare(0, 2, "//") == 0) { |
| 526 | // skip comments and empty lines |
| 527 | continue; |
| 528 | } |
| 529 | |
| 530 | string decl; |
| 531 | vector<string> package; |
| 532 | string class_name; |
| 533 | if (!ParsePreprocessedLine(line, &decl, &package, &class_name)) { |
| 534 | success = false; |
| 535 | break; |
| 536 | } |
| 537 | |
| 538 | if (decl == "parcelable") { |
| Jiyong Park | 1deecc3 | 2018-07-17 01:14:41 +0900 | [diff] [blame] | 539 | AidlParcelable* doc = |
| 540 | new AidlParcelable(new AidlQualifiedName(class_name, ""), lineno, package); |
| 541 | types->AddParcelableType(*doc, filename); |
| 542 | typenames.AddPreprocessedType(unique_ptr<AidlParcelable>(doc)); |
| Steven Moreland | ed83a28 | 2018-07-17 13:27:29 -0700 | [diff] [blame] | 543 | } else if (decl == "structured_parcelable") { |
| 544 | auto temp = new std::vector<std::unique_ptr<AidlVariableDeclaration>>(); |
| Jiyong Park | 1deecc3 | 2018-07-17 01:14:41 +0900 | [diff] [blame] | 545 | AidlStructuredParcelable* doc = new AidlStructuredParcelable( |
| 546 | new AidlQualifiedName(class_name, ""), lineno, package, temp); |
| 547 | types->AddParcelableType(*doc, filename); |
| 548 | typenames.AddPreprocessedType(unique_ptr<AidlStructuredParcelable>(doc)); |
| Christopher Wiley | ef14093 | 2015-11-03 09:29:19 -0800 | [diff] [blame] | 549 | } else if (decl == "interface") { |
| Casey Dahlin | d40e2fe | 2015-11-24 14:06:52 -0800 | [diff] [blame] | 550 | auto temp = new std::vector<std::unique_ptr<AidlMember>>(); |
| Jiyong Park | 1deecc3 | 2018-07-17 01:14:41 +0900 | [diff] [blame] | 551 | AidlInterface* doc = new AidlInterface(class_name, lineno, "", false, temp, package); |
| 552 | types->AddBinderType(*doc, filename); |
| 553 | typenames.AddPreprocessedType(unique_ptr<AidlInterface>(doc)); |
| Christopher Wiley | ef14093 | 2015-11-03 09:29:19 -0800 | [diff] [blame] | 554 | } else { |
| 555 | success = false; |
| 556 | break; |
| 557 | } |
| 558 | } |
| 559 | if (!success) { |
| 560 | LOG(ERROR) << filename << ':' << lineno |
| 561 | << " malformed preprocessed file line: '" << line << "'"; |
| 562 | } |
| 563 | |
| 564 | return success; |
| 565 | } |
| 566 | |
| Steven Moreland | 5557f1c | 2018-07-02 13:50:23 -0700 | [diff] [blame] | 567 | AidlError load_and_validate_aidl(const std::vector<std::string>& preprocessed_files, |
| 568 | const std::vector<std::string>& import_paths, |
| 569 | const std::string& input_file_name, const bool generate_traces, |
| 570 | const IoDelegate& io_delegate, TypeNamespace* types, |
| 571 | std::unique_ptr<AidlDefinedType>* returned_type, |
| 572 | std::vector<std::unique_ptr<AidlImport>>* returned_imports) { |
| Christopher Wiley | 632801d | 2015-11-05 14:15:49 -0800 | [diff] [blame] | 573 | AidlError err = AidlError::OK; |
| Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 574 | |
| Casey Dahlin | c1f39b4 | 2015-11-24 10:34:34 -0800 | [diff] [blame] | 575 | std::map<AidlImport*,std::unique_ptr<AidlDocument>> docs; |
| Casey Dahlin | 624358c | 2015-10-12 19:29:51 -0700 | [diff] [blame] | 576 | |
| Jiyong Park | 1deecc3 | 2018-07-17 01:14:41 +0900 | [diff] [blame] | 577 | AidlTypenames typenames; |
| 578 | |
| Christopher Wiley | 3a9d158 | 2015-09-16 12:42:14 -0700 | [diff] [blame] | 579 | // import the preprocessed file |
| Christopher Wiley | eb1acc1 | 2015-09-16 11:25:13 -0700 | [diff] [blame] | 580 | for (const string& s : preprocessed_files) { |
| Jiyong Park | 1deecc3 | 2018-07-17 01:14:41 +0900 | [diff] [blame] | 581 | if (!parse_preprocessed_file(io_delegate, s, types, typenames)) { |
| Christopher Wiley | 632801d | 2015-11-05 14:15:49 -0800 | [diff] [blame] | 582 | err = AidlError::BAD_PRE_PROCESSED_FILE; |
| Christopher Wiley | ef14093 | 2015-11-03 09:29:19 -0800 | [diff] [blame] | 583 | } |
| Christopher Wiley | 3a9d158 | 2015-09-16 12:42:14 -0700 | [diff] [blame] | 584 | } |
| Christopher Wiley | 632801d | 2015-11-05 14:15:49 -0800 | [diff] [blame] | 585 | if (err != AidlError::OK) { |
| Christopher Wiley | 3a9d158 | 2015-09-16 12:42:14 -0700 | [diff] [blame] | 586 | return err; |
| 587 | } |
| Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 588 | |
| Christopher Wiley | c16e5e7 | 2015-09-16 10:49:40 -0700 | [diff] [blame] | 589 | // parse the input file |
| Jiyong Park | 1deecc3 | 2018-07-17 01:14:41 +0900 | [diff] [blame] | 590 | Parser p{io_delegate, &typenames}; |
| Christopher Wiley | 4a2884b | 2015-10-07 11:27:45 -0700 | [diff] [blame] | 591 | if (!p.ParseFile(input_file_name)) { |
| Christopher Wiley | 632801d | 2015-11-05 14:15:49 -0800 | [diff] [blame] | 592 | return AidlError::PARSE_ERROR; |
| Christopher Wiley | 4a2884b | 2015-10-07 11:27:45 -0700 | [diff] [blame] | 593 | } |
| Casey Dahlin | 2cc9316 | 2015-10-02 16:14:17 -0700 | [diff] [blame] | 594 | |
| Casey Dahlin | c1f39b4 | 2015-11-24 10:34:34 -0800 | [diff] [blame] | 595 | AidlDocument* parsed_doc = p.GetDocument(); |
| 596 | |
| Steven Moreland | c258abc | 2018-07-10 14:03:38 -0700 | [diff] [blame] | 597 | if (parsed_doc->GetDefinedTypes().empty()) { |
| 598 | LOG(ERROR) << "Cannot generate file without any definitions."; |
| 599 | return AidlError::BAD_TYPE; |
| 600 | } |
| Casey Dahlin | c1f39b4 | 2015-11-24 10:34:34 -0800 | [diff] [blame] | 601 | |
| Steven Moreland | c258abc | 2018-07-10 14:03:38 -0700 | [diff] [blame] | 602 | bool has_only_unstructured_parcelables = true; |
| 603 | for (const auto& defined_type : parsed_doc->GetDefinedTypes()) { |
| 604 | if (defined_type->AsStructuredParcelable() != nullptr || |
| 605 | defined_type->AsInterface() != nullptr) { |
| 606 | has_only_unstructured_parcelables = false; |
| 607 | break; |
| 608 | } |
| 609 | } |
| 610 | if (has_only_unstructured_parcelables) { |
| 611 | LOG(ERROR) << "Refusing to generate code with unstructured parcelables."; |
| Christopher Wiley | 632801d | 2015-11-05 14:15:49 -0800 | [diff] [blame] | 612 | return AidlError::FOUND_PARCELABLE; |
| 613 | } |
| 614 | |
| Steven Moreland | c258abc | 2018-07-10 14:03:38 -0700 | [diff] [blame] | 615 | if (parsed_doc->GetDefinedTypes().size() > 1) { |
| 616 | LOG(ERROR) << "Exactly one structured type is required to be defined."; |
| 617 | return AidlError::BAD_TYPE; |
| 618 | } |
| 619 | |
| 620 | unique_ptr<AidlDefinedType> defined_type(parsed_doc->ReleaseDefinedType()); |
| 621 | AidlInterface* interface = defined_type->AsInterface(); |
| 622 | AidlStructuredParcelable* parcelable = defined_type->AsStructuredParcelable(); |
| 623 | |
| 624 | CHECK(interface != nullptr || parcelable != nullptr); |
| 625 | |
| Steven Moreland | 5557f1c | 2018-07-02 13:50:23 -0700 | [diff] [blame] | 626 | if (!check_filename(input_file_name.c_str(), defined_type->GetPackage(), defined_type->GetName(), |
| 627 | defined_type->GetLine()) || |
| 628 | !types->IsValidPackage(defined_type->GetPackage())) { |
| 629 | LOG(ERROR) << "Invalid package declaration '" << defined_type->GetPackage() << "'"; |
| Christopher Wiley | 632801d | 2015-11-05 14:15:49 -0800 | [diff] [blame] | 630 | return AidlError::BAD_PACKAGE; |
| 631 | } |
| Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 632 | |
| Christopher Wiley | c16e5e7 | 2015-09-16 10:49:40 -0700 | [diff] [blame] | 633 | // parse the imports of the input file |
| Christopher Wiley | 72877ac | 2015-10-06 14:41:42 -0700 | [diff] [blame] | 634 | ImportResolver import_resolver{io_delegate, import_paths}; |
| Casey Dahlin | 0edf342 | 2015-10-07 12:34:59 -0700 | [diff] [blame] | 635 | for (auto& import : p.GetImports()) { |
| Christopher Wiley | 934a82d | 2016-01-27 13:02:24 -0800 | [diff] [blame] | 636 | if (types->HasImportType(*import)) { |
| Christopher Wiley | fb4b22d | 2015-09-25 15:16:13 -0700 | [diff] [blame] | 637 | // There are places in the Android tree where an import doesn't resolve, |
| 638 | // but we'll pick the type up through the preprocessed types. |
| 639 | // This seems like an error, but legacy support demands we support it... |
| Christopher Wiley | c16e5e7 | 2015-09-16 10:49:40 -0700 | [diff] [blame] | 640 | continue; |
| 641 | } |
| Casey Dahlin | 0edf342 | 2015-10-07 12:34:59 -0700 | [diff] [blame] | 642 | string import_path = import_resolver.FindImportFile(import->GetNeededClass()); |
| Christopher Wiley | 72877ac | 2015-10-06 14:41:42 -0700 | [diff] [blame] | 643 | if (import_path.empty()) { |
| Casey Dahlin | 0edf342 | 2015-10-07 12:34:59 -0700 | [diff] [blame] | 644 | cerr << import->GetFileFrom() << ":" << import->GetLine() |
| Christopher Wiley | c16e5e7 | 2015-09-16 10:49:40 -0700 | [diff] [blame] | 645 | << ": couldn't find import for class " |
| Casey Dahlin | 0edf342 | 2015-10-07 12:34:59 -0700 | [diff] [blame] | 646 | << import->GetNeededClass() << endl; |
| Christopher Wiley | 632801d | 2015-11-05 14:15:49 -0800 | [diff] [blame] | 647 | err = AidlError::BAD_IMPORT; |
| Christopher Wiley | c16e5e7 | 2015-09-16 10:49:40 -0700 | [diff] [blame] | 648 | continue; |
| 649 | } |
| Casey Dahlin | 0edf342 | 2015-10-07 12:34:59 -0700 | [diff] [blame] | 650 | import->SetFilename(import_path); |
| Casey Dahlin | 2cc9316 | 2015-10-02 16:14:17 -0700 | [diff] [blame] | 651 | |
| Jiyong Park | 1deecc3 | 2018-07-17 01:14:41 +0900 | [diff] [blame] | 652 | Parser p{io_delegate, &typenames}; |
| Casey Dahlin | 0edf342 | 2015-10-07 12:34:59 -0700 | [diff] [blame] | 653 | if (!p.ParseFile(import->GetFilename())) { |
| Christopher Wiley | c16e5e7 | 2015-09-16 10:49:40 -0700 | [diff] [blame] | 654 | cerr << "error while parsing import for class " |
| Casey Dahlin | 0edf342 | 2015-10-07 12:34:59 -0700 | [diff] [blame] | 655 | << import->GetNeededClass() << endl; |
| Christopher Wiley | 632801d | 2015-11-05 14:15:49 -0800 | [diff] [blame] | 656 | err = AidlError::BAD_IMPORT; |
| Christopher Wiley | c16e5e7 | 2015-09-16 10:49:40 -0700 | [diff] [blame] | 657 | continue; |
| 658 | } |
| Christopher Wiley | 4a2884b | 2015-10-07 11:27:45 -0700 | [diff] [blame] | 659 | |
| Casey Dahlin | c1f39b4 | 2015-11-24 10:34:34 -0800 | [diff] [blame] | 660 | std::unique_ptr<AidlDocument> document(p.ReleaseDocument()); |
| Steven Moreland | c258abc | 2018-07-10 14:03:38 -0700 | [diff] [blame] | 661 | if (!check_filenames(import->GetFilename(), *document)) err = AidlError::BAD_IMPORT; |
| Casey Dahlin | c1f39b4 | 2015-11-24 10:34:34 -0800 | [diff] [blame] | 662 | docs[import.get()] = std::move(document); |
| Christopher Wiley | c16e5e7 | 2015-09-16 10:49:40 -0700 | [diff] [blame] | 663 | } |
| Christopher Wiley | 632801d | 2015-11-05 14:15:49 -0800 | [diff] [blame] | 664 | if (err != AidlError::OK) { |
| Christopher Wiley | c16e5e7 | 2015-09-16 10:49:40 -0700 | [diff] [blame] | 665 | return err; |
| Christopher Wiley | 3a9d158 | 2015-09-16 12:42:14 -0700 | [diff] [blame] | 666 | } |
| Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 667 | |
| Jiyong Park | 1deecc3 | 2018-07-17 01:14:41 +0900 | [diff] [blame] | 668 | if (!p.Resolve()) { |
| 669 | return AidlError::BAD_TYPE; |
| 670 | } |
| 671 | |
| Steven Moreland | 5557f1c | 2018-07-02 13:50:23 -0700 | [diff] [blame] | 672 | if (interface) { |
| 673 | // gather the types that have been declared |
| Steven Moreland | c258abc | 2018-07-10 14:03:38 -0700 | [diff] [blame] | 674 | if (!types->AddBinderType(*interface, input_file_name)) { |
| Steven Moreland | 5557f1c | 2018-07-02 13:50:23 -0700 | [diff] [blame] | 675 | err = AidlError::BAD_TYPE; |
| 676 | } |
| 677 | |
| 678 | interface->SetGenerateTraces(generate_traces); |
| Christopher Wiley | 84c1eac | 2015-09-23 13:29:28 -0700 | [diff] [blame] | 679 | } |
| Casey Dahlin | c1f39b4 | 2015-11-24 10:34:34 -0800 | [diff] [blame] | 680 | |
| Steven Moreland | 5557f1c | 2018-07-02 13:50:23 -0700 | [diff] [blame] | 681 | if (parcelable) { |
| Steven Moreland | c258abc | 2018-07-10 14:03:38 -0700 | [diff] [blame] | 682 | if (!types->AddParcelableType(*parcelable, input_file_name)) { |
| Steven Moreland | 5557f1c | 2018-07-02 13:50:23 -0700 | [diff] [blame] | 683 | err = AidlError::BAD_TYPE; |
| 684 | } |
| 685 | } |
| Casey Dahlin | a2f77c4 | 2015-12-01 18:26:02 -0800 | [diff] [blame] | 686 | |
| Steven Moreland | 5557f1c | 2018-07-02 13:50:23 -0700 | [diff] [blame] | 687 | defined_type->SetLanguageType(types->GetDefinedType(*defined_type)); |
| Martijn Coenen | f1b5078 | 2018-02-21 21:06:23 +0100 | [diff] [blame] | 688 | |
| Casey Dahlin | 0edf342 | 2015-10-07 12:34:59 -0700 | [diff] [blame] | 689 | for (const auto& import : p.GetImports()) { |
| Casey Dahlin | c1f39b4 | 2015-11-24 10:34:34 -0800 | [diff] [blame] | 690 | // If we skipped an unresolved import above (see comment there) we'll have |
| 691 | // an empty bucket here. |
| 692 | const auto import_itr = docs.find(import.get()); |
| 693 | if (import_itr == docs.cend()) { |
| 694 | continue; |
| 695 | } |
| 696 | |
| Steven Moreland | c258abc | 2018-07-10 14:03:38 -0700 | [diff] [blame] | 697 | if (!gather_types(import->GetFilename(), *import_itr->second, types)) { |
| Christopher Wiley | 632801d | 2015-11-05 14:15:49 -0800 | [diff] [blame] | 698 | err = AidlError::BAD_TYPE; |
| Christopher Wiley | 84c1eac | 2015-09-23 13:29:28 -0700 | [diff] [blame] | 699 | } |
| Christopher Wiley | 3a9d158 | 2015-09-16 12:42:14 -0700 | [diff] [blame] | 700 | } |
| Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 701 | |
| Christopher Wiley | c16e5e7 | 2015-09-16 10:49:40 -0700 | [diff] [blame] | 702 | // check the referenced types in parsed_doc to make sure we've imported them |
| Steven Moreland | c258abc | 2018-07-10 14:03:38 -0700 | [diff] [blame] | 703 | if (interface && check_types(input_file_name, interface, types) != 0) { |
| Steven Moreland | 5557f1c | 2018-07-02 13:50:23 -0700 | [diff] [blame] | 704 | err = AidlError::BAD_TYPE; |
| 705 | } |
| Steven Moreland | c258abc | 2018-07-10 14:03:38 -0700 | [diff] [blame] | 706 | if (parcelable && check_types(input_file_name, parcelable, types) != 0) { |
| Christopher Wiley | 632801d | 2015-11-05 14:15:49 -0800 | [diff] [blame] | 707 | err = AidlError::BAD_TYPE; |
| 708 | } |
| 709 | if (err != AidlError::OK) { |
| 710 | return err; |
| 711 | } |
| Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 712 | |
| Christopher Wiley | c16e5e7 | 2015-09-16 10:49:40 -0700 | [diff] [blame] | 713 | // assign method ids and validate. |
| Steven Moreland | 5557f1c | 2018-07-02 13:50:23 -0700 | [diff] [blame] | 714 | if (interface && |
| 715 | check_and_assign_method_ids(input_file_name.c_str(), interface->GetMethods()) != 0) { |
| Christopher Wiley | 632801d | 2015-11-05 14:15:49 -0800 | [diff] [blame] | 716 | return AidlError::BAD_METHOD_ID; |
| Christopher Wiley | 3a9d158 | 2015-09-16 12:42:14 -0700 | [diff] [blame] | 717 | } |
| Steven Moreland | 5557f1c | 2018-07-02 13:50:23 -0700 | [diff] [blame] | 718 | if (interface && !validate_constants(*interface)) { |
| Christopher Wiley | 69b44cf | 2016-05-03 13:43:33 -0700 | [diff] [blame] | 719 | return AidlError::BAD_CONSTANTS; |
| 720 | } |
| Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 721 | |
| Steven Moreland | c258abc | 2018-07-10 14:03:38 -0700 | [diff] [blame] | 722 | *returned_type = std::move(defined_type); |
| Casey Dahlin | 0edf342 | 2015-10-07 12:34:59 -0700 | [diff] [blame] | 723 | |
| 724 | if (returned_imports) |
| 725 | p.ReleaseImports(returned_imports); |
| 726 | |
| Christopher Wiley | 632801d | 2015-11-05 14:15:49 -0800 | [diff] [blame] | 727 | return AidlError::OK; |
| Christopher Wiley | eb1acc1 | 2015-09-16 11:25:13 -0700 | [diff] [blame] | 728 | } |
| 729 | |
| Casey Dahlin | 2cc9316 | 2015-10-02 16:14:17 -0700 | [diff] [blame] | 730 | } // namespace internals |
| 731 | |
| Christopher Wiley | 4a2884b | 2015-10-07 11:27:45 -0700 | [diff] [blame] | 732 | int compile_aidl_to_cpp(const CppOptions& options, |
| 733 | const IoDelegate& io_delegate) { |
| Steven Moreland | 5557f1c | 2018-07-02 13:50:23 -0700 | [diff] [blame] | 734 | unique_ptr<AidlDefinedType> defined_type; |
| Casey Dahlin | 0edf342 | 2015-10-07 12:34:59 -0700 | [diff] [blame] | 735 | std::vector<std::unique_ptr<AidlImport>> imports; |
| Christopher Wiley | e3550c6 | 2015-09-29 13:26:10 -0700 | [diff] [blame] | 736 | unique_ptr<cpp::TypeNamespace> types(new cpp::TypeNamespace()); |
| Christopher Wiley | 5679952 | 2015-10-31 10:17:04 -0700 | [diff] [blame] | 737 | types->Init(); |
| Christopher Wiley | 632801d | 2015-11-05 14:15:49 -0800 | [diff] [blame] | 738 | AidlError err = internals::load_and_validate_aidl( |
| Steven Moreland | 4b37e26 | 2018-07-17 12:49:38 -0700 | [diff] [blame] | 739 | options.preprocessed_files_, options.ImportPaths(), options.InputFileName(), |
| 740 | options.ShouldGenTraces(), io_delegate, types.get(), &defined_type, &imports); |
| Christopher Wiley | 632801d | 2015-11-05 14:15:49 -0800 | [diff] [blame] | 741 | if (err != AidlError::OK) { |
| 742 | return 1; |
| Christopher Wiley | eb1acc1 | 2015-09-16 11:25:13 -0700 | [diff] [blame] | 743 | } |
| 744 | |
| Steven Moreland | 5557f1c | 2018-07-02 13:50:23 -0700 | [diff] [blame] | 745 | CHECK(defined_type != nullptr); |
| 746 | |
| 747 | if (!write_cpp_dep_file(options, *defined_type, imports, io_delegate)) { |
| Christopher Wiley | 3a9911c | 2016-01-19 12:59:09 -0800 | [diff] [blame] | 748 | return 1; |
| Christopher Wiley | 19059cb | 2015-11-05 16:11:56 -0800 | [diff] [blame] | 749 | } |
| Christopher Wiley | eb1acc1 | 2015-09-16 11:25:13 -0700 | [diff] [blame] | 750 | |
| Steven Moreland | 5557f1c | 2018-07-02 13:50:23 -0700 | [diff] [blame] | 751 | return (cpp::GenerateCpp(options, *types, *defined_type, io_delegate)) ? 0 : 1; |
| Christopher Wiley | eb1acc1 | 2015-09-16 11:25:13 -0700 | [diff] [blame] | 752 | } |
| 753 | |
| Christopher Wiley | 4a2884b | 2015-10-07 11:27:45 -0700 | [diff] [blame] | 754 | int compile_aidl_to_java(const JavaOptions& options, |
| 755 | const IoDelegate& io_delegate) { |
| Steven Moreland | 5557f1c | 2018-07-02 13:50:23 -0700 | [diff] [blame] | 756 | unique_ptr<AidlDefinedType> defined_type; |
| Casey Dahlin | 0edf342 | 2015-10-07 12:34:59 -0700 | [diff] [blame] | 757 | std::vector<std::unique_ptr<AidlImport>> imports; |
| Christopher Wiley | db154a5 | 2015-09-28 16:32:25 -0700 | [diff] [blame] | 758 | unique_ptr<java::JavaTypeNamespace> types(new java::JavaTypeNamespace()); |
| Christopher Wiley | 5679952 | 2015-10-31 10:17:04 -0700 | [diff] [blame] | 759 | types->Init(); |
| Christopher Wiley | 632801d | 2015-11-05 14:15:49 -0800 | [diff] [blame] | 760 | AidlError aidl_err = internals::load_and_validate_aidl( |
| Steven Moreland | 5557f1c | 2018-07-02 13:50:23 -0700 | [diff] [blame] | 761 | options.preprocessed_files_, options.import_paths_, options.input_file_name_, |
| 762 | options.gen_traces_, io_delegate, types.get(), &defined_type, &imports); |
| Christopher Wiley | 632801d | 2015-11-05 14:15:49 -0800 | [diff] [blame] | 763 | if (aidl_err == AidlError::FOUND_PARCELABLE && !options.fail_on_parcelable_) { |
| 764 | // We aborted code generation because this file contains parcelables. |
| 765 | // However, we were not told to complain if we find parcelables. |
| Christopher Wiley | b1bbdf8 | 2016-04-21 11:43:45 -0700 | [diff] [blame] | 766 | // Just generate a dep file and exit quietly. The dep file is for a legacy |
| 767 | // use case by the SDK. |
| 768 | write_java_dep_file(options, imports, io_delegate, ""); |
| Christopher Wiley | 632801d | 2015-11-05 14:15:49 -0800 | [diff] [blame] | 769 | return 0; |
| 770 | } |
| 771 | if (aidl_err != AidlError::OK) { |
| 772 | return 1; |
| Christopher Wiley | eb1acc1 | 2015-09-16 11:25:13 -0700 | [diff] [blame] | 773 | } |
| Christopher Wiley | eb1acc1 | 2015-09-16 11:25:13 -0700 | [diff] [blame] | 774 | |
| Steven Moreland | 5557f1c | 2018-07-02 13:50:23 -0700 | [diff] [blame] | 775 | CHECK(defined_type != nullptr); |
| 776 | |
| Christopher Wiley | 3a9d158 | 2015-09-16 12:42:14 -0700 | [diff] [blame] | 777 | string output_file_name = options.output_file_name_; |
| 778 | // if needed, generate the output file name from the base folder |
| Christopher Wiley | 632801d | 2015-11-05 14:15:49 -0800 | [diff] [blame] | 779 | if (output_file_name.empty() && !options.output_base_folder_.empty()) { |
| Steven Moreland | 5557f1c | 2018-07-02 13:50:23 -0700 | [diff] [blame] | 780 | output_file_name = generate_outputFileName(options, *defined_type); |
| Christopher Wiley | 3a9d158 | 2015-09-16 12:42:14 -0700 | [diff] [blame] | 781 | } |
| Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 782 | |
| Casey Dahlin | 6453351 | 2015-10-23 17:11:21 -0700 | [diff] [blame] | 783 | // make sure the folders of the output file all exists |
| 784 | if (!io_delegate.CreatePathForFile(output_file_name)) { |
| 785 | return 1; |
| 786 | } |
| 787 | |
| Christopher Wiley | f813619 | 2016-04-12 14:19:35 -0700 | [diff] [blame] | 788 | if (!write_java_dep_file(options, imports, io_delegate, output_file_name)) { |
| Christopher Wiley | 3a9911c | 2016-01-19 12:59:09 -0800 | [diff] [blame] | 789 | return 1; |
| Christopher Wiley | 3a9d158 | 2015-09-16 12:42:14 -0700 | [diff] [blame] | 790 | } |
| Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 791 | |
| Steven Moreland | 5557f1c | 2018-07-02 13:50:23 -0700 | [diff] [blame] | 792 | return generate_java(output_file_name, options.input_file_name_.c_str(), defined_type.get(), |
| 793 | types.get(), io_delegate, options); |
| Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 794 | } |
| 795 | |
| Casey Dahlin | c1f39b4 | 2015-11-24 10:34:34 -0800 | [diff] [blame] | 796 | bool preprocess_aidl(const JavaOptions& options, |
| 797 | const IoDelegate& io_delegate) { |
| 798 | unique_ptr<CodeWriter> writer = |
| 799 | io_delegate.GetCodeWriter(options.output_file_name_); |
| Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 800 | |
| Casey Dahlin | c1f39b4 | 2015-11-24 10:34:34 -0800 | [diff] [blame] | 801 | for (const auto& file : options.files_to_preprocess_) { |
| Jiyong Park | 1deecc3 | 2018-07-17 01:14:41 +0900 | [diff] [blame] | 802 | AidlTypenames typenames; |
| 803 | Parser p{io_delegate, &typenames}; |
| Casey Dahlin | c1f39b4 | 2015-11-24 10:34:34 -0800 | [diff] [blame] | 804 | if (!p.ParseFile(file)) |
| 805 | return false; |
| 806 | AidlDocument* doc = p.GetDocument(); |
| 807 | string line; |
| Casey Dahlin | 59401da | 2015-10-09 18:16:45 -0700 | [diff] [blame] | 808 | |
| Steven Moreland | c258abc | 2018-07-10 14:03:38 -0700 | [diff] [blame] | 809 | for (const auto& defined_type : doc->GetDefinedTypes()) { |
| Steven Moreland | ed83a28 | 2018-07-17 13:27:29 -0700 | [diff] [blame] | 810 | if (!writer->Write("%s %s;\n", defined_type->GetPreprocessDeclarationName().c_str(), |
| Steven Moreland | c258abc | 2018-07-10 14:03:38 -0700 | [diff] [blame] | 811 | defined_type->GetCanonicalName().c_str())) { |
| Casey Dahlin | c1f39b4 | 2015-11-24 10:34:34 -0800 | [diff] [blame] | 812 | return false; |
| 813 | } |
| Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 814 | } |
| Casey Dahlin | c1f39b4 | 2015-11-24 10:34:34 -0800 | [diff] [blame] | 815 | } |
| Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 816 | |
| Casey Dahlin | c1f39b4 | 2015-11-24 10:34:34 -0800 | [diff] [blame] | 817 | return writer->Close(); |
| Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 818 | } |
| Christopher Wiley | fdeb0f4 | 2015-09-11 15:38:22 -0700 | [diff] [blame] | 819 | |
| 820 | } // namespace android |
| 821 | } // namespace aidl |