blob: 292e4dfc7cc0e2a0d6434a87c267c77d88c87bf1 [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
Andrei Homescub62afd92020-05-11 19:24:59 -070032 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
37enum class ReferenceMode {
38 VALUE,
39 REF,
Andrei Homescub62afd92020-05-11 19:24:59 -070040 MUT_REF,
41 AS_REF,
42 AS_DEREF,
43};
44
45inline bool IsReference(ReferenceMode ref_mode) {
46 switch (ref_mode) {
47 case ReferenceMode::REF:
Andrei Homescub62afd92020-05-11 19:24:59 -070048 case ReferenceMode::MUT_REF:
49 return true;
50
51 default:
52 return false;
53 }
54}
55
56std::string ConstantValueDecorator(const AidlTypeSpecifier& type, const std::string& raw_value);
57
58std::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.
62std::string RustNameOf(const AidlTypeSpecifier& aidl, const AidlTypenames& typenames,
63 StorageMode mode);
64
65StorageMode ArgumentStorageMode(const AidlArgument& arg, const AidlTypenames& typenames);
66
67ReferenceMode ArgumentReferenceMode(const AidlArgument& arg, const AidlTypenames& typenames);
68
69std::string TakeReference(ReferenceMode ref_mode, const std::string& name);
70
71bool TypeIsInterface(const AidlTypeSpecifier& type, const AidlTypenames& typenames);
72
73bool TypeHasDefault(const AidlTypeSpecifier& type, const AidlTypenames& typenames);
74
75} // namespace rust
76} // namespace aidl
77} // namespace android