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