Andrei Homescu | b62afd9 | 2020-05-11 19:24:59 -0700 | [diff] [blame] | 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 |
Andrei Homescu | b62afd9 | 2020-05-11 19:24:59 -0700 | [diff] [blame] | 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, |
Andrei Homescu | b62afd9 | 2020-05-11 19:24:59 -0700 | [diff] [blame] | 40 | MUT_REF, |
| 41 | AS_REF, |
| 42 | AS_DEREF, |
| 43 | }; |
| 44 | |
Alice Ryhl | 2e6d93f | 2021-09-28 11:57:22 +0000 | [diff] [blame] | 45 | enum class Lifetime { |
| 46 | NONE, |
| 47 | A, |
| 48 | }; |
| 49 | |
Andrei Homescu | b62afd9 | 2020-05-11 19:24:59 -0700 | [diff] [blame] | 50 | inline bool IsReference(ReferenceMode ref_mode) { |
| 51 | switch (ref_mode) { |
| 52 | case ReferenceMode::REF: |
Andrei Homescu | b62afd9 | 2020-05-11 19:24:59 -0700 | [diff] [blame] | 53 | case ReferenceMode::MUT_REF: |
| 54 | return true; |
| 55 | |
| 56 | default: |
| 57 | return false; |
| 58 | } |
| 59 | } |
| 60 | |
Jooyung Han | aeb0167 | 2021-11-30 17:29:22 +0900 | [diff] [blame] | 61 | std::string ConstantValueDecorator( |
| 62 | const AidlTypeSpecifier& type, |
| 63 | const std::variant<std::string, std::vector<std::string>>& raw_value); |
Andrei Homescu | b62afd9 | 2020-05-11 19:24:59 -0700 | [diff] [blame] | 64 | |
Jooyung Han | aeb0167 | 2021-11-30 17:29:22 +0900 | [diff] [blame] | 65 | std::string ConstantValueDecoratorRef( |
| 66 | const AidlTypeSpecifier& type, |
| 67 | const std::variant<std::string, std::vector<std::string>>& raw_value); |
Andrei Homescu | b62afd9 | 2020-05-11 19:24:59 -0700 | [diff] [blame] | 68 | |
Jooyung Han | e76bcc2 | 2022-01-23 22:49:45 +0900 | [diff] [blame] | 69 | std::string ArrayDefaultValue(const AidlTypeSpecifier& type); |
| 70 | |
Alice Ryhl | 2e6d93f | 2021-09-28 11:57:22 +0000 | [diff] [blame] | 71 | // Returns "'lifetime_name " including the initial apostrophe and the trailing space. |
| 72 | // Returns empty string for NONE. |
| 73 | std::string RustLifetimeName(Lifetime lifetime); |
| 74 | |
| 75 | // Returns "<'lifetime_name>" or empty string for NONE. |
| 76 | std::string RustLifetimeGeneric(Lifetime lifetime); |
| 77 | |
Andrei Homescu | b62afd9 | 2020-05-11 19:24:59 -0700 | [diff] [blame] | 78 | // Returns the Rust type signature of the AIDL type spec |
| 79 | // This includes generic type parameters with array modifiers. |
Alice Ryhl | 2e6d93f | 2021-09-28 11:57:22 +0000 | [diff] [blame] | 80 | // |
| 81 | // The lifetime argument is used to annotate all references. |
Andrei Homescu | b62afd9 | 2020-05-11 19:24:59 -0700 | [diff] [blame] | 82 | std::string RustNameOf(const AidlTypeSpecifier& aidl, const AidlTypenames& typenames, |
Alice Ryhl | 2e6d93f | 2021-09-28 11:57:22 +0000 | [diff] [blame] | 83 | StorageMode mode, Lifetime lifetime); |
Andrei Homescu | b62afd9 | 2020-05-11 19:24:59 -0700 | [diff] [blame] | 84 | |
| 85 | StorageMode ArgumentStorageMode(const AidlArgument& arg, const AidlTypenames& typenames); |
| 86 | |
| 87 | ReferenceMode ArgumentReferenceMode(const AidlArgument& arg, const AidlTypenames& typenames); |
| 88 | |
| 89 | std::string TakeReference(ReferenceMode ref_mode, const std::string& name); |
| 90 | |
| 91 | bool TypeIsInterface(const AidlTypeSpecifier& type, const AidlTypenames& typenames); |
| 92 | |
Andrei Homescu | b2316a5 | 2021-09-04 04:41:12 +0000 | [diff] [blame] | 93 | bool TypeNeedsOption(const AidlTypeSpecifier& type, const AidlTypenames& typenames); |
Andrei Homescu | b62afd9 | 2020-05-11 19:24:59 -0700 | [diff] [blame] | 94 | |
| 95 | } // namespace rust |
| 96 | } // namespace aidl |
| 97 | } // namespace android |