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