Type.cpp revision e9667849dd310995d1e4d12f31942f68bf992d1c
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::isScalar() const {
50    return false;
51}
52
53bool Type::isString() const {
54    return false;
55}
56
57bool Type::isEnum() const {
58    return false;
59}
60
61bool Type::isBitField() const {
62    return false;
63}
64
65bool Type::isHandle() const {
66    return false;
67}
68
69bool Type::isTypeDef() const {
70    return false;
71}
72
73bool Type::isBinder() const {
74    return false;
75}
76
77bool Type::isNamedType() const {
78    return false;
79}
80
81bool Type::isCompoundType() const {
82    return false;
83}
84
85bool Type::isArray() const {
86    return false;
87}
88
89bool Type::isVector() const {
90    return false;
91}
92
93bool Type::isTemplatedType() const {
94    return false;
95}
96
97bool Type::isPointer() const {
98    return false;
99}
100
101std::string Type::typeName() const {
102    return "";
103}
104
105const ScalarType *Type::resolveToScalarType() const {
106    return NULL;
107}
108
109bool Type::isValidEnumStorageType() const {
110    const ScalarType *scalarType = resolveToScalarType();
111
112    if (scalarType == NULL) {
113        return false;
114    }
115
116    return scalarType->isValidEnumStorageType();
117}
118
119bool Type::isElidableType() const {
120    return false;
121}
122
123bool Type::canCheckEquality() const {
124    return false;
125}
126
127std::string Type::getCppType(StorageMode, bool) const {
128    CHECK(!"Should not be here");
129    return std::string();
130}
131
132std::string Type::decorateCppName(
133        const std::string &name, StorageMode mode, bool specifyNamespaces) const {
134    return getCppType(mode, specifyNamespaces) + " " + name;
135}
136
137std::string Type::getJavaType(bool /* forInitializer */) const {
138    CHECK(!"Should not be here");
139    return std::string();
140}
141
142std::string Type::getJavaWrapperType() const {
143    return getJavaType();
144}
145
146std::string Type::getJavaSuffix() const {
147    CHECK(!"Should not be here");
148    return std::string();
149}
150
151std::string Type::getVtsType() const {
152    CHECK(!"Should not be here");
153    return std::string();
154}
155
156std::string Type::getVtsValueName() const {
157    CHECK(!"Should not be here");
158    return std::string();
159}
160
161void Type::emitReaderWriter(
162        Formatter &,
163        const std::string &,
164        const std::string &,
165        bool,
166        bool,
167        ErrorMode) const {
168    CHECK(!"Should not be here");
169}
170
171void Type::emitResolveReferences(
172        Formatter &,
173        const std::string &,
174        bool,
175        const std::string &,
176        bool,
177        bool,
178        ErrorMode) const {
179    CHECK(!"Should not be here");
180}
181
182void Type::emitResolveReferencesEmbedded(
183        Formatter &,
184        size_t,
185        const std::string &,
186        const std::string &,
187        bool,
188        const std::string &,
189        bool,
190        bool,
191        ErrorMode,
192        const std::string &,
193        const std::string &) const {
194    CHECK(!"Should not be here");
195}
196
197void Type::emitDump(
198        Formatter &out,
199        const std::string &streamName,
200        const std::string &name) const {
201    emitDumpWithMethod(out, streamName, "::android::hardware::details::toString", name);
202}
203
204void Type::emitDumpWithMethod(
205        Formatter &out,
206        const std::string &streamName,
207        const std::string &methodName,
208        const std::string &name) const {
209    out << streamName
210        << " += "
211        << methodName
212        << "("
213        << name
214        << ");\n";
215}
216
217bool Type::useParentInEmitResolveReferencesEmbedded() const {
218    return needsResolveReferences();
219}
220
221bool Type::useNameInEmitReaderWriterEmbedded(bool) const {
222    return needsEmbeddedReadWrite();
223}
224
225void Type::emitReaderWriterEmbedded(
226        Formatter &,
227        size_t,
228        const std::string &,
229        const std::string &,
230        bool,
231        const std::string &,
232        bool,
233        bool,
234        ErrorMode,
235        const std::string &,
236        const std::string &) const {
237    CHECK(!"Should not be here");
238}
239
240void Type::emitJavaReaderWriter(
241        Formatter &out,
242        const std::string &parcelObj,
243        const std::string &argName,
244        bool isReader) const {
245    emitJavaReaderWriterWithSuffix(
246            out,
247            parcelObj,
248            argName,
249            isReader,
250            getJavaSuffix(),
251            "" /* extra */);
252}
253
254void Type::emitJavaFieldInitializer(
255        Formatter &out,
256        const std::string &fieldName) const {
257    out << getJavaType()
258        << " "
259        << fieldName
260        << ";\n";
261}
262
263void Type::emitJavaFieldReaderWriter(
264        Formatter &,
265        size_t,
266        const std::string &,
267        const std::string &,
268        const std::string &,
269        const std::string &,
270        bool) const {
271    CHECK(!"Should not be here");
272}
273
274void Type::handleError(Formatter &out, ErrorMode mode) const {
275    switch (mode) {
276        case ErrorMode_Ignore:
277        {
278            out << "/* _hidl_err ignored! */\n\n";
279            break;
280        }
281
282        case ErrorMode_Goto:
283        {
284            out << "if (_hidl_err != ::android::OK) { goto _hidl_error; }\n\n";
285            break;
286        }
287
288        case ErrorMode_Break:
289        {
290            out << "if (_hidl_err != ::android::OK) { break; }\n\n";
291            break;
292        }
293
294        case ErrorMode_Return:
295        {
296            out << "if (_hidl_err != ::android::OK) { return _hidl_err; }\n\n";
297            break;
298        }
299    }
300}
301
302void Type::emitReaderWriterEmbeddedForTypeName(
303        Formatter &out,
304        const std::string &name,
305        bool nameIsPointer,
306        const std::string &parcelObj,
307        bool parcelObjIsPointer,
308        bool isReader,
309        ErrorMode mode,
310        const std::string &parentName,
311        const std::string &offsetText,
312        const std::string &typeName,
313        const std::string &childName,
314        const std::string &funcNamespace) const {
315
316        const std::string parcelObjDeref =
317        parcelObjIsPointer ? ("*" + parcelObj) : parcelObj;
318
319    const std::string parcelObjPointer =
320        parcelObjIsPointer ? parcelObj : ("&" + parcelObj);
321
322    const std::string nameDerefed = nameIsPointer ? ("*" + name) : name;
323    const std::string namePointer = nameIsPointer ? name : ("&" + name);
324
325    out << "_hidl_err = ";
326
327    if (!funcNamespace.empty()) {
328        out << funcNamespace << "::";
329    }
330
331    out << (isReader ? "readEmbeddedFromParcel(\n" : "writeEmbeddedToParcel(\n");
332
333    out.indent();
334    out.indent();
335
336    if (isReader) {
337        out << "const_cast<"
338            << typeName
339            << " *>("
340            << namePointer
341            << "),\n";
342    } else {
343        out << nameDerefed
344            << ",\n";
345    }
346
347    out << (isReader ? parcelObjDeref : parcelObjPointer)
348        << ",\n"
349        << parentName
350        << ",\n"
351        << offsetText;
352
353    if (!childName.empty()) {
354        out << ", &"
355            << childName;
356    }
357
358    out << ");\n\n";
359
360    out.unindent();
361    out.unindent();
362
363    handleError(out, mode);
364}
365
366status_t Type::emitTypeDeclarations(Formatter &) const {
367    return OK;
368}
369
370status_t Type::emitGlobalTypeDeclarations(Formatter &) const {
371    return OK;
372}
373
374status_t Type::emitGlobalHwDeclarations(Formatter &) const {
375    return OK;
376}
377
378status_t Type::emitTypeDefinitions(
379        Formatter &, const std::string) const {
380    return OK;
381}
382
383status_t Type::emitJavaTypeDeclarations(Formatter &, bool) const {
384    return OK;
385}
386
387bool Type::needsEmbeddedReadWrite() const {
388    return false;
389}
390
391bool Type::needsResolveReferences() const {
392    return false;
393}
394
395bool Type::resultNeedsDeref() const {
396    return false;
397}
398
399std::string Type::getCppStackType(bool specifyNamespaces) const {
400    return getCppType(StorageMode_Stack, specifyNamespaces);
401}
402
403std::string Type::getCppResultType(bool specifyNamespaces) const {
404    return getCppType(StorageMode_Result, specifyNamespaces);
405}
406
407std::string Type::getCppArgumentType(bool specifyNamespaces) const {
408    return getCppType(StorageMode_Argument, specifyNamespaces);
409}
410
411void Type::emitJavaReaderWriterWithSuffix(
412        Formatter &out,
413        const std::string &parcelObj,
414        const std::string &argName,
415        bool isReader,
416        const std::string &suffix,
417        const std::string &extra) const {
418    out << parcelObj
419        << "."
420        << (isReader ? "read" : "write")
421        << suffix
422        << "(";
423
424    if (isReader) {
425        out << extra;
426    } else {
427        out << (extra.empty() ? "" : (extra + ", "));
428        out << argName;
429    }
430
431    out << ");\n";
432}
433
434status_t Type::emitVtsTypeDeclarations(Formatter &) const {
435    return OK;
436}
437
438status_t Type::emitVtsAttributeType(Formatter &out) const {
439    return emitVtsTypeDeclarations(out);
440}
441
442bool Type::isJavaCompatible() const {
443    return true;
444}
445
446void Type::getAlignmentAndSize(size_t *, size_t *) const {
447    CHECK(!"Should not be here");
448}
449
450void Type::appendToExportedTypesVector(
451        std::vector<const Type *> * /* exportedTypes */) const {
452}
453
454status_t Type::emitExportedHeader(
455        Formatter & /* out */, bool /* forJava */) const {
456    return OK;
457}
458
459////////////////////////////////////////
460
461TemplatedType::TemplatedType() : mElementType(nullptr) {
462}
463
464void TemplatedType::setElementType(Type *elementType) {
465    CHECK(mElementType == nullptr); // can only be set once.
466    CHECK(isCompatibleElementType(elementType));
467    mElementType = elementType;
468}
469
470Type *TemplatedType::getElementType() const {
471    return mElementType;
472}
473
474bool TemplatedType::isTemplatedType() const {
475    return true;
476}
477
478status_t TemplatedType::emitVtsTypeDeclarations(Formatter &out) const {
479    out << "type: " << getVtsType() << "\n";
480    out << getVtsValueName() << ": {\n";
481    out.indent();
482    status_t err = mElementType->emitVtsTypeDeclarations(out);
483    if (err != OK) {
484        return err;
485    }
486    out.unindent();
487    out << "}\n";
488    return OK;
489}
490
491status_t TemplatedType::emitVtsAttributeType(Formatter &out) const {
492    out << "type: " << getVtsType() << "\n";
493    out << getVtsValueName() << ": {\n";
494    out.indent();
495    status_t status = mElementType->emitVtsAttributeType(out);
496    if (status != OK) {
497        return status;
498    }
499    out.unindent();
500    out << "}\n";
501    return OK;
502}
503}  // namespace android
504
505