Jiyong Park | 1d2df7d | 2018-07-23 15:22:50 +0900 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2018, 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 | */ |
Steven Moreland | 9fccf58 | 2018-08-27 20:36:27 -0700 | [diff] [blame] | 16 | #pragma once |
Jiyong Park | 1d2df7d | 2018-07-23 15:22:50 +0900 | [diff] [blame] | 17 | |
| 18 | #include "aidl_language.h" |
| 19 | #include "aidl_typenames.h" |
| 20 | |
| 21 | #include <map> |
| 22 | #include <memory> |
| 23 | #include <set> |
| 24 | #include <sstream> |
| 25 | #include <string> |
| 26 | #include <utility> |
| 27 | #include <vector> |
| 28 | |
| 29 | namespace android { |
| 30 | namespace aidl { |
| 31 | namespace java { |
| 32 | |
| 33 | using std::map; |
| 34 | using std::pair; |
| 35 | using std::set; |
| 36 | using std::string; |
| 37 | using std::unique_ptr; |
| 38 | using std::vector; |
| 39 | |
Steven Moreland | 860b194 | 2018-08-16 14:59:28 -0700 | [diff] [blame] | 40 | // This header provides functions that translate AIDL things to Java things. |
| 41 | |
| 42 | std::string ConstantValueDecorator(const AidlTypeSpecifier& type, const std::string& raw_value); |
Jiyong Park | 1d2df7d | 2018-07-23 15:22:50 +0900 | [diff] [blame] | 43 | |
Jiyong Park | 1d2df7d | 2018-07-23 15:22:50 +0900 | [diff] [blame] | 44 | // Returns the Java type signature of the AIDL type spec |
Steven Moreland | 3dc29d8 | 2019-08-21 17:23:11 -0700 | [diff] [blame] | 45 | // This includes generic type parameters with array modifiers. |
Daniel Norman | 716d311 | 2019-09-10 13:11:56 -0700 | [diff] [blame] | 46 | string JavaSignatureOf(const AidlTypeSpecifier& aidl, const AidlTypenames& typenames); |
Jiyong Park | 1d2df7d | 2018-07-23 15:22:50 +0900 | [diff] [blame] | 47 | |
Jeongik Cha | a2080bf | 2019-06-18 16:44:29 +0900 | [diff] [blame] | 48 | // Returns the instantiable Jva type signature of the AIDL type spec |
| 49 | // This includes generic type parameters, but excludes array modifiers. |
Daniel Norman | 716d311 | 2019-09-10 13:11:56 -0700 | [diff] [blame] | 50 | string InstantiableJavaSignatureOf(const AidlTypeSpecifier& aidl, const AidlTypenames& typenames); |
Jeongik Cha | a2080bf | 2019-06-18 16:44:29 +0900 | [diff] [blame] | 51 | |
Jiyong Park | 1d2df7d | 2018-07-23 15:22:50 +0900 | [diff] [blame] | 52 | // Returns the default Java value of the AIDL type spec |
Daniel Norman | 716d311 | 2019-09-10 13:11:56 -0700 | [diff] [blame] | 53 | string DefaultJavaValueOf(const AidlTypeSpecifier& aidl, const AidlTypenames& typenames); |
Jiyong Park | 1d2df7d | 2018-07-23 15:22:50 +0900 | [diff] [blame] | 54 | |
| 55 | // This carries information that is required to generate code for |
| 56 | // marshalling and unmarshalling a method argument or a parcelable field |
| 57 | struct CodeGeneratorContext { |
| 58 | CodeWriter& writer; // CodeWriter::Write() is mutable |
| 59 | const AidlTypenames& typenames; |
| 60 | const AidlTypeSpecifier& type; |
| 61 | const string parcel; |
| 62 | const string var; |
| 63 | |
| 64 | // Set to true when the marshalled data will be returned to the client |
| 65 | // This is given as a hint to the Parcelable that is being marshalled |
| 66 | // so that the Parcelable can release its resource after the marshalling |
| 67 | // is done. |
| 68 | const bool is_return_value; |
| 69 | |
| 70 | // Most of the variables created by AIDL compiler are typed, i.e., the code |
| 71 | // knows exactly what type of object is in the parcel -- because the parcel |
| 72 | // itself is created by the code generated by AIDL compiler. |
| 73 | // |
| 74 | // However, for some collection types (List and Map for now), we write the |
| 75 | // elements in them untyped (object is flattened along with its type name) |
| 76 | // as the AIDL compiler does not know the type of the contained elements. |
| 77 | // So, when unmarshalling such collection, we need to provide a classloader |
| 78 | // from where the parcel can reflectively find a class object for |
| 79 | // the contained element. |
| 80 | // |
| 81 | // This field is a pointer to a boolean state variable that indicates whether |
| 82 | // the code for declaring and getting the classloader has been emitted or not. |
| 83 | // We emit the code at most once per an AIDL method, otherwise we are wasting |
| 84 | // time doing the same thing multiple time. |
| 85 | bool* const is_classloader_created; |
| 86 | |
| 87 | // for error message printing |
| 88 | const string filename; |
Jiyong Park | 1d2df7d | 2018-07-23 15:22:50 +0900 | [diff] [blame] | 89 | }; |
| 90 | |
| 91 | // Writes code fragment that writes a variable to the parcel. |
| 92 | bool WriteToParcelFor(const CodeGeneratorContext& c); |
| 93 | |
| 94 | // Writes code fragment that reads data from the parcel into a variable. When |
| 95 | // the variable type is array or List, the array or List is created. |
| 96 | bool CreateFromParcelFor(const CodeGeneratorContext& c); |
| 97 | |
| 98 | // Writes code fragment that reads data from the parcel into an existing |
| 99 | // array or a List. |
| 100 | bool ReadFromParcelFor(const CodeGeneratorContext& c); |
| 101 | |
Jiyong Park | 43113fb | 2020-07-20 16:26:19 +0900 | [diff] [blame] | 102 | // Writes an expression that returns the string representation of a field |
| 103 | // in a parcelable |
| 104 | void ToStringFor(const CodeGeneratorContext& c); |
| 105 | |
Jiyong Park | 1d2df7d | 2018-07-23 15:22:50 +0900 | [diff] [blame] | 106 | } // namespace java |
| 107 | } // namespace aidl |
| 108 | } // namespace android |