blob: 9a4c41cc4283192057707c6b188fb968e7a73d46 [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 COMPOUND_TYPE_H_
18
19#define COMPOUND_TYPE_H_
20
Timur Iskhakov505316c2017-08-05 03:38:59 +000021#include "Reference.h"
Andreas Huberc9410c72016-07-28 12:18:40 -070022#include "Scope.h"
23
Neel Mehta69920a62019-07-22 16:22:13 -070024#include <string>
Andreas Huber881227d2016-08-02 14:20:21 -070025#include <vector>
Andreas Huberc9410c72016-07-28 12:18:40 -070026
27namespace android {
28
Andreas Huberc9410c72016-07-28 12:18:40 -070029struct CompoundType : public Scope {
30 enum Style {
31 STYLE_STRUCT,
32 STYLE_UNION,
Nirav Atre0ab82652018-06-04 11:49:14 -070033 STYLE_SAFE_UNION,
Andreas Huberc9410c72016-07-28 12:18:40 -070034 };
35
Neel Mehta69920a62019-07-22 16:22:13 -070036 CompoundType(Style style, const std::string& localName, const FQName& fullName,
Timur Iskhakov565b0132017-09-06 18:07:11 -070037 const Location& location, Scope* parent);
Andreas Huberc9410c72016-07-28 12:18:40 -070038
Yifan Hong27e85db2016-11-09 15:45:52 -080039 Style style() const;
40
Neel Mehta7bfd90b2019-07-29 17:14:32 -070041 std::vector<const NamedReference<Type>*> getFields() const;
Neel Mehtadd9063f2019-08-16 19:37:11 -070042 void addField(NamedReference<Type>* field);
Andreas Huberc9410c72016-07-28 12:18:40 -070043
Andreas Huberf630bc82016-09-09 14:52:25 -070044 bool isCompoundType() const override;
45
Timur Iskhakov5dc72fe2017-09-07 23:13:44 -070046 bool deepCanCheckEquality(std::unordered_set<const Type*>* visited) const override;
Yifan Hongc6752dc2016-12-20 14:00:14 -080047
Steven Moreland0ecc7b82017-07-19 12:59:23 -070048 std::string typeName() const override;
49
Timur Iskhakovb58f4182017-08-29 15:19:24 -070050 std::vector<const Reference<Type>*> getReferences() const override;
Timur Iskhakov33431e62017-08-21 17:31:23 -070051
Timur Iskhakovcec46c42017-08-09 00:22:02 -070052 status_t validate() const override;
53 status_t validateUniqueNames() const;
Nirav Atre0ab82652018-06-04 11:49:14 -070054 status_t validateSubTypeNames() const;
Timur Iskhakovcec46c42017-08-09 00:22:02 -070055
Steven Moreland979e0992016-09-07 09:18:08 -070056 std::string getCppType(StorageMode mode,
Steven Moreland979e0992016-09-07 09:18:08 -070057 bool specifyNamespaces) const override;
Andreas Huber881227d2016-08-02 14:20:21 -070058
Yifan Hong4ed13472016-11-02 10:44:11 -070059 std::string getJavaType(bool forInitializer) const override;
Andreas Huber85eabdb2016-08-25 11:24:49 -070060
Zhuoyao Zhangc5ea9f52016-10-06 15:05:39 -070061 std::string getVtsType() const override;
62
Andreas Huber881227d2016-08-02 14:20:21 -070063 void emitReaderWriter(
64 Formatter &out,
65 const std::string &name,
66 const std::string &parcelObj,
67 bool parcelObjIsPointer,
68 bool isReader,
69 ErrorMode mode) const override;
70
71 void emitReaderWriterEmbedded(
72 Formatter &out,
Andreas Huberf9d49f12016-09-12 14:58:36 -070073 size_t depth,
Andreas Huber881227d2016-08-02 14:20:21 -070074 const std::string &name,
Yifan Hongbe2a3732016-10-05 13:33:41 -070075 const std::string &sanitizedName,
Andreas Huber881227d2016-08-02 14:20:21 -070076 bool nameIsPointer,
77 const std::string &parcelObj,
78 bool parcelObjIsPointer,
79 bool isReader,
80 ErrorMode mode,
81 const std::string &parentName,
82 const std::string &offsetText) const override;
83
Andreas Huber85eabdb2016-08-25 11:24:49 -070084 void emitJavaReaderWriter(
85 Formatter &out,
86 const std::string &parcelObj,
87 const std::string &argName,
88 bool isReader) const override;
89
90 void emitJavaFieldInitializer(
91 Formatter &out, const std::string &fieldName) const override;
92
Nirav Atre66842a92018-06-28 18:14:13 -070093 void emitJavaFieldDefaultInitialValue(
94 Formatter &out, const std::string &declaredFieldName) const override;
95
Andreas Huber85eabdb2016-08-25 11:24:49 -070096 void emitJavaFieldReaderWriter(
97 Formatter &out,
Andreas Huber4c865b72016-09-14 15:26:27 -070098 size_t depth,
Andreas Huber709b62d2016-09-19 11:21:18 -070099 const std::string &parcelName,
Andreas Huber85eabdb2016-08-25 11:24:49 -0700100 const std::string &blobName,
101 const std::string &fieldName,
102 const std::string &offset,
103 bool isReader) const override;
104
Neel Mehta3b414a82019-07-02 15:47:48 -0700105 void emitHidlDefinition(Formatter& out) const override;
Steven Moreland6ec9eb92018-02-16 14:21:49 -0800106 void emitTypeDeclarations(Formatter& out) const override;
Timur Iskhakovfd3f2502017-09-05 16:25:02 -0700107 void emitTypeForwardDeclaration(Formatter& out) const override;
Steven Moreland6ec9eb92018-02-16 14:21:49 -0800108 void emitPackageTypeDeclarations(Formatter& out) const override;
Steven Moreland09c6ebe2018-10-09 10:15:48 -0700109 void emitPackageTypeHeaderDefinitions(Formatter& out) const override;
Steven Moreland6ec9eb92018-02-16 14:21:49 -0800110 void emitPackageHwDeclarations(Formatter& out) const override;
Andreas Huber881227d2016-08-02 14:20:21 -0700111
Steven Moreland6ec9eb92018-02-16 14:21:49 -0800112 void emitTypeDefinitions(Formatter& out, const std::string& prefix) const override;
Andreas Huber881227d2016-08-02 14:20:21 -0700113
Steven Moreland6ec9eb92018-02-16 14:21:49 -0800114 void emitJavaTypeDeclarations(Formatter& out, bool atTopLevel) const override;
Andreas Huber85eabdb2016-08-25 11:24:49 -0700115
Andreas Huber881227d2016-08-02 14:20:21 -0700116 bool needsEmbeddedReadWrite() const override;
117 bool resultNeedsDeref() const override;
118
Steven Moreland6ec9eb92018-02-16 14:21:49 -0800119 void emitVtsTypeDeclarations(Formatter& out) const override;
120 void emitVtsAttributeType(Formatter& out) const override;
Zhuoyao Zhang5158db42016-08-10 10:25:20 -0700121
Timur Iskhakov5dc72fe2017-09-07 23:13:44 -0700122 bool deepIsJavaCompatible(std::unordered_set<const Type*>* visited) const override;
123 bool deepContainsPointer(std::unordered_set<const Type*>* visited) const override;
Andreas Huber70a59e12016-08-16 12:57:01 -0700124
Yi Kongc8c053e2019-03-25 02:13:39 -0700125 void getAlignmentAndSize(size_t *align, size_t *size) const override;
Andreas Huber85eabdb2016-08-25 11:24:49 -0700126
Howard Chenecfb4512017-11-21 18:28:53 +0800127 bool containsInterface() const;
Andreas Huberc9410c72016-07-28 12:18:40 -0700128private:
Nirav Atre0ab82652018-06-04 11:49:14 -0700129
130 struct Layout {
131 size_t offset;
132 size_t align;
133 size_t size;
134
135 Layout() : offset(0), align(1), size(0) {}
136 static size_t getPad(size_t offset, size_t align);
137 };
138
139 struct CompoundLayout {
Steven Moreland5e20b652019-04-22 18:42:38 -0700140 // Layout of this entire object including metadata.
141 // For struct/union, this is the same as innerStruct.
Nirav Atre0ab82652018-06-04 11:49:14 -0700142 Layout overall;
Steven Moreland5e20b652019-04-22 18:42:38 -0700143 // Layout of user-specified data
Nirav Atre0ab82652018-06-04 11:49:14 -0700144 Layout innerStruct;
Steven Moreland5e20b652019-04-22 18:42:38 -0700145 // Layout of discriminator for safe union (otherwise zero)
Nirav Atre0ab82652018-06-04 11:49:14 -0700146 Layout discriminator;
147 };
148
Andreas Huberc9410c72016-07-28 12:18:40 -0700149 Style mStyle;
Neel Mehtadd9063f2019-08-16 19:37:11 -0700150 std::vector<NamedReference<Type>*> mFields;
Andreas Huber881227d2016-08-02 14:20:21 -0700151
Neel Mehta6de3cf82019-08-06 16:49:33 -0700152 // only emits the struct body. doesn't emit the last ";\n" from the definition
153 void emitInlineHidlDefinition(Formatter& out) const;
Neel Mehta7cc72132019-08-09 15:48:09 -0700154 // emits the hidl definition line for a field inside the struct. used by emitHidlDefinition
155 void emitFieldHidlDefinition(Formatter& out, const NamedReference<Type>& ref) const;
Neel Mehta6de3cf82019-08-06 16:49:33 -0700156
Nirav Atre0ab82652018-06-04 11:49:14 -0700157 void emitLayoutAsserts(Formatter& out, const Layout& localLayout,
158 const std::string& localLayoutName) const;
159
160 void emitInvalidSubTypeNamesError(const std::string& subTypeName,
161 const Location& location) const;
162
163 void emitSafeUnionTypeDefinitions(Formatter& out) const;
164 void emitSafeUnionTypeConstructors(Formatter& out) const;
165 void emitSafeUnionTypeDeclarations(Formatter& out) const;
166 std::unique_ptr<ScalarType> getUnionDiscriminatorType() const;
167
Steven Moreland5add34d2018-11-08 16:31:30 -0800168 void emitSafeUnionUnknownDiscriminatorError(Formatter& out, const std::string& value,
169 bool fatal) const;
Nirav Atree3cae852018-07-16 12:19:16 -0700170
Steven Moreland9f8aedb2021-03-25 22:58:57 +0000171 void emitSafeUnionAssignDefinition(Formatter& out, const std::string& parameterName,
172 bool usesMoveSemantics) const;
Nirav Atre64868d32018-07-15 19:15:18 -0700173
Nirav Atre0ab82652018-06-04 11:49:14 -0700174 CompoundLayout getCompoundAlignmentAndSize() const;
Steven Moreland5e20b652019-04-22 18:42:38 -0700175 void emitPaddingZero(Formatter& out, size_t offset, size_t size) const;
Nirav Atre0ab82652018-06-04 11:49:14 -0700176
Nirav Atreca7a5022018-06-29 20:43:49 -0700177 void emitSafeUnionReaderWriterForInterfaces(
178 Formatter &out,
179 const std::string &name,
180 const std::string &parcelObj,
181 bool parcelObjIsPointer,
182 bool isReader,
183 ErrorMode mode) const;
184
Andreas Huber881227d2016-08-02 14:20:21 -0700185 void emitStructReaderWriter(
186 Formatter &out, const std::string &prefix, bool isReader) const;
Andreas Huberc9410c72016-07-28 12:18:40 -0700187
188 DISALLOW_COPY_AND_ASSIGN(CompoundType);
189};
190
Andreas Huberc9410c72016-07-28 12:18:40 -0700191} // namespace android
192
193#endif // COMPOUND_TYPE_H_
194