Christopher Wiley | a8a2dc0 | 2015-09-28 16:35:31 -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 | */ |
| 16 | |
Steven Moreland | 9fccf58 | 2018-08-27 20:36:27 -0700 | [diff] [blame] | 17 | #pragma once |
Christopher Wiley | a8a2dc0 | 2015-09-28 16:35:31 -0700 | [diff] [blame] | 18 | |
| 19 | #include <memory> |
| 20 | #include <string> |
Christopher Wiley | 8a65797 | 2015-10-28 16:03:45 -0700 | [diff] [blame] | 21 | #include <set> |
Christopher Wiley | a8a2dc0 | 2015-09-28 16:35:31 -0700 | [diff] [blame] | 22 | #include <vector> |
| 23 | |
Elliott Hughes | 0a62067 | 2015-12-04 13:53:18 -0800 | [diff] [blame] | 24 | #include <android-base/macros.h> |
Christopher Wiley | a8a2dc0 | 2015-09-28 16:35:31 -0700 | [diff] [blame] | 25 | |
| 26 | #include "type_namespace.h" |
| 27 | |
| 28 | namespace android { |
| 29 | namespace aidl { |
| 30 | namespace cpp { |
| 31 | |
| 32 | class Type : public ValidatableType { |
| 33 | public: |
Christopher Wiley | 09af469 | 2015-10-30 11:48:25 -0700 | [diff] [blame] | 34 | Type(int kind, // from ValidatableType |
| 35 | const std::string& package, |
Casey Dahlin | ce776cf | 2015-10-15 18:45:54 -0700 | [diff] [blame] | 36 | const std::string& aidl_type, |
Casey Dahlin | a2f77c4 | 2015-12-01 18:26:02 -0800 | [diff] [blame] | 37 | const std::vector<std::string>& header, |
Casey Dahlin | b096661 | 2015-10-19 16:35:26 -0700 | [diff] [blame] | 38 | const std::string& cpp_type, |
| 39 | const std::string& read_method, |
| 40 | const std::string& write_method, |
Casey Dahlin | a2f77c4 | 2015-12-01 18:26:02 -0800 | [diff] [blame] | 41 | Type* array_type = nullptr, |
Casey Dahlin | 57dbe24 | 2015-12-04 11:44:02 -0800 | [diff] [blame] | 42 | Type* nullable_type = nullptr, |
Christopher Wiley | 09af469 | 2015-10-30 11:48:25 -0700 | [diff] [blame] | 43 | const std::string& src_file_name = "", |
| 44 | int line = -1); |
Christopher Wiley | a8a2dc0 | 2015-09-28 16:35:31 -0700 | [diff] [blame] | 45 | virtual ~Type() = default; |
| 46 | |
| 47 | // overrides of ValidatableType |
Christopher Wiley | a8a2dc0 | 2015-09-28 16:35:31 -0700 | [diff] [blame] | 48 | bool CanWriteToParcel() const override; |
| 49 | |
Casey Dahlin | a2f77c4 | 2015-12-01 18:26:02 -0800 | [diff] [blame] | 50 | const Type* ArrayType() const override { return array_type_.get(); } |
Casey Dahlin | 57dbe24 | 2015-12-04 11:44:02 -0800 | [diff] [blame] | 51 | const Type* NullableType() const override { return nullable_type_.get(); } |
Casey Dahlin | a2f77c4 | 2015-12-01 18:26:02 -0800 | [diff] [blame] | 52 | std::string CppType() const { return cpp_type_; } |
| 53 | const std::string& ReadFromParcelMethod() const { |
| 54 | return parcel_read_method_; |
| 55 | } |
| 56 | const std::string& WriteToParcelMethod() const { |
| 57 | return parcel_write_method_; |
| 58 | } |
| 59 | |
| 60 | void GetHeaders(std::set<std::string>* headers) const { |
Chih-Hung Hsieh | f05cc26 | 2016-07-27 11:42:51 -0700 | [diff] [blame] | 61 | for (const std::string& header : headers_) { |
Casey Dahlin | a2f77c4 | 2015-12-01 18:26:02 -0800 | [diff] [blame] | 62 | if (!header.empty()) { |
| 63 | headers->insert(header); |
| 64 | } |
| 65 | } |
| 66 | } |
Christopher Wiley | b8e49a4 | 2015-10-27 12:55:18 -0700 | [diff] [blame] | 67 | virtual bool IsCppPrimitive() const { return false; } |
Casey Dahlin | 389781f | 2015-10-22 13:13:21 -0700 | [diff] [blame] | 68 | virtual std::string WriteCast(const std::string& value) const { |
| 69 | return value; |
| 70 | } |
Christopher Wiley | a8a2dc0 | 2015-09-28 16:35:31 -0700 | [diff] [blame] | 71 | |
| 72 | private: |
Casey Dahlin | a2f77c4 | 2015-12-01 18:26:02 -0800 | [diff] [blame] | 73 | // |headers| are the headers we must include to use this type |
| 74 | const std::vector<std::string> headers_; |
Christopher Wiley | a8a2dc0 | 2015-09-28 16:35:31 -0700 | [diff] [blame] | 75 | // |aidl_type| is what we find in the yacc generated AST (e.g. "int"). |
| 76 | const std::string aidl_type_; |
| 77 | // |cpp_type| is what we use in the generated C++ code (e.g. "int32_t"). |
| 78 | const std::string cpp_type_; |
| 79 | const std::string parcel_read_method_; |
| 80 | const std::string parcel_write_method_; |
Casey Dahlin | a2f77c4 | 2015-12-01 18:26:02 -0800 | [diff] [blame] | 81 | |
| 82 | const std::unique_ptr<Type> array_type_; |
Casey Dahlin | 57dbe24 | 2015-12-04 11:44:02 -0800 | [diff] [blame] | 83 | const std::unique_ptr<Type> nullable_type_; |
Christopher Wiley | a8a2dc0 | 2015-09-28 16:35:31 -0700 | [diff] [blame] | 84 | |
| 85 | DISALLOW_COPY_AND_ASSIGN(Type); |
| 86 | }; // class Type |
| 87 | |
Christopher Wiley | 09af469 | 2015-10-30 11:48:25 -0700 | [diff] [blame] | 88 | class TypeNamespace : public ::android::aidl::LanguageTypeNamespace<Type> { |
Christopher Wiley | a8a2dc0 | 2015-09-28 16:35:31 -0700 | [diff] [blame] | 89 | public: |
Christopher Wiley | 5679952 | 2015-10-31 10:17:04 -0700 | [diff] [blame] | 90 | TypeNamespace() = default; |
Christopher Wiley | a8a2dc0 | 2015-09-28 16:35:31 -0700 | [diff] [blame] | 91 | virtual ~TypeNamespace() = default; |
| 92 | |
Christopher Wiley | 5679952 | 2015-10-31 10:17:04 -0700 | [diff] [blame] | 93 | void Init() override; |
Casey Dahlin | c1f39b4 | 2015-11-24 10:34:34 -0800 | [diff] [blame] | 94 | bool AddParcelableType(const AidlParcelable& p, |
Christopher Wiley | a8a2dc0 | 2015-09-28 16:35:31 -0700 | [diff] [blame] | 95 | const std::string& filename) override; |
Casey Dahlin | c1f39b4 | 2015-11-24 10:34:34 -0800 | [diff] [blame] | 96 | bool AddBinderType(const AidlInterface& b, |
Christopher Wiley | a8a2dc0 | 2015-09-28 16:35:31 -0700 | [diff] [blame] | 97 | const std::string& filename) override; |
Christopher Wiley | cd8e897 | 2015-10-26 10:24:35 -0700 | [diff] [blame] | 98 | bool AddListType(const std::string& type_name) override; |
| 99 | bool AddMapType(const std::string& key_type_name, |
| 100 | const std::string& value_type_name) override; |
| 101 | |
Steven Moreland | 5557f1c | 2018-07-02 13:50:23 -0700 | [diff] [blame] | 102 | const ValidatableType* GetArgType(const AidlArgument& a, int arg_index, |
Steven Moreland | 5557f1c | 2018-07-02 13:50:23 -0700 | [diff] [blame] | 103 | const AidlDefinedType& context) const override; |
Christopher Wiley | a8a2dc0 | 2015-09-28 16:35:31 -0700 | [diff] [blame] | 104 | |
Christopher Wiley | ad33927 | 2015-10-05 19:11:58 -0700 | [diff] [blame] | 105 | const Type* VoidType() const { return void_type_; } |
Casey Dahlin | 7ecd69f | 2015-11-03 13:52:38 -0800 | [diff] [blame] | 106 | const Type* IBinderType() const { return ibinder_type_; } |
Christopher Wiley | ad33927 | 2015-10-05 19:11:58 -0700 | [diff] [blame] | 107 | |
Christopher Wiley | a8a2dc0 | 2015-09-28 16:35:31 -0700 | [diff] [blame] | 108 | private: |
George Burgess IV | 2c6d06f | 2019-03-19 17:02:01 -0700 | [diff] [blame] | 109 | const Type* void_type_ = nullptr; |
| 110 | const Type* string_type_ = nullptr; |
| 111 | const Type* ibinder_type_ = nullptr; |
Christopher Wiley | ad33927 | 2015-10-05 19:11:58 -0700 | [diff] [blame] | 112 | |
Christopher Wiley | a8a2dc0 | 2015-09-28 16:35:31 -0700 | [diff] [blame] | 113 | DISALLOW_COPY_AND_ASSIGN(TypeNamespace); |
| 114 | }; // class TypeNamespace |
| 115 | |
| 116 | } // namespace cpp |
| 117 | } // namespace aidl |
| 118 | } // namespace android |