blob: fb015a4da58df3c00049645c55ab71c7dd5521bb [file] [log] [blame]
Andreas Huber4b2cf352016-08-31 13:58:19 -07001/*
2 * Copyright (C) 2016 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
17#include "PredefinedType.h"
18
Iliyan Malcheva72e0d22016-09-09 11:03:08 -070019#include <hidl-util/Formatter.h>
Andreas Huber4b2cf352016-08-31 13:58:19 -070020#include <android-base/logging.h>
21
22namespace android {
23
Yifan Hong244e82d2016-11-11 11:13:57 -080024PredefinedType::PredefinedType(const char *nsp, const char *name)
25 : mNamespace(nsp), mName(name) {
Andreas Huber4b2cf352016-08-31 13:58:19 -070026}
27
Andreas Huber4bcf97d2016-08-30 11:27:49 -070028void PredefinedType::addNamedTypesToSet(std::set<const FQName> &) const {
29 // do nothing
30}
Yifan Hong244e82d2016-11-11 11:13:57 -080031
32std::string PredefinedType::fullName() const {
33 return mNamespace +
34 (mNamespace.empty() ? "" : "::") +
35 mName;
36}
37
Andreas Huber4b2cf352016-08-31 13:58:19 -070038std::string PredefinedType::getCppType(
Steven Moreland979e0992016-09-07 09:18:08 -070039 StorageMode mode,
Steven Moreland979e0992016-09-07 09:18:08 -070040 bool) const {
Andreas Huber4b2cf352016-08-31 13:58:19 -070041
Yifan Hong244e82d2016-11-11 11:13:57 -080042 const std::string base = fullName();
Andreas Huber4b2cf352016-08-31 13:58:19 -070043
44 switch (mode) {
45 case StorageMode_Stack:
46 return base;
47
48 case StorageMode_Argument:
49 return "const " + base + "&";
50
51 case StorageMode_Result:
52 return "const " + base + "*";
53 }
54}
55
Andreas Huber4b2cf352016-08-31 13:58:19 -070056void PredefinedType::emitReaderWriter(
57 Formatter &out,
58 const std::string &name,
59 const std::string &parcelObj,
60 bool parcelObjIsPointer,
61 bool isReader,
62 ErrorMode mode) const {
63 const std::string parentName = "_hidl_" + name + "_parent";
64
65 out << "size_t " << parentName << ";\n\n";
66
67 const std::string parcelObjDeref =
68 parcelObj + (parcelObjIsPointer ? "->" : ".");
69
70 if (isReader) {
71 out << name
72 << " = (const "
Yifan Hong244e82d2016-11-11 11:13:57 -080073 << fullName()
Andreas Huber4b2cf352016-08-31 13:58:19 -070074 << " *)"
75 << parcelObjDeref
76 << "readBuffer("
77 << "&"
78 << parentName
79 << ");\n";
80
81 out << "if ("
82 << name
83 << " == nullptr) {\n";
84
85 out.indent();
86
87 out << "_hidl_err = ::android::UNKNOWN_ERROR;\n";
88 handleError2(out, mode);
89
90 out.unindent();
91 out << "}\n\n";
92 } else {
93 out << "_hidl_err = "
94 << parcelObjDeref
95 << "writeBuffer(&"
96 << name
97 << ", sizeof("
98 << name
99 << "), &"
100 << parentName
101 << ");\n";
102
103 handleError(out, mode);
104 }
105
106 emitReaderWriterEmbedded(
107 out,
Andreas Huberf9d49f12016-09-12 14:58:36 -0700108 0 /* depth */,
Andreas Huber4b2cf352016-08-31 13:58:19 -0700109 name,
Yifan Hongbe2a3732016-10-05 13:33:41 -0700110 name /* sanitizedName */,
Andreas Huber4b2cf352016-08-31 13:58:19 -0700111 isReader /* nameIsPointer */,
112 parcelObj,
113 parcelObjIsPointer,
114 isReader,
115 mode,
116 parentName,
117 "0 /* parentOffset */");
118}
119
120void PredefinedType::emitReaderWriterEmbedded(
121 Formatter &out,
Andreas Huberf9d49f12016-09-12 14:58:36 -0700122 size_t /* depth */,
Andreas Huber4b2cf352016-08-31 13:58:19 -0700123 const std::string &name,
Yifan Hongbe2a3732016-10-05 13:33:41 -0700124 const std::string & /* sanitizedName */,
Andreas Huber4b2cf352016-08-31 13:58:19 -0700125 bool nameIsPointer,
126 const std::string &parcelObj,
127 bool parcelObjIsPointer,
128 bool isReader,
129 ErrorMode mode,
130 const std::string &parentName,
131 const std::string &offsetText) const {
132 emitReaderWriterEmbeddedForTypeName(
133 out,
134 name,
135 nameIsPointer,
136 parcelObj,
137 parcelObjIsPointer,
138 isReader,
139 mode,
140 parentName,
141 offsetText,
Yifan Hong244e82d2016-11-11 11:13:57 -0800142 fullName(),
143 "" /* childName */,
144 mNamespace);
Andreas Huber4b2cf352016-08-31 13:58:19 -0700145}
146
147bool PredefinedType::isJavaCompatible() const {
148 return false;
149}
150
151bool PredefinedType::needsEmbeddedReadWrite() const {
152 return true;
153}
154
155bool PredefinedType::resultNeedsDeref() const {
156 return true;
157}
158
159} // namespace android
160