blob: 63c9a4451811823f6f6a63f699887391ec63e5af [file] [log] [blame]
Andreas Huberc9410c72016-07-28 12:18:40 -07001#ifndef TYPE_H_
2
3#define TYPE_H_
4
5#include <android-base/macros.h>
Andreas Huber881227d2016-08-02 14:20:21 -07006#include <string>
7#include <utils/Errors.h>
Andreas Huberc9410c72016-07-28 12:18:40 -07008
9namespace android {
10
11struct Formatter;
Andreas Huber737080b2016-08-02 15:38:04 -070012struct ScalarType;
Andreas Huberc9410c72016-07-28 12:18:40 -070013
14struct Type {
15 Type();
16 virtual ~Type();
17
Andreas Huberfd4afab2016-08-03 13:02:57 -070018 Type *ref() { return this; }
19
Andreas Huber5345ec22016-07-29 13:33:27 -070020 virtual bool isScope() const;
Andreas Hubera2723d22016-07-29 15:36:07 -070021 virtual bool isInterface() const;
Andreas Huber8d3ac0c2016-08-04 14:49:23 -070022 virtual bool isEnum() const;
23 virtual bool isTypeDef() const;
Andreas Huber295ad302016-08-16 11:35:00 -070024 virtual bool isBinder() const;
Andreas Huber39fa7182016-08-19 14:27:33 -070025 virtual bool isNamedType() const;
Andreas Huber8d3ac0c2016-08-04 14:49:23 -070026
Andreas Huber737080b2016-08-02 15:38:04 -070027 virtual const ScalarType *resolveToScalarType() const;
Andreas Huberc9410c72016-07-28 12:18:40 -070028
Andreas Huber8d3ac0c2016-08-04 14:49:23 -070029 bool isValidEnumStorageType() const;
30
Andreas Huber881227d2016-08-02 14:20:21 -070031 enum StorageMode {
32 StorageMode_Stack,
33 StorageMode_Argument,
34 StorageMode_Result
35 };
36 virtual std::string getCppType(
37 StorageMode mode, std::string *extra) const;
38
39 // Convenience, gets StorageMode_Stack type.
40 std::string getCppType(std::string *extra) const;
41
42 std::string getCppResultType(std::string *extra) const;
43 std::string getCppArgumentType(std::string *extra) const;
44
Andreas Huber2831d512016-08-15 09:33:47 -070045 virtual std::string getJavaType() const = 0;
Andreas Huber85eabdb2016-08-25 11:24:49 -070046 virtual std::string getJavaWrapperType() const;
Andreas Huber2831d512016-08-15 09:33:47 -070047 virtual std::string getJavaSuffix() const;
48
Andreas Huber881227d2016-08-02 14:20:21 -070049 enum ErrorMode {
50 ErrorMode_Ignore,
51 ErrorMode_Goto,
52 ErrorMode_Break,
Andreas Huber737080b2016-08-02 15:38:04 -070053 ErrorMode_Return,
Andreas Huber881227d2016-08-02 14:20:21 -070054 };
55 virtual void emitReaderWriter(
56 Formatter &out,
57 const std::string &name,
58 const std::string &parcelObj,
59 bool parcelObjIsPointer,
60 bool isReader,
61 ErrorMode mode) const;
62
63 virtual void emitReaderWriterEmbedded(
64 Formatter &out,
65 const std::string &name,
66 bool nameIsPointer,
67 const std::string &parcelObj,
68 bool parcelObjIsPointer,
69 bool isReader,
70 ErrorMode mode,
71 const std::string &parentName,
72 const std::string &offsetText) const;
73
Andreas Huber2831d512016-08-15 09:33:47 -070074 virtual void emitJavaReaderWriter(
75 Formatter &out,
76 const std::string &parcelObj,
77 const std::string &argName,
78 bool isReader) const;
79
Andreas Huber85eabdb2016-08-25 11:24:49 -070080 virtual void emitJavaFieldInitializer(
81 Formatter &out,
82 const std::string &fieldName) const;
83
84 virtual void emitJavaFieldReaderWriter(
85 Formatter &out,
86 const std::string &blobName,
87 const std::string &fieldName,
88 const std::string &offset,
89 bool isReader) const;
90
Andreas Huber881227d2016-08-02 14:20:21 -070091 virtual status_t emitTypeDeclarations(Formatter &out) const;
92
93 virtual status_t emitTypeDefinitions(
94 Formatter &out, const std::string prefix) const;
95
Andreas Huber85eabdb2016-08-25 11:24:49 -070096 virtual status_t emitJavaTypeDeclarations(
97 Formatter &out, bool atTopLevel) const;
Andreas Huber2831d512016-08-15 09:33:47 -070098
Andreas Huber881227d2016-08-02 14:20:21 -070099 virtual bool needsEmbeddedReadWrite() const;
100 virtual bool resultNeedsDeref() const;
101
Zhuoyao Zhang5158db42016-08-10 10:25:20 -0700102 // Generates type declaration for vts proto file.
103 // TODO (b/30844146): make it a pure virtual method.
104 virtual status_t emitVtsTypeDeclarations(Formatter &out) const;
Zhuoyao Zhang864c7712016-08-16 15:35:28 -0700105 // Generates type declaration as attribute of method (return value or method
106 // argument) or attribute of compound type for vts proto file.
107 virtual status_t emitVtsAttributeType(Formatter &out) const;
Zhuoyao Zhang5158db42016-08-10 10:25:20 -0700108
Andreas Huber70a59e12016-08-16 12:57:01 -0700109 // Returns true iff this type is supported through the Java backend.
110 virtual bool isJavaCompatible() const;
111
Andreas Huber85eabdb2016-08-25 11:24:49 -0700112 virtual void getAlignmentAndSize(size_t *align, size_t *size) const;
113
Andreas Huber881227d2016-08-02 14:20:21 -0700114protected:
115 void handleError(Formatter &out, ErrorMode mode) const;
116 void handleError2(Formatter &out, ErrorMode mode) const;
117
118 void emitReaderWriterEmbeddedForTypeName(
119 Formatter &out,
120 const std::string &name,
121 bool nameIsPointer,
122 const std::string &parcelObj,
123 bool parcelObjIsPointer,
124 bool isReader,
125 ErrorMode mode,
126 const std::string &parentName,
127 const std::string &offsetText,
128 const std::string &typeName,
129 const std::string &childName) const;
130
Andreas Huber2831d512016-08-15 09:33:47 -0700131 void emitJavaReaderWriterWithSuffix(
132 Formatter &out,
133 const std::string &parcelObj,
134 const std::string &argName,
135 bool isReader,
136 const std::string &suffix,
137 const std::string &extra) const;
138
Andreas Huberc9410c72016-07-28 12:18:40 -0700139private:
140 DISALLOW_COPY_AND_ASSIGN(Type);
141};
142
143} // namespace android
144
145#endif // TYPE_H_
146