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#ifndef COMPOUND_TYPE_H_
18
19#define COMPOUND_TYPE_H_
20
21#include "Reference.h"
22#include "Scope.h"
23
24#include <vector>
25
26namespace android {
27
28struct CompoundType : public Scope {
29    enum Style {
30        STYLE_STRUCT,
31        STYLE_UNION,
32    };
33
34    CompoundType(Style style, const char* localName, const FQName& fullName,
35                 const Location& location, Scope* parent);
36
37    Style style() const;
38
39    void setFields(std::vector<NamedReference<Type>*>* fields);
40
41    bool isCompoundType() const override;
42
43    bool deepCanCheckEquality(std::unordered_set<const Type*>* visited) const override;
44
45    std::string typeName() const override;
46
47    std::vector<const Reference<Type>*> getReferences() const override;
48
49    status_t validate() const override;
50    status_t validateUniqueNames() const;
51
52    std::string getCppType(StorageMode mode,
53                           bool specifyNamespaces) const override;
54
55    std::string getJavaType(bool forInitializer) const override;
56
57    std::string getVtsType() const override;
58
59    void emitReaderWriter(
60            Formatter &out,
61            const std::string &name,
62            const std::string &parcelObj,
63            bool parcelObjIsPointer,
64            bool isReader,
65            ErrorMode mode) const override;
66
67    void emitReaderWriterEmbedded(
68            Formatter &out,
69            size_t depth,
70            const std::string &name,
71            const std::string &sanitizedName,
72            bool nameIsPointer,
73            const std::string &parcelObj,
74            bool parcelObjIsPointer,
75            bool isReader,
76            ErrorMode mode,
77            const std::string &parentName,
78            const std::string &offsetText) const override;
79
80    void emitResolveReferences(
81            Formatter &out,
82            const std::string &name,
83            bool nameIsPointer,
84            const std::string &parcelObj,
85            bool parcelObjIsPointer,
86            bool isReader,
87            ErrorMode mode) const override;
88
89    void emitResolveReferencesEmbedded(
90            Formatter &out,
91            size_t depth,
92            const std::string &name,
93            const std::string &sanitizedName,
94            bool nameIsPointer,
95            const std::string &parcelObj,
96            bool parcelObjIsPointer,
97            bool isReader,
98            ErrorMode mode,
99            const std::string &parentName,
100            const std::string &offsetText) const override;
101
102    void emitJavaReaderWriter(
103            Formatter &out,
104            const std::string &parcelObj,
105            const std::string &argName,
106            bool isReader) const override;
107
108    void emitJavaFieldInitializer(
109            Formatter &out, const std::string &fieldName) const override;
110
111    void emitJavaFieldReaderWriter(
112            Formatter &out,
113            size_t depth,
114            const std::string &parcelName,
115            const std::string &blobName,
116            const std::string &fieldName,
117            const std::string &offset,
118            bool isReader) const override;
119
120    void emitTypeDeclarations(Formatter& out) const override;
121    void emitTypeForwardDeclaration(Formatter& out) const override;
122    void emitPackageTypeDeclarations(Formatter& out) const override;
123    void emitPackageHwDeclarations(Formatter& out) const override;
124
125    void emitTypeDefinitions(Formatter& out, const std::string& prefix) const override;
126
127    void emitJavaTypeDeclarations(Formatter& out, bool atTopLevel) const override;
128
129    bool needsEmbeddedReadWrite() const override;
130    bool deepNeedsResolveReferences(std::unordered_set<const Type*>* visited) const override;
131    bool resultNeedsDeref() const override;
132
133    void emitVtsTypeDeclarations(Formatter& out) const override;
134    void emitVtsAttributeType(Formatter& out) const override;
135
136    bool deepIsJavaCompatible(std::unordered_set<const Type*>* visited) const override;
137    bool deepContainsPointer(std::unordered_set<const Type*>* visited) const override;
138
139    void getAlignmentAndSize(size_t *align, size_t *size) const;
140
141    bool containsInterface() const;
142private:
143    Style mStyle;
144    std::vector<NamedReference<Type>*>* mFields;
145
146    void emitStructReaderWriter(
147            Formatter &out, const std::string &prefix, bool isReader) const;
148    void emitResolveReferenceDef(Formatter& out, const std::string& prefix, bool isReader) const;
149
150    DISALLOW_COPY_AND_ASSIGN(CompoundType);
151};
152
153}  // namespace android
154
155#endif  // COMPOUND_TYPE_H_
156
157