blob: 61592311212a3cb2338bde61aa75065ad74d6e1f [file] [log] [blame]
Christopher Wileya8a2dc02015-09-28 16:35:31 -07001/*
2 * Copyright (C) 2015, The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "type_cpp.h"
18
Christopher Wileyb656a3b2015-10-16 11:11:09 -070019#include <algorithm>
20#include <iostream>
Casey Dahlin389781f2015-10-22 13:13:21 -070021#include <vector>
Christopher Wileyb656a3b2015-10-16 11:11:09 -070022
Elliott Hughes0a620672015-12-04 13:53:18 -080023#include <android-base/stringprintf.h>
24#include <android-base/strings.h>
Christopher Wileyb656a3b2015-10-16 11:11:09 -070025
Christopher Wileya8a2dc02015-09-28 16:35:31 -070026#include "logging.h"
27
Christopher Wileyb656a3b2015-10-16 11:11:09 -070028using std::cerr;
29using std::endl;
Christopher Wiley8a657972015-10-28 16:03:45 -070030using std::set;
Christopher Wileya8a2dc02015-09-28 16:35:31 -070031using std::string;
32using std::unique_ptr;
Casey Dahlin389781f2015-10-22 13:13:21 -070033using std::vector;
Christopher Wileya8a2dc02015-09-28 16:35:31 -070034
Christopher Wileyb656a3b2015-10-16 11:11:09 -070035using android::base::Split;
Casey Dahlin389781f2015-10-22 13:13:21 -070036using android::base::Join;
Christopher Wileyb656a3b2015-10-16 11:11:09 -070037using android::base::StringPrintf;
38
Christopher Wileya8a2dc02015-09-28 16:35:31 -070039namespace android {
40namespace aidl {
41namespace cpp {
Christopher Wileyad339272015-10-05 19:11:58 -070042namespace {
43
Christopher Wiley09af4692015-10-30 11:48:25 -070044const char kNoPackage[] = "";
45const char kNoHeader[] = "";
46const char kNoValidMethod[] = "";
Casey Dahlina2f77c42015-12-01 18:26:02 -080047Type* const kNoArrayType = nullptr;
Casey Dahlin57dbe242015-12-04 11:44:02 -080048Type* const kNoNullableType = nullptr;
Christopher Wiley09af4692015-10-30 11:48:25 -070049
Christopher Wileyb656a3b2015-10-16 11:11:09 -070050bool is_cpp_keyword(const std::string& str) {
51 static const std::vector<std::string> kCppKeywords{
52 "alignas", "alignof", "and", "and_eq", "asm", "auto", "bitand", "bitor",
53 "bool", "break", "case", "catch", "char", "char16_t", "char32_t", "class",
54 "compl", "concept", "const", "constexpr", "const_cast", "continue",
55 "decltype", "default", "delete", "do", "double", "dynamic_cast", "else",
56 "enum", "explicit", "export", "extern", "false", "float", "for", "friend",
57 "goto", "if", "inline", "int", "long", "mutable", "namespace", "new",
58 "noexcept", "not", "not_eq", "nullptr", "operator", "or", "or_eq",
59 "private", "protected", "public", "register", "reinterpret_cast",
60 "requires", "return", "short", "signed", "sizeof", "static",
61 "static_assert", "static_cast", "struct", "switch", "template", "this",
62 "thread_local", "throw", "true", "try", "typedef", "typeid", "typename",
63 "union", "unsigned", "using", "virtual", "void", "volatile", "wchar_t",
64 "while", "xor", "xor_eq",
65 };
66 return std::find(kCppKeywords.begin(), kCppKeywords.end(), str) !=
67 kCppKeywords.end();
68}
69
Christopher Wileyad339272015-10-05 19:11:58 -070070class VoidType : public Type {
71 public:
Christopher Wiley09af4692015-10-30 11:48:25 -070072 VoidType() : Type(ValidatableType::KIND_BUILT_IN, kNoPackage, "void",
Casey Dahlina2f77c42015-12-01 18:26:02 -080073 {}, "void", kNoValidMethod, kNoValidMethod) {}
Christopher Wileyad339272015-10-05 19:11:58 -070074 virtual ~VoidType() = default;
Christopher Wileyad339272015-10-05 19:11:58 -070075 bool CanBeOutParameter() const override { return false; }
76 bool CanWriteToParcel() const override { return false; }
77}; // class VoidType
78
Christopher Wiley041c8d72016-08-18 13:52:51 -070079class CppArrayType : public Type {
80 public:
81 CppArrayType(int kind, // from ValidatableType
82 const std::string& package,
83 const string& underlying_aidl_type,
84 const string& cpp_header,
85 const string& underlying_cpp_type,
Casey Dahlind2bcf2f2017-08-16 14:24:16 -070086 const string& underlying_cpp_type_nulllable,
Christopher Wiley041c8d72016-08-18 13:52:51 -070087 const string& read_method,
88 const string& write_method,
89 bool is_nullable,
90 const string& src_file_name = "")
91 : Type(kind, package,
92 underlying_aidl_type + "[]",
93 GetHeaders(is_nullable, cpp_header),
94 GetCppType(is_nullable, underlying_cpp_type),
95 read_method, write_method, kNoArrayType,
96 (is_nullable)
97 ? kNoNullableType
98 // All arrays are nullable.
99 : new CppArrayType(kind, package, underlying_aidl_type,
Casey Dahlind2bcf2f2017-08-16 14:24:16 -0700100 cpp_header, underlying_cpp_type_nulllable,
101 underlying_cpp_type_nulllable,
Christopher Wiley041c8d72016-08-18 13:52:51 -0700102 read_method, write_method, true),
103 src_file_name) {}
104
105 bool CanBeOutParameter() const override { return true; }
106
107 private:
108 static vector<string> GetHeaders(bool is_nullable, const string& cpp_header) {
109 vector<string> result = {"vector"};
110 if (is_nullable) {
111 result.push_back("memory");
112 }
113 if (!cpp_header.empty()) {
114 result.push_back(cpp_header);
115 }
116 return result;
117 }
118
119 static string GetCppType(bool is_nullable,
120 const string& underlying_cpp_type) {
121 if (is_nullable)
122 return StringPrintf("::std::unique_ptr<::std::vector<%s>>",
123 underlying_cpp_type.c_str());
124 return StringPrintf("::std::vector<%s>",
125 underlying_cpp_type.c_str());
126 }
127
128 DISALLOW_COPY_AND_ASSIGN(CppArrayType);
129}; // class CppArrayType
130
Casey Dahlina2f77c42015-12-01 18:26:02 -0800131class PrimitiveType : public Type {
132 public:
Christopher Wiley041c8d72016-08-18 13:52:51 -0700133 PrimitiveType(const std::string& aidl_type,
Casey Dahlina2f77c42015-12-01 18:26:02 -0800134 const std::string& header,
135 const std::string& cpp_type,
136 const std::string& read_method,
137 const std::string& write_method,
138 const std::string& read_array_method,
139 const std::string& write_array_method)
Christopher Wiley041c8d72016-08-18 13:52:51 -0700140 : Type(ValidatableType::KIND_BUILT_IN, kNoPackage, aidl_type, {header},
141 cpp_type, read_method, write_method,
142 new CppArrayType(ValidatableType::KIND_BUILT_IN, kNoPackage,
Casey Dahlind2bcf2f2017-08-16 14:24:16 -0700143 aidl_type, header, cpp_type, cpp_type,
Christopher Wiley041c8d72016-08-18 13:52:51 -0700144 read_array_method, write_array_method,
145 false)) {}
Casey Dahlin57dbe242015-12-04 11:44:02 -0800146
Casey Dahlina2f77c42015-12-01 18:26:02 -0800147 virtual ~PrimitiveType() = default;
148 bool IsCppPrimitive() const override { return true; }
Casey Dahlina2f77c42015-12-01 18:26:02 -0800149
150 private:
Casey Dahlina2f77c42015-12-01 18:26:02 -0800151 DISALLOW_COPY_AND_ASSIGN(PrimitiveType);
152}; // class PrimitiveType
153
Christopher Wiley041c8d72016-08-18 13:52:51 -0700154// Unfortunately, bytes in Java are signed. However, most C authors would
155// say that a byte is not in fact signed. Compromise: customize this otherwise
156// normal primitive to use signed single bytes, but unsigned byte arrays.
Casey Dahline9e73f12016-02-09 11:10:17 -0800157class ByteType : public Type {
158 public:
Christopher Wiley041c8d72016-08-18 13:52:51 -0700159 ByteType()
160 : Type(ValidatableType::KIND_BUILT_IN, kNoPackage, "byte",
161 {"cstdint"}, "int8_t", "readByte", "writeByte",
162 new CppArrayType(ValidatableType::KIND_BUILT_IN, kNoPackage,
Casey Dahlind2bcf2f2017-08-16 14:24:16 -0700163 "byte", "cstdint", "uint8_t", "uint8_t",
Christopher Wiley041c8d72016-08-18 13:52:51 -0700164 "readByteVector", "writeByteVector",
165 false)) {}
Casey Dahline9e73f12016-02-09 11:10:17 -0800166
167 virtual ~ByteType() = default;
168 bool IsCppPrimitive() const override { return true; }
Casey Dahline9e73f12016-02-09 11:10:17 -0800169
170 private:
Casey Dahline9e73f12016-02-09 11:10:17 -0800171 DISALLOW_COPY_AND_ASSIGN(ByteType);
172}; // class PrimitiveType
173
Casey Dahlin389781f2015-10-22 13:13:21 -0700174class BinderType : public Type {
175 public:
Christopher Wiley09af4692015-10-30 11:48:25 -0700176 BinderType(const AidlInterface& interface, const std::string& src_file_name)
Casey Dahlinef88bce2016-04-15 11:55:30 -0700177 : BinderType(interface, src_file_name,
178 new BinderType(interface, src_file_name, kNoNullableType,
179 "readNullableStrongBinder"),
180 "readStrongBinder") {}
Casey Dahlin389781f2015-10-22 13:13:21 -0700181 virtual ~BinderType() = default;
182
183 string WriteCast(const string& val) const override {
Casey Dahlin2dbab062016-01-05 17:41:08 -0800184 return write_cast_ + "(" + val + ")";
Casey Dahlin389781f2015-10-22 13:13:21 -0700185 }
186
187 private:
Casey Dahlinef88bce2016-04-15 11:55:30 -0700188 BinderType(const AidlInterface& interface,
189 const std::string& src_file_name,
190 Type* nullable_type, const std::string& read)
191 : Type(ValidatableType::KIND_GENERATED,
192 interface.GetPackage(), interface.GetName(),
193 {GetCppHeader(interface)}, GetCppName(interface),
194 read, "writeStrongBinder", kNoArrayType, nullable_type,
195 src_file_name, interface.GetLine()),
196 write_cast_(GetRawCppName(interface) + "::asBinder") {}
197
Casey Dahlin389781f2015-10-22 13:13:21 -0700198 static string GetCppName(const AidlInterface& interface) {
Casey Dahlin2dbab062016-01-05 17:41:08 -0800199 return "::android::sp<" + GetRawCppName(interface) + ">";
200 }
201
202 static string GetRawCppName(const AidlInterface& interface) {
Casey Dahlin389781f2015-10-22 13:13:21 -0700203 vector<string> name = interface.GetSplitPackage();
Casey Dahlin2dbab062016-01-05 17:41:08 -0800204 string ret;
Casey Dahlin389781f2015-10-22 13:13:21 -0700205
206 name.push_back(interface.GetName());
207
208 for (const auto& term : name) {
209 ret += "::" + term;
210 }
211
Casey Dahlin2dbab062016-01-05 17:41:08 -0800212 return ret;
Casey Dahlin389781f2015-10-22 13:13:21 -0700213 }
214
215 static string GetCppHeader(const AidlInterface& interface) {
216 vector<string> name = interface.GetSplitPackage();
217 name.push_back(interface.GetName());
218 return Join(name, '/') + ".h";
219 }
Casey Dahlin2dbab062016-01-05 17:41:08 -0800220
221 std::string write_cast_;
Casey Dahlin389781f2015-10-22 13:13:21 -0700222};
223
Casey Dahlin57dbe242015-12-04 11:44:02 -0800224class NullableParcelableType : public Type {
225 public:
226 NullableParcelableType(const AidlParcelable& parcelable,
227 const std::string& src_file_name)
228 : Type(ValidatableType::KIND_PARCELABLE,
229 parcelable.GetPackage(), parcelable.GetName(),
230 {parcelable.GetCppHeader()}, GetCppName(parcelable),
231 "readParcelable", "writeNullableParcelable",
232 kNoArrayType, kNoNullableType,
233 src_file_name, parcelable.GetLine()) {}
234 virtual ~NullableParcelableType() = default;
235 bool CanBeOutParameter() const override { return true; }
236
237 private:
238 static string GetCppName(const AidlParcelable& parcelable) {
239 return "::std::unique_ptr<::" + Join(parcelable.GetSplitPackage(), "::") +
Ningyuan Wangd17c58b2016-09-29 14:33:14 -0700240 "::" + parcelable.GetCppName() + ">";
Casey Dahlin57dbe242015-12-04 11:44:02 -0800241 }
242};
243
Christopher Wiley9078d722015-11-17 10:23:49 -0800244class ParcelableType : public Type {
245 public:
246 ParcelableType(const AidlParcelable& parcelable,
247 const std::string& src_file_name)
248 : Type(ValidatableType::KIND_PARCELABLE,
249 parcelable.GetPackage(), parcelable.GetName(),
Casey Dahlina2f77c42015-12-01 18:26:02 -0800250 {parcelable.GetCppHeader()}, GetCppName(parcelable),
Christopher Wiley9078d722015-11-17 10:23:49 -0800251 "readParcelable", "writeParcelable",
Christopher Wiley041c8d72016-08-18 13:52:51 -0700252 new CppArrayType(
253 ValidatableType::KIND_PARCELABLE, parcelable.GetPackage(),
254 parcelable.GetName(), parcelable.GetCppHeader(),
Casey Dahlind2bcf2f2017-08-16 14:24:16 -0700255 GetCppName(parcelable), GetCppName(parcelable),
Christopher Wiley041c8d72016-08-18 13:52:51 -0700256 "readParcelableVector", "writeParcelableVector", false,
257 src_file_name),
Casey Dahlin57dbe242015-12-04 11:44:02 -0800258 new NullableParcelableType(parcelable, src_file_name),
Christopher Wiley9078d722015-11-17 10:23:49 -0800259 src_file_name, parcelable.GetLine()) {}
260 virtual ~ParcelableType() = default;
261 bool CanBeOutParameter() const override { return true; }
262
263 private:
264 static string GetCppName(const AidlParcelable& parcelable) {
Casey Dahlin8cfb5882015-12-03 16:15:41 -0800265 return "::" + Join(parcelable.GetSplitPackage(), "::") +
Ningyuan Wangd17c58b2016-09-29 14:33:14 -0700266 "::" + parcelable.GetCppName();
Christopher Wiley9078d722015-11-17 10:23:49 -0800267 }
268};
269
Robert Quattlebaumcd8c1352017-01-04 13:42:51 -0800270class NullableMap : public Type {
271 public:
272 NullableMap()
273 : Type(ValidatableType::KIND_BUILT_IN,
274 "java.util", "Map",
275 {"binder/Map.h", "binder/Value.h"},
276 "::std::unique_ptr<::android::binder::Map>",
277 "readNullableMap", "writeNullableMap") {}
278 virtual ~NullableMap() = default;
279 bool CanBeOutParameter() const override { return true; }
280};
281
282
283class MapType : public Type {
284 public:
285 MapType()
286 : Type(ValidatableType::KIND_BUILT_IN,
287 "java.util", "Map",
288 {"binder/Map.h","binder/Value.h"},
289 "::android::binder::Map",
290 "readMap", "writeMap",
291 kNoArrayType,
292 new NullableMap() ) {}
293 virtual ~MapType() = default;
294 bool CanBeOutParameter() const override { return true; }
295
296 private:
297 DISALLOW_COPY_AND_ASSIGN(MapType);
298}; // class MapType
299
Casey Dahlin57dbe242015-12-04 11:44:02 -0800300class NullableStringListType : public Type {
301 public:
302 NullableStringListType()
Christopher Wiley5d9bc932016-02-01 13:23:16 -0800303 : Type(ValidatableType::KIND_BUILT_IN,
304 "java.util", "List<" + string(kStringCanonicalName) + ">",
305 {"utils/String16.h", "memory", "vector"},
Casey Dahlin57dbe242015-12-04 11:44:02 -0800306 "::std::unique_ptr<::std::vector<std::unique_ptr<::android::String16>>>",
307 "readString16Vector", "writeString16Vector") {}
308 virtual ~NullableStringListType() = default;
309 bool CanBeOutParameter() const override { return true; }
310
311 private:
312 DISALLOW_COPY_AND_ASSIGN(NullableStringListType);
313}; // class NullableStringListType
314
Christopher Wiley56c9bf32015-10-30 10:41:12 -0700315class StringListType : public Type {
316 public:
317 StringListType()
Christopher Wiley5d9bc932016-02-01 13:23:16 -0800318 : Type(ValidatableType::KIND_BUILT_IN,
319 "java.util", "List<" + string(kStringCanonicalName) + ">",
Casey Dahlina2f77c42015-12-01 18:26:02 -0800320 {"utils/String16.h", "vector"},
321 "::std::vector<::android::String16>",
Casey Dahlin57dbe242015-12-04 11:44:02 -0800322 "readString16Vector", "writeString16Vector",
323 kNoArrayType, new NullableStringListType()) {}
Christopher Wiley56c9bf32015-10-30 10:41:12 -0700324 virtual ~StringListType() = default;
325 bool CanBeOutParameter() const override { return true; }
326
Christopher Wiley56c9bf32015-10-30 10:41:12 -0700327 private:
328 DISALLOW_COPY_AND_ASSIGN(StringListType);
329}; // class StringListType
330
Christopher Wiley5d9bc932016-02-01 13:23:16 -0800331class NullableUtf8InCppStringListType : public Type {
332 public:
333 NullableUtf8InCppStringListType()
334 : Type(ValidatableType::KIND_BUILT_IN,
335 "java.util", "List<" + string(kUtf8InCppStringCanonicalName) + ">",
336 {"memory", "string", "vector"},
337 "::std::unique_ptr<::std::vector<std::unique_ptr<::std::string>>>",
338 "readUtf8VectorFromUtf16Vector", "writeUtf8VectorAsUtf16Vector") {}
339 virtual ~NullableUtf8InCppStringListType() = default;
340 bool CanBeOutParameter() const override { return true; }
341
342 private:
343 DISALLOW_COPY_AND_ASSIGN(NullableUtf8InCppStringListType);
344}; // class NullableUtf8InCppStringListType
345
346class Utf8InCppStringListType : public Type {
347 public:
348 Utf8InCppStringListType()
349 : Type(ValidatableType::KIND_BUILT_IN,
350 "java.util", "List<" + string(kUtf8InCppStringCanonicalName) + ">",
351 {"string", "vector"},
352 "::std::vector<::std::string>",
353 "readUtf8VectorFromUtf16Vector", "writeUtf8VectorAsUtf16Vector",
354 kNoArrayType, new NullableUtf8InCppStringListType()) {}
355 virtual ~Utf8InCppStringListType() = default;
356 bool CanBeOutParameter() const override { return true; }
357
358 private:
359 DISALLOW_COPY_AND_ASSIGN(Utf8InCppStringListType);
360}; // class Utf8InCppStringListType
361
Casey Dahlin57dbe242015-12-04 11:44:02 -0800362class NullableBinderListType : public Type {
363 public:
364 NullableBinderListType()
365 : Type(ValidatableType::KIND_BUILT_IN, "java.util",
366 "List<android.os.IBinder>", {"binder/IBinder.h", "vector"},
367 "::std::unique_ptr<::std::vector<::android::sp<::android::IBinder>>>",
368 "readStrongBinderVector", "writeStrongBinderVector") {}
369 virtual ~NullableBinderListType() = default;
370 bool CanBeOutParameter() const override { return true; }
371
372 private:
373 DISALLOW_COPY_AND_ASSIGN(NullableBinderListType);
374}; // class NullableBinderListType
375
Casey Dahlin7ecd69f2015-11-03 13:52:38 -0800376class BinderListType : public Type {
377 public:
378 BinderListType()
379 : Type(ValidatableType::KIND_BUILT_IN, "java.util",
Casey Dahlina2f77c42015-12-01 18:26:02 -0800380 "List<android.os.IBinder>", {"binder/IBinder.h", "vector"},
Casey Dahlinb8d9e882015-11-24 10:57:23 -0800381 "::std::vector<::android::sp<::android::IBinder>>",
Casey Dahlin57dbe242015-12-04 11:44:02 -0800382 "readStrongBinderVector", "writeStrongBinderVector",
383 kNoArrayType, new NullableBinderListType()) {}
Casey Dahlin7ecd69f2015-11-03 13:52:38 -0800384 virtual ~BinderListType() = default;
385 bool CanBeOutParameter() const override { return true; }
386
Casey Dahlin7ecd69f2015-11-03 13:52:38 -0800387 private:
388 DISALLOW_COPY_AND_ASSIGN(BinderListType);
389}; // class BinderListType
390
Christopher Wileyad339272015-10-05 19:11:58 -0700391} // namespace
Christopher Wileya8a2dc02015-09-28 16:35:31 -0700392
Christopher Wiley09af4692015-10-30 11:48:25 -0700393Type::Type(int kind,
394 const std::string& package,
395 const std::string& aidl_type,
Casey Dahlina2f77c42015-12-01 18:26:02 -0800396 const vector<string>& headers,
Casey Dahlinb0966612015-10-19 16:35:26 -0700397 const string& cpp_type,
398 const string& read_method,
399 const string& write_method,
Casey Dahlina2f77c42015-12-01 18:26:02 -0800400 Type* array_type,
Casey Dahlin57dbe242015-12-04 11:44:02 -0800401 Type* nullable_type,
Christopher Wiley09af4692015-10-30 11:48:25 -0700402 const string& src_file_name,
403 int line)
404 : ValidatableType(kind, package, aidl_type, src_file_name, line),
Casey Dahlina2f77c42015-12-01 18:26:02 -0800405 headers_(headers),
Casey Dahlince776cf2015-10-15 18:45:54 -0700406 aidl_type_(aidl_type),
Christopher Wileya8a2dc02015-09-28 16:35:31 -0700407 cpp_type_(cpp_type),
408 parcel_read_method_(read_method),
Casey Dahlinb0966612015-10-19 16:35:26 -0700409 parcel_write_method_(write_method),
Casey Dahlin57dbe242015-12-04 11:44:02 -0800410 array_type_(array_type),
411 nullable_type_(nullable_type) {}
Christopher Wileya8a2dc02015-09-28 16:35:31 -0700412
Casey Dahlin2cc93162015-10-02 16:14:17 -0700413bool Type::CanWriteToParcel() const { return true; }
Christopher Wileya8a2dc02015-09-28 16:35:31 -0700414
Christopher Wiley56799522015-10-31 10:17:04 -0700415void TypeNamespace::Init() {
Casey Dahline9e73f12016-02-09 11:10:17 -0800416 Add(new ByteType());
Christopher Wiley09af4692015-10-30 11:48:25 -0700417 Add(new PrimitiveType(
Christopher Wiley041c8d72016-08-18 13:52:51 -0700418 "int",
Christopher Wiley09af4692015-10-30 11:48:25 -0700419 "cstdint", "int32_t", "readInt32", "writeInt32",
Christopher Wileyb8e49a42015-10-27 12:55:18 -0700420 "readInt32Vector", "writeInt32Vector"));
Christopher Wiley09af4692015-10-30 11:48:25 -0700421 Add(new PrimitiveType(
Christopher Wiley041c8d72016-08-18 13:52:51 -0700422 "long",
Christopher Wiley09af4692015-10-30 11:48:25 -0700423 "cstdint", "int64_t", "readInt64", "writeInt64",
Christopher Wileyb8e49a42015-10-27 12:55:18 -0700424 "readInt64Vector", "writeInt64Vector"));
Christopher Wiley09af4692015-10-30 11:48:25 -0700425 Add(new PrimitiveType(
Christopher Wiley041c8d72016-08-18 13:52:51 -0700426 "float",
Christopher Wiley09af4692015-10-30 11:48:25 -0700427 kNoHeader, "float", "readFloat", "writeFloat",
Christopher Wileyb8e49a42015-10-27 12:55:18 -0700428 "readFloatVector", "writeFloatVector"));
Christopher Wiley09af4692015-10-30 11:48:25 -0700429 Add(new PrimitiveType(
Christopher Wiley041c8d72016-08-18 13:52:51 -0700430 "double",
Christopher Wiley09af4692015-10-30 11:48:25 -0700431 kNoHeader, "double", "readDouble", "writeDouble",
Christopher Wileyb8e49a42015-10-27 12:55:18 -0700432 "readDoubleVector", "writeDoubleVector"));
Christopher Wiley09af4692015-10-30 11:48:25 -0700433 Add(new PrimitiveType(
Christopher Wiley041c8d72016-08-18 13:52:51 -0700434 "boolean",
Christopher Wiley09af4692015-10-30 11:48:25 -0700435 kNoHeader, "bool", "readBool", "writeBool",
Christopher Wileyb8e49a42015-10-27 12:55:18 -0700436 "readBoolVector", "writeBoolVector"));
437 // C++11 defines the char16_t type as a built in for Unicode characters.
Christopher Wiley09af4692015-10-30 11:48:25 -0700438 Add(new PrimitiveType(
Christopher Wiley041c8d72016-08-18 13:52:51 -0700439 "char",
Christopher Wiley09af4692015-10-30 11:48:25 -0700440 kNoHeader, "char16_t", "readChar", "writeChar",
Christopher Wileyb8e49a42015-10-27 12:55:18 -0700441 "readCharVector", "writeCharVector"));
Christopher Wileya8a2dc02015-09-28 16:35:31 -0700442
Christopher Wiley041c8d72016-08-18 13:52:51 -0700443 Type* string_array_type = new CppArrayType(
444 ValidatableType::KIND_BUILT_IN, "java.lang", "String",
445 "utils/String16.h", "::android::String16",
Casey Dahlind2bcf2f2017-08-16 14:24:16 -0700446 "::std::unique_ptr<::android::String16>", "readString16Vector",
447 "writeString16Vector", false);
Casey Dahlin57dbe242015-12-04 11:44:02 -0800448
449 Type* nullable_string_type =
Christopher Wiley9ab06232016-01-27 14:55:18 -0800450 new Type(ValidatableType::KIND_BUILT_IN, "java.lang", "String",
Christopher Wileye3946d42016-01-28 15:51:39 -0800451 {"memory", "utils/String16.h"}, "::std::unique_ptr<::android::String16>",
Casey Dahlin57dbe242015-12-04 11:44:02 -0800452 "readString16", "writeString16");
453
Christopher Wiley9ab06232016-01-27 14:55:18 -0800454 string_type_ = new Type(ValidatableType::KIND_BUILT_IN, "java.lang", "String",
Casey Dahlina2f77c42015-12-01 18:26:02 -0800455 {"utils/String16.h"}, "::android::String16",
Christopher Wiley56c9bf32015-10-30 10:41:12 -0700456 "readString16", "writeString16",
Casey Dahlin57dbe242015-12-04 11:44:02 -0800457 string_array_type, nullable_string_type);
Christopher Wiley56c9bf32015-10-30 10:41:12 -0700458 Add(string_type_);
Christopher Wileyad339272015-10-05 19:11:58 -0700459
Christopher Wileyb7e01172016-01-28 16:32:34 -0800460 using ::android::aidl::kAidlReservedTypePackage;
461 using ::android::aidl::kUtf8InCppStringClass;
462
463 // This type is a Utf16 string in the parcel, but deserializes to
464 // a std::string in Utf8 format when we use it in C++.
Christopher Wiley041c8d72016-08-18 13:52:51 -0700465 Type* cpp_utf8_string_array = new CppArrayType(
Christopher Wileyb7e01172016-01-28 16:32:34 -0800466 ValidatableType::KIND_BUILT_IN,
Christopher Wiley041c8d72016-08-18 13:52:51 -0700467 kAidlReservedTypePackage, kUtf8InCppStringClass,
Casey Dahlind2bcf2f2017-08-16 14:24:16 -0700468 "string", "::std::string", "::std::unique_ptr<::std::string>",
Christopher Wileyb7e01172016-01-28 16:32:34 -0800469 "readUtf8VectorFromUtf16Vector", "writeUtf8VectorAsUtf16Vector",
Christopher Wiley041c8d72016-08-18 13:52:51 -0700470 false);
Christopher Wileyb7e01172016-01-28 16:32:34 -0800471 Type* nullable_cpp_utf8_string_type = new Type(
472 ValidatableType::KIND_BUILT_IN,
473 kAidlReservedTypePackage, kUtf8InCppStringClass,
474 {"string", "memory"}, "::std::unique_ptr<::std::string>",
475 "readUtf8FromUtf16", "writeUtf8AsUtf16");
476 Add(new Type(
477 ValidatableType::KIND_BUILT_IN,
478 kAidlReservedTypePackage, kUtf8InCppStringClass,
479 {"string"}, "::std::string", "readUtf8FromUtf16", "writeUtf8AsUtf16",
480 cpp_utf8_string_array, nullable_cpp_utf8_string_type));
481
Christopher Wileyd646e0b2016-03-09 09:24:55 -0800482 Type* nullable_ibinder = new Type(
483 ValidatableType::KIND_BUILT_IN, "android.os", "IBinder",
484 {"binder/IBinder.h"}, "::android::sp<::android::IBinder>",
485 "readNullableStrongBinder", "writeStrongBinder");
486 ibinder_type_ = new Type(
487 ValidatableType::KIND_BUILT_IN, "android.os", "IBinder",
488 {"binder/IBinder.h"}, "::android::sp<::android::IBinder>",
489 "readStrongBinder", "writeStrongBinder",
490 kNoArrayType, nullable_ibinder);
Casey Dahlin7ecd69f2015-11-03 13:52:38 -0800491 Add(ibinder_type_);
492
Robert Quattlebaumcd8c1352017-01-04 13:42:51 -0800493 Add(new MapType());
494
Casey Dahlin7ecd69f2015-11-03 13:52:38 -0800495 Add(new BinderListType());
496 Add(new StringListType());
Christopher Wiley5d9bc932016-02-01 13:23:16 -0800497 Add(new Utf8InCppStringListType());
Casey Dahlin7ecd69f2015-11-03 13:52:38 -0800498
Christopher Wiley041c8d72016-08-18 13:52:51 -0700499 Type* fd_vector_type = new CppArrayType(
500 ValidatableType::KIND_BUILT_IN, kNoPackage, "FileDescriptor",
501 "android-base/unique_fd.h",
Casey Dahlind2bcf2f2017-08-16 14:24:16 -0700502 "::android::base::unique_fd", "::android::base::unique_fd",
Christopher Wiley041c8d72016-08-18 13:52:51 -0700503 "readUniqueFileDescriptorVector", "writeUniqueFileDescriptorVector",
504 false);
Casey Dahlina2f77c42015-12-01 18:26:02 -0800505
Casey Dahlina4ba4b62015-11-02 15:43:30 -0800506 Add(new Type(
507 ValidatableType::KIND_BUILT_IN, kNoPackage, "FileDescriptor",
Christopher Wiley7cb9c252016-04-11 11:07:33 -0700508 {"android-base/unique_fd.h"}, "::android::base::unique_fd",
Casey Dahlina4ba4b62015-11-02 15:43:30 -0800509 "readUniqueFileDescriptor", "writeUniqueFileDescriptor",
Casey Dahlina2f77c42015-12-01 18:26:02 -0800510 fd_vector_type));
Casey Dahlina4ba4b62015-11-02 15:43:30 -0800511
Christopher Wileyad339272015-10-05 19:11:58 -0700512 void_type_ = new class VoidType();
Christopher Wiley09af4692015-10-30 11:48:25 -0700513 Add(void_type_);
Christopher Wileya8a2dc02015-09-28 16:35:31 -0700514}
515
Casey Dahlinc1f39b42015-11-24 10:34:34 -0800516bool TypeNamespace::AddParcelableType(const AidlParcelable& p,
Christopher Wiley9078d722015-11-17 10:23:49 -0800517 const string& filename) {
Christopher Wileye9351cc2016-01-21 15:56:30 -0800518 if (p.GetCppHeader().empty()) {
519 LOG(ERROR) << "Parcelable " << p.GetCanonicalName()
520 << " has no C++ header defined.";
521 return false;
522 }
Casey Dahlinc1f39b42015-11-24 10:34:34 -0800523 Add(new ParcelableType(p, filename));
Casey Dahlin2cc93162015-10-02 16:14:17 -0700524 return true;
Christopher Wileya8a2dc02015-09-28 16:35:31 -0700525}
526
Casey Dahlinc1f39b42015-11-24 10:34:34 -0800527bool TypeNamespace::AddBinderType(const AidlInterface& b,
Christopher Wiley09af4692015-10-30 11:48:25 -0700528 const string& file_name) {
Casey Dahlinc1f39b42015-11-24 10:34:34 -0800529 Add(new BinderType(b, file_name));
Casey Dahlin2cc93162015-10-02 16:14:17 -0700530 return true;
Christopher Wileya8a2dc02015-09-28 16:35:31 -0700531}
532
Christopher Wiley56c9bf32015-10-30 10:41:12 -0700533bool TypeNamespace::AddListType(const std::string& type_name) {
Christopher Wiley9ab06232016-01-27 14:55:18 -0800534 const Type* contained_type = FindTypeByCanonicalName(type_name);
Christopher Wiley56c9bf32015-10-30 10:41:12 -0700535 if (!contained_type) {
536 LOG(ERROR) << "Cannot create List<" << type_name << "> because contained "
537 "type cannot be found or is invalid.";
538 return false;
539 }
540 if (contained_type->IsCppPrimitive()) {
541 LOG(ERROR) << "Cannot create List<" << type_name << "> because contained "
542 "type is a primitive in Java and Java List cannot hold "
543 "primitives.";
544 return false;
545 }
546
Christopher Wiley5d9bc932016-02-01 13:23:16 -0800547 if (contained_type->CanonicalName() == kStringCanonicalName ||
548 contained_type->CanonicalName() == kUtf8InCppStringCanonicalName ||
549 contained_type == IBinderType()) {
Christopher Wiley56c9bf32015-10-30 10:41:12 -0700550 return true;
551 }
Casey Dahlin7ecd69f2015-11-03 13:52:38 -0800552
Christopher Wiley56c9bf32015-10-30 10:41:12 -0700553 // TODO Support lists of parcelables b/23600712
Christopher Wiley56c9bf32015-10-30 10:41:12 -0700554
555 LOG(ERROR) << "aidl-cpp does not yet support List<" << type_name << ">";
Christopher Wileycd8e8972015-10-26 10:24:35 -0700556 return false;
557}
558
559bool TypeNamespace::AddMapType(const std::string& /* key_type_name */,
560 const std::string& /* value_type_name */) {
561 // TODO Support list types b/25242025
562 LOG(ERROR) << "aidl does not implement support for typed maps!";
563 return false;
Christopher Wileya8a2dc02015-09-28 16:35:31 -0700564}
565
Christopher Wileyb656a3b2015-10-16 11:11:09 -0700566bool TypeNamespace::IsValidPackage(const string& package) const {
567 if (package.empty()) {
568 return false;
569 }
570
571 auto pieces = Split(package, ".");
572 for (const string& piece : pieces) {
573 if (is_cpp_keyword(piece)) {
574 return false;
575 }
576 }
577
578 return true;
579}
580
Casey Dahlin57dbe242015-12-04 11:44:02 -0800581const ValidatableType* TypeNamespace::GetArgType(const AidlArgument& a,
582 int arg_index,
Casey Dahlinc5afb402016-03-01 13:54:05 -0800583 const std::string& filename,
584 const AidlInterface& interface) const {
Christopher Wileyb656a3b2015-10-16 11:11:09 -0700585 const string error_prefix = StringPrintf(
586 "In file %s line %d parameter %s (%d):\n ",
587 filename.c_str(), a.GetLine(), a.GetName().c_str(), arg_index);
588
589 // check that the name doesn't match a keyword
590 if (is_cpp_keyword(a.GetName().c_str())) {
591 cerr << error_prefix << "Argument name is a C++ keyword"
592 << endl;
Casey Dahlin57dbe242015-12-04 11:44:02 -0800593 return nullptr;
Christopher Wileyb656a3b2015-10-16 11:11:09 -0700594 }
595
Casey Dahlinc5afb402016-03-01 13:54:05 -0800596 return ::android::aidl::TypeNamespace::GetArgType(a, arg_index, filename,
597 interface);
Christopher Wileyb656a3b2015-10-16 11:11:09 -0700598}
599
Christopher Wileya8a2dc02015-09-28 16:35:31 -0700600} // namespace cpp
601} // namespace aidl
602} // namespace android