blob: 1e6a33437a71e90e2a0f59775b85361160351013 [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 ENUM_TYPE_H_
18
19#define ENUM_TYPE_H_
20
Yifan Hong57886972016-08-17 10:42:15 -070021#include "ConstantExpression.h"
Yifan Hongf24fa852016-09-23 11:03:15 -070022#include "Scope.h"
Andreas Huberc9410c72016-07-28 12:18:40 -070023
Andreas Huber881227d2016-08-02 14:20:21 -070024#include <vector>
Andreas Huberc9410c72016-07-28 12:18:40 -070025
26namespace android {
27
Andreas Huber31629bc2016-08-03 09:06:40 -070028struct EnumValue;
Andreas Huberc9410c72016-07-28 12:18:40 -070029
Yifan Hongf24fa852016-09-23 11:03:15 -070030struct EnumType : public Scope {
Andreas Huber9ed827c2016-08-22 12:31:13 -070031 EnumType(const char *localName,
Andreas Huberc9410c72016-07-28 12:18:40 -070032 Type *storageType = NULL);
33
Andreas Huber8d3ac0c2016-08-04 14:49:23 -070034 const Type *storageType() const;
35 const std::vector<EnumValue *> &values() const;
Yifan Hongf24fa852016-09-23 11:03:15 -070036 void addValue(EnumValue *value);
37
38 LocalIdentifier *lookupIdentifier(const std::string &name) const override;
Andreas Huber8d3ac0c2016-08-04 14:49:23 -070039
Andreas Huber737080b2016-08-02 15:38:04 -070040 const ScalarType *resolveToScalarType() const override;
41
Andreas Huber8d3ac0c2016-08-04 14:49:23 -070042 bool isEnum() const override;
43
Steven Moreland979e0992016-09-07 09:18:08 -070044 std::string getCppType(StorageMode mode,
45 std::string *extra,
46 bool specifyNamespaces) const override;
Andreas Huber881227d2016-08-02 14:20:21 -070047
Andreas Huber4c865b72016-09-14 15:26:27 -070048 std::string getJavaType(
49 std::string *extra, bool forInitializer) const override;
50
Andreas Huber2831d512016-08-15 09:33:47 -070051 std::string getJavaSuffix() const override;
52
Andreas Hubera3558b32016-09-14 09:12:42 -070053 std::string getJavaWrapperType() const override;
54
Andreas Huber881227d2016-08-02 14:20:21 -070055 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 override;
62
Andreas Huber85eabdb2016-08-25 11:24:49 -070063 void emitJavaFieldReaderWriter(
64 Formatter &out,
Andreas Huber4c865b72016-09-14 15:26:27 -070065 size_t depth,
Andreas Huber709b62d2016-09-19 11:21:18 -070066 const std::string &parcelName,
Andreas Huber85eabdb2016-08-25 11:24:49 -070067 const std::string &blobName,
68 const std::string &fieldName,
69 const std::string &offset,
70 bool isReader) const override;
71
Andreas Huber881227d2016-08-02 14:20:21 -070072 status_t emitTypeDeclarations(Formatter &out) const override;
Andreas Huber85eabdb2016-08-25 11:24:49 -070073
74 status_t emitJavaTypeDeclarations(
75 Formatter &out, bool atTopLevel) const override;
Andreas Huber881227d2016-08-02 14:20:21 -070076
Zhuoyao Zhang5158db42016-08-10 10:25:20 -070077 status_t emitVtsTypeDeclarations(Formatter &out) const override;
Zhuoyao Zhang864c7712016-08-16 15:35:28 -070078 status_t emitVtsAttributeType(Formatter &out) const override;
Zhuoyao Zhang5158db42016-08-10 10:25:20 -070079
Andreas Huber85eabdb2016-08-25 11:24:49 -070080 void getAlignmentAndSize(size_t *align, size_t *size) const override;
81
Andreas Huber019d21d2016-10-03 12:59:47 -070082 void appendToExportedTypesVector(
83 std::vector<const Type *> *exportedTypes) const override;
84
85 status_t emitExportedHeader(Formatter &out) const override;
86
Andreas Huberc9410c72016-07-28 12:18:40 -070087private:
Zhuoyao Zhang864c7712016-08-16 15:35:28 -070088 void getTypeChain(std::vector<const EnumType *> *out) const;
Andreas Huber019d21d2016-10-03 12:59:47 -070089 const Annotation *findExportAnnotation() const;
90
Yifan Hongf24fa852016-09-23 11:03:15 -070091 std::vector<EnumValue *> mValues;
Andreas Huberc9410c72016-07-28 12:18:40 -070092 Type *mStorageType;
93
94 DISALLOW_COPY_AND_ASSIGN(EnumType);
95};
96
Yifan Hongf24fa852016-09-23 11:03:15 -070097struct EnumValue : public LocalIdentifier {
98 EnumValue(const char *name, ConstantExpression *value = nullptr);
Andreas Huber31629bc2016-08-03 09:06:40 -070099
100 std::string name() const;
Yifan Hongfc610cd2016-09-22 13:34:45 -0700101 std::string value() const;
102 std::string cppValue(ScalarType::Kind castKind) const;
103 std::string javaValue(ScalarType::Kind castKind) const;
104 std::string comment() const;
Yifan Hongf24fa852016-09-23 11:03:15 -0700105 void autofill(const EnumValue *prev, const ScalarType *type);
106 ConstantExpression *constExpr() const;
Andreas Huber31629bc2016-08-03 09:06:40 -0700107
Yifan Hongf24fa852016-09-23 11:03:15 -0700108 bool isAutoFill() const;
109 bool isEnumValue() const override;
110
111
Andreas Huber31629bc2016-08-03 09:06:40 -0700112 std::string mName;
Yifan Hongf24fa852016-09-23 11:03:15 -0700113 ConstantExpression *mValue;
114 bool mIsAutoFill;
Andreas Huber31629bc2016-08-03 09:06:40 -0700115
116 DISALLOW_COPY_AND_ASSIGN(EnumValue);
117};
118
Andreas Huberc9410c72016-07-28 12:18:40 -0700119} // namespace android
120
121#endif // ENUM_TYPE_H_
122