blob: b87168e3b20ed0bed0f8cbf41da9e20b163e670f [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[] = {
22 "char",
23 "bool",
24 "void *",
25 "int8_t",
26 "uint8_t",
27 "int16_t",
28 "uint16_t",
29 "int32_t",
30 "uint32_t",
31 "int64_t",
32 "uint64_t",
33 "float",
34 "double"
35 };
36
37 extra->clear();
38
39 return kName[mKind];
40}
41
Andreas Huber2831d512016-08-15 09:33:47 -070042std::string ScalarType::getJavaType() const {
43 static const char *const kName[] = {
44 "byte",
45 "boolean",
46 "long",
47 "byte",
48 "byte",
49 "short",
50 "short",
51 "int",
52 "int",
53 "long",
54 "long",
55 "float",
56 "double"
57 };
58
59 return kName[mKind];
60}
61
62std::string ScalarType::getJavaSuffix() const {
63 static const char *const kSuffix[] = {
64 "Int8",
65 "Int8",
66 "Pointer",
67 "Int8",
68 "Int8",
69 "Int16",
70 "Int16",
71 "Int32",
72 "Int32",
73 "Int64",
74 "Int64",
75 "Float",
76 "Double"
77 };
78
79 return kSuffix[mKind];
80}
81
Andreas Huber881227d2016-08-02 14:20:21 -070082void ScalarType::emitReaderWriter(
83 Formatter &out,
84 const std::string &name,
85 const std::string &parcelObj,
86 bool parcelObjIsPointer,
87 bool isReader,
88 ErrorMode mode) const {
Andreas Huber737080b2016-08-02 15:38:04 -070089 emitReaderWriterWithCast(
90 out,
91 name,
92 parcelObj,
93 parcelObjIsPointer,
94 isReader,
95 mode,
96 false /* needsCast */);
97}
98
99void ScalarType::emitReaderWriterWithCast(
100 Formatter &out,
101 const std::string &name,
102 const std::string &parcelObj,
103 bool parcelObjIsPointer,
104 bool isReader,
105 ErrorMode mode,
106 bool needsCast) const {
Andreas Huber881227d2016-08-02 14:20:21 -0700107 static const char *const kSuffix[] = {
108 "Uint8",
109 "Uint8",
110 "Pointer",
111 "Int8",
112 "Uint8",
113 "Int16",
114 "Uint16",
115 "Int32",
116 "Uint32",
117 "Int64",
118 "Uint64",
119 "Float",
120 "Double"
121 };
122
123 const std::string parcelObjDeref =
124 parcelObj + (parcelObjIsPointer ? "->" : ".");
125
Iliyan Malchev549e2592016-08-10 08:59:12 -0700126 out << "_hidl_err = "
Andreas Huber881227d2016-08-02 14:20:21 -0700127 << parcelObjDeref
128 << (isReader ? "read" : "write")
129 << kSuffix[mKind]
130 << "(";
131
Andreas Huber737080b2016-08-02 15:38:04 -0700132 if (needsCast) {
133 std::string extra;
134
135 out << "("
136 << Type::getCppType(&extra)
137 << (isReader ? " *)" : ")");
138 }
139
Andreas Huber881227d2016-08-02 14:20:21 -0700140 if (isReader) {
141 out << "&";
142 }
143
144 out << name
145 << ");\n";
146
147 handleError(out, mode);
148}
149
Zhuoyao Zhang5158db42016-08-10 10:25:20 -0700150
151status_t ScalarType::emitVtsTypeDeclarations(Formatter &out) const {
152 std::string extra;
153 out << "type: TYPE_SCALAR\n"
154 << "scalar_type: "
155 << getCppType(StorageMode_Stack, &extra)
156 << "\n";
157 return OK;
158}
159
Andreas Huberc9410c72016-07-28 12:18:40 -0700160} // namespace android
161