blob: 8a49a56af163d5dc754a8c8067600418c0cfb40c [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
Alice Ryhl2e6d93f2021-09-28 11:57:22 +000045enum class Lifetime {
46 NONE,
47 A,
48};
49
Andrei Homescub62afd92020-05-11 19:24:59 -070050inline bool IsReference(ReferenceMode ref_mode) {
51 switch (ref_mode) {
52 case ReferenceMode::REF:
Andrei Homescub62afd92020-05-11 19:24:59 -070053 case ReferenceMode::MUT_REF:
54 return true;
55
56 default:
57 return false;
58 }
59}
60
Jooyung Hanaeb01672021-11-30 17:29:22 +090061std::string ConstantValueDecorator(
62 const AidlTypeSpecifier& type,
63 const std::variant<std::string, std::vector<std::string>>& raw_value);
Andrei Homescub62afd92020-05-11 19:24:59 -070064
Jooyung Hanaeb01672021-11-30 17:29:22 +090065std::string ConstantValueDecoratorRef(
66 const AidlTypeSpecifier& type,
67 const std::variant<std::string, std::vector<std::string>>& raw_value);
Andrei Homescub62afd92020-05-11 19:24:59 -070068
Jooyung Hane76bcc22022-01-23 22:49:45 +090069std::string ArrayDefaultValue(const AidlTypeSpecifier& type);
70
Alice Ryhl2e6d93f2021-09-28 11:57:22 +000071// Returns "'lifetime_name " including the initial apostrophe and the trailing space.
72// Returns empty string for NONE.
73std::string RustLifetimeName(Lifetime lifetime);
74
75// Returns "<'lifetime_name>" or empty string for NONE.
76std::string RustLifetimeGeneric(Lifetime lifetime);
77
Andrei Homescub62afd92020-05-11 19:24:59 -070078// Returns the Rust type signature of the AIDL type spec
79// This includes generic type parameters with array modifiers.
Alice Ryhl2e6d93f2021-09-28 11:57:22 +000080//
81// The lifetime argument is used to annotate all references.
Andrei Homescub62afd92020-05-11 19:24:59 -070082std::string RustNameOf(const AidlTypeSpecifier& aidl, const AidlTypenames& typenames,
Alice Ryhl2e6d93f2021-09-28 11:57:22 +000083 StorageMode mode, Lifetime lifetime);
Andrei Homescub62afd92020-05-11 19:24:59 -070084
85StorageMode ArgumentStorageMode(const AidlArgument& arg, const AidlTypenames& typenames);
86
87ReferenceMode ArgumentReferenceMode(const AidlArgument& arg, const AidlTypenames& typenames);
88
89std::string TakeReference(ReferenceMode ref_mode, const std::string& name);
90
91bool TypeIsInterface(const AidlTypeSpecifier& type, const AidlTypenames& typenames);
92
Andrei Homescub2316a52021-09-04 04:41:12 +000093bool TypeNeedsOption(const AidlTypeSpecifier& type, const AidlTypenames& typenames);
Andrei Homescub62afd92020-05-11 19:24:59 -070094
95} // namespace rust
96} // namespace aidl
97} // namespace android