blob: 1ef0790af1b2c00b7efa8d8c9fffb504a52552d2 [file] [log] [blame]
Andreas Huber1aec3972016-08-26 09:26:32 -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
Andreas Huberc9410c72016-07-28 12:18:40 -070017#include "ScalarType.h"
18
Iliyan Malcheva72e0d22016-09-09 11:03:08 -070019#include <hidl-util/Formatter.h>
Andreas Huberc9410c72016-07-28 12:18:40 -070020
21namespace android {
22
23ScalarType::ScalarType(Kind kind)
24 : mKind(kind) {
25}
26
Andreas Huber737080b2016-08-02 15:38:04 -070027const ScalarType *ScalarType::resolveToScalarType() const {
28 return this;
29}
30
Andreas Huber8d3ac0c2016-08-04 14:49:23 -070031bool ScalarType::isValidEnumStorageType() const {
32 // Only integer types.
33 return mKind >= KIND_INT8 && mKind <= KIND_UINT64;
34}
35
Steven Moreland979e0992016-09-07 09:18:08 -070036void ScalarType::addNamedTypesToSet(std::set<const FQName> &) const {
37 // do nothing
38}
39
40std::string ScalarType::getCppType(StorageMode, std::string *extra, bool) const {
Andreas Huber881227d2016-08-02 14:20:21 -070041 static const char *const kName[] = {
Andreas Huber881227d2016-08-02 14:20:21 -070042 "bool",
43 "void *",
44 "int8_t",
45 "uint8_t",
46 "int16_t",
47 "uint16_t",
48 "int32_t",
49 "uint32_t",
50 "int64_t",
51 "uint64_t",
52 "float",
53 "double"
54 };
55
56 extra->clear();
57
58 return kName[mKind];
59}
60
Andreas Huber4c865b72016-09-14 15:26:27 -070061std::string ScalarType::getJavaType(
62 std::string *extra, bool /* forInitializer */) const {
Andreas Huber2831d512016-08-15 09:33:47 -070063 static const char *const kName[] = {
Andreas Huber2831d512016-08-15 09:33:47 -070064 "boolean",
65 "long",
66 "byte",
67 "byte",
68 "short",
69 "short",
70 "int",
71 "int",
72 "long",
73 "long",
74 "float",
75 "double"
76 };
77
Andreas Huber4c865b72016-09-14 15:26:27 -070078 extra->clear();
Andreas Huber2831d512016-08-15 09:33:47 -070079 return kName[mKind];
80}
81
Andreas Huber85eabdb2016-08-25 11:24:49 -070082std::string ScalarType::getJavaWrapperType() const {
83 static const char *const kName[] = {
84 "Boolean",
85 "Long",
86 "Byte",
87 "Byte",
88 "Short",
89 "Short",
Andreas Huber5001d932016-09-14 09:09:09 -070090 "Integer",
91 "Integer",
Andreas Huber85eabdb2016-08-25 11:24:49 -070092 "Long",
93 "Long",
94 "Float",
95 "Double"
96 };
97
98 return kName[mKind];
99}
100
Andreas Huber2831d512016-08-15 09:33:47 -0700101std::string ScalarType::getJavaSuffix() const {
102 static const char *const kSuffix[] = {
Andreas Huberd85a5092016-08-25 09:51:52 -0700103 "Bool",
Andreas Huber2831d512016-08-15 09:33:47 -0700104 "Pointer",
105 "Int8",
106 "Int8",
107 "Int16",
108 "Int16",
109 "Int32",
110 "Int32",
111 "Int64",
112 "Int64",
113 "Float",
114 "Double"
115 };
116
117 return kSuffix[mKind];
118}
119
Andreas Huber881227d2016-08-02 14:20:21 -0700120void ScalarType::emitReaderWriter(
121 Formatter &out,
122 const std::string &name,
123 const std::string &parcelObj,
124 bool parcelObjIsPointer,
125 bool isReader,
126 ErrorMode mode) const {
Andreas Huber737080b2016-08-02 15:38:04 -0700127 emitReaderWriterWithCast(
128 out,
129 name,
130 parcelObj,
131 parcelObjIsPointer,
132 isReader,
133 mode,
134 false /* needsCast */);
135}
136
137void ScalarType::emitReaderWriterWithCast(
138 Formatter &out,
139 const std::string &name,
140 const std::string &parcelObj,
141 bool parcelObjIsPointer,
142 bool isReader,
143 ErrorMode mode,
144 bool needsCast) const {
Andreas Huber881227d2016-08-02 14:20:21 -0700145 static const char *const kSuffix[] = {
Iliyan Malcheve0b28672016-08-14 13:35:12 -0700146 "Bool",
Andreas Huber881227d2016-08-02 14:20:21 -0700147 "Pointer",
148 "Int8",
149 "Uint8",
150 "Int16",
151 "Uint16",
152 "Int32",
153 "Uint32",
154 "Int64",
155 "Uint64",
156 "Float",
157 "Double"
158 };
159
160 const std::string parcelObjDeref =
161 parcelObj + (parcelObjIsPointer ? "->" : ".");
162
Iliyan Malchev549e2592016-08-10 08:59:12 -0700163 out << "_hidl_err = "
Andreas Huber881227d2016-08-02 14:20:21 -0700164 << parcelObjDeref
165 << (isReader ? "read" : "write")
166 << kSuffix[mKind]
167 << "(";
168
Andreas Huber737080b2016-08-02 15:38:04 -0700169 if (needsCast) {
170 std::string extra;
171
172 out << "("
173 << Type::getCppType(&extra)
174 << (isReader ? " *)" : ")");
175 }
176
Andreas Huber881227d2016-08-02 14:20:21 -0700177 if (isReader) {
178 out << "&";
179 }
180
181 out << name
182 << ");\n";
183
184 handleError(out, mode);
185}
186
Andreas Huber85eabdb2016-08-25 11:24:49 -0700187void ScalarType::emitJavaFieldReaderWriter(
188 Formatter &out,
Andreas Huber4c865b72016-09-14 15:26:27 -0700189 size_t /* depth */,
Andreas Huber709b62d2016-09-19 11:21:18 -0700190 const std::string & /* parcelName */,
Andreas Huber85eabdb2016-08-25 11:24:49 -0700191 const std::string &blobName,
192 const std::string &fieldName,
193 const std::string &offset,
194 bool isReader) const {
195 if (isReader) {
196 out << fieldName
197 << " = "
198 << blobName
199 << ".get"
200 << getJavaSuffix()
201 << "("
202 << offset
203 << ");\n";
204
205 return;
206 }
207
208 out << blobName
209 << ".put"
210 << getJavaSuffix()
211 << "("
212 << offset
213 << ", "
214 << fieldName
215 << ");\n";
216}
217
Zhuoyao Zhang5158db42016-08-10 10:25:20 -0700218status_t ScalarType::emitVtsTypeDeclarations(Formatter &out) const {
Zhuoyao Zhang19933522016-08-29 15:06:38 -0700219 static const char *const kName[] = {
220 "bool_t",
221 "opaque",
222 "int8_t",
223 "uint8_t",
224 "int16_t",
225 "uint16_t",
226 "int32_t",
227 "uint32_t",
228 "int64_t",
229 "uint64_t",
230 "float_t",
231 "double_t"
232 };
233 out << "type: TYPE_SCALAR\n"<< "scalar_type: \""<< kName[mKind]<< "\"\n";
Zhuoyao Zhang5158db42016-08-10 10:25:20 -0700234 return OK;
235}
236
Andreas Huber85eabdb2016-08-25 11:24:49 -0700237void ScalarType::getAlignmentAndSize(size_t *align, size_t *size) const {
238 static const size_t kAlign[] = {
239 1, // bool, this is NOT standardized!
240 8, // void *, 64-bit mode
241 1, // int8_t
242 1, // uint8_t
243 2, // int16_t
244 2, // uint16_t
245 4, // int32_t
246 4, // uint32_t
247 8, // int64_t
248 8, // uint64_t
249 4, // float
250 8 // double
251 };
252
253 *align = *size = kAlign[mKind];
254}
255
Yifan Hong57886972016-08-17 10:42:15 -0700256ScalarType::Kind ScalarType::getKind() const {
257 return mKind;
258}
259
Andreas Huberc9410c72016-07-28 12:18:40 -0700260} // namespace android
261