1 /*
2 * Copyright (C) 2020, 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_typenames.h"
20
21 namespace android {
22 namespace aidl {
23 namespace rust {
24
25 // This header provides functions that translate AIDL things to Rust things.
26
27 enum class StorageMode {
28 VALUE,
29 DEFAULT_VALUE, // Value that implements Default::default()
30 IN_ARGUMENT, // Value for primitives, & for larger types
31 UNSIZED_ARGUMENT, // Unsized input argument, e.g., str/slice
32 OUT_ARGUMENT, // Mutable reference to write-only raw type
33 INOUT_ARGUMENT, // Mutable reference to inout argument
34 PARCELABLE_FIELD, // Field in a parcelable
35 };
36
37 enum class ReferenceMode {
38 VALUE,
39 REF,
40 MUT_REF,
41 AS_REF,
42 AS_DEREF,
43 };
44
45 enum class Lifetime {
46 NONE,
47 A,
48 };
49
IsReference(ReferenceMode ref_mode)50 inline bool IsReference(ReferenceMode ref_mode) {
51 switch (ref_mode) {
52 case ReferenceMode::REF:
53 case ReferenceMode::MUT_REF:
54 return true;
55
56 default:
57 return false;
58 }
59 }
60
61 std::string ConstantValueDecorator(
62 const AidlTypeSpecifier& type,
63 const std::variant<std::string, std::vector<std::string>>& raw_value);
64
65 std::string ConstantValueDecoratorRef(
66 const AidlTypeSpecifier& type,
67 const std::variant<std::string, std::vector<std::string>>& raw_value);
68
69 std::string ArrayDefaultValue(const AidlTypeSpecifier& type);
70
71 // Returns "'lifetime_name " including the initial apostrophe and the trailing space.
72 // Returns empty string for NONE.
73 std::string RustLifetimeName(Lifetime lifetime);
74
75 // Returns "<'lifetime_name>" or empty string for NONE.
76 std::string RustLifetimeGeneric(Lifetime lifetime);
77
78 // Returns the Rust type signature of the AIDL type spec
79 // This includes generic type parameters with array modifiers.
80 //
81 // The lifetime argument is used to annotate all references.
82 std::string RustNameOf(const AidlTypeSpecifier& aidl, const AidlTypenames& typenames,
83 StorageMode mode, Lifetime lifetime);
84
85 StorageMode ArgumentStorageMode(const AidlArgument& arg, const AidlTypenames& typenames);
86
87 ReferenceMode ArgumentReferenceMode(const AidlArgument& arg, const AidlTypenames& typenames);
88
89 std::string TakeReference(ReferenceMode ref_mode, const std::string& name);
90
91 bool TypeIsInterface(const AidlTypeSpecifier& type, const AidlTypenames& typenames);
92
93 bool TypeNeedsOption(const AidlTypeSpecifier& type, const AidlTypenames& typenames);
94
95 } // namespace rust
96 } // namespace aidl
97 } // namespace android
98