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