Type.cpp revision 019d21db821ee4ae6dd3858174a0a5cee4d33c25
1/*
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
17#include "Type.h"
18
19#include "Annotation.h"
20#include "ScalarType.h"
21
22#include <hidl-util/Formatter.h>
23#include <android-base/logging.h>
24
25namespace android {
26
27Type::Type()
28    : mAnnotations(nullptr) {
29}
30
31Type::~Type() {}
32
33void Type::setAnnotations(std::vector<Annotation *> *annotations) {
34    mAnnotations = annotations;
35}
36
37const std::vector<Annotation *> &Type::annotations() const {
38    return *mAnnotations;
39}
40
41bool Type::isScope() const {
42    return false;
43}
44
45bool Type::isInterface() const {
46    return false;
47}
48
49bool Type::isEnum() const {
50    return false;
51}
52
53bool Type::isTypeDef() const {
54    return false;
55}
56
57bool Type::isBinder() const {
58    return false;
59}
60
61bool Type::isNamedType() const {
62    return false;
63}
64
65bool Type::isCompoundType() const {
66    return false;
67}
68
69bool Type::isArray() const {
70    return false;
71}
72
73bool Type::isVector() const {
74    return false;
75}
76
77const ScalarType *Type::resolveToScalarType() const {
78    return NULL;
79}
80
81bool Type::isValidEnumStorageType() const {
82    const ScalarType *scalarType = resolveToScalarType();
83
84    if (scalarType == NULL) {
85        return false;
86    }
87
88    return scalarType->isValidEnumStorageType();
89}
90
91std::string Type::getCppType(StorageMode, std::string *, bool) const {
92    CHECK(!"Should not be here");
93    return std::string();
94}
95
96std::string Type::getJavaType(
97        std::string *extra, bool /* forInitializer */) const {
98    CHECK(!"Should not be here");
99    extra->clear();
100    return std::string();
101}
102
103std::string Type::getJavaWrapperType() const {
104    std::string extra;
105    return getJavaType(&extra);
106}
107
108std::string Type::getJavaSuffix() const {
109    CHECK(!"Should not be here");
110    return std::string();
111}
112
113void Type::emitReaderWriter(
114        Formatter &,
115        const std::string &,
116        const std::string &,
117        bool,
118        bool,
119        ErrorMode) const {
120    CHECK(!"Should not be here");
121}
122
123void Type::emitResolveReferences(
124        Formatter &,
125        const std::string &,
126        bool,
127        const std::string &,
128        bool,
129        bool,
130        ErrorMode) const {
131    CHECK(!"Should not be here");
132}
133
134void Type::emitResolveReferencesEmbedded(
135        Formatter &,
136        size_t,
137        const std::string &,
138        const std::string &,
139        bool,
140        const std::string &,
141        bool,
142        bool,
143        ErrorMode,
144        const std::string &,
145        const std::string &) const {
146    CHECK(!"Should not be here");
147}
148
149bool Type::useParentInEmitResolveReferencesEmbedded() const {
150    return true;
151}
152
153void Type::emitReaderWriterEmbedded(
154        Formatter &,
155        size_t,
156        const std::string &,
157        bool,
158        const std::string &,
159        bool,
160        bool,
161        ErrorMode,
162        const std::string &,
163        const std::string &) const {
164    CHECK(!"Should not be here");
165}
166
167void Type::emitJavaReaderWriter(
168        Formatter &out,
169        const std::string &parcelObj,
170        const std::string &argName,
171        bool isReader) const {
172    emitJavaReaderWriterWithSuffix(
173            out,
174            parcelObj,
175            argName,
176            isReader,
177            getJavaSuffix(),
178            "" /* extra */);
179}
180
181void Type::emitJavaFieldInitializer(
182        Formatter &out,
183        const std::string &fieldName) const {
184    std::string extra;
185    out << getJavaType(&extra)
186        << " "
187        << fieldName
188        << ";\n";
189}
190
191void Type::emitJavaFieldReaderWriter(
192        Formatter &,
193        size_t,
194        const std::string &,
195        const std::string &,
196        const std::string &,
197        const std::string &,
198        bool) const {
199    CHECK(!"Should not be here");
200}
201
202void Type::handleError(Formatter &out, ErrorMode mode) const {
203    switch (mode) {
204        case ErrorMode_Ignore:
205        {
206            out << "/* _hidl_err ignored! */\n\n";
207            break;
208        }
209
210        case ErrorMode_Goto:
211        {
212            out << "if (_hidl_err != ::android::OK) { goto _hidl_error; }\n\n";
213            break;
214        }
215
216        case ErrorMode_Break:
217        {
218            out << "if (_hidl_err != ::android::OK) { break; }\n\n";
219            break;
220        }
221
222        case ErrorMode_Return:
223        {
224            out << "if (_hidl_err != ::android::OK) { return _hidl_err; }\n\n";
225            break;
226        }
227    }
228}
229
230void Type::handleError2(Formatter &out, ErrorMode mode) const {
231    switch (mode) {
232        case ErrorMode_Goto:
233        {
234            out << "goto _hidl_error;\n";
235            break;
236        }
237
238        case ErrorMode_Break:
239        {
240            out << "break;\n";
241            break;
242        }
243
244        case ErrorMode_Ignore:
245        {
246            out << "/* ignoring _hidl_error! */";
247            break;
248        }
249
250        case ErrorMode_Return:
251        {
252            out << "return _hidl_err;\n";
253            break;
254        }
255    }
256}
257
258void Type::emitReaderWriterEmbeddedForTypeName(
259        Formatter &out,
260        const std::string &name,
261        bool nameIsPointer,
262        const std::string &parcelObj,
263        bool parcelObjIsPointer,
264        bool isReader,
265        ErrorMode mode,
266        const std::string &parentName,
267        const std::string &offsetText,
268        const std::string &typeName,
269        const std::string &childName) const {
270    const std::string parcelObjDeref =
271        parcelObjIsPointer ? ("*" + parcelObj) : parcelObj;
272
273    const std::string parcelObjPointer =
274        parcelObjIsPointer ? parcelObj : ("&" + parcelObj);
275
276    const std::string nameDeref = name + (nameIsPointer ? "->" : ".");
277    const std::string namePointer = nameIsPointer ? name : ("&" + name);
278
279    out << "_hidl_err = ";
280
281    if (isReader) {
282        out << "const_cast<"
283            << typeName
284            << " *>("
285            << namePointer
286            << ")->readEmbeddedFromParcel(\n";
287    } else {
288        out << nameDeref
289            << "writeEmbeddedToParcel(\n";
290    }
291
292    out.indent();
293    out.indent();
294
295    out << (isReader ? parcelObjDeref : parcelObjPointer)
296        << ",\n"
297        << parentName
298        << ",\n"
299        << offsetText;
300
301    if (!childName.empty()) {
302        out << ", &"
303            << childName;
304    }
305
306    out << ");\n\n";
307
308    out.unindent();
309    out.unindent();
310
311    handleError(out, mode);
312}
313
314status_t Type::emitTypeDeclarations(Formatter &) const {
315    return OK;
316}
317
318status_t Type::emitTypeDefinitions(
319        Formatter &, const std::string) const {
320    return OK;
321}
322
323status_t Type::emitJavaTypeDeclarations(Formatter &, bool) const {
324    return OK;
325}
326
327bool Type::needsEmbeddedReadWrite() const {
328    return false;
329}
330
331bool Type::needsResolveReferences() const {
332    return false;
333}
334
335bool Type::resultNeedsDeref() const {
336    return false;
337}
338
339std::string Type::getCppType(std::string *extra,
340                             bool specifyNamespaces) const {
341    return getCppType(StorageMode_Stack, extra, specifyNamespaces);
342}
343
344std::string Type::getCppResultType(std::string *extra,
345                                   bool specifyNamespaces) const {
346    return getCppType(StorageMode_Result, extra, specifyNamespaces);
347}
348
349std::string Type::getCppArgumentType(std::string *extra,
350                                     bool specifyNamespaces) const {
351    return getCppType(StorageMode_Argument, extra, specifyNamespaces);
352}
353
354void Type::emitJavaReaderWriterWithSuffix(
355        Formatter &out,
356        const std::string &parcelObj,
357        const std::string &argName,
358        bool isReader,
359        const std::string &suffix,
360        const std::string &extra) const {
361    out << parcelObj
362        << "."
363        << (isReader ? "read" : "write")
364        << suffix
365        << "(";
366
367    if (isReader) {
368        out << extra;
369    } else {
370        out << (extra.empty() ? "" : (extra + ", "));
371        out << argName;
372    }
373
374    out << ");\n";
375}
376
377status_t Type::emitVtsTypeDeclarations(Formatter &) const {
378    return OK;
379}
380
381status_t Type::emitVtsAttributeType(Formatter &out) const {
382    return emitVtsTypeDeclarations(out);
383}
384
385bool Type::isJavaCompatible() const {
386    return true;
387}
388
389void Type::getAlignmentAndSize(size_t *, size_t *) const {
390    CHECK(!"Should not be here");
391}
392
393void Type::appendToExportedTypesVector(
394        std::vector<const Type *> * /* exportedTypes */) const {
395}
396
397status_t Type::emitExportedHeader(Formatter & /* out */) const {
398    return OK;
399}
400
401////////////////////////////////////////
402
403TemplatedType::TemplatedType() : mElementType(nullptr) {
404}
405void TemplatedType::setElementType(Type *elementType) {
406    CHECK(mElementType == nullptr); // can only be set once.
407    mElementType = elementType;
408}
409
410}  // namespace android
411
412