blob: fe5043b6c369d7c5d9a38cb9db789e89404803c2 [file] [log] [blame]
Steven Morelandb0057e72018-08-27 01:44:11 -07001/*
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
Jooyung Han3df81ae2020-10-17 17:59:59 +090019#include <functional>
Steven Morelandb0057e72018-08-27 01:44:11 -070020#include <string>
Jooyung Han7a9aceb2019-12-17 14:18:15 +000021#include <type_traits>
Steven Morelandb0057e72018-08-27 01:44:11 -070022
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
28namespace android {
29namespace aidl {
30namespace cpp {
31
Jooyung Hanb8e01b92020-11-01 16:49:13 +090032// provides _call_toString(expr) which call expr.toString() if possible
33extern char kToStringHelper[];
34
Jooyung Han81003c32020-11-04 14:08:26 +090035extern char kTransactionLogStruct[];
36
Steven Morelandb0057e72018-08-27 01:44:11 -070037// These roughly correspond to the various class names in the C++ hierarchy:
38enum class ClassNames {
39 BASE, // Foo (not a real class, but useful in some circumstances).
40 CLIENT, // BpFoo
41 SERVER, // BnFoo
42 INTERFACE, // IFoo
43 DEFAULT_IMPL, // IFooDefault
Jiyong Park5b7e5322019-04-03 20:05:01 +090044 RAW, // (as shown in the file)
Steven Morelandb0057e72018-08-27 01:44:11 -070045};
46
47string ClassName(const AidlDefinedType& defined_type, ClassNames type);
48
49// Generate the relative path to a header file. If |use_os_sep| we'll use the
50// operating system specific path separator rather than C++'s expected '/' when
51// including headers.
52std::string HeaderFile(const AidlDefinedType& defined_type, ClassNames class_type,
53 bool use_os_sep = true);
54
55void EnterNamespace(CodeWriter& out, const AidlDefinedType& defined_type);
56void LeaveNamespace(CodeWriter& out, const AidlDefinedType& defined_type);
57
Steven Moreland3b1325f2018-09-20 14:45:40 -070058string BuildVarName(const AidlArgument& a);
Jeongik Cha37e2ad52019-04-18 13:44:26 +090059const string GenLogBeforeExecute(const string className, const AidlMethod& method, bool isServer,
60 bool isNdk);
61const string GenLogAfterExecute(const string className, const AidlInterface& interface,
62 const AidlMethod& method, const string& statusVarName,
63 const string& returnVarName, bool isServer, bool isNdk);
Jooyung Han7a9aceb2019-12-17 14:18:15 +000064
65template <typename T, typename = std::enable_if_t<std::is_copy_constructible_v<T>>>
66std::vector<T> Append(std::vector<T> as, const std::vector<T>& bs) {
67 as.insert(as.end(), bs.begin(), bs.end());
68 return as;
69}
70
71template <typename T>
72std::vector<T> Append(std::vector<T>&& as, std::vector<T>&& bs) {
73 std::vector<T> appended = std::move(as);
74 std::copy(std::move_iterator(bs.begin()), std::move_iterator(bs.end()),
75 std::back_inserter(appended));
76 return appended;
77}
78
79std::string GenerateEnumValues(const AidlEnumDeclaration& enum_decl,
80 const std::vector<std::string>& enclosing_namespaces_of_enum_decl);
Jooyung Han6beac042020-10-17 21:59:43 +090081std::string TemplateDecl(const AidlParcelable& defined_type);
Jooyung Han7a9aceb2019-12-17 14:18:15 +000082
Jooyung Han3df81ae2020-10-17 17:59:59 +090083void GenerateParcelableComparisonOperators(CodeWriter& out, const AidlParcelable& parcelable);
84
Jooyung Han74b1dd42020-11-01 22:17:16 +090085void GenerateToString(CodeWriter& out, const AidlStructuredParcelable& parcelable);
86void GenerateToString(CodeWriter& out, const AidlUnionDecl& parcelable);
87
Jooyung Han720253d2021-01-05 19:13:17 +090088template <typename Stream, typename Type>
89void GenerateDeprecated(Stream& out, const Type& type) {
90 if (type.IsDeprecated()) {
91 out << " __attribute__((deprecated))";
92 }
93}
94
95template <typename Type>
96std::string GetDeprecatedAttribute(const Type& type) {
97 if (type.IsDeprecated()) {
98 return "__attribute__((deprecated))";
99 }
100 return "";
101}
102
Jooyung Han3df81ae2020-10-17 17:59:59 +0900103struct ParcelWriterContext {
104 string status_type;
105 string status_ok;
106 string status_bad;
107 std::function<void(CodeWriter& out, const std::string& var, const AidlTypeSpecifier& type)>
108 read_func;
109 std::function<void(CodeWriter& out, const std::string& value, const AidlTypeSpecifier& type)>
110 write_func;
111};
112
113struct UnionWriter {
114 const AidlUnionDecl& decl;
115 const AidlTypenames& typenames;
116 const std::function<std::string(const AidlTypeSpecifier&, const AidlTypenames&)> name_of;
117 const ::ConstantValueDecorator& decorator;
118 static const std::vector<std::string> headers;
119
120 void PrivateFields(CodeWriter& out) const;
121 void PublicFields(CodeWriter& out) const;
122 void ReadFromParcel(CodeWriter& out, const ParcelWriterContext&) const;
123 void WriteToParcel(CodeWriter& out, const ParcelWriterContext&) const;
124};
125
Steven Morelandb0057e72018-08-27 01:44:11 -0700126} // namespace cpp
127} // namespace aidl
128} // namespace android