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 
IsReference(ReferenceMode ref_mode)45 inline bool IsReference(ReferenceMode ref_mode) {
46   switch (ref_mode) {
47     case ReferenceMode::REF:
48     case ReferenceMode::MUT_REF:
49       return true;
50 
51     default:
52       return false;
53   }
54 }
55 
56 std::string ConstantValueDecorator(const AidlTypeSpecifier& type, const std::string& raw_value);
57 
58 std::string ConstantValueDecoratorRef(const AidlTypeSpecifier& type, const std::string& raw_value);
59 
60 // Returns the Rust type signature of the AIDL type spec
61 // This includes generic type parameters with array modifiers.
62 std::string RustNameOf(const AidlTypeSpecifier& aidl, const AidlTypenames& typenames,
63                        StorageMode mode);
64 
65 StorageMode ArgumentStorageMode(const AidlArgument& arg, const AidlTypenames& typenames);
66 
67 ReferenceMode ArgumentReferenceMode(const AidlArgument& arg, const AidlTypenames& typenames);
68 
69 std::string TakeReference(ReferenceMode ref_mode, const std::string& name);
70 
71 bool TypeIsInterface(const AidlTypeSpecifier& type, const AidlTypenames& typenames);
72 
73 bool TypeHasDefault(const AidlTypeSpecifier& type, const AidlTypenames& typenames);
74 
75 }  // namespace rust
76 }  // namespace aidl
77 }  // namespace android
78