blob: a27132b937cad09eca03e70fbd6755359af8c61a [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
Steven Moreland9fccf582018-08-27 20:36:27 -070017#pragma once
Christopher Wileya8a2dc02015-09-28 16:35:31 -070018
19#include <memory>
20#include <string>
Christopher Wiley8a657972015-10-28 16:03:45 -070021#include <set>
Christopher Wileya8a2dc02015-09-28 16:35:31 -070022#include <vector>
23
Elliott Hughes0a620672015-12-04 13:53:18 -080024#include <android-base/macros.h>
Christopher Wileya8a2dc02015-09-28 16:35:31 -070025
26#include "type_namespace.h"
27
28namespace android {
29namespace aidl {
30namespace cpp {
31
32class Type : public ValidatableType {
33 public:
Christopher Wiley09af4692015-10-30 11:48:25 -070034 Type(int kind, // from ValidatableType
35 const std::string& package,
Casey Dahlince776cf2015-10-15 18:45:54 -070036 const std::string& aidl_type,
Casey Dahlina2f77c42015-12-01 18:26:02 -080037 const std::vector<std::string>& header,
Casey Dahlinb0966612015-10-19 16:35:26 -070038 const std::string& cpp_type,
39 const std::string& read_method,
40 const std::string& write_method,
Casey Dahlina2f77c42015-12-01 18:26:02 -080041 Type* array_type = nullptr,
Casey Dahlin57dbe242015-12-04 11:44:02 -080042 Type* nullable_type = nullptr,
Christopher Wiley09af4692015-10-30 11:48:25 -070043 const std::string& src_file_name = "",
44 int line = -1);
Christopher Wileya8a2dc02015-09-28 16:35:31 -070045 virtual ~Type() = default;
46
47 // overrides of ValidatableType
Christopher Wileya8a2dc02015-09-28 16:35:31 -070048 bool CanWriteToParcel() const override;
49
Casey Dahlina2f77c42015-12-01 18:26:02 -080050 const Type* ArrayType() const override { return array_type_.get(); }
Casey Dahlin57dbe242015-12-04 11:44:02 -080051 const Type* NullableType() const override { return nullable_type_.get(); }
Casey Dahlina2f77c42015-12-01 18:26:02 -080052 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 Hsiehf05cc262016-07-27 11:42:51 -070061 for (const std::string& header : headers_) {
Casey Dahlina2f77c42015-12-01 18:26:02 -080062 if (!header.empty()) {
63 headers->insert(header);
64 }
65 }
66 }
Christopher Wileyb8e49a42015-10-27 12:55:18 -070067 virtual bool IsCppPrimitive() const { return false; }
Casey Dahlin389781f2015-10-22 13:13:21 -070068 virtual std::string WriteCast(const std::string& value) const {
69 return value;
70 }
Christopher Wileya8a2dc02015-09-28 16:35:31 -070071
72 private:
Casey Dahlina2f77c42015-12-01 18:26:02 -080073 // |headers| are the headers we must include to use this type
74 const std::vector<std::string> headers_;
Christopher Wileya8a2dc02015-09-28 16:35:31 -070075 // |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 Dahlina2f77c42015-12-01 18:26:02 -080081
82 const std::unique_ptr<Type> array_type_;
Casey Dahlin57dbe242015-12-04 11:44:02 -080083 const std::unique_ptr<Type> nullable_type_;
Christopher Wileya8a2dc02015-09-28 16:35:31 -070084
85 DISALLOW_COPY_AND_ASSIGN(Type);
86}; // class Type
87
Christopher Wiley09af4692015-10-30 11:48:25 -070088class TypeNamespace : public ::android::aidl::LanguageTypeNamespace<Type> {
Christopher Wileya8a2dc02015-09-28 16:35:31 -070089 public:
Christopher Wiley56799522015-10-31 10:17:04 -070090 TypeNamespace() = default;
Christopher Wileya8a2dc02015-09-28 16:35:31 -070091 virtual ~TypeNamespace() = default;
92
Christopher Wiley56799522015-10-31 10:17:04 -070093 void Init() override;
Casey Dahlinc1f39b42015-11-24 10:34:34 -080094 bool AddParcelableType(const AidlParcelable& p,
Christopher Wileya8a2dc02015-09-28 16:35:31 -070095 const std::string& filename) override;
Casey Dahlinc1f39b42015-11-24 10:34:34 -080096 bool AddBinderType(const AidlInterface& b,
Christopher Wileya8a2dc02015-09-28 16:35:31 -070097 const std::string& filename) override;
Christopher Wileycd8e8972015-10-26 10:24:35 -070098 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 Moreland5557f1c2018-07-02 13:50:23 -0700102 const ValidatableType* GetArgType(const AidlArgument& a, int arg_index,
Steven Moreland5557f1c2018-07-02 13:50:23 -0700103 const AidlDefinedType& context) const override;
Christopher Wileya8a2dc02015-09-28 16:35:31 -0700104
Christopher Wileyad339272015-10-05 19:11:58 -0700105 const Type* VoidType() const { return void_type_; }
Casey Dahlin7ecd69f2015-11-03 13:52:38 -0800106 const Type* IBinderType() const { return ibinder_type_; }
Christopher Wileyad339272015-10-05 19:11:58 -0700107
Christopher Wileya8a2dc02015-09-28 16:35:31 -0700108 private:
George Burgess IV2c6d06f2019-03-19 17:02:01 -0700109 const Type* void_type_ = nullptr;
110 const Type* string_type_ = nullptr;
111 const Type* ibinder_type_ = nullptr;
Christopher Wileyad339272015-10-05 19:11:58 -0700112
Christopher Wileya8a2dc02015-09-28 16:35:31 -0700113 DISALLOW_COPY_AND_ASSIGN(TypeNamespace);
114}; // class TypeNamespace
115
116} // namespace cpp
117} // namespace aidl
118} // namespace android