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  */
16 #pragma once
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 
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);
43 
44 // Returns the Java type signature of the AIDL type spec
45 // This includes generic type parameters with array modifiers.
46 string JavaSignatureOf(const AidlTypeSpecifier& aidl, const AidlTypenames& typenames);
47 
48 // Returns the instantiable Jva type signature of the AIDL type spec
49 // This includes generic type parameters, but excludes array modifiers.
50 string InstantiableJavaSignatureOf(const AidlTypeSpecifier& aidl, const AidlTypenames& typenames);
51 
52 // Returns the default Java value of the AIDL type spec
53 string DefaultJavaValueOf(const AidlTypeSpecifier& aidl, const AidlTypenames& typenames);
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;
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 
102 // Writes an expression that returns the string representation of a field
103 // in a parcelable
104 void ToStringFor(const CodeGeneratorContext& c);
105 
106 }  // namespace java
107 }  // namespace aidl
108 }  // namespace android
109