blob: a60215a8df10ef46101f767b1e9f4b9714bc5189 [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
Steven Moreland30bb6a82016-11-30 09:18:34 -080040std::string ScalarType::typeName() const {
41 return getCppStackType();
42}
43
Yifan Hong3b320f82016-11-01 15:15:54 -070044std::string ScalarType::getCppType(StorageMode, bool) const {
Andreas Huber881227d2016-08-02 14:20:21 -070045 static const char *const kName[] = {
Andreas Huber881227d2016-08-02 14:20:21 -070046 "bool",
Andreas Huber881227d2016-08-02 14:20:21 -070047 "int8_t",
48 "uint8_t",
49 "int16_t",
50 "uint16_t",
51 "int32_t",
52 "uint32_t",
53 "int64_t",
54 "uint64_t",
55 "float",
56 "double"
57 };
58
Andreas Huber881227d2016-08-02 14:20:21 -070059 return kName[mKind];
60}
61
Yifan Hong4ed13472016-11-02 10:44:11 -070062std::string ScalarType::getJavaType(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",
Andreas Huber2831d512016-08-15 09:33:47 -070065 "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",
Andreas Huber85eabdb2016-08-25 11:24:49 -070083 "Byte",
84 "Byte",
85 "Short",
86 "Short",
Andreas Huber5001d932016-09-14 09:09:09 -070087 "Integer",
88 "Integer",
Andreas Huber85eabdb2016-08-25 11:24:49 -070089 "Long",
90 "Long",
91 "Float",
92 "Double"
93 };
94
95 return kName[mKind];
96}
97
Andreas Huber2831d512016-08-15 09:33:47 -070098std::string ScalarType::getJavaSuffix() const {
99 static const char *const kSuffix[] = {
Andreas Huberd85a5092016-08-25 09:51:52 -0700100 "Bool",
Andreas Huber2831d512016-08-15 09:33:47 -0700101 "Int8",
102 "Int8",
103 "Int16",
104 "Int16",
105 "Int32",
106 "Int32",
107 "Int64",
108 "Int64",
109 "Float",
110 "Double"
111 };
112
113 return kSuffix[mKind];
114}
115
Zhuoyao Zhangc5ea9f52016-10-06 15:05:39 -0700116std::string ScalarType::getVtsType() const {
117 return "TYPE_SCALAR";
118}
119
120std::string ScalarType::getVtsScalarType() const {
121 static const char * const kName[] = {
122 "bool_t",
123 "int8_t",
124 "uint8_t",
125 "int16_t",
126 "uint16_t",
127 "int32_t",
128 "uint32_t",
129 "int64_t",
130 "uint64_t",
131 "float_t",
132 "double_t"
133 };
134
135 return kName[mKind];
136}
137
Andreas Huber881227d2016-08-02 14:20:21 -0700138void ScalarType::emitReaderWriter(
139 Formatter &out,
140 const std::string &name,
141 const std::string &parcelObj,
142 bool parcelObjIsPointer,
143 bool isReader,
144 ErrorMode mode) const {
Andreas Huber737080b2016-08-02 15:38:04 -0700145 emitReaderWriterWithCast(
146 out,
147 name,
148 parcelObj,
149 parcelObjIsPointer,
150 isReader,
151 mode,
152 false /* needsCast */);
153}
154
155void ScalarType::emitReaderWriterWithCast(
156 Formatter &out,
157 const std::string &name,
158 const std::string &parcelObj,
159 bool parcelObjIsPointer,
160 bool isReader,
161 ErrorMode mode,
162 bool needsCast) const {
Andreas Huber881227d2016-08-02 14:20:21 -0700163 static const char *const kSuffix[] = {
Iliyan Malcheve0b28672016-08-14 13:35:12 -0700164 "Bool",
Andreas Huber881227d2016-08-02 14:20:21 -0700165 "Int8",
166 "Uint8",
167 "Int16",
168 "Uint16",
169 "Int32",
170 "Uint32",
171 "Int64",
172 "Uint64",
173 "Float",
174 "Double"
175 };
176
177 const std::string parcelObjDeref =
178 parcelObj + (parcelObjIsPointer ? "->" : ".");
179
Iliyan Malchev549e2592016-08-10 08:59:12 -0700180 out << "_hidl_err = "
Andreas Huber881227d2016-08-02 14:20:21 -0700181 << parcelObjDeref
182 << (isReader ? "read" : "write")
183 << kSuffix[mKind]
184 << "(";
185
Andreas Huber737080b2016-08-02 15:38:04 -0700186 if (needsCast) {
Andreas Huber737080b2016-08-02 15:38:04 -0700187 out << "("
Yifan Hong3b320f82016-11-01 15:15:54 -0700188 << getCppStackType()
Andreas Huber737080b2016-08-02 15:38:04 -0700189 << (isReader ? " *)" : ")");
190 }
191
Andreas Huber881227d2016-08-02 14:20:21 -0700192 if (isReader) {
193 out << "&";
194 }
195
196 out << name
197 << ");\n";
198
199 handleError(out, mode);
200}
201
Andreas Huber85eabdb2016-08-25 11:24:49 -0700202void ScalarType::emitJavaFieldReaderWriter(
203 Formatter &out,
Andreas Huber4c865b72016-09-14 15:26:27 -0700204 size_t /* depth */,
Andreas Huber709b62d2016-09-19 11:21:18 -0700205 const std::string & /* parcelName */,
Andreas Huber85eabdb2016-08-25 11:24:49 -0700206 const std::string &blobName,
207 const std::string &fieldName,
208 const std::string &offset,
209 bool isReader) const {
210 if (isReader) {
211 out << fieldName
212 << " = "
213 << blobName
214 << ".get"
215 << getJavaSuffix()
216 << "("
217 << offset
218 << ");\n";
219
220 return;
221 }
222
223 out << blobName
224 << ".put"
225 << getJavaSuffix()
226 << "("
227 << offset
228 << ", "
229 << fieldName
230 << ");\n";
231}
232
Zhuoyao Zhang5158db42016-08-10 10:25:20 -0700233status_t ScalarType::emitVtsTypeDeclarations(Formatter &out) const {
Zhuoyao Zhangc5ea9f52016-10-06 15:05:39 -0700234 out << "type: " << getVtsType() << "\n";
235 out << "scalar_type: \"" << getVtsScalarType() << "\"\n";
Zhuoyao Zhang5158db42016-08-10 10:25:20 -0700236 return OK;
237}
238
Andreas Huber85eabdb2016-08-25 11:24:49 -0700239void ScalarType::getAlignmentAndSize(size_t *align, size_t *size) const {
240 static const size_t kAlign[] = {
241 1, // bool, this is NOT standardized!
Andreas Huber85eabdb2016-08-25 11:24:49 -0700242 1, // int8_t
243 1, // uint8_t
244 2, // int16_t
245 2, // uint16_t
246 4, // int32_t
247 4, // uint32_t
248 8, // int64_t
249 8, // uint64_t
250 4, // float
251 8 // double
252 };
253
254 *align = *size = kAlign[mKind];
255}
256
Yifan Hong57886972016-08-17 10:42:15 -0700257ScalarType::Kind ScalarType::getKind() const {
258 return mKind;
259}
260
Andreas Huberc9410c72016-07-28 12:18:40 -0700261} // namespace android
262