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 TYPE_H_
18 #define TYPE_H_
19 
20 #include "Expression.h"
21 
22 #include <android-base/macros.h>
23 #include <android-base/logging.h>
24 #include <string>
25 #include <vector>
26 #include <map>
27 
28 namespace android {
29 
30 struct Type {
31 
32     struct Qualifier {
33 
34         enum Qualification {
35             NONE = 0,
36             STRUCT,
37             UNION,
38             SIGNED,
39             UNSIGNED,
40             VOID,
41             POINTER,
42             CONST,
43             GENERICS,
44             ID,
45             ENUM
46         } qualification;
47 
48         union {
49             std::string id;
50             Type *generics;
51         };
52 
QualifierType::Qualifier53         Qualifier(Qualification qualification)
54         : qualification(qualification) {}
QualifierType::Qualifier55         Qualifier(Qualification qualification, std::string id)
56         : qualification(qualification), id(id) {}
QualifierType::Qualifier57         Qualifier(Qualification qualification, Type* generics)
58         : qualification(qualification), generics(generics) {}
59 
~QualifierType::Qualifier60         ~Qualifier() {
61             if (qualification == GENERICS) {
62                 delete generics;
63             }
64         }
65     };
66 
67     Type(std::vector<Qualifier*> *qualifiers);
68     ~Type();
69 
qualifierTextType70     static std::string qualifierText(Qualifier::Qualification qual) {
71         switch(qual) {
72             case Qualifier::STRUCT: return "struct";
73             case Qualifier::UNION: return "union";
74             case Qualifier::ENUM: return "enum";
75             case Qualifier::SIGNED: return "signed";
76             case Qualifier::UNSIGNED: return "unsigned";
77             case Qualifier::VOID: return "void";
78             case Qualifier::POINTER: return "*";
79             case Qualifier::CONST: return "const";
80             case Qualifier::ID: return "ID";
81             case Qualifier::NONE: return "";
82             default: return "/* UNKNOWN TYPE QUALIFIER */";
83         }
84     }
85 
86     void setArrays(std::vector<Expression *> *arrays);
87 
88     const std::string decorateName(const std::string &name) const;
89 
90     bool isVoid() const;
91     bool isHwDevice() const;
92     std::string removeLastId();
93 
94 private:
95 
96     static std::map<std::string, std::string> kSignedToUnsignedMap;
97     static const std::string signedToUnsigned(const std::string &signedType);
98 
99     static std::map<std::string, std::string> kCToHidlMap;
100     static const std::string cToHidlType(const std::string &cType);
101 
102     const std::string getHidlType() const;
103 
104     const std::string getRawQualifierList() const;
105     const std::string getSpecialTypeName() const;
106 
107     std::vector<Qualifier*> *mQualifiers = nullptr;
108 
109     /* [ expression ] [ expression ] ... [ expression ] */
110     std::vector<Expression*> *mArrays = nullptr;
111 
112     DISALLOW_COPY_AND_ASSIGN(Type);
113 };
114 
115 }  // namespace android
116 
117 #endif  // TYPE_H_
118