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