1 /*
2 * Copyright (C) 2018, 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 #pragma once
18
19 #include <functional>
20 #include <string>
21 #include <type_traits>
22
23 #include "aidl_language.h"
24
25 // This is used to help generate code targetting C++ (the language) whether using the libbinder or
26 // libbinder_ndk backend.
27
28 namespace android {
29 namespace aidl {
30 namespace cpp {
31
32 extern char kTransactionLogStruct[];
33
34 // These roughly correspond to the various class names in the C++ hierarchy:
35 enum class ClassNames {
36 BASE, // Foo (not a real class, but useful in some circumstances).
37 CLIENT, // BpFoo
38 SERVER, // BnFoo
39 INTERFACE, // IFoo
40 DEFAULT_IMPL, // IFooDefault
41 RAW, // (as shown in the file)
42 };
43
44 string ClassName(const AidlDefinedType& defined_type, ClassNames type);
45
46 // Generate the relative path to a header file. If |use_os_sep| we'll use the
47 // operating system specific path separator rather than C++'s expected '/' when
48 // including headers.
49 std::string HeaderFile(const AidlDefinedType& defined_type, ClassNames class_type,
50 bool use_os_sep = true);
51
52 void EnterNamespace(CodeWriter& out, const AidlDefinedType& defined_type);
53 void LeaveNamespace(CodeWriter& out, const AidlDefinedType& defined_type);
54
55 string BuildVarName(const AidlArgument& a);
56 const string GenLogBeforeExecute(const string className, const AidlMethod& method, bool isServer,
57 bool isNdk);
58 const string GenLogAfterExecute(const string className, const AidlInterface& interface,
59 const AidlMethod& method, const string& statusVarName,
60 const string& returnVarName, bool isServer, bool isNdk);
61
62 template <typename T, typename = std::enable_if_t<std::is_copy_constructible_v<T>>>
Append(std::vector<T> as,const std::vector<T> & bs)63 std::vector<T> Append(std::vector<T> as, const std::vector<T>& bs) {
64 as.insert(as.end(), bs.begin(), bs.end());
65 return as;
66 }
67
68 template <typename T>
Append(std::vector<T> && as,std::vector<T> && bs)69 std::vector<T> Append(std::vector<T>&& as, std::vector<T>&& bs) {
70 std::vector<T> appended = std::move(as);
71 std::copy(std::move_iterator(bs.begin()), std::move_iterator(bs.end()),
72 std::back_inserter(appended));
73 return appended;
74 }
75
76 std::string GenerateEnumValues(const AidlEnumDeclaration& enum_decl,
77 const std::vector<std::string>& enclosing_namespaces_of_enum_decl);
78 std::string TemplateDecl(const AidlParcelable& defined_type);
79
80 void GenerateParcelableComparisonOperators(CodeWriter& out, const AidlParcelable& parcelable);
81
82 void GenerateToString(CodeWriter& out, const AidlStructuredParcelable& parcelable);
83 void GenerateToString(CodeWriter& out, const AidlUnionDecl& parcelable);
84
85 std::string GetDeprecatedAttribute(const AidlCommentable& type);
86
87 template <typename Stream>
GenerateDeprecated(Stream & out,const AidlCommentable & type)88 void GenerateDeprecated(Stream& out, const AidlCommentable& type) {
89 if (auto deprecated = GetDeprecatedAttribute(type); !deprecated.empty()) {
90 out << " " + deprecated;
91 }
92 }
93
94 struct ParcelWriterContext {
95 string status_type;
96 string status_ok;
97 string status_bad;
98 std::function<void(CodeWriter& out, const std::string& var, const AidlTypeSpecifier& type)>
99 read_func;
100 std::function<void(CodeWriter& out, const std::string& value, const AidlTypeSpecifier& type)>
101 write_func;
102 };
103
104 struct UnionWriter {
105 const AidlUnionDecl& decl;
106 const AidlTypenames& typenames;
107 const std::function<std::string(const AidlTypeSpecifier&, const AidlTypenames&)> name_of;
108 const ::ConstantValueDecorator& decorator;
109 static const std::vector<std::string> headers;
110
111 void PrivateFields(CodeWriter& out) const;
112 void PublicFields(CodeWriter& out) const;
113 void ReadFromParcel(CodeWriter& out, const ParcelWriterContext&) const;
114 void WriteToParcel(CodeWriter& out, const ParcelWriterContext&) const;
115 };
116
117 } // namespace cpp
118 } // namespace aidl
119 } // namespace android
120