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