blob: 14fa4e2e0560c49a825b8cac200ad9feebc61469 [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
11void ScalarType::dump(Formatter &out) const {
12 static const char *const kName[] = {
13 "char", "bool", "opaque", "int8_t", "uint8_t", "int16_t", "uint16_t",
14 "int32_t", "uint32_t", "int64_t", "uint64_t", "float", "double"
15 };
16 out << kName[mKind];
17}
18
Andreas Huber737080b2016-08-02 15:38:04 -070019const ScalarType *ScalarType::resolveToScalarType() const {
20 return this;
21}
22
Andreas Huber881227d2016-08-02 14:20:21 -070023std::string ScalarType::getCppType(StorageMode, std::string *extra) const {
24 static const char *const kName[] = {
25 "char",
26 "bool",
27 "void *",
28 "int8_t",
29 "uint8_t",
30 "int16_t",
31 "uint16_t",
32 "int32_t",
33 "uint32_t",
34 "int64_t",
35 "uint64_t",
36 "float",
37 "double"
38 };
39
40 extra->clear();
41
42 return kName[mKind];
43}
44
45void ScalarType::emitReaderWriter(
46 Formatter &out,
47 const std::string &name,
48 const std::string &parcelObj,
49 bool parcelObjIsPointer,
50 bool isReader,
51 ErrorMode mode) const {
Andreas Huber737080b2016-08-02 15:38:04 -070052 emitReaderWriterWithCast(
53 out,
54 name,
55 parcelObj,
56 parcelObjIsPointer,
57 isReader,
58 mode,
59 false /* needsCast */);
60}
61
62void ScalarType::emitReaderWriterWithCast(
63 Formatter &out,
64 const std::string &name,
65 const std::string &parcelObj,
66 bool parcelObjIsPointer,
67 bool isReader,
68 ErrorMode mode,
69 bool needsCast) const {
Andreas Huber881227d2016-08-02 14:20:21 -070070 static const char *const kSuffix[] = {
71 "Uint8",
72 "Uint8",
73 "Pointer",
74 "Int8",
75 "Uint8",
76 "Int16",
77 "Uint16",
78 "Int32",
79 "Uint32",
80 "Int64",
81 "Uint64",
82 "Float",
83 "Double"
84 };
85
86 const std::string parcelObjDeref =
87 parcelObj + (parcelObjIsPointer ? "->" : ".");
88
89 out << "_aidl_err = "
90 << parcelObjDeref
91 << (isReader ? "read" : "write")
92 << kSuffix[mKind]
93 << "(";
94
Andreas Huber737080b2016-08-02 15:38:04 -070095 if (needsCast) {
96 std::string extra;
97
98 out << "("
99 << Type::getCppType(&extra)
100 << (isReader ? " *)" : ")");
101 }
102
Andreas Huber881227d2016-08-02 14:20:21 -0700103 if (isReader) {
104 out << "&";
105 }
106
107 out << name
108 << ");\n";
109
110 handleError(out, mode);
111}
112
Andreas Huberc9410c72016-07-28 12:18:40 -0700113} // namespace android
114