blob: 04cb1c364c90c7b918b073dbba9dab6423c2b82e [file] [log] [blame]
Andreas Huberc9410c72016-07-28 12:18:40 -07001#include "ScalarType.h"
2
3#include "Formatter.h"
4
5namespace android {
6
7ScalarType::ScalarType(Kind kind)
8 : mKind(kind) {
9}
10
Andreas Huber737080b2016-08-02 15:38:04 -070011const ScalarType *ScalarType::resolveToScalarType() const {
12 return this;
13}
14
Andreas Huber8d3ac0c2016-08-04 14:49:23 -070015bool ScalarType::isValidEnumStorageType() const {
16 // Only integer types.
17 return mKind >= KIND_INT8 && mKind <= KIND_UINT64;
18}
19
Andreas Huber881227d2016-08-02 14:20:21 -070020std::string ScalarType::getCppType(StorageMode, std::string *extra) const {
21 static const char *const kName[] = {
Andreas Huber881227d2016-08-02 14:20:21 -070022 "bool",
23 "void *",
24 "int8_t",
25 "uint8_t",
26 "int16_t",
27 "uint16_t",
28 "int32_t",
29 "uint32_t",
30 "int64_t",
31 "uint64_t",
32 "float",
33 "double"
34 };
35
36 extra->clear();
37
38 return kName[mKind];
39}
40
Andreas Huber2831d512016-08-15 09:33:47 -070041std::string ScalarType::getJavaType() const {
42 static const char *const kName[] = {
Andreas Huber2831d512016-08-15 09:33:47 -070043 "boolean",
44 "long",
45 "byte",
46 "byte",
47 "short",
48 "short",
49 "int",
50 "int",
51 "long",
52 "long",
53 "float",
54 "double"
55 };
56
57 return kName[mKind];
58}
59
Andreas Huber85eabdb2016-08-25 11:24:49 -070060std::string ScalarType::getJavaWrapperType() const {
61 static const char *const kName[] = {
62 "Boolean",
63 "Long",
64 "Byte",
65 "Byte",
66 "Short",
67 "Short",
68 "Int",
69 "Int",
70 "Long",
71 "Long",
72 "Float",
73 "Double"
74 };
75
76 return kName[mKind];
77}
78
Andreas Huber2831d512016-08-15 09:33:47 -070079std::string ScalarType::getJavaSuffix() const {
80 static const char *const kSuffix[] = {
Andreas Huberd85a5092016-08-25 09:51:52 -070081 "Bool",
Andreas Huber2831d512016-08-15 09:33:47 -070082 "Pointer",
83 "Int8",
84 "Int8",
85 "Int16",
86 "Int16",
87 "Int32",
88 "Int32",
89 "Int64",
90 "Int64",
91 "Float",
92 "Double"
93 };
94
95 return kSuffix[mKind];
96}
97
Andreas Huber881227d2016-08-02 14:20:21 -070098void ScalarType::emitReaderWriter(
99 Formatter &out,
100 const std::string &name,
101 const std::string &parcelObj,
102 bool parcelObjIsPointer,
103 bool isReader,
104 ErrorMode mode) const {
Andreas Huber737080b2016-08-02 15:38:04 -0700105 emitReaderWriterWithCast(
106 out,
107 name,
108 parcelObj,
109 parcelObjIsPointer,
110 isReader,
111 mode,
112 false /* needsCast */);
113}
114
115void ScalarType::emitReaderWriterWithCast(
116 Formatter &out,
117 const std::string &name,
118 const std::string &parcelObj,
119 bool parcelObjIsPointer,
120 bool isReader,
121 ErrorMode mode,
122 bool needsCast) const {
Andreas Huber881227d2016-08-02 14:20:21 -0700123 static const char *const kSuffix[] = {
Iliyan Malcheve0b28672016-08-14 13:35:12 -0700124 "Bool",
Andreas Huber881227d2016-08-02 14:20:21 -0700125 "Pointer",
126 "Int8",
127 "Uint8",
128 "Int16",
129 "Uint16",
130 "Int32",
131 "Uint32",
132 "Int64",
133 "Uint64",
134 "Float",
135 "Double"
136 };
137
138 const std::string parcelObjDeref =
139 parcelObj + (parcelObjIsPointer ? "->" : ".");
140
Iliyan Malchev549e2592016-08-10 08:59:12 -0700141 out << "_hidl_err = "
Andreas Huber881227d2016-08-02 14:20:21 -0700142 << parcelObjDeref
143 << (isReader ? "read" : "write")
144 << kSuffix[mKind]
145 << "(";
146
Andreas Huber737080b2016-08-02 15:38:04 -0700147 if (needsCast) {
148 std::string extra;
149
150 out << "("
151 << Type::getCppType(&extra)
152 << (isReader ? " *)" : ")");
153 }
154
Andreas Huber881227d2016-08-02 14:20:21 -0700155 if (isReader) {
156 out << "&";
157 }
158
159 out << name
160 << ");\n";
161
162 handleError(out, mode);
163}
164
Andreas Huber85eabdb2016-08-25 11:24:49 -0700165void ScalarType::emitJavaFieldReaderWriter(
166 Formatter &out,
167 const std::string &blobName,
168 const std::string &fieldName,
169 const std::string &offset,
170 bool isReader) const {
171 if (isReader) {
172 out << fieldName
173 << " = "
174 << blobName
175 << ".get"
176 << getJavaSuffix()
177 << "("
178 << offset
179 << ");\n";
180
181 return;
182 }
183
184 out << blobName
185 << ".put"
186 << getJavaSuffix()
187 << "("
188 << offset
189 << ", "
190 << fieldName
191 << ");\n";
192}
193
Zhuoyao Zhang5158db42016-08-10 10:25:20 -0700194status_t ScalarType::emitVtsTypeDeclarations(Formatter &out) const {
195 std::string extra;
196 out << "type: TYPE_SCALAR\n"
197 << "scalar_type: "
198 << getCppType(StorageMode_Stack, &extra)
199 << "\n";
200 return OK;
201}
202
Andreas Huber85eabdb2016-08-25 11:24:49 -0700203void ScalarType::getAlignmentAndSize(size_t *align, size_t *size) const {
204 static const size_t kAlign[] = {
205 1, // bool, this is NOT standardized!
206 8, // void *, 64-bit mode
207 1, // int8_t
208 1, // uint8_t
209 2, // int16_t
210 2, // uint16_t
211 4, // int32_t
212 4, // uint32_t
213 8, // int64_t
214 8, // uint64_t
215 4, // float
216 8 // double
217 };
218
219 *align = *size = kAlign[mKind];
220}
221
Andreas Huberc9410c72016-07-28 12:18:40 -0700222} // namespace android
223