blob: 7cfacd56baab6c25fe11c83aeef0a8f77a566bd3 [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
60std::string ScalarType::getJavaSuffix() const {
61 static const char *const kSuffix[] = {
Andreas Huberd85a5092016-08-25 09:51:52 -070062 "Bool",
Andreas Huber2831d512016-08-15 09:33:47 -070063 "Pointer",
64 "Int8",
65 "Int8",
66 "Int16",
67 "Int16",
68 "Int32",
69 "Int32",
70 "Int64",
71 "Int64",
72 "Float",
73 "Double"
74 };
75
76 return kSuffix[mKind];
77}
78
Andreas Huber881227d2016-08-02 14:20:21 -070079void ScalarType::emitReaderWriter(
80 Formatter &out,
81 const std::string &name,
82 const std::string &parcelObj,
83 bool parcelObjIsPointer,
84 bool isReader,
85 ErrorMode mode) const {
Andreas Huber737080b2016-08-02 15:38:04 -070086 emitReaderWriterWithCast(
87 out,
88 name,
89 parcelObj,
90 parcelObjIsPointer,
91 isReader,
92 mode,
93 false /* needsCast */);
94}
95
96void ScalarType::emitReaderWriterWithCast(
97 Formatter &out,
98 const std::string &name,
99 const std::string &parcelObj,
100 bool parcelObjIsPointer,
101 bool isReader,
102 ErrorMode mode,
103 bool needsCast) const {
Andreas Huber881227d2016-08-02 14:20:21 -0700104 static const char *const kSuffix[] = {
Iliyan Malcheve0b28672016-08-14 13:35:12 -0700105 "Bool",
Andreas Huber881227d2016-08-02 14:20:21 -0700106 "Pointer",
107 "Int8",
108 "Uint8",
109 "Int16",
110 "Uint16",
111 "Int32",
112 "Uint32",
113 "Int64",
114 "Uint64",
115 "Float",
116 "Double"
117 };
118
119 const std::string parcelObjDeref =
120 parcelObj + (parcelObjIsPointer ? "->" : ".");
121
Iliyan Malchev549e2592016-08-10 08:59:12 -0700122 out << "_hidl_err = "
Andreas Huber881227d2016-08-02 14:20:21 -0700123 << parcelObjDeref
124 << (isReader ? "read" : "write")
125 << kSuffix[mKind]
126 << "(";
127
Andreas Huber737080b2016-08-02 15:38:04 -0700128 if (needsCast) {
129 std::string extra;
130
131 out << "("
132 << Type::getCppType(&extra)
133 << (isReader ? " *)" : ")");
134 }
135
Andreas Huber881227d2016-08-02 14:20:21 -0700136 if (isReader) {
137 out << "&";
138 }
139
140 out << name
141 << ");\n";
142
143 handleError(out, mode);
144}
145
Zhuoyao Zhang5158db42016-08-10 10:25:20 -0700146status_t ScalarType::emitVtsTypeDeclarations(Formatter &out) const {
147 std::string extra;
148 out << "type: TYPE_SCALAR\n"
149 << "scalar_type: "
150 << getCppType(StorageMode_Stack, &extra)
151 << "\n";
152 return OK;
153}
154
Andreas Huberc9410c72016-07-28 12:18:40 -0700155} // namespace android
156