blob: 9dbcadd0ff072f0d4ad4102ab4ca7b26e16323fc [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#include "aidl_to_cpp_common.h"
18
19#include "os.h"
20
21namespace android {
22namespace aidl {
23namespace cpp {
24
25string ClassName(const AidlDefinedType& defined_type, ClassNames type) {
Jiyong Park081b90a2019-04-03 20:05:01 +090026 string base_name = defined_type.GetName();
27 if (base_name.length() >= 2 && base_name[0] == 'I' && isupper(base_name[1])) {
28 base_name = base_name.substr(1);
29 }
Steven Morelandb0057e72018-08-27 01:44:11 -070030
31 switch (type) {
32 case ClassNames::CLIENT:
Jiyong Park081b90a2019-04-03 20:05:01 +090033 return "Bp" + base_name;
Steven Morelandb0057e72018-08-27 01:44:11 -070034 case ClassNames::SERVER:
Jiyong Park081b90a2019-04-03 20:05:01 +090035 return "Bn" + base_name;
Steven Morelandb0057e72018-08-27 01:44:11 -070036 case ClassNames::INTERFACE:
Jiyong Park081b90a2019-04-03 20:05:01 +090037 return "I" + base_name;
Steven Morelandb0057e72018-08-27 01:44:11 -070038 case ClassNames::DEFAULT_IMPL:
Jiyong Park081b90a2019-04-03 20:05:01 +090039 return "I" + base_name + "Default";
Steven Morelandb0057e72018-08-27 01:44:11 -070040 case ClassNames::BASE:
Jiyong Park081b90a2019-04-03 20:05:01 +090041 return base_name;
42 case ClassNames::RAW:
43 [[fallthrough]];
44 default:
45 return defined_type.GetName();
Steven Morelandb0057e72018-08-27 01:44:11 -070046 }
Steven Morelandb0057e72018-08-27 01:44:11 -070047}
48
49std::string HeaderFile(const AidlDefinedType& defined_type, ClassNames class_type,
50 bool use_os_sep) {
51 std::string file_path = defined_type.GetPackage();
52 for (char& c : file_path) {
53 if (c == '.') {
54 c = (use_os_sep) ? OS_PATH_SEPARATOR : '/';
55 }
56 }
57 if (!file_path.empty()) {
58 file_path += (use_os_sep) ? OS_PATH_SEPARATOR : '/';
59 }
60 file_path += ClassName(defined_type, class_type);
61 file_path += ".h";
62
63 return file_path;
64}
65
66void EnterNamespace(CodeWriter& out, const AidlDefinedType& defined_type) {
67 const std::vector<std::string> packages = defined_type.GetSplitPackage();
68 for (const std::string& package : packages) {
69 out << "namespace " << package << " {\n";
70 }
71}
72void LeaveNamespace(CodeWriter& out, const AidlDefinedType& defined_type) {
73 const std::vector<std::string> packages = defined_type.GetSplitPackage();
74 for (auto it = packages.rbegin(); it != packages.rend(); ++it) {
75 out << "} // namespace " << *it << "\n";
76 }
77}
78
Steven Moreland3b1325f2018-09-20 14:45:40 -070079string BuildVarName(const AidlArgument& a) {
80 string prefix = "out_";
81 if (a.GetDirection() & AidlArgument::IN_DIR) {
82 prefix = "in_";
83 }
84 return prefix + a.GetName();
85}
86
Steven Morelandb0057e72018-08-27 01:44:11 -070087} // namespace cpp
88} // namespace aidl
89} // namespace android