blob: bb535739e78a9fa7f57ab37788b5efc92faae8e6 [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 Huber7c5ddfb2016-09-29 13:45:22 -070024#include <vector>
Steven Moreland979e0992016-09-07 09:18:08 -070025#include <set>
Andreas Huberc9410c72016-07-28 12:18:40 -070026
Timur Iskhakov505316c2017-08-05 03:38:59 +000027#include "Reference.h"
28
Andreas Huberc9410c72016-07-28 12:18:40 -070029namespace android {
30
31struct Formatter;
Andreas Huber737080b2016-08-02 15:38:04 -070032struct ScalarType;
Steven Moreland979e0992016-09-07 09:18:08 -070033struct FQName;
Andreas Huberc9410c72016-07-28 12:18:40 -070034
35struct Type {
36 Type();
37 virtual ~Type();
38
Andreas Huber709b62d2016-09-19 11:21:18 -070039 virtual bool isArray() const;
Martijn Coenen99e6beb2016-12-01 15:48:42 +010040 virtual bool isBinder() const;
Yifan Hongabf73ee2016-12-05 18:47:00 -080041 virtual bool isBitField() const;
Martijn Coenen99e6beb2016-12-01 15:48:42 +010042 virtual bool isCompoundType() const;
43 virtual bool isEnum() const;
Yifan Hongabf73ee2016-12-05 18:47:00 -080044 virtual bool isHandle() const;
Martijn Coenen99e6beb2016-12-01 15:48:42 +010045 virtual bool isInterface() const;
46 virtual bool isNamedType() const;
Steven Moreland397b5e12017-06-08 14:02:26 -070047 virtual bool isMemory() const;
Martijn Coenen99e6beb2016-12-01 15:48:42 +010048 virtual bool isPointer() const;
49 virtual bool isScope() const;
Yifan Hongabf73ee2016-12-05 18:47:00 -080050 virtual bool isScalar() const;
51 virtual bool isString() const;
52 virtual bool isTemplatedType() const;
Martijn Coenen99e6beb2016-12-01 15:48:42 +010053 virtual bool isTypeDef() const;
Andreas Huber709b62d2016-09-19 11:21:18 -070054 virtual bool isVector() const;
Andreas Huber8d3ac0c2016-08-04 14:49:23 -070055
Andreas Huber737080b2016-08-02 15:38:04 -070056 virtual const ScalarType *resolveToScalarType() const;
Andreas Huberc9410c72016-07-28 12:18:40 -070057
Steven Moreland0ecc7b82017-07-19 12:59:23 -070058 virtual std::string typeName() const = 0;
Steven Moreland30bb6a82016-11-30 09:18:34 -080059
Andreas Huber8d3ac0c2016-08-04 14:49:23 -070060 bool isValidEnumStorageType() const;
Steven Moreland9df52442016-12-12 08:51:14 -080061 virtual bool isElidableType() const;
Yifan Hongc6752dc2016-12-20 14:00:14 -080062 virtual bool canCheckEquality() const;
Andreas Huber8d3ac0c2016-08-04 14:49:23 -070063
Andreas Huber881227d2016-08-02 14:20:21 -070064 enum StorageMode {
65 StorageMode_Stack,
66 StorageMode_Argument,
Martijn Coenenac587892016-11-17 15:14:19 +010067 StorageMode_Result,
Andreas Huber881227d2016-08-02 14:20:21 -070068 };
Yifan Hong3b320f82016-11-01 15:15:54 -070069
Steven Morelande30ee9b2017-05-09 13:31:01 -070070 // specifyNamespaces: whether to specify namespaces for built-in types
Andreas Huber881227d2016-08-02 14:20:21 -070071 virtual std::string getCppType(
Steven Moreland979e0992016-09-07 09:18:08 -070072 StorageMode mode,
Yifan Hong3b320f82016-11-01 15:15:54 -070073 bool specifyNamespaces) const;
74
75 std::string decorateCppName(
76 const std::string &name,
77 StorageMode mode,
Steven Moreland979e0992016-09-07 09:18:08 -070078 bool specifyNamespaces) const;
79
Yifan Hong3b320f82016-11-01 15:15:54 -070080 std::string getCppStackType(bool specifyNamespaces = true) const;
Andreas Huber881227d2016-08-02 14:20:21 -070081
Yifan Hong3b320f82016-11-01 15:15:54 -070082 std::string getCppResultType(bool specifyNamespaces = true) const;
Andreas Huber881227d2016-08-02 14:20:21 -070083
Yifan Hong3b320f82016-11-01 15:15:54 -070084 std::string getCppArgumentType(bool specifyNamespaces = true) const;
Andreas Huber4c865b72016-09-14 15:26:27 -070085
Yifan Hong4ed13472016-11-02 10:44:11 -070086 // For an array type, dimensionality information will be accumulated at the
87 // end of the returned string.
Andreas Huber4c865b72016-09-14 15:26:27 -070088 // if forInitializer == true, actual dimensions are included, i.e. [3][5],
89 // otherwise (and by default), they are omitted, i.e. [][].
Yifan Hong4ed13472016-11-02 10:44:11 -070090 virtual std::string getJavaType(bool forInitializer = false) const;
Andreas Huber4c865b72016-09-14 15:26:27 -070091
Andreas Huber85eabdb2016-08-25 11:24:49 -070092 virtual std::string getJavaWrapperType() const;
Andreas Huber2831d512016-08-15 09:33:47 -070093 virtual std::string getJavaSuffix() const;
94
Zhuoyao Zhangc5ea9f52016-10-06 15:05:39 -070095 virtual std::string getVtsType() const;
Zhuoyao Zhange9667842017-01-19 12:35:32 -080096 virtual std::string getVtsValueName() const;
Zhuoyao Zhangc5ea9f52016-10-06 15:05:39 -070097
Andreas Huber881227d2016-08-02 14:20:21 -070098 enum ErrorMode {
99 ErrorMode_Ignore,
100 ErrorMode_Goto,
101 ErrorMode_Break,
Andreas Huber737080b2016-08-02 15:38:04 -0700102 ErrorMode_Return,
Andreas Huber881227d2016-08-02 14:20:21 -0700103 };
104 virtual void emitReaderWriter(
105 Formatter &out,
106 const std::string &name,
107 const std::string &parcelObj,
108 bool parcelObjIsPointer,
109 bool isReader,
110 ErrorMode mode) const;
111
112 virtual void emitReaderWriterEmbedded(
113 Formatter &out,
Andreas Huberf9d49f12016-09-12 14:58:36 -0700114 size_t depth,
Andreas Huber881227d2016-08-02 14:20:21 -0700115 const std::string &name,
Yifan Hongbe2a3732016-10-05 13:33:41 -0700116 const std::string &sanitizedName,
Andreas Huber881227d2016-08-02 14:20:21 -0700117 bool nameIsPointer,
118 const std::string &parcelObj,
119 bool parcelObjIsPointer,
120 bool isReader,
121 ErrorMode mode,
122 const std::string &parentName,
123 const std::string &offsetText) const;
124
Yifan Hongbf459bc2016-08-23 16:50:37 -0700125 virtual void emitResolveReferences(
126 Formatter &out,
127 const std::string &name,
128 bool nameIsPointer,
129 const std::string &parcelObj,
130 bool parcelObjIsPointer,
131 bool isReader,
132 ErrorMode mode) const;
133
134 virtual void emitResolveReferencesEmbedded(
135 Formatter &out,
136 size_t depth,
137 const std::string &name,
138 const std::string &sanitizedName,
139 bool nameIsPointer,
140 const std::string &parcelObj,
141 bool parcelObjIsPointer,
142 bool isReader,
143 ErrorMode mode,
144 const std::string &parentName,
145 const std::string &offsetText) const;
146
Yifan Hongf5cc2f72017-01-04 18:02:34 -0800147 virtual void emitDump(
148 Formatter &out,
149 const std::string &streamName,
150 const std::string &name) const;
151
Yifan Honge45b5302017-02-22 10:49:07 -0800152 virtual void emitJavaDump(
153 Formatter &out,
154 const std::string &streamName,
155 const std::string &name) const;
156
Yifan Hong00f47172016-09-30 14:40:45 -0700157 virtual bool useParentInEmitResolveReferencesEmbedded() const;
158
Yifan Hong244e82d2016-11-11 11:13:57 -0800159 virtual bool useNameInEmitReaderWriterEmbedded(bool isReader) const;
160
Andreas Huber2831d512016-08-15 09:33:47 -0700161 virtual void emitJavaReaderWriter(
162 Formatter &out,
163 const std::string &parcelObj,
164 const std::string &argName,
165 bool isReader) const;
166
Andreas Huber85eabdb2016-08-25 11:24:49 -0700167 virtual void emitJavaFieldInitializer(
168 Formatter &out,
169 const std::string &fieldName) const;
170
171 virtual void emitJavaFieldReaderWriter(
172 Formatter &out,
Andreas Huber4c865b72016-09-14 15:26:27 -0700173 size_t depth,
Andreas Huber709b62d2016-09-19 11:21:18 -0700174 const std::string &parcelName,
Andreas Huber85eabdb2016-08-25 11:24:49 -0700175 const std::string &blobName,
176 const std::string &fieldName,
177 const std::string &offset,
178 bool isReader) const;
179
Andreas Huber881227d2016-08-02 14:20:21 -0700180 virtual status_t emitTypeDeclarations(Formatter &out) const;
181
Andreas Hubere3f769a2016-10-10 10:54:44 -0700182 // Emit any declarations pertaining to this type that have to be
183 // at global scope, i.e. enum class operators.
184 virtual status_t emitGlobalTypeDeclarations(Formatter &out) const;
185
Yifan Hong244e82d2016-11-11 11:13:57 -0800186 // Emit any declarations pertaining to this type that have to be
187 // at global scope for transport, e.g. read/writeEmbeddedTo/FromParcel
188 virtual status_t emitGlobalHwDeclarations(Formatter &out) const;
189
Chih-Hung Hsieh8c90cc52017-08-03 14:51:13 -0700190 virtual status_t emitTypeDefinitions(Formatter& out, const std::string& prefix) const;
Andreas Huber881227d2016-08-02 14:20:21 -0700191
Andreas Huber85eabdb2016-08-25 11:24:49 -0700192 virtual status_t emitJavaTypeDeclarations(
193 Formatter &out, bool atTopLevel) const;
Andreas Huber2831d512016-08-15 09:33:47 -0700194
Andreas Huber881227d2016-08-02 14:20:21 -0700195 virtual bool needsEmbeddedReadWrite() const;
Yifan Hongbf459bc2016-08-23 16:50:37 -0700196 virtual bool needsResolveReferences() const;
Andreas Huber881227d2016-08-02 14:20:21 -0700197 virtual bool resultNeedsDeref() const;
198
Zhuoyao Zhang5158db42016-08-10 10:25:20 -0700199 // Generates type declaration for vts proto file.
200 // TODO (b/30844146): make it a pure virtual method.
201 virtual status_t emitVtsTypeDeclarations(Formatter &out) const;
Zhuoyao Zhang864c7712016-08-16 15:35:28 -0700202 // Generates type declaration as attribute of method (return value or method
203 // argument) or attribute of compound type for vts proto file.
204 virtual status_t emitVtsAttributeType(Formatter &out) const;
Zhuoyao Zhang5158db42016-08-10 10:25:20 -0700205
Andreas Huber70a59e12016-08-16 12:57:01 -0700206 // Returns true iff this type is supported through the Java backend.
207 virtual bool isJavaCompatible() const;
Andreas Huber60d3b222017-03-30 09:10:56 -0700208 virtual bool containsPointer() const;
Andreas Huber85eabdb2016-08-25 11:24:49 -0700209 virtual void getAlignmentAndSize(size_t *align, size_t *size) const;
210
Andreas Huber019d21d2016-10-03 12:59:47 -0700211 virtual void appendToExportedTypesVector(
212 std::vector<const Type *> *exportedTypes) const;
213
Andreas Huber1c507272016-10-05 14:33:21 -0700214 virtual status_t emitExportedHeader(Formatter &out, bool forJava) const;
Andreas Huber019d21d2016-10-03 12:59:47 -0700215
Andreas Huber881227d2016-08-02 14:20:21 -0700216protected:
217 void handleError(Formatter &out, ErrorMode mode) const;
Andreas Huber881227d2016-08-02 14:20:21 -0700218
219 void emitReaderWriterEmbeddedForTypeName(
220 Formatter &out,
221 const std::string &name,
222 bool nameIsPointer,
223 const std::string &parcelObj,
224 bool parcelObjIsPointer,
225 bool isReader,
226 ErrorMode mode,
227 const std::string &parentName,
228 const std::string &offsetText,
229 const std::string &typeName,
Yifan Hong244e82d2016-11-11 11:13:57 -0800230 const std::string &childName,
231 const std::string &funcNamespace) const;
Andreas Huber881227d2016-08-02 14:20:21 -0700232
Andreas Huber2831d512016-08-15 09:33:47 -0700233 void emitJavaReaderWriterWithSuffix(
234 Formatter &out,
235 const std::string &parcelObj,
236 const std::string &argName,
237 bool isReader,
238 const std::string &suffix,
239 const std::string &extra) const;
240
Yifan Hongf5cc2f72017-01-04 18:02:34 -0800241 void emitDumpWithMethod(
242 Formatter &out,
243 const std::string &streamName,
244 const std::string &methodName,
245 const std::string &name) const;
246
Andreas Huberc9410c72016-07-28 12:18:40 -0700247private:
Andreas Huberc9410c72016-07-28 12:18:40 -0700248 DISALLOW_COPY_AND_ASSIGN(Type);
249};
250
Yifan Hongbf459bc2016-08-23 16:50:37 -0700251/* Base type for VectorType and RefType. */
252struct TemplatedType : public Type {
Timur Iskhakov505316c2017-08-05 03:38:59 +0000253 void setElementType(const Reference<Type>& elementType);
254 Type* getElementType() const;
Yifan Hongabf73ee2016-12-05 18:47:00 -0800255 bool isTemplatedType() const override;
Timur Iskhakov505316c2017-08-05 03:38:59 +0000256 virtual bool isCompatibleElementType(Type* elementType) const = 0;
Zhuoyao Zhange9667842017-01-19 12:35:32 -0800257 status_t emitVtsTypeDeclarations(Formatter &out) const override;
258 status_t emitVtsAttributeType(Formatter &out) const override;
Yifan Hongbf459bc2016-08-23 16:50:37 -0700259protected:
260 TemplatedType();
Timur Iskhakov505316c2017-08-05 03:38:59 +0000261 Reference<Type> mElementType;
262
263 private:
Yifan Hongbf459bc2016-08-23 16:50:37 -0700264 DISALLOW_COPY_AND_ASSIGN(TemplatedType);
265};
266
Andreas Huberc9410c72016-07-28 12:18:40 -0700267} // namespace android
268
269#endif // TYPE_H_
270