blob: f9fe80b1fa25f5851026ae983f98971b0ddfa2fe [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 Han81003c32020-11-04 14:08:26 +090032extern char kTransactionLogStruct[];
33
Steven Morelandb0057e72018-08-27 01:44:11 -070034// These roughly correspond to the various class names in the C++ hierarchy:
35enum 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
Jiyong Park5b7e5322019-04-03 20:05:01 +090041 RAW, // (as shown in the file)
Steven Morelandb0057e72018-08-27 01:44:11 -070042};
43
44string 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.
49std::string HeaderFile(const AidlDefinedType& defined_type, ClassNames class_type,
50 bool use_os_sep = true);
51
52void EnterNamespace(CodeWriter& out, const AidlDefinedType& defined_type);
53void LeaveNamespace(CodeWriter& out, const AidlDefinedType& defined_type);
54
Steven Moreland3b1325f2018-09-20 14:45:40 -070055string BuildVarName(const AidlArgument& a);
Jeongik Cha37e2ad52019-04-18 13:44:26 +090056const string GenLogBeforeExecute(const string className, const AidlMethod& method, bool isServer,
57 bool isNdk);
58const string GenLogAfterExecute(const string className, const AidlInterface& interface,
59 const AidlMethod& method, const string& statusVarName,
60 const string& returnVarName, bool isServer, bool isNdk);
Jooyung Han7a9aceb2019-12-17 14:18:15 +000061
62template <typename T, typename = std::enable_if_t<std::is_copy_constructible_v<T>>>
63std::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
68template <typename T>
69std::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
76std::string GenerateEnumValues(const AidlEnumDeclaration& enum_decl,
77 const std::vector<std::string>& enclosing_namespaces_of_enum_decl);
Jooyung Han6beac042020-10-17 21:59:43 +090078std::string TemplateDecl(const AidlParcelable& defined_type);
Jooyung Han7a9aceb2019-12-17 14:18:15 +000079
Jooyung Han3df81ae2020-10-17 17:59:59 +090080void GenerateParcelableComparisonOperators(CodeWriter& out, const AidlParcelable& parcelable);
81
Jooyung Han74b1dd42020-11-01 22:17:16 +090082void GenerateToString(CodeWriter& out, const AidlStructuredParcelable& parcelable);
83void GenerateToString(CodeWriter& out, const AidlUnionDecl& parcelable);
84
Jooyung Hand4fe00e2021-01-11 16:21:53 +090085std::string GetDeprecatedAttribute(const AidlCommentable& type);
Jooyung Han720253d2021-01-05 19:13:17 +090086
Jooyung Hand4fe00e2021-01-11 16:21:53 +090087template <typename Stream>
88void GenerateDeprecated(Stream& out, const AidlCommentable& type) {
89 if (auto deprecated = GetDeprecatedAttribute(type); !deprecated.empty()) {
90 out << " " + deprecated;
Jooyung Han720253d2021-01-05 19:13:17 +090091 }
Jooyung Han720253d2021-01-05 19:13:17 +090092}
93
Jooyung Han3df81ae2020-10-17 17:59:59 +090094struct 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
104struct 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
Steven Morelandb0057e72018-08-27 01:44:11 -0700117} // namespace cpp
118} // namespace aidl
119} // namespace android