blob: d33b25bd8a4036064dbfa71f07942e28164d0093 [file] [log] [blame]
Jiyong Park1d2df7d2018-07-23 15:22:50 +09001/*
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 Moreland9fccf582018-08-27 20:36:27 -070016#pragma once
Jiyong Park1d2df7d2018-07-23 15:22:50 +090017
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
29namespace android {
30namespace aidl {
31namespace java {
32
33using std::map;
34using std::pair;
35using std::set;
36using std::string;
37using std::unique_ptr;
38using std::vector;
39
Steven Moreland860b1942018-08-16 14:59:28 -070040// This header provides functions that translate AIDL things to Java things.
41
42std::string ConstantValueDecorator(const AidlTypeSpecifier& type, const std::string& raw_value);
Jiyong Park1d2df7d2018-07-23 15:22:50 +090043
Jiyong Park1d2df7d2018-07-23 15:22:50 +090044// Returns the Java type signature of the AIDL type spec
Steven Moreland3dc29d82019-08-21 17:23:11 -070045// This includes generic type parameters with array modifiers.
Daniel Norman716d3112019-09-10 13:11:56 -070046string JavaSignatureOf(const AidlTypeSpecifier& aidl, const AidlTypenames& typenames);
Jiyong Park1d2df7d2018-07-23 15:22:50 +090047
Jeongik Chaa2080bf2019-06-18 16:44:29 +090048// Returns the instantiable Jva type signature of the AIDL type spec
49// This includes generic type parameters, but excludes array modifiers.
Daniel Norman716d3112019-09-10 13:11:56 -070050string InstantiableJavaSignatureOf(const AidlTypeSpecifier& aidl, const AidlTypenames& typenames);
Jeongik Chaa2080bf2019-06-18 16:44:29 +090051
Jiyong Park1d2df7d2018-07-23 15:22:50 +090052// Returns the default Java value of the AIDL type spec
Daniel Norman716d3112019-09-10 13:11:56 -070053string DefaultJavaValueOf(const AidlTypeSpecifier& aidl, const AidlTypenames& typenames);
Jiyong Park1d2df7d2018-07-23 15:22:50 +090054
55// This carries information that is required to generate code for
56// marshalling and unmarshalling a method argument or a parcelable field
57struct 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 Park1d2df7d2018-07-23 15:22:50 +090089};
90
91// Writes code fragment that writes a variable to the parcel.
92bool 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.
96bool CreateFromParcelFor(const CodeGeneratorContext& c);
97
98// Writes code fragment that reads data from the parcel into an existing
99// array or a List.
100bool ReadFromParcelFor(const CodeGeneratorContext& c);
101
102} // namespace java
103} // namespace aidl
104} // namespace android