Type.cpp revision 244e82d34bf0ef5d8d5b0730a9dea3e3fc4de7c5
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, bool) const {
92    CHECK(!"Should not be here");
93    return std::string();
94}
95
96std::string Type::decorateCppName(
97        const std::string &name, StorageMode mode, bool specifyNamespaces) const {
98    return getCppType(mode, specifyNamespaces) + " " + name;
99}
100
101std::string Type::getJavaType(bool /* forInitializer */) const {
102    CHECK(!"Should not be here");
103    return std::string();
104}
105
106std::string Type::getJavaWrapperType() const {
107    return getJavaType();
108}
109
110std::string Type::getJavaSuffix() const {
111    CHECK(!"Should not be here");
112    return std::string();
113}
114
115std::string Type::getVtsType() const {
116    CHECK(!"Should not be here");
117    return std::string();
118}
119
120void Type::emitReaderWriter(
121        Formatter &,
122        const std::string &,
123        const std::string &,
124        bool,
125        bool,
126        ErrorMode) const {
127    CHECK(!"Should not be here");
128}
129
130void Type::emitResolveReferences(
131        Formatter &,
132        const std::string &,
133        bool,
134        const std::string &,
135        bool,
136        bool,
137        ErrorMode) const {
138    CHECK(!"Should not be here");
139}
140
141void Type::emitResolveReferencesEmbedded(
142        Formatter &,
143        size_t,
144        const std::string &,
145        const std::string &,
146        bool,
147        const std::string &,
148        bool,
149        bool,
150        ErrorMode,
151        const std::string &,
152        const std::string &) const {
153    CHECK(!"Should not be here");
154}
155
156bool Type::useParentInEmitResolveReferencesEmbedded() const {
157    return needsResolveReferences();
158}
159
160bool Type::useNameInEmitReaderWriterEmbedded(bool) const {
161    return needsEmbeddedReadWrite();
162}
163
164void Type::emitReaderWriterEmbedded(
165        Formatter &,
166        size_t,
167        const std::string &,
168        const std::string &,
169        bool,
170        const std::string &,
171        bool,
172        bool,
173        ErrorMode,
174        const std::string &,
175        const std::string &) const {
176    CHECK(!"Should not be here");
177}
178
179void Type::emitJavaReaderWriter(
180        Formatter &out,
181        const std::string &parcelObj,
182        const std::string &argName,
183        bool isReader) const {
184    emitJavaReaderWriterWithSuffix(
185            out,
186            parcelObj,
187            argName,
188            isReader,
189            getJavaSuffix(),
190            "" /* extra */);
191}
192
193void Type::emitJavaFieldInitializer(
194        Formatter &out,
195        const std::string &fieldName) const {
196    out << getJavaType()
197        << " "
198        << fieldName
199        << ";\n";
200}
201
202void Type::emitJavaFieldReaderWriter(
203        Formatter &,
204        size_t,
205        const std::string &,
206        const std::string &,
207        const std::string &,
208        const std::string &,
209        bool) const {
210    CHECK(!"Should not be here");
211}
212
213void Type::handleError(Formatter &out, ErrorMode mode) const {
214    switch (mode) {
215        case ErrorMode_Ignore:
216        {
217            out << "/* _hidl_err ignored! */\n\n";
218            break;
219        }
220
221        case ErrorMode_Goto:
222        {
223            out << "if (_hidl_err != ::android::OK) { goto _hidl_error; }\n\n";
224            break;
225        }
226
227        case ErrorMode_Break:
228        {
229            out << "if (_hidl_err != ::android::OK) { break; }\n\n";
230            break;
231        }
232
233        case ErrorMode_Return:
234        {
235            out << "if (_hidl_err != ::android::OK) { return _hidl_err; }\n\n";
236            break;
237        }
238    }
239}
240
241void Type::handleError2(Formatter &out, ErrorMode mode) const {
242    switch (mode) {
243        case ErrorMode_Goto:
244        {
245            out << "goto _hidl_error;\n";
246            break;
247        }
248
249        case ErrorMode_Break:
250        {
251            out << "break;\n";
252            break;
253        }
254
255        case ErrorMode_Ignore:
256        {
257            out << "/* ignoring _hidl_error! */";
258            break;
259        }
260
261        case ErrorMode_Return:
262        {
263            out << "return _hidl_err;\n";
264            break;
265        }
266    }
267}
268
269void Type::emitReaderWriterEmbeddedForTypeName(
270        Formatter &out,
271        const std::string &name,
272        bool nameIsPointer,
273        const std::string &parcelObj,
274        bool parcelObjIsPointer,
275        bool isReader,
276        ErrorMode mode,
277        const std::string &parentName,
278        const std::string &offsetText,
279        const std::string &typeName,
280        const std::string &childName,
281        const std::string &funcNamespace) const {
282
283        const std::string parcelObjDeref =
284        parcelObjIsPointer ? ("*" + parcelObj) : parcelObj;
285
286    const std::string parcelObjPointer =
287        parcelObjIsPointer ? parcelObj : ("&" + parcelObj);
288
289    const std::string nameDerefed = nameIsPointer ? ("*" + name) : name;
290    const std::string namePointer = nameIsPointer ? name : ("&" + name);
291
292    out << "_hidl_err = ";
293
294    if (!funcNamespace.empty()) {
295        out << funcNamespace << "::";
296    }
297
298    out << (isReader ? "readEmbeddedFromParcel(\n" : "writeEmbeddedToParcel(\n");
299
300    out.indent();
301    out.indent();
302
303    if (isReader) {
304        out << "const_cast<"
305            << typeName
306            << " *>("
307            << namePointer
308            << "),\n";
309    } else {
310        out << nameDerefed
311            << ",\n";
312    }
313
314    out << (isReader ? parcelObjDeref : parcelObjPointer)
315        << ",\n"
316        << parentName
317        << ",\n"
318        << offsetText;
319
320    if (!childName.empty()) {
321        out << ", &"
322            << childName;
323    }
324
325    out << ");\n\n";
326
327    out.unindent();
328    out.unindent();
329
330    handleError(out, mode);
331}
332
333status_t Type::emitTypeDeclarations(Formatter &) const {
334    return OK;
335}
336
337status_t Type::emitGlobalTypeDeclarations(Formatter &) const {
338    return OK;
339}
340
341status_t Type::emitGlobalHwDeclarations(Formatter &) const {
342    return OK;
343}
344
345status_t Type::emitTypeDefinitions(
346        Formatter &, const std::string) const {
347    return OK;
348}
349
350status_t Type::emitJavaTypeDeclarations(Formatter &, bool) const {
351    return OK;
352}
353
354bool Type::needsEmbeddedReadWrite() const {
355    return false;
356}
357
358bool Type::needsResolveReferences() const {
359    return false;
360}
361
362bool Type::resultNeedsDeref() const {
363    return false;
364}
365
366std::string Type::getCppStackType(bool specifyNamespaces) const {
367    return getCppType(StorageMode_Stack, specifyNamespaces);
368}
369
370std::string Type::getCppResultType(bool specifyNamespaces) const {
371    return getCppType(StorageMode_Result, specifyNamespaces);
372}
373
374std::string Type::getCppArgumentType(bool specifyNamespaces) const {
375    return getCppType(StorageMode_Argument, specifyNamespaces);
376}
377
378void Type::emitJavaReaderWriterWithSuffix(
379        Formatter &out,
380        const std::string &parcelObj,
381        const std::string &argName,
382        bool isReader,
383        const std::string &suffix,
384        const std::string &extra) const {
385    out << parcelObj
386        << "."
387        << (isReader ? "read" : "write")
388        << suffix
389        << "(";
390
391    if (isReader) {
392        out << extra;
393    } else {
394        out << (extra.empty() ? "" : (extra + ", "));
395        out << argName;
396    }
397
398    out << ");\n";
399}
400
401status_t Type::emitVtsTypeDeclarations(Formatter &) const {
402    return OK;
403}
404
405status_t Type::emitVtsAttributeType(Formatter &out) const {
406    return emitVtsTypeDeclarations(out);
407}
408
409bool Type::isJavaCompatible() const {
410    return true;
411}
412
413void Type::getAlignmentAndSize(size_t *, size_t *) const {
414    CHECK(!"Should not be here");
415}
416
417void Type::appendToExportedTypesVector(
418        std::vector<const Type *> * /* exportedTypes */) const {
419}
420
421status_t Type::emitExportedHeader(
422        Formatter & /* out */, bool /* forJava */) const {
423    return OK;
424}
425
426////////////////////////////////////////
427
428TemplatedType::TemplatedType() : mElementType(nullptr) {
429}
430void TemplatedType::setElementType(Type *elementType) {
431    CHECK(mElementType == nullptr); // can only be set once.
432    mElementType = elementType;
433}
434
435}  // namespace android
436
437