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 SCOPE_H_
18 
19 #define SCOPE_H_
20 
21 #include "NamedType.h"
22 
23 #include <map>
24 #include <string>
25 #include <unordered_map>
26 #include <vector>
27 
28 namespace android {
29 
30 struct Annotation;
31 struct ConstantExpression;
32 struct Formatter;
33 struct Interface;
34 struct LocalIdentifier;
35 
36 struct Scope : public NamedType {
37     Scope(const std::string& localName, const FQName& fullName, const Location& location,
38           Scope* parent);
39     virtual ~Scope();
40 
41     void addType(NamedType* type);
42 
43     status_t validateUniqueNames() const;
44 
45     // lookup a type given an FQName.
46     // Assume fqName.package(), fqName.version(), fqName.valueName() is empty.
47     NamedType *lookupType(const FQName &fqName) const;
48 
49     virtual LocalIdentifier *lookupIdentifier(const std::string &name) const;
50 
51     bool isScope() const override;
52 
53     // Returns the single interface or NULL.
54     Interface *getInterface() const;
55 
56     bool definesInterfaces() const;
57 
58     const std::vector<Annotation*>& annotations() const;
59 
60     void setAnnotations(std::vector<Annotation*>* annotations);
61 
62     std::vector<const Type*> getDefinedTypes() const override;
63     std::vector<const NamedType*> getSortedDefinedTypes() const;
64 
65     void topologicalReorder(const std::unordered_map<const Type*, size_t>& reversedOrder);
66 
67     void emitTypeDeclarations(Formatter& out) const override;
68     void emitGlobalTypeDeclarations(Formatter& out) const override;
69     void emitPackageTypeDeclarations(Formatter& out) const override;
70     void emitPackageTypeHeaderDefinitions(Formatter& out) const override;
71     void emitPackageHwDeclarations(Formatter& out) const override;
72 
73     void emitHidlDefinition(Formatter& out) const override;
74 
75     void emitJavaTypeDeclarations(Formatter& out, bool atTopLevel) const override;
76 
77     void emitTypeDefinitions(Formatter& out, const std::string& prefix) const override;
78 
79     const std::vector<NamedType *> &getSubTypes() const;
80 
81     void emitVtsTypeDeclarations(Formatter& out) const override;
82 
83     bool deepIsJavaCompatible(std::unordered_set<const Type*>* visited) const override;
84 
85     void appendToExportedTypesVector(
86             std::vector<const Type *> *exportedTypes) const override;
87 
88   private:
89     std::vector<NamedType *> mTypes;
90     std::map<std::string, size_t> mTypeIndexByName;
91     std::vector<Annotation*> mAnnotations;
92 
93     bool mTypeOrderChanged = false;
94 
95     DISALLOW_COPY_AND_ASSIGN(Scope);
96 };
97 
98 struct RootScope : public Scope {
99     RootScope(const char* localName, const FQName& fullName, const Location& location,
100               Scope* parent);
101     virtual ~RootScope();
102 
103     virtual status_t validate() const override;
104 
105     std::string typeName() const override;
106 };
107 
108 struct LocalIdentifier {
109     LocalIdentifier();
110     virtual ~LocalIdentifier();
111     virtual bool isEnumValue() const;
112 
113     const LocalIdentifier* resolve() const;
114     LocalIdentifier* resolve();
115 
116     virtual ConstantExpression* constExpr() const;
117 };
118 
119 }  // namespace android
120 
121 #endif  // SCOPE_H_
122 
123