type_cpp.cpp revision c1f39b4ea30e7313eeb7f3d3fd5ec66ba00f2bb5
1a8a2dc0ad258c1390c5d55b302774df357450cafChristopher Wiley/*
2a8a2dc0ad258c1390c5d55b302774df357450cafChristopher Wiley * Copyright (C) 2015, The Android Open Source Project
3a8a2dc0ad258c1390c5d55b302774df357450cafChristopher Wiley *
4a8a2dc0ad258c1390c5d55b302774df357450cafChristopher Wiley * Licensed under the Apache License, Version 2.0 (the "License");
5a8a2dc0ad258c1390c5d55b302774df357450cafChristopher Wiley * you may not use this file except in compliance with the License.
6a8a2dc0ad258c1390c5d55b302774df357450cafChristopher Wiley * You may obtain a copy of the License at
7a8a2dc0ad258c1390c5d55b302774df357450cafChristopher Wiley *
8a8a2dc0ad258c1390c5d55b302774df357450cafChristopher Wiley *     http://www.apache.org/licenses/LICENSE-2.0
9a8a2dc0ad258c1390c5d55b302774df357450cafChristopher Wiley *
10a8a2dc0ad258c1390c5d55b302774df357450cafChristopher Wiley * Unless required by applicable law or agreed to in writing, software
11a8a2dc0ad258c1390c5d55b302774df357450cafChristopher Wiley * distributed under the License is distributed on an "AS IS" BASIS,
12a8a2dc0ad258c1390c5d55b302774df357450cafChristopher Wiley * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13a8a2dc0ad258c1390c5d55b302774df357450cafChristopher Wiley * See the License for the specific language governing permissions and
14a8a2dc0ad258c1390c5d55b302774df357450cafChristopher Wiley * limitations under the License.
15a8a2dc0ad258c1390c5d55b302774df357450cafChristopher Wiley */
16a8a2dc0ad258c1390c5d55b302774df357450cafChristopher Wiley
17a8a2dc0ad258c1390c5d55b302774df357450cafChristopher Wiley#include "type_cpp.h"
18a8a2dc0ad258c1390c5d55b302774df357450cafChristopher Wiley
19b656a3b1954bbe4b548a3c11274c833bbca35935Christopher Wiley#include <algorithm>
20b656a3b1954bbe4b548a3c11274c833bbca35935Christopher Wiley#include <iostream>
21389781f56bb6b4363e9eb03acf915ba297f607d5Casey Dahlin#include <vector>
22b656a3b1954bbe4b548a3c11274c833bbca35935Christopher Wiley
23b656a3b1954bbe4b548a3c11274c833bbca35935Christopher Wiley#include <base/stringprintf.h>
24b656a3b1954bbe4b548a3c11274c833bbca35935Christopher Wiley#include <base/strings.h>
25b656a3b1954bbe4b548a3c11274c833bbca35935Christopher Wiley
26a8a2dc0ad258c1390c5d55b302774df357450cafChristopher Wiley#include "logging.h"
27a8a2dc0ad258c1390c5d55b302774df357450cafChristopher Wiley
28b656a3b1954bbe4b548a3c11274c833bbca35935Christopher Wileyusing std::cerr;
29b656a3b1954bbe4b548a3c11274c833bbca35935Christopher Wileyusing std::endl;
308a6579721e9390d8ed2cb49e0146a025194799f6Christopher Wileyusing std::set;
31a8a2dc0ad258c1390c5d55b302774df357450cafChristopher Wileyusing std::string;
32a8a2dc0ad258c1390c5d55b302774df357450cafChristopher Wileyusing std::unique_ptr;
33389781f56bb6b4363e9eb03acf915ba297f607d5Casey Dahlinusing std::vector;
34a8a2dc0ad258c1390c5d55b302774df357450cafChristopher Wiley
35b656a3b1954bbe4b548a3c11274c833bbca35935Christopher Wileyusing android::base::Split;
36389781f56bb6b4363e9eb03acf915ba297f607d5Casey Dahlinusing android::base::Join;
37b656a3b1954bbe4b548a3c11274c833bbca35935Christopher Wileyusing android::base::StringPrintf;
38b656a3b1954bbe4b548a3c11274c833bbca35935Christopher Wiley
39a8a2dc0ad258c1390c5d55b302774df357450cafChristopher Wileynamespace android {
40a8a2dc0ad258c1390c5d55b302774df357450cafChristopher Wileynamespace aidl {
41a8a2dc0ad258c1390c5d55b302774df357450cafChristopher Wileynamespace cpp {
42ad3392747003a30928da6cd206e41f66398c2062Christopher Wileynamespace {
43ad3392747003a30928da6cd206e41f66398c2062Christopher Wiley
4409af469476ea6d187b52f22113d3b168a887100cChristopher Wileyconst char kNoPackage[] = "";
4509af469476ea6d187b52f22113d3b168a887100cChristopher Wileyconst char kNoHeader[] = "";
4609af469476ea6d187b52f22113d3b168a887100cChristopher Wileyconst char kNoValidMethod[] = "";
4709af469476ea6d187b52f22113d3b168a887100cChristopher Wiley
48b656a3b1954bbe4b548a3c11274c833bbca35935Christopher Wileybool is_cpp_keyword(const std::string& str) {
49b656a3b1954bbe4b548a3c11274c833bbca35935Christopher Wiley  static const std::vector<std::string> kCppKeywords{
50b656a3b1954bbe4b548a3c11274c833bbca35935Christopher Wiley    "alignas", "alignof", "and", "and_eq", "asm", "auto", "bitand", "bitor",
51b656a3b1954bbe4b548a3c11274c833bbca35935Christopher Wiley    "bool", "break", "case", "catch", "char", "char16_t", "char32_t", "class",
52b656a3b1954bbe4b548a3c11274c833bbca35935Christopher Wiley    "compl", "concept", "const", "constexpr", "const_cast", "continue",
53b656a3b1954bbe4b548a3c11274c833bbca35935Christopher Wiley    "decltype", "default", "delete", "do", "double", "dynamic_cast", "else",
54b656a3b1954bbe4b548a3c11274c833bbca35935Christopher Wiley    "enum", "explicit", "export", "extern", "false", "float", "for", "friend",
55b656a3b1954bbe4b548a3c11274c833bbca35935Christopher Wiley    "goto", "if", "inline", "int", "long", "mutable", "namespace", "new",
56b656a3b1954bbe4b548a3c11274c833bbca35935Christopher Wiley    "noexcept", "not", "not_eq", "nullptr", "operator", "or", "or_eq",
57b656a3b1954bbe4b548a3c11274c833bbca35935Christopher Wiley    "private", "protected", "public", "register", "reinterpret_cast",
58b656a3b1954bbe4b548a3c11274c833bbca35935Christopher Wiley    "requires", "return", "short", "signed", "sizeof", "static",
59b656a3b1954bbe4b548a3c11274c833bbca35935Christopher Wiley    "static_assert", "static_cast", "struct", "switch", "template", "this",
60b656a3b1954bbe4b548a3c11274c833bbca35935Christopher Wiley    "thread_local", "throw", "true", "try", "typedef", "typeid", "typename",
61b656a3b1954bbe4b548a3c11274c833bbca35935Christopher Wiley    "union", "unsigned", "using", "virtual", "void", "volatile", "wchar_t",
62b656a3b1954bbe4b548a3c11274c833bbca35935Christopher Wiley    "while", "xor", "xor_eq",
63b656a3b1954bbe4b548a3c11274c833bbca35935Christopher Wiley  };
64b656a3b1954bbe4b548a3c11274c833bbca35935Christopher Wiley  return std::find(kCppKeywords.begin(), kCppKeywords.end(), str) !=
65b656a3b1954bbe4b548a3c11274c833bbca35935Christopher Wiley      kCppKeywords.end();
66b656a3b1954bbe4b548a3c11274c833bbca35935Christopher Wiley}
67b656a3b1954bbe4b548a3c11274c833bbca35935Christopher Wiley
68ad3392747003a30928da6cd206e41f66398c2062Christopher Wileyclass VoidType : public Type {
69ad3392747003a30928da6cd206e41f66398c2062Christopher Wiley public:
7009af469476ea6d187b52f22113d3b168a887100cChristopher Wiley  VoidType() : Type(ValidatableType::KIND_BUILT_IN, kNoPackage, "void",
7109af469476ea6d187b52f22113d3b168a887100cChristopher Wiley                    kNoHeader, "void", kNoValidMethod, kNoValidMethod) {}
72ad3392747003a30928da6cd206e41f66398c2062Christopher Wiley  virtual ~VoidType() = default;
73ad3392747003a30928da6cd206e41f66398c2062Christopher Wiley  bool CanBeOutParameter() const override { return false; }
74ad3392747003a30928da6cd206e41f66398c2062Christopher Wiley  bool CanWriteToParcel() const override { return false; }
75ad3392747003a30928da6cd206e41f66398c2062Christopher Wiley};  // class VoidType
76ad3392747003a30928da6cd206e41f66398c2062Christopher Wiley
77389781f56bb6b4363e9eb03acf915ba297f607d5Casey Dahlinclass BinderType : public Type {
78389781f56bb6b4363e9eb03acf915ba297f607d5Casey Dahlin public:
7909af469476ea6d187b52f22113d3b168a887100cChristopher Wiley  BinderType(const AidlInterface& interface, const std::string& src_file_name)
8009af469476ea6d187b52f22113d3b168a887100cChristopher Wiley      : Type(ValidatableType::KIND_GENERATED,
8109af469476ea6d187b52f22113d3b168a887100cChristopher Wiley             interface.GetPackage(), interface.GetName(),
8209af469476ea6d187b52f22113d3b168a887100cChristopher Wiley             GetCppHeader(interface), GetCppName(interface),
8309af469476ea6d187b52f22113d3b168a887100cChristopher Wiley             "readStrongBinder", "writeStrongBinder",
8409af469476ea6d187b52f22113d3b168a887100cChristopher Wiley             kNoValidMethod, kNoValidMethod,
8509af469476ea6d187b52f22113d3b168a887100cChristopher Wiley             src_file_name, interface.GetLine()) {}
86389781f56bb6b4363e9eb03acf915ba297f607d5Casey Dahlin  virtual ~BinderType() = default;
87389781f56bb6b4363e9eb03acf915ba297f607d5Casey Dahlin
88389781f56bb6b4363e9eb03acf915ba297f607d5Casey Dahlin  string WriteCast(const string& val) const override {
8909af469476ea6d187b52f22113d3b168a887100cChristopher Wiley    return Name() + "::asBinder(" + val + ")";
90389781f56bb6b4363e9eb03acf915ba297f607d5Casey Dahlin  }
91389781f56bb6b4363e9eb03acf915ba297f607d5Casey Dahlin
92389781f56bb6b4363e9eb03acf915ba297f607d5Casey Dahlin private:
93389781f56bb6b4363e9eb03acf915ba297f607d5Casey Dahlin  static string GetCppName(const AidlInterface& interface) {
94389781f56bb6b4363e9eb03acf915ba297f607d5Casey Dahlin    vector<string> name = interface.GetSplitPackage();
95389781f56bb6b4363e9eb03acf915ba297f607d5Casey Dahlin    string ret = "android::sp<";
96389781f56bb6b4363e9eb03acf915ba297f607d5Casey Dahlin
97389781f56bb6b4363e9eb03acf915ba297f607d5Casey Dahlin    name.push_back(interface.GetName());
98389781f56bb6b4363e9eb03acf915ba297f607d5Casey Dahlin
99389781f56bb6b4363e9eb03acf915ba297f607d5Casey Dahlin    for (const auto& term : name) {
100389781f56bb6b4363e9eb03acf915ba297f607d5Casey Dahlin      ret += "::" + term;
101389781f56bb6b4363e9eb03acf915ba297f607d5Casey Dahlin    }
102389781f56bb6b4363e9eb03acf915ba297f607d5Casey Dahlin
103389781f56bb6b4363e9eb03acf915ba297f607d5Casey Dahlin    return ret + ">";
104389781f56bb6b4363e9eb03acf915ba297f607d5Casey Dahlin  }
105389781f56bb6b4363e9eb03acf915ba297f607d5Casey Dahlin
106389781f56bb6b4363e9eb03acf915ba297f607d5Casey Dahlin  static string GetCppHeader(const AidlInterface& interface) {
107389781f56bb6b4363e9eb03acf915ba297f607d5Casey Dahlin    vector<string> name = interface.GetSplitPackage();
108389781f56bb6b4363e9eb03acf915ba297f607d5Casey Dahlin    name.push_back(interface.GetName());
109389781f56bb6b4363e9eb03acf915ba297f607d5Casey Dahlin    return Join(name, '/') + ".h";
110389781f56bb6b4363e9eb03acf915ba297f607d5Casey Dahlin  }
111389781f56bb6b4363e9eb03acf915ba297f607d5Casey Dahlin};
112389781f56bb6b4363e9eb03acf915ba297f607d5Casey Dahlin
1139078d724c0e9017e2a9bf8defb954d5620da1e8aChristopher Wileyclass ParcelableType : public Type {
1149078d724c0e9017e2a9bf8defb954d5620da1e8aChristopher Wiley public:
1159078d724c0e9017e2a9bf8defb954d5620da1e8aChristopher Wiley  ParcelableType(const AidlParcelable& parcelable,
1169078d724c0e9017e2a9bf8defb954d5620da1e8aChristopher Wiley                 const std::string& src_file_name)
1179078d724c0e9017e2a9bf8defb954d5620da1e8aChristopher Wiley      : Type(ValidatableType::KIND_PARCELABLE,
1189078d724c0e9017e2a9bf8defb954d5620da1e8aChristopher Wiley             parcelable.GetPackage(), parcelable.GetName(),
1199078d724c0e9017e2a9bf8defb954d5620da1e8aChristopher Wiley             parcelable.GetCppHeader(), GetCppName(parcelable),
1209078d724c0e9017e2a9bf8defb954d5620da1e8aChristopher Wiley             "readParcelable", "writeParcelable",
1219078d724c0e9017e2a9bf8defb954d5620da1e8aChristopher Wiley             "readParcelableVector", "writeParcelableVector",
1229078d724c0e9017e2a9bf8defb954d5620da1e8aChristopher Wiley             src_file_name, parcelable.GetLine()) {}
1239078d724c0e9017e2a9bf8defb954d5620da1e8aChristopher Wiley  virtual ~ParcelableType() = default;
1249078d724c0e9017e2a9bf8defb954d5620da1e8aChristopher Wiley  bool CanBeOutParameter() const override { return true; }
1259078d724c0e9017e2a9bf8defb954d5620da1e8aChristopher Wiley
1269078d724c0e9017e2a9bf8defb954d5620da1e8aChristopher Wiley private:
1279078d724c0e9017e2a9bf8defb954d5620da1e8aChristopher Wiley  static string GetCppName(const AidlParcelable& parcelable) {
1289078d724c0e9017e2a9bf8defb954d5620da1e8aChristopher Wiley    return Join(parcelable.GetSplitPackage(), "::") +
1299078d724c0e9017e2a9bf8defb954d5620da1e8aChristopher Wiley        "::" + parcelable.GetName();
1309078d724c0e9017e2a9bf8defb954d5620da1e8aChristopher Wiley  }
1319078d724c0e9017e2a9bf8defb954d5620da1e8aChristopher Wiley};
1329078d724c0e9017e2a9bf8defb954d5620da1e8aChristopher Wiley
13356c9bf3c4fb731d5557427667f13f963fa5ef0f7Christopher Wileyclass StringListType : public Type {
13456c9bf3c4fb731d5557427667f13f963fa5ef0f7Christopher Wiley public:
13556c9bf3c4fb731d5557427667f13f963fa5ef0f7Christopher Wiley  StringListType()
13656c9bf3c4fb731d5557427667f13f963fa5ef0f7Christopher Wiley      : Type(ValidatableType::KIND_BUILT_IN, "java.util", "List<String>",
13756c9bf3c4fb731d5557427667f13f963fa5ef0f7Christopher Wiley            "utils/String16.h", "std::vector<android::String16>",
13856c9bf3c4fb731d5557427667f13f963fa5ef0f7Christopher Wiley             "readString16Vector", "writeString16Vector") {}
13956c9bf3c4fb731d5557427667f13f963fa5ef0f7Christopher Wiley  virtual ~StringListType() = default;
14056c9bf3c4fb731d5557427667f13f963fa5ef0f7Christopher Wiley  bool CanBeOutParameter() const override { return true; }
14156c9bf3c4fb731d5557427667f13f963fa5ef0f7Christopher Wiley
142b7014328e383c1e45a1687499eef09decd68d044Christopher Wiley  void GetHeaders(bool is_array, set<string>* headers) const override {
14356c9bf3c4fb731d5557427667f13f963fa5ef0f7Christopher Wiley    if (is_array) {
14456c9bf3c4fb731d5557427667f13f963fa5ef0f7Christopher Wiley      LOG(FATAL) << "Type checking did not catch that List<String> "
14556c9bf3c4fb731d5557427667f13f963fa5ef0f7Christopher Wiley                    "was marked as array";
14656c9bf3c4fb731d5557427667f13f963fa5ef0f7Christopher Wiley    }
14756c9bf3c4fb731d5557427667f13f963fa5ef0f7Christopher Wiley    Type::GetHeaders(is_array, headers);
14856c9bf3c4fb731d5557427667f13f963fa5ef0f7Christopher Wiley    headers->insert("vector");
14956c9bf3c4fb731d5557427667f13f963fa5ef0f7Christopher Wiley  }
15056c9bf3c4fb731d5557427667f13f963fa5ef0f7Christopher Wiley
15156c9bf3c4fb731d5557427667f13f963fa5ef0f7Christopher Wiley private:
15256c9bf3c4fb731d5557427667f13f963fa5ef0f7Christopher Wiley  DISALLOW_COPY_AND_ASSIGN(StringListType);
15356c9bf3c4fb731d5557427667f13f963fa5ef0f7Christopher Wiley};  // class StringListType
15456c9bf3c4fb731d5557427667f13f963fa5ef0f7Christopher Wiley
1557ecd69f2cc3c31cf569840e9c2d53ed352328d18Casey Dahlinclass BinderListType : public Type {
1567ecd69f2cc3c31cf569840e9c2d53ed352328d18Casey Dahlin public:
1577ecd69f2cc3c31cf569840e9c2d53ed352328d18Casey Dahlin  BinderListType()
1587ecd69f2cc3c31cf569840e9c2d53ed352328d18Casey Dahlin      : Type(ValidatableType::KIND_BUILT_IN, "java.util",
1597ecd69f2cc3c31cf569840e9c2d53ed352328d18Casey Dahlin             "List<android.os.IBinder>", "binder/IBinder.h",
1607ecd69f2cc3c31cf569840e9c2d53ed352328d18Casey Dahlin             "std::vector<android::sp<::android::IBinder>>",
1617ecd69f2cc3c31cf569840e9c2d53ed352328d18Casey Dahlin             "readStrongBinderVector", "writeStrongBinderVector") {}
1627ecd69f2cc3c31cf569840e9c2d53ed352328d18Casey Dahlin  virtual ~BinderListType() = default;
1637ecd69f2cc3c31cf569840e9c2d53ed352328d18Casey Dahlin  bool CanBeOutParameter() const override { return true; }
1647ecd69f2cc3c31cf569840e9c2d53ed352328d18Casey Dahlin
165b7014328e383c1e45a1687499eef09decd68d044Christopher Wiley  void GetHeaders(bool is_array, set<string>* headers) const override {
1667ecd69f2cc3c31cf569840e9c2d53ed352328d18Casey Dahlin    if (is_array) {
1677ecd69f2cc3c31cf569840e9c2d53ed352328d18Casey Dahlin      LOG(FATAL) << "Type checking did not catch that List<Binder> "
1687ecd69f2cc3c31cf569840e9c2d53ed352328d18Casey Dahlin                    "was marked as array";
1697ecd69f2cc3c31cf569840e9c2d53ed352328d18Casey Dahlin    }
1707ecd69f2cc3c31cf569840e9c2d53ed352328d18Casey Dahlin    Type::GetHeaders(is_array, headers);
1717ecd69f2cc3c31cf569840e9c2d53ed352328d18Casey Dahlin    headers->insert("vector");
1727ecd69f2cc3c31cf569840e9c2d53ed352328d18Casey Dahlin  }
1737ecd69f2cc3c31cf569840e9c2d53ed352328d18Casey Dahlin
1747ecd69f2cc3c31cf569840e9c2d53ed352328d18Casey Dahlin private:
1757ecd69f2cc3c31cf569840e9c2d53ed352328d18Casey Dahlin  DISALLOW_COPY_AND_ASSIGN(BinderListType);
1767ecd69f2cc3c31cf569840e9c2d53ed352328d18Casey Dahlin};  // class BinderListType
1777ecd69f2cc3c31cf569840e9c2d53ed352328d18Casey Dahlin
178ad3392747003a30928da6cd206e41f66398c2062Christopher Wiley}  // namespace
179a8a2dc0ad258c1390c5d55b302774df357450cafChristopher Wiley
18009af469476ea6d187b52f22113d3b168a887100cChristopher WileyType::Type(int kind,
18109af469476ea6d187b52f22113d3b168a887100cChristopher Wiley           const std::string& package,
18209af469476ea6d187b52f22113d3b168a887100cChristopher Wiley           const std::string& aidl_type,
18309af469476ea6d187b52f22113d3b168a887100cChristopher Wiley           const string& header,
184b0966619850093e117104dc34d8a6ae4c56ea3ffCasey Dahlin           const string& cpp_type,
185b0966619850093e117104dc34d8a6ae4c56ea3ffCasey Dahlin           const string& read_method,
186b0966619850093e117104dc34d8a6ae4c56ea3ffCasey Dahlin           const string& write_method,
187b0966619850093e117104dc34d8a6ae4c56ea3ffCasey Dahlin           const string& read_array_method,
18809af469476ea6d187b52f22113d3b168a887100cChristopher Wiley           const string& write_array_method,
18909af469476ea6d187b52f22113d3b168a887100cChristopher Wiley           const string& src_file_name,
19009af469476ea6d187b52f22113d3b168a887100cChristopher Wiley           int line)
19109af469476ea6d187b52f22113d3b168a887100cChristopher Wiley    : ValidatableType(kind, package, aidl_type, src_file_name, line),
19209af469476ea6d187b52f22113d3b168a887100cChristopher Wiley      header_(header),
193ce776cfc9560ee541464a23fb64ecd74e442b96fCasey Dahlin      aidl_type_(aidl_type),
194a8a2dc0ad258c1390c5d55b302774df357450cafChristopher Wiley      cpp_type_(cpp_type),
195a8a2dc0ad258c1390c5d55b302774df357450cafChristopher Wiley      parcel_read_method_(read_method),
196b0966619850093e117104dc34d8a6ae4c56ea3ffCasey Dahlin      parcel_write_method_(write_method),
197b0966619850093e117104dc34d8a6ae4c56ea3ffCasey Dahlin      parcel_read_array_method_(read_array_method),
198b0966619850093e117104dc34d8a6ae4c56ea3ffCasey Dahlin      parcel_write_array_method_(write_array_method) {}
199a8a2dc0ad258c1390c5d55b302774df357450cafChristopher Wiley
200b0966619850093e117104dc34d8a6ae4c56ea3ffCasey Dahlinbool Type::CanBeArray() const { return ! parcel_read_array_method_.empty(); }
2012cc93169679e222dda71c73814f28a9fb0832166Casey Dahlinbool Type::CanWriteToParcel() const { return true; }
2028a6579721e9390d8ed2cb49e0146a025194799f6Christopher Wileyvoid Type::GetHeaders(bool is_array, set<string>* headers) const {
2038a6579721e9390d8ed2cb49e0146a025194799f6Christopher Wiley  if (!header_.empty()) {
2048a6579721e9390d8ed2cb49e0146a025194799f6Christopher Wiley    headers->insert(header_);
2058a6579721e9390d8ed2cb49e0146a025194799f6Christopher Wiley  }
2068a6579721e9390d8ed2cb49e0146a025194799f6Christopher Wiley  if (is_array) {
2078a6579721e9390d8ed2cb49e0146a025194799f6Christopher Wiley    headers->insert("vector");
2088a6579721e9390d8ed2cb49e0146a025194799f6Christopher Wiley  }
2098a6579721e9390d8ed2cb49e0146a025194799f6Christopher Wiley}
210b0966619850093e117104dc34d8a6ae4c56ea3ffCasey Dahlin
211b0966619850093e117104dc34d8a6ae4c56ea3ffCasey Dahlinstring Type::CppType(bool is_array) const {
212b0966619850093e117104dc34d8a6ae4c56ea3ffCasey Dahlin  if (is_array) {
213b0966619850093e117104dc34d8a6ae4c56ea3ffCasey Dahlin    return "std::vector<" + cpp_type_ + ">";
214b0966619850093e117104dc34d8a6ae4c56ea3ffCasey Dahlin  } else {
215b0966619850093e117104dc34d8a6ae4c56ea3ffCasey Dahlin    return cpp_type_;
216b0966619850093e117104dc34d8a6ae4c56ea3ffCasey Dahlin  }
217b0966619850093e117104dc34d8a6ae4c56ea3ffCasey Dahlin}
218b0966619850093e117104dc34d8a6ae4c56ea3ffCasey Dahlin
219b0966619850093e117104dc34d8a6ae4c56ea3ffCasey Dahlinconst string& Type::ReadFromParcelMethod(bool is_array) const {
220b0966619850093e117104dc34d8a6ae4c56ea3ffCasey Dahlin  if (is_array) {
221b0966619850093e117104dc34d8a6ae4c56ea3ffCasey Dahlin    return parcel_read_array_method_;
222b0966619850093e117104dc34d8a6ae4c56ea3ffCasey Dahlin  } else {
223b0966619850093e117104dc34d8a6ae4c56ea3ffCasey Dahlin    return parcel_read_method_;
224b0966619850093e117104dc34d8a6ae4c56ea3ffCasey Dahlin  }
225b0966619850093e117104dc34d8a6ae4c56ea3ffCasey Dahlin}
226b0966619850093e117104dc34d8a6ae4c56ea3ffCasey Dahlin
227b0966619850093e117104dc34d8a6ae4c56ea3ffCasey Dahlinconst string& Type::WriteToParcelMethod(bool is_array) const {
228b0966619850093e117104dc34d8a6ae4c56ea3ffCasey Dahlin  if (is_array) {
229b0966619850093e117104dc34d8a6ae4c56ea3ffCasey Dahlin    return parcel_write_array_method_;
230b0966619850093e117104dc34d8a6ae4c56ea3ffCasey Dahlin  } else {
231b0966619850093e117104dc34d8a6ae4c56ea3ffCasey Dahlin    return parcel_write_method_;
232b0966619850093e117104dc34d8a6ae4c56ea3ffCasey Dahlin  }
233b0966619850093e117104dc34d8a6ae4c56ea3ffCasey Dahlin}
234a8a2dc0ad258c1390c5d55b302774df357450cafChristopher Wiley
2355679952c38925a54b75b087068887002e57f3174Christopher Wileyvoid TypeNamespace::Init() {
23609af469476ea6d187b52f22113d3b168a887100cChristopher Wiley  Add(new PrimitiveType(
23709af469476ea6d187b52f22113d3b168a887100cChristopher Wiley      ValidatableType::KIND_BUILT_IN, kNoPackage, "byte",
23809af469476ea6d187b52f22113d3b168a887100cChristopher Wiley      "cstdint", "int8_t", "readByte", "writeByte",
239b8e49a407e8648afb28ca9bad4bf8434224f0b18Christopher Wiley      "readByteVector", "writeByteVector"));
24009af469476ea6d187b52f22113d3b168a887100cChristopher Wiley  Add(new PrimitiveType(
24109af469476ea6d187b52f22113d3b168a887100cChristopher Wiley      ValidatableType::KIND_BUILT_IN, kNoPackage, "int",
24209af469476ea6d187b52f22113d3b168a887100cChristopher Wiley      "cstdint", "int32_t", "readInt32", "writeInt32",
243b8e49a407e8648afb28ca9bad4bf8434224f0b18Christopher Wiley      "readInt32Vector", "writeInt32Vector"));
24409af469476ea6d187b52f22113d3b168a887100cChristopher Wiley  Add(new PrimitiveType(
24509af469476ea6d187b52f22113d3b168a887100cChristopher Wiley      ValidatableType::KIND_BUILT_IN, kNoPackage, "long",
24609af469476ea6d187b52f22113d3b168a887100cChristopher Wiley      "cstdint", "int64_t", "readInt64", "writeInt64",
247b8e49a407e8648afb28ca9bad4bf8434224f0b18Christopher Wiley      "readInt64Vector", "writeInt64Vector"));
24809af469476ea6d187b52f22113d3b168a887100cChristopher Wiley  Add(new PrimitiveType(
24909af469476ea6d187b52f22113d3b168a887100cChristopher Wiley      ValidatableType::KIND_BUILT_IN, kNoPackage, "float",
25009af469476ea6d187b52f22113d3b168a887100cChristopher Wiley      kNoHeader, "float", "readFloat", "writeFloat",
251b8e49a407e8648afb28ca9bad4bf8434224f0b18Christopher Wiley      "readFloatVector", "writeFloatVector"));
25209af469476ea6d187b52f22113d3b168a887100cChristopher Wiley  Add(new PrimitiveType(
25309af469476ea6d187b52f22113d3b168a887100cChristopher Wiley      ValidatableType::KIND_BUILT_IN, kNoPackage, "double",
25409af469476ea6d187b52f22113d3b168a887100cChristopher Wiley      kNoHeader, "double", "readDouble", "writeDouble",
255b8e49a407e8648afb28ca9bad4bf8434224f0b18Christopher Wiley      "readDoubleVector", "writeDoubleVector"));
25609af469476ea6d187b52f22113d3b168a887100cChristopher Wiley  Add(new PrimitiveType(
25709af469476ea6d187b52f22113d3b168a887100cChristopher Wiley      ValidatableType::KIND_BUILT_IN, kNoPackage, "boolean",
25809af469476ea6d187b52f22113d3b168a887100cChristopher Wiley      kNoHeader, "bool", "readBool", "writeBool",
259b8e49a407e8648afb28ca9bad4bf8434224f0b18Christopher Wiley      "readBoolVector", "writeBoolVector"));
260b8e49a407e8648afb28ca9bad4bf8434224f0b18Christopher Wiley  // C++11 defines the char16_t type as a built in for Unicode characters.
26109af469476ea6d187b52f22113d3b168a887100cChristopher Wiley  Add(new PrimitiveType(
26209af469476ea6d187b52f22113d3b168a887100cChristopher Wiley      ValidatableType::KIND_BUILT_IN, kNoPackage, "char",
26309af469476ea6d187b52f22113d3b168a887100cChristopher Wiley      kNoHeader, "char16_t", "readChar", "writeChar",
264b8e49a407e8648afb28ca9bad4bf8434224f0b18Christopher Wiley      "readCharVector", "writeCharVector"));
265a8a2dc0ad258c1390c5d55b302774df357450cafChristopher Wiley
26656c9bf3c4fb731d5557427667f13f963fa5ef0f7Christopher Wiley  string_type_ = new Type(ValidatableType::KIND_BUILT_IN, kNoPackage, "String",
26756c9bf3c4fb731d5557427667f13f963fa5ef0f7Christopher Wiley                          "utils/String16.h", "android::String16",
26856c9bf3c4fb731d5557427667f13f963fa5ef0f7Christopher Wiley                          "readString16", "writeString16",
26956c9bf3c4fb731d5557427667f13f963fa5ef0f7Christopher Wiley                          "readString16Vector", "writeString16Vector");
27056c9bf3c4fb731d5557427667f13f963fa5ef0f7Christopher Wiley  Add(string_type_);
271ad3392747003a30928da6cd206e41f66398c2062Christopher Wiley
2727ecd69f2cc3c31cf569840e9c2d53ed352328d18Casey Dahlin  ibinder_type_ = new Type(ValidatableType::KIND_BUILT_IN, "android.os",
2737ecd69f2cc3c31cf569840e9c2d53ed352328d18Casey Dahlin                           "IBinder", "binder/IBinder.h",
2747ecd69f2cc3c31cf569840e9c2d53ed352328d18Casey Dahlin                           "sp<android::IBinder>", "readStrongBinder",
2757ecd69f2cc3c31cf569840e9c2d53ed352328d18Casey Dahlin                           "writeStrongBinder");
2767ecd69f2cc3c31cf569840e9c2d53ed352328d18Casey Dahlin  Add(ibinder_type_);
2777ecd69f2cc3c31cf569840e9c2d53ed352328d18Casey Dahlin
2787ecd69f2cc3c31cf569840e9c2d53ed352328d18Casey Dahlin  Add(new BinderListType());
2797ecd69f2cc3c31cf569840e9c2d53ed352328d18Casey Dahlin  Add(new StringListType());
2807ecd69f2cc3c31cf569840e9c2d53ed352328d18Casey Dahlin
281ad3392747003a30928da6cd206e41f66398c2062Christopher Wiley  void_type_ = new class VoidType();
28209af469476ea6d187b52f22113d3b168a887100cChristopher Wiley  Add(void_type_);
283a8a2dc0ad258c1390c5d55b302774df357450cafChristopher Wiley}
284a8a2dc0ad258c1390c5d55b302774df357450cafChristopher Wiley
285c1f39b4ea30e7313eeb7f3d3fd5ec66ba00f2bb5Casey Dahlinbool TypeNamespace::AddParcelableType(const AidlParcelable& p,
2869078d724c0e9017e2a9bf8defb954d5620da1e8aChristopher Wiley                                      const string& filename) {
287c1f39b4ea30e7313eeb7f3d3fd5ec66ba00f2bb5Casey Dahlin  Add(new ParcelableType(p, filename));
2882cc93169679e222dda71c73814f28a9fb0832166Casey Dahlin  return true;
289a8a2dc0ad258c1390c5d55b302774df357450cafChristopher Wiley}
290a8a2dc0ad258c1390c5d55b302774df357450cafChristopher Wiley
291c1f39b4ea30e7313eeb7f3d3fd5ec66ba00f2bb5Casey Dahlinbool TypeNamespace::AddBinderType(const AidlInterface& b,
29209af469476ea6d187b52f22113d3b168a887100cChristopher Wiley                                  const string& file_name) {
293c1f39b4ea30e7313eeb7f3d3fd5ec66ba00f2bb5Casey Dahlin  Add(new BinderType(b, file_name));
2942cc93169679e222dda71c73814f28a9fb0832166Casey Dahlin  return true;
295a8a2dc0ad258c1390c5d55b302774df357450cafChristopher Wiley}
296a8a2dc0ad258c1390c5d55b302774df357450cafChristopher Wiley
29756c9bf3c4fb731d5557427667f13f963fa5ef0f7Christopher Wileybool TypeNamespace::AddListType(const std::string& type_name) {
29856c9bf3c4fb731d5557427667f13f963fa5ef0f7Christopher Wiley  const Type* contained_type = Find(type_name);
29956c9bf3c4fb731d5557427667f13f963fa5ef0f7Christopher Wiley  if (!contained_type) {
30056c9bf3c4fb731d5557427667f13f963fa5ef0f7Christopher Wiley    LOG(ERROR) << "Cannot create List<" << type_name << "> because contained "
30156c9bf3c4fb731d5557427667f13f963fa5ef0f7Christopher Wiley                  "type cannot be found or is invalid.";
30256c9bf3c4fb731d5557427667f13f963fa5ef0f7Christopher Wiley    return false;
30356c9bf3c4fb731d5557427667f13f963fa5ef0f7Christopher Wiley  }
30456c9bf3c4fb731d5557427667f13f963fa5ef0f7Christopher Wiley  if (contained_type->IsCppPrimitive()) {
30556c9bf3c4fb731d5557427667f13f963fa5ef0f7Christopher Wiley    LOG(ERROR) << "Cannot create List<" << type_name << "> because contained "
30656c9bf3c4fb731d5557427667f13f963fa5ef0f7Christopher Wiley                  "type is a primitive in Java and Java List cannot hold "
30756c9bf3c4fb731d5557427667f13f963fa5ef0f7Christopher Wiley                  "primitives.";
30856c9bf3c4fb731d5557427667f13f963fa5ef0f7Christopher Wiley    return false;
30956c9bf3c4fb731d5557427667f13f963fa5ef0f7Christopher Wiley  }
31056c9bf3c4fb731d5557427667f13f963fa5ef0f7Christopher Wiley
3117ecd69f2cc3c31cf569840e9c2d53ed352328d18Casey Dahlin  if (contained_type == StringType() || contained_type == IBinderType()) {
31256c9bf3c4fb731d5557427667f13f963fa5ef0f7Christopher Wiley    return true;
31356c9bf3c4fb731d5557427667f13f963fa5ef0f7Christopher Wiley  }
3147ecd69f2cc3c31cf569840e9c2d53ed352328d18Casey Dahlin
31556c9bf3c4fb731d5557427667f13f963fa5ef0f7Christopher Wiley  // TODO Support lists of parcelables b/23600712
31656c9bf3c4fb731d5557427667f13f963fa5ef0f7Christopher Wiley
31756c9bf3c4fb731d5557427667f13f963fa5ef0f7Christopher Wiley  LOG(ERROR) << "aidl-cpp does not yet support List<" << type_name << ">";
318cd8e89795c331e3e5c75e92d7c7804f389804e08Christopher Wiley  return false;
319cd8e89795c331e3e5c75e92d7c7804f389804e08Christopher Wiley}
320cd8e89795c331e3e5c75e92d7c7804f389804e08Christopher Wiley
321cd8e89795c331e3e5c75e92d7c7804f389804e08Christopher Wileybool TypeNamespace::AddMapType(const std::string& /* key_type_name */,
322cd8e89795c331e3e5c75e92d7c7804f389804e08Christopher Wiley                               const std::string& /* value_type_name */) {
323cd8e89795c331e3e5c75e92d7c7804f389804e08Christopher Wiley  // TODO Support list types b/25242025
324cd8e89795c331e3e5c75e92d7c7804f389804e08Christopher Wiley  LOG(ERROR) << "aidl does not implement support for typed maps!";
325cd8e89795c331e3e5c75e92d7c7804f389804e08Christopher Wiley  return false;
326a8a2dc0ad258c1390c5d55b302774df357450cafChristopher Wiley}
327a8a2dc0ad258c1390c5d55b302774df357450cafChristopher Wiley
328b656a3b1954bbe4b548a3c11274c833bbca35935Christopher Wileybool TypeNamespace::IsValidPackage(const string& package) const {
329b656a3b1954bbe4b548a3c11274c833bbca35935Christopher Wiley  if (package.empty()) {
330b656a3b1954bbe4b548a3c11274c833bbca35935Christopher Wiley    return false;
331b656a3b1954bbe4b548a3c11274c833bbca35935Christopher Wiley  }
332b656a3b1954bbe4b548a3c11274c833bbca35935Christopher Wiley
333b656a3b1954bbe4b548a3c11274c833bbca35935Christopher Wiley  auto pieces = Split(package, ".");
334b656a3b1954bbe4b548a3c11274c833bbca35935Christopher Wiley  for (const string& piece : pieces) {
335b656a3b1954bbe4b548a3c11274c833bbca35935Christopher Wiley    if (is_cpp_keyword(piece)) {
336b656a3b1954bbe4b548a3c11274c833bbca35935Christopher Wiley      return false;
337b656a3b1954bbe4b548a3c11274c833bbca35935Christopher Wiley    }
338b656a3b1954bbe4b548a3c11274c833bbca35935Christopher Wiley  }
339b656a3b1954bbe4b548a3c11274c833bbca35935Christopher Wiley
340b656a3b1954bbe4b548a3c11274c833bbca35935Christopher Wiley  return true;
341b656a3b1954bbe4b548a3c11274c833bbca35935Christopher Wiley}
342b656a3b1954bbe4b548a3c11274c833bbca35935Christopher Wiley
343b656a3b1954bbe4b548a3c11274c833bbca35935Christopher Wileybool TypeNamespace::IsValidArg(const AidlArgument& a,
344b656a3b1954bbe4b548a3c11274c833bbca35935Christopher Wiley                               int arg_index,
345b656a3b1954bbe4b548a3c11274c833bbca35935Christopher Wiley                               const std::string& filename) const {
346b656a3b1954bbe4b548a3c11274c833bbca35935Christopher Wiley  if (!::android::aidl::TypeNamespace::IsValidArg(a, arg_index, filename)) {
347b656a3b1954bbe4b548a3c11274c833bbca35935Christopher Wiley    return false;
348b656a3b1954bbe4b548a3c11274c833bbca35935Christopher Wiley  }
349b656a3b1954bbe4b548a3c11274c833bbca35935Christopher Wiley  const string error_prefix = StringPrintf(
350b656a3b1954bbe4b548a3c11274c833bbca35935Christopher Wiley      "In file %s line %d parameter %s (%d):\n    ",
351b656a3b1954bbe4b548a3c11274c833bbca35935Christopher Wiley      filename.c_str(), a.GetLine(), a.GetName().c_str(), arg_index);
352b656a3b1954bbe4b548a3c11274c833bbca35935Christopher Wiley
353b656a3b1954bbe4b548a3c11274c833bbca35935Christopher Wiley  // check that the name doesn't match a keyword
354b656a3b1954bbe4b548a3c11274c833bbca35935Christopher Wiley  if (is_cpp_keyword(a.GetName().c_str())) {
355b656a3b1954bbe4b548a3c11274c833bbca35935Christopher Wiley    cerr << error_prefix << "Argument name is a C++ keyword"
356b656a3b1954bbe4b548a3c11274c833bbca35935Christopher Wiley         << endl;
357b656a3b1954bbe4b548a3c11274c833bbca35935Christopher Wiley    return false;
358b656a3b1954bbe4b548a3c11274c833bbca35935Christopher Wiley  }
359b656a3b1954bbe4b548a3c11274c833bbca35935Christopher Wiley
360b656a3b1954bbe4b548a3c11274c833bbca35935Christopher Wiley  return true;
361b656a3b1954bbe4b548a3c11274c833bbca35935Christopher Wiley}
362b656a3b1954bbe4b548a3c11274c833bbca35935Christopher Wiley
363a8a2dc0ad258c1390c5d55b302774df357450cafChristopher Wiley}  // namespace cpp
364a8a2dc0ad258c1390c5d55b302774df357450cafChristopher Wiley}  // namespace aidl
365a8a2dc0ad258c1390c5d55b302774df357450cafChristopher Wiley}  // namespace android
366