blob: be83b018720d77366ab522017412cb1d82cd7b3e [file] [log] [blame]
Andrei Homescub62afd92020-05-11 19:24:59 -07001/*
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
21namespace android {
22namespace aidl {
23namespace rust {
24
25// This header provides functions that translate AIDL things to Rust things.
26
27enum 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 INTERFACE_ARGUMENT, // Interface argument, e.g., dyn IFoo
33 OUT_ARGUMENT, // Mutable reference to write-only raw type
34 INOUT_ARGUMENT, // Mutable reference to inout argument
35 PARCELABLE_FIELD, // Field in a parcelable
36};
37
38enum class ReferenceMode {
39 VALUE,
40 REF,
41 REF_DEREF,
42 MUT_REF,
43 AS_REF,
44 AS_DEREF,
45};
46
47inline bool IsReference(ReferenceMode ref_mode) {
48 switch (ref_mode) {
49 case ReferenceMode::REF:
50 case ReferenceMode::REF_DEREF:
51 case ReferenceMode::MUT_REF:
52 return true;
53
54 default:
55 return false;
56 }
57}
58
59std::string ConstantValueDecorator(const AidlTypeSpecifier& type, const std::string& raw_value);
60
61std::string ConstantValueDecoratorRef(const AidlTypeSpecifier& type, const std::string& raw_value);
62
63// Returns the Rust type signature of the AIDL type spec
64// This includes generic type parameters with array modifiers.
65std::string RustNameOf(const AidlTypeSpecifier& aidl, const AidlTypenames& typenames,
66 StorageMode mode);
67
68StorageMode ArgumentStorageMode(const AidlArgument& arg, const AidlTypenames& typenames);
69
70ReferenceMode ArgumentReferenceMode(const AidlArgument& arg, const AidlTypenames& typenames);
71
72std::string TakeReference(ReferenceMode ref_mode, const std::string& name);
73
74bool TypeIsInterface(const AidlTypeSpecifier& type, const AidlTypenames& typenames);
75
76bool TypeHasDefault(const AidlTypeSpecifier& type, const AidlTypenames& typenames);
77
78} // namespace rust
79} // namespace aidl
80} // namespace android