blob: b2b869e0980ef9b3e8cf8be27909b4ecc249c0c3 [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 Huber2831d512016-08-15 09:33:47 -070061std::string ScalarType::getJavaType() const {
62 static const char *const kName[] = {
Andreas Huber2831d512016-08-15 09:33:47 -070063 "boolean",
64 "long",
65 "byte",
66 "byte",
67 "short",
68 "short",
69 "int",
70 "int",
71 "long",
72 "long",
73 "float",
74 "double"
75 };
76
77 return kName[mKind];
78}
79
Andreas Huber85eabdb2016-08-25 11:24:49 -070080std::string ScalarType::getJavaWrapperType() const {
81 static const char *const kName[] = {
82 "Boolean",
83 "Long",
84 "Byte",
85 "Byte",
86 "Short",
87 "Short",
Andreas Huber5001d932016-09-14 09:09:09 -070088 "Integer",
89 "Integer",
Andreas Huber85eabdb2016-08-25 11:24:49 -070090 "Long",
91 "Long",
92 "Float",
93 "Double"
94 };
95
96 return kName[mKind];
97}
98
Andreas Huber2831d512016-08-15 09:33:47 -070099std::string ScalarType::getJavaSuffix() const {
100 static const char *const kSuffix[] = {
Andreas Huberd85a5092016-08-25 09:51:52 -0700101 "Bool",
Andreas Huber2831d512016-08-15 09:33:47 -0700102 "Pointer",
103 "Int8",
104 "Int8",
105 "Int16",
106 "Int16",
107 "Int32",
108 "Int32",
109 "Int64",
110 "Int64",
111 "Float",
112 "Double"
113 };
114
115 return kSuffix[mKind];
116}
117
Andreas Huber881227d2016-08-02 14:20:21 -0700118void ScalarType::emitReaderWriter(
119 Formatter &out,
120 const std::string &name,
121 const std::string &parcelObj,
122 bool parcelObjIsPointer,
123 bool isReader,
124 ErrorMode mode) const {
Andreas Huber737080b2016-08-02 15:38:04 -0700125 emitReaderWriterWithCast(
126 out,
127 name,
128 parcelObj,
129 parcelObjIsPointer,
130 isReader,
131 mode,
132 false /* needsCast */);
133}
134
135void ScalarType::emitReaderWriterWithCast(
136 Formatter &out,
137 const std::string &name,
138 const std::string &parcelObj,
139 bool parcelObjIsPointer,
140 bool isReader,
141 ErrorMode mode,
142 bool needsCast) const {
Andreas Huber881227d2016-08-02 14:20:21 -0700143 static const char *const kSuffix[] = {
Iliyan Malcheve0b28672016-08-14 13:35:12 -0700144 "Bool",
Andreas Huber881227d2016-08-02 14:20:21 -0700145 "Pointer",
146 "Int8",
147 "Uint8",
148 "Int16",
149 "Uint16",
150 "Int32",
151 "Uint32",
152 "Int64",
153 "Uint64",
154 "Float",
155 "Double"
156 };
157
158 const std::string parcelObjDeref =
159 parcelObj + (parcelObjIsPointer ? "->" : ".");
160
Iliyan Malchev549e2592016-08-10 08:59:12 -0700161 out << "_hidl_err = "
Andreas Huber881227d2016-08-02 14:20:21 -0700162 << parcelObjDeref
163 << (isReader ? "read" : "write")
164 << kSuffix[mKind]
165 << "(";
166
Andreas Huber737080b2016-08-02 15:38:04 -0700167 if (needsCast) {
168 std::string extra;
169
170 out << "("
171 << Type::getCppType(&extra)
172 << (isReader ? " *)" : ")");
173 }
174
Andreas Huber881227d2016-08-02 14:20:21 -0700175 if (isReader) {
176 out << "&";
177 }
178
179 out << name
180 << ");\n";
181
182 handleError(out, mode);
183}
184
Andreas Huber85eabdb2016-08-25 11:24:49 -0700185void ScalarType::emitJavaFieldReaderWriter(
186 Formatter &out,
187 const std::string &blobName,
188 const std::string &fieldName,
189 const std::string &offset,
190 bool isReader) const {
191 if (isReader) {
192 out << fieldName
193 << " = "
194 << blobName
195 << ".get"
196 << getJavaSuffix()
197 << "("
198 << offset
199 << ");\n";
200
201 return;
202 }
203
204 out << blobName
205 << ".put"
206 << getJavaSuffix()
207 << "("
208 << offset
209 << ", "
210 << fieldName
211 << ");\n";
212}
213
Zhuoyao Zhang5158db42016-08-10 10:25:20 -0700214status_t ScalarType::emitVtsTypeDeclarations(Formatter &out) const {
Zhuoyao Zhang19933522016-08-29 15:06:38 -0700215 static const char *const kName[] = {
216 "bool_t",
217 "opaque",
218 "int8_t",
219 "uint8_t",
220 "int16_t",
221 "uint16_t",
222 "int32_t",
223 "uint32_t",
224 "int64_t",
225 "uint64_t",
226 "float_t",
227 "double_t"
228 };
229 out << "type: TYPE_SCALAR\n"<< "scalar_type: \""<< kName[mKind]<< "\"\n";
Zhuoyao Zhang5158db42016-08-10 10:25:20 -0700230 return OK;
231}
232
Andreas Huber85eabdb2016-08-25 11:24:49 -0700233void ScalarType::getAlignmentAndSize(size_t *align, size_t *size) const {
234 static const size_t kAlign[] = {
235 1, // bool, this is NOT standardized!
236 8, // void *, 64-bit mode
237 1, // int8_t
238 1, // uint8_t
239 2, // int16_t
240 2, // uint16_t
241 4, // int32_t
242 4, // uint32_t
243 8, // int64_t
244 8, // uint64_t
245 4, // float
246 8 // double
247 };
248
249 *align = *size = kAlign[mKind];
250}
251
Yifan Hong57886972016-08-17 10:42:15 -0700252ScalarType::Kind ScalarType::getKind() const {
253 return mKind;
254}
255
Andreas Huberc9410c72016-07-28 12:18:40 -0700256} // namespace android
257