blob: cff91c3a33dbd1ea3ea42a7c7378ed83818520c7 [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
19#include "Formatter.h"
20
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
Andreas Huber881227d2016-08-02 14:20:21 -070036std::string ScalarType::getCppType(StorageMode, std::string *extra) const {
37 static const char *const kName[] = {
Andreas Huber881227d2016-08-02 14:20:21 -070038 "bool",
39 "void *",
40 "int8_t",
41 "uint8_t",
42 "int16_t",
43 "uint16_t",
44 "int32_t",
45 "uint32_t",
46 "int64_t",
47 "uint64_t",
48 "float",
49 "double"
50 };
51
52 extra->clear();
53
54 return kName[mKind];
55}
56
Andreas Huber2831d512016-08-15 09:33:47 -070057std::string ScalarType::getJavaType() const {
58 static const char *const kName[] = {
Andreas Huber2831d512016-08-15 09:33:47 -070059 "boolean",
60 "long",
61 "byte",
62 "byte",
63 "short",
64 "short",
65 "int",
66 "int",
67 "long",
68 "long",
69 "float",
70 "double"
71 };
72
73 return kName[mKind];
74}
75
Andreas Huber85eabdb2016-08-25 11:24:49 -070076std::string ScalarType::getJavaWrapperType() const {
77 static const char *const kName[] = {
78 "Boolean",
79 "Long",
80 "Byte",
81 "Byte",
82 "Short",
83 "Short",
84 "Int",
85 "Int",
86 "Long",
87 "Long",
88 "Float",
89 "Double"
90 };
91
92 return kName[mKind];
93}
94
Andreas Huber2831d512016-08-15 09:33:47 -070095std::string ScalarType::getJavaSuffix() const {
96 static const char *const kSuffix[] = {
Andreas Huberd85a5092016-08-25 09:51:52 -070097 "Bool",
Andreas Huber2831d512016-08-15 09:33:47 -070098 "Pointer",
99 "Int8",
100 "Int8",
101 "Int16",
102 "Int16",
103 "Int32",
104 "Int32",
105 "Int64",
106 "Int64",
107 "Float",
108 "Double"
109 };
110
111 return kSuffix[mKind];
112}
113
Andreas Huber881227d2016-08-02 14:20:21 -0700114void ScalarType::emitReaderWriter(
115 Formatter &out,
116 const std::string &name,
117 const std::string &parcelObj,
118 bool parcelObjIsPointer,
119 bool isReader,
120 ErrorMode mode) const {
Andreas Huber737080b2016-08-02 15:38:04 -0700121 emitReaderWriterWithCast(
122 out,
123 name,
124 parcelObj,
125 parcelObjIsPointer,
126 isReader,
127 mode,
128 false /* needsCast */);
129}
130
131void ScalarType::emitReaderWriterWithCast(
132 Formatter &out,
133 const std::string &name,
134 const std::string &parcelObj,
135 bool parcelObjIsPointer,
136 bool isReader,
137 ErrorMode mode,
138 bool needsCast) const {
Andreas Huber881227d2016-08-02 14:20:21 -0700139 static const char *const kSuffix[] = {
Iliyan Malcheve0b28672016-08-14 13:35:12 -0700140 "Bool",
Andreas Huber881227d2016-08-02 14:20:21 -0700141 "Pointer",
142 "Int8",
143 "Uint8",
144 "Int16",
145 "Uint16",
146 "Int32",
147 "Uint32",
148 "Int64",
149 "Uint64",
150 "Float",
151 "Double"
152 };
153
154 const std::string parcelObjDeref =
155 parcelObj + (parcelObjIsPointer ? "->" : ".");
156
Iliyan Malchev549e2592016-08-10 08:59:12 -0700157 out << "_hidl_err = "
Andreas Huber881227d2016-08-02 14:20:21 -0700158 << parcelObjDeref
159 << (isReader ? "read" : "write")
160 << kSuffix[mKind]
161 << "(";
162
Andreas Huber737080b2016-08-02 15:38:04 -0700163 if (needsCast) {
164 std::string extra;
165
166 out << "("
167 << Type::getCppType(&extra)
168 << (isReader ? " *)" : ")");
169 }
170
Andreas Huber881227d2016-08-02 14:20:21 -0700171 if (isReader) {
172 out << "&";
173 }
174
175 out << name
176 << ");\n";
177
178 handleError(out, mode);
179}
180
Andreas Huber85eabdb2016-08-25 11:24:49 -0700181void ScalarType::emitJavaFieldReaderWriter(
182 Formatter &out,
183 const std::string &blobName,
184 const std::string &fieldName,
185 const std::string &offset,
186 bool isReader) const {
187 if (isReader) {
188 out << fieldName
189 << " = "
190 << blobName
191 << ".get"
192 << getJavaSuffix()
193 << "("
194 << offset
195 << ");\n";
196
197 return;
198 }
199
200 out << blobName
201 << ".put"
202 << getJavaSuffix()
203 << "("
204 << offset
205 << ", "
206 << fieldName
207 << ");\n";
208}
209
Zhuoyao Zhang5158db42016-08-10 10:25:20 -0700210status_t ScalarType::emitVtsTypeDeclarations(Formatter &out) const {
211 std::string extra;
212 out << "type: TYPE_SCALAR\n"
213 << "scalar_type: "
214 << getCppType(StorageMode_Stack, &extra)
215 << "\n";
216 return OK;
217}
218
Andreas Huber85eabdb2016-08-25 11:24:49 -0700219void ScalarType::getAlignmentAndSize(size_t *align, size_t *size) const {
220 static const size_t kAlign[] = {
221 1, // bool, this is NOT standardized!
222 8, // void *, 64-bit mode
223 1, // int8_t
224 1, // uint8_t
225 2, // int16_t
226 2, // uint16_t
227 4, // int32_t
228 4, // uint32_t
229 8, // int64_t
230 8, // uint64_t
231 4, // float
232 8 // double
233 };
234
235 *align = *size = kAlign[mKind];
236}
237
Andreas Huberc9410c72016-07-28 12:18:40 -0700238} // namespace android
239