blob: 59c92ad6fe40b067ef4c14ed0fbdcf3ea591aaf8 [file] [log] [blame]
Andreas Huber1aec3972016-08-26 09:26:32 -07001/*
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
Andreas Huberc9410c72016-07-28 12:18:40 -070017#ifndef TYPE_H_
18
19#define TYPE_H_
20
21#include <android-base/macros.h>
Andreas Huber881227d2016-08-02 14:20:21 -070022#include <string>
23#include <utils/Errors.h>
Andreas Huberc9410c72016-07-28 12:18:40 -070024
25namespace android {
26
27struct Formatter;
Andreas Huber737080b2016-08-02 15:38:04 -070028struct ScalarType;
Andreas Huberc9410c72016-07-28 12:18:40 -070029
30struct Type {
31 Type();
32 virtual ~Type();
33
Andreas Huberfd4afab2016-08-03 13:02:57 -070034 Type *ref() { return this; }
35
Andreas Huber5345ec22016-07-29 13:33:27 -070036 virtual bool isScope() const;
Andreas Hubera2723d22016-07-29 15:36:07 -070037 virtual bool isInterface() const;
Andreas Huber8d3ac0c2016-08-04 14:49:23 -070038 virtual bool isEnum() const;
39 virtual bool isTypeDef() const;
Andreas Huber295ad302016-08-16 11:35:00 -070040 virtual bool isBinder() const;
Andreas Huber39fa7182016-08-19 14:27:33 -070041 virtual bool isNamedType() const;
Andreas Huber8d3ac0c2016-08-04 14:49:23 -070042
Andreas Huber737080b2016-08-02 15:38:04 -070043 virtual const ScalarType *resolveToScalarType() const;
Andreas Huberc9410c72016-07-28 12:18:40 -070044
Andreas Huber8d3ac0c2016-08-04 14:49:23 -070045 bool isValidEnumStorageType() const;
46
Andreas Huber881227d2016-08-02 14:20:21 -070047 enum StorageMode {
48 StorageMode_Stack,
49 StorageMode_Argument,
50 StorageMode_Result
51 };
52 virtual std::string getCppType(
53 StorageMode mode, std::string *extra) const;
54
55 // Convenience, gets StorageMode_Stack type.
56 std::string getCppType(std::string *extra) const;
57
58 std::string getCppResultType(std::string *extra) const;
59 std::string getCppArgumentType(std::string *extra) const;
60
Andreas Huber2831d512016-08-15 09:33:47 -070061 virtual std::string getJavaType() const = 0;
Andreas Huber85eabdb2016-08-25 11:24:49 -070062 virtual std::string getJavaWrapperType() const;
Andreas Huber2831d512016-08-15 09:33:47 -070063 virtual std::string getJavaSuffix() const;
64
Andreas Huber881227d2016-08-02 14:20:21 -070065 enum ErrorMode {
66 ErrorMode_Ignore,
67 ErrorMode_Goto,
68 ErrorMode_Break,
Andreas Huber737080b2016-08-02 15:38:04 -070069 ErrorMode_Return,
Andreas Huber881227d2016-08-02 14:20:21 -070070 };
71 virtual void emitReaderWriter(
72 Formatter &out,
73 const std::string &name,
74 const std::string &parcelObj,
75 bool parcelObjIsPointer,
76 bool isReader,
77 ErrorMode mode) const;
78
79 virtual void emitReaderWriterEmbedded(
80 Formatter &out,
81 const std::string &name,
82 bool nameIsPointer,
83 const std::string &parcelObj,
84 bool parcelObjIsPointer,
85 bool isReader,
86 ErrorMode mode,
87 const std::string &parentName,
88 const std::string &offsetText) const;
89
Andreas Huber2831d512016-08-15 09:33:47 -070090 virtual void emitJavaReaderWriter(
91 Formatter &out,
92 const std::string &parcelObj,
93 const std::string &argName,
94 bool isReader) const;
95
Andreas Huber85eabdb2016-08-25 11:24:49 -070096 virtual void emitJavaFieldInitializer(
97 Formatter &out,
98 const std::string &fieldName) const;
99
100 virtual void emitJavaFieldReaderWriter(
101 Formatter &out,
102 const std::string &blobName,
103 const std::string &fieldName,
104 const std::string &offset,
105 bool isReader) const;
106
Andreas Huber881227d2016-08-02 14:20:21 -0700107 virtual status_t emitTypeDeclarations(Formatter &out) const;
108
109 virtual status_t emitTypeDefinitions(
110 Formatter &out, const std::string prefix) const;
111
Andreas Huber85eabdb2016-08-25 11:24:49 -0700112 virtual status_t emitJavaTypeDeclarations(
113 Formatter &out, bool atTopLevel) const;
Andreas Huber2831d512016-08-15 09:33:47 -0700114
Andreas Huber881227d2016-08-02 14:20:21 -0700115 virtual bool needsEmbeddedReadWrite() const;
116 virtual bool resultNeedsDeref() const;
117
Zhuoyao Zhang5158db42016-08-10 10:25:20 -0700118 // Generates type declaration for vts proto file.
119 // TODO (b/30844146): make it a pure virtual method.
120 virtual status_t emitVtsTypeDeclarations(Formatter &out) const;
Zhuoyao Zhang864c7712016-08-16 15:35:28 -0700121 // Generates type declaration as attribute of method (return value or method
122 // argument) or attribute of compound type for vts proto file.
123 virtual status_t emitVtsAttributeType(Formatter &out) const;
Zhuoyao Zhang5158db42016-08-10 10:25:20 -0700124
Andreas Huber70a59e12016-08-16 12:57:01 -0700125 // Returns true iff this type is supported through the Java backend.
126 virtual bool isJavaCompatible() const;
127
Andreas Huber85eabdb2016-08-25 11:24:49 -0700128 virtual void getAlignmentAndSize(size_t *align, size_t *size) const;
129
Andreas Huber881227d2016-08-02 14:20:21 -0700130protected:
131 void handleError(Formatter &out, ErrorMode mode) const;
132 void handleError2(Formatter &out, ErrorMode mode) const;
133
134 void emitReaderWriterEmbeddedForTypeName(
135 Formatter &out,
136 const std::string &name,
137 bool nameIsPointer,
138 const std::string &parcelObj,
139 bool parcelObjIsPointer,
140 bool isReader,
141 ErrorMode mode,
142 const std::string &parentName,
143 const std::string &offsetText,
144 const std::string &typeName,
145 const std::string &childName) const;
146
Andreas Huber2831d512016-08-15 09:33:47 -0700147 void emitJavaReaderWriterWithSuffix(
148 Formatter &out,
149 const std::string &parcelObj,
150 const std::string &argName,
151 bool isReader,
152 const std::string &suffix,
153 const std::string &extra) const;
154
Andreas Huberc9410c72016-07-28 12:18:40 -0700155private:
156 DISALLOW_COPY_AND_ASSIGN(Type);
157};
158
159} // namespace android
160
161#endif // TYPE_H_
162