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 FRESH,
49 };
50
IsReference(ReferenceMode ref_mode)51 inline bool IsReference(ReferenceMode ref_mode) {
52 switch (ref_mode) {
53 case ReferenceMode::REF:
54 case ReferenceMode::MUT_REF:
55 return true;
56
57 default:
58 return false;
59 }
60 }
61
62 std::string ConstantValueDecorator(
63 const AidlTypeSpecifier& type,
64 const std::variant<std::string, std::vector<std::string>>& raw_value);
65
66 std::string ConstantValueDecoratorRef(
67 const AidlTypeSpecifier& type,
68 const std::variant<std::string, std::vector<std::string>>& raw_value);
69
70 std::string ArrayDefaultValue(const AidlTypeSpecifier& type);
71
72 // Returns "'lifetime_name " including the initial apostrophe and the trailing space.
73 // Returns empty string for NONE.
74 //
75 // Adds the lifetime to `lifetimes` if it is not already present.
76 std::string RustLifetimeName(Lifetime lifetime, std::vector<std::string>& lifetimes);
77
78 // Returns the Rust type signature of the AIDL type spec
79 // This includes generic type parameters with array modifiers.
80 std::string RustNameOf(const AidlTypeSpecifier& aidl, const AidlTypenames& typenames,
81 StorageMode mode, bool is_vintf_stability);
82
83 // Returns the Rust type signature of the AIDL type spec
84 // This includes generic type parameters with array modifiers.
85 //
86 // The lifetime argument is used to annotate all references. If a new lifetime is generated then it
87 // is added to `lifetimes`.
88 std::string RustNameOf(const AidlTypeSpecifier& aidl, const AidlTypenames& typenames,
89 StorageMode mode, Lifetime lifetime, bool is_vintf_stability,
90 std::vector<std::string>& lifetimes);
91
92 StorageMode ArgumentStorageMode(const AidlArgument& arg, const AidlTypenames& typenames);
93
94 ReferenceMode ArgumentReferenceMode(const AidlArgument& arg, const AidlTypenames& typenames);
95
96 std::string TakeReference(ReferenceMode ref_mode, const std::string& name);
97
98 bool TypeIsInterface(const AidlTypeSpecifier& type, const AidlTypenames& typenames);
99
100 bool TypeNeedsOption(const AidlTypeSpecifier& type, const AidlTypenames& typenames);
101
102 } // namespace rust
103 } // namespace aidl
104 } // namespace android
105