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 #pragma once
17
18 #include "aidl_language.h"
19 #include "aidl_to_cpp_common.h"
20
21 namespace android {
22 namespace aidl {
23 namespace ndk {
24
25 enum class StorageMode {
26 STACK,
27 ARGUMENT, // Value for primitives, const& for larger types
28 OUT_ARGUMENT, // Pointer to raw type
29 };
30
31 std::string NdkHeaderFile(const AidlDefinedType& defined_type, cpp::ClassNames name,
32 bool use_os_sep = true);
33
34 std::string ConstantValueDecorator(
35 const AidlTypeSpecifier& type,
36 const std::variant<std::string, std::vector<std::string>>& raw_value);
37
38 // Returns ::aidl::some_package::some_sub_package::foo::IFoo/BpFoo/BnFoo
39 std::string NdkFullClassName(const AidlDefinedType& type, cpp::ClassNames name);
40
41 // Returns the corresponding Ndk type name for an AIDL type spec including
42 // array modifiers.
43 std::string NdkNameOf(const AidlTypenames& types, const AidlTypeSpecifier& aidl, StorageMode mode);
44
45 struct CodeGeneratorContext {
46 CodeWriter& writer;
47
48 const AidlTypenames& types;
49 const AidlTypeSpecifier& type;
50
51 const string parcel;
52 const string var;
53 };
54
55 void WriteToParcelFor(const CodeGeneratorContext& c);
56 void ReadFromParcelFor(const CodeGeneratorContext& c);
57
58 // Returns argument list of a method where each arg is formatted by the fomatter
59 std::string NdkArgList(
60 const AidlTypenames& types, const AidlMethod& method,
61 std::function<std::string(const std::string& type, const std::string& name, bool isOut)>
62 formatter);
63
FormatArgForDecl(const std::string & type,const std::string & name,bool)64 inline std::string FormatArgForDecl(const std::string& type, const std::string& name,
65 bool /*isOut*/) {
66 return type + " " + name;
67 }
68
FormatArgNameUnused(const std::string & type,const std::string & name,bool)69 inline std::string FormatArgNameUnused(const std::string& type, const std::string& name,
70 bool /*isOut*/) {
71 return type + " /*" + name + "*/";
72 }
73
FormatArgForCall(const std::string &,const std::string & name,bool isOut)74 inline std::string FormatArgForCall(const std::string& /*type*/, const std::string& name,
75 bool isOut) {
76 std::string reference_prefix = isOut ? "&" : "";
77 return reference_prefix + name;
78 }
79
FormatArgNameOnly(const std::string &,const std::string & name,bool)80 inline std::string FormatArgNameOnly(const std::string& /*type*/, const std::string& name,
81 bool /*isOut*/) {
82 return name;
83 }
84
85 // -> 'status (class::)name(type name, ...)' for a method
86 std::string NdkMethodDecl(const AidlTypenames& types, const AidlMethod& method,
87 const std::string& clazz = "");
88
89 } // namespace ndk
90 } // namespace aidl
91 } // namespace android
92