• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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   DELEGATOR_IMPL,  // IFooDelegator
43 };
44 
45 string ClassName(const AidlDefinedType& defined_type, ClassNames type);
46 
47 // Return the alignment of known types and enum backing types.
48 // If the alignment is unknown, or it is a FizedSize parcelable with its
49 // own guaranteed alignment(so it does not need to be specified), 0 will be
50 // returned.
51 size_t AlignmentOf(const AidlTypeSpecifier& type, const AidlTypenames& typenames);
52 
53 // Generate the relative path to a header file.  If |use_os_sep| we'll use the
54 // operating system specific path separator rather than C++'s expected '/' when
55 // including headers.
56 std::string HeaderFile(const AidlDefinedType& defined_type, ClassNames class_type,
57                        bool use_os_sep = true);
58 
59 void EnterNamespace(CodeWriter& out, const AidlDefinedType& defined_type);
60 void LeaveNamespace(CodeWriter& out, const AidlDefinedType& defined_type);
61 
62 string BuildVarName(const AidlArgument& a);
63 const string GenLogBeforeExecute(const string className, const AidlMethod& method, bool isServer,
64                                  bool isNdk);
65 const string GenLogAfterExecute(const string className, const AidlInterface& interface,
66                                 const AidlMethod& method, const string& statusVarName,
67                                 const string& returnVarName, bool isServer, bool isNdk);
68 
69 template <typename T, typename = std::enable_if_t<std::is_copy_constructible_v<T>>>
Append(std::vector<T> as,const std::vector<T> & bs)70 std::vector<T> Append(std::vector<T> as, const std::vector<T>& bs) {
71   as.insert(as.end(), bs.begin(), bs.end());
72   return as;
73 }
74 
75 template <typename T>
Append(std::vector<T> && as,std::vector<T> && bs)76 std::vector<T> Append(std::vector<T>&& as, std::vector<T>&& bs) {
77   std::vector<T> appended = std::move(as);
78   std::copy(std::move_iterator(bs.begin()), std::move_iterator(bs.end()),
79             std::back_inserter(appended));
80   return appended;
81 }
82 
83 // Returns Parent1::Parent2::Self. Namespaces are not included.
84 std::string GetQualifiedName(const AidlDefinedType& type, ClassNames name = ClassNames::RAW);
85 
86 void GenerateEnumClassDecl(CodeWriter& out, const AidlEnumDeclaration& enum_decl,
87                            const std::string& backing_type, ::ConstantValueDecorator decorator);
88 std::string GenerateEnumToString(const AidlEnumDeclaration& enum_decl,
89                                  const std::string& backing_type);
90 std::string GenerateEnumValues(const AidlEnumDeclaration& enum_decl,
91                                const std::vector<std::string>& enclosing_namespaces_of_enum_decl);
92 std::string TemplateDecl(const AidlParcelable& defined_type);
93 
94 void GenerateParcelableComparisonOperators(CodeWriter& out, const AidlParcelable& parcelable);
95 
96 void GenerateToString(CodeWriter& out, const AidlStructuredParcelable& parcelable);
97 void GenerateToString(CodeWriter& out, const AidlUnionDecl& parcelable);
98 
99 std::string GetDeprecatedAttribute(const AidlCommentable& type);
100 
101 template <typename Stream>
GenerateDeprecated(Stream & out,const AidlCommentable & type)102 void GenerateDeprecated(Stream& out, const AidlCommentable& type) {
103   if (auto deprecated = GetDeprecatedAttribute(type); !deprecated.empty()) {
104     out << " " + deprecated;
105   }
106 }
107 
108 struct ParcelWriterContext {
109   string status_type;
110   string status_ok;
111   string status_bad;
112   std::function<void(CodeWriter& out, const std::string& var, const AidlTypeSpecifier& type)>
113       read_func;
114   std::function<void(CodeWriter& out, const std::string& value, const AidlTypeSpecifier& type)>
115       write_func;
116 };
117 
118 struct UnionWriter {
119   const AidlUnionDecl& decl;
120   const AidlTypenames& typenames;
121   const std::function<std::string(const AidlTypeSpecifier&, const AidlTypenames&)> name_of;
122   const ::ConstantValueDecorator& decorator;
123 
124   static std::set<std::string> GetHeaders(const AidlUnionDecl&);
125 
126   void PrivateFields(CodeWriter& out) const;
127   void PublicFields(CodeWriter& out) const;
128   void ReadFromParcel(CodeWriter& out, const ParcelWriterContext&) const;
129   void WriteToParcel(CodeWriter& out, const ParcelWriterContext&) const;
130 };
131 
132 std::string CppConstantValueDecorator(
133     const AidlTypeSpecifier& type,
134     const std::variant<std::string, std::vector<std::string>>& raw_value, bool is_ndk);
135 }  // namespace cpp
136 }  // namespace aidl
137 }  // namespace android
138