blob: f9afb31fd2a0e151a091d3ecfc8ad4d40585c3d7 [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,
Yifan Honga4b53d02016-10-31 17:29:10 -070032 const Location &location,
Andreas Huberc9410c72016-07-28 12:18:40 -070033 Type *storageType = NULL);
34
Andreas Huber8d3ac0c2016-08-04 14:49:23 -070035 const Type *storageType() const;
36 const std::vector<EnumValue *> &values() const;
Yifan Hongf24fa852016-09-23 11:03:15 -070037 void addValue(EnumValue *value);
38
39 LocalIdentifier *lookupIdentifier(const std::string &name) const override;
Andreas Huber8d3ac0c2016-08-04 14:49:23 -070040
Andreas Huber737080b2016-08-02 15:38:04 -070041 const ScalarType *resolveToScalarType() const override;
42
Andreas Huber8d3ac0c2016-08-04 14:49:23 -070043 bool isEnum() const override;
44
Steven Moreland979e0992016-09-07 09:18:08 -070045 std::string getCppType(StorageMode mode,
46 std::string *extra,
47 bool specifyNamespaces) const override;
Andreas Huber881227d2016-08-02 14:20:21 -070048
Andreas Huber4c865b72016-09-14 15:26:27 -070049 std::string getJavaType(
50 std::string *extra, bool forInitializer) const override;
51
Andreas Huber2831d512016-08-15 09:33:47 -070052 std::string getJavaSuffix() const override;
53
Andreas Hubera3558b32016-09-14 09:12:42 -070054 std::string getJavaWrapperType() const override;
55
Zhuoyao Zhangc5ea9f52016-10-06 15:05:39 -070056 std::string getVtsType() const override;
57
Andreas Huber881227d2016-08-02 14:20:21 -070058 void emitReaderWriter(
59 Formatter &out,
60 const std::string &name,
61 const std::string &parcelObj,
62 bool parcelObjIsPointer,
63 bool isReader,
64 ErrorMode mode) const override;
65
Andreas Huber85eabdb2016-08-25 11:24:49 -070066 void emitJavaFieldReaderWriter(
67 Formatter &out,
Andreas Huber4c865b72016-09-14 15:26:27 -070068 size_t depth,
Andreas Huber709b62d2016-09-19 11:21:18 -070069 const std::string &parcelName,
Andreas Huber85eabdb2016-08-25 11:24:49 -070070 const std::string &blobName,
71 const std::string &fieldName,
72 const std::string &offset,
73 bool isReader) const override;
74
Andreas Huber881227d2016-08-02 14:20:21 -070075 status_t emitTypeDeclarations(Formatter &out) const override;
Andreas Hubere3f769a2016-10-10 10:54:44 -070076 status_t emitGlobalTypeDeclarations(Formatter &out) const override;
Andreas Huber85eabdb2016-08-25 11:24:49 -070077
78 status_t emitJavaTypeDeclarations(
79 Formatter &out, bool atTopLevel) const override;
Andreas Huber881227d2016-08-02 14:20:21 -070080
Zhuoyao Zhang5158db42016-08-10 10:25:20 -070081 status_t emitVtsTypeDeclarations(Formatter &out) const override;
Zhuoyao Zhang864c7712016-08-16 15:35:28 -070082 status_t emitVtsAttributeType(Formatter &out) const override;
Zhuoyao Zhang5158db42016-08-10 10:25:20 -070083
Andreas Huber85eabdb2016-08-25 11:24:49 -070084 void getAlignmentAndSize(size_t *align, size_t *size) const override;
85
Andreas Huber019d21d2016-10-03 12:59:47 -070086 void appendToExportedTypesVector(
87 std::vector<const Type *> *exportedTypes) const override;
88
Andreas Huber1c507272016-10-05 14:33:21 -070089 status_t emitExportedHeader(Formatter &out, bool forJava) const override;
Andreas Huber019d21d2016-10-03 12:59:47 -070090
Andreas Huberc9410c72016-07-28 12:18:40 -070091private:
Zhuoyao Zhang864c7712016-08-16 15:35:28 -070092 void getTypeChain(std::vector<const EnumType *> *out) const;
Andreas Huber019d21d2016-10-03 12:59:47 -070093 const Annotation *findExportAnnotation() const;
94
Andreas Hubere3f769a2016-10-10 10:54:44 -070095 void emitEnumBitwiseOrOperator(Formatter &out, bool mutating) const;
96
Yifan Hongf24fa852016-09-23 11:03:15 -070097 std::vector<EnumValue *> mValues;
Andreas Huberc9410c72016-07-28 12:18:40 -070098 Type *mStorageType;
99
100 DISALLOW_COPY_AND_ASSIGN(EnumType);
101};
102
Yifan Hongf24fa852016-09-23 11:03:15 -0700103struct EnumValue : public LocalIdentifier {
104 EnumValue(const char *name, ConstantExpression *value = nullptr);
Andreas Huber31629bc2016-08-03 09:06:40 -0700105
106 std::string name() const;
Yifan Hongfc610cd2016-09-22 13:34:45 -0700107 std::string value() const;
108 std::string cppValue(ScalarType::Kind castKind) const;
109 std::string javaValue(ScalarType::Kind castKind) const;
110 std::string comment() const;
Yifan Hongf24fa852016-09-23 11:03:15 -0700111 void autofill(const EnumValue *prev, const ScalarType *type);
112 ConstantExpression *constExpr() const;
Andreas Huber31629bc2016-08-03 09:06:40 -0700113
Yifan Hongf24fa852016-09-23 11:03:15 -0700114 bool isAutoFill() const;
115 bool isEnumValue() const override;
116
117
Andreas Huber31629bc2016-08-03 09:06:40 -0700118 std::string mName;
Yifan Hongf24fa852016-09-23 11:03:15 -0700119 ConstantExpression *mValue;
120 bool mIsAutoFill;
Andreas Huber31629bc2016-08-03 09:06:40 -0700121
122 DISALLOW_COPY_AND_ASSIGN(EnumValue);
123};
124
Andreas Huberc9410c72016-07-28 12:18:40 -0700125} // namespace android
126
127#endif // ENUM_TYPE_H_
128