Scope.h revision 8e61c5af41226b0e1e88aa439c1360836f71795b
11aec397b1fdea7db4120dbe55b6995bb2a9d9138Andreas Huber/*
21aec397b1fdea7db4120dbe55b6995bb2a9d9138Andreas Huber * Copyright (C) 2016 The Android Open Source Project
31aec397b1fdea7db4120dbe55b6995bb2a9d9138Andreas Huber *
41aec397b1fdea7db4120dbe55b6995bb2a9d9138Andreas Huber * Licensed under the Apache License, Version 2.0 (the "License");
51aec397b1fdea7db4120dbe55b6995bb2a9d9138Andreas Huber * you may not use this file except in compliance with the License.
61aec397b1fdea7db4120dbe55b6995bb2a9d9138Andreas Huber * You may obtain a copy of the License at
71aec397b1fdea7db4120dbe55b6995bb2a9d9138Andreas Huber *
81aec397b1fdea7db4120dbe55b6995bb2a9d9138Andreas Huber *      http://www.apache.org/licenses/LICENSE-2.0
91aec397b1fdea7db4120dbe55b6995bb2a9d9138Andreas Huber *
101aec397b1fdea7db4120dbe55b6995bb2a9d9138Andreas Huber * Unless required by applicable law or agreed to in writing, software
111aec397b1fdea7db4120dbe55b6995bb2a9d9138Andreas Huber * distributed under the License is distributed on an "AS IS" BASIS,
121aec397b1fdea7db4120dbe55b6995bb2a9d9138Andreas Huber * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
131aec397b1fdea7db4120dbe55b6995bb2a9d9138Andreas Huber * See the License for the specific language governing permissions and
141aec397b1fdea7db4120dbe55b6995bb2a9d9138Andreas Huber * limitations under the License.
151aec397b1fdea7db4120dbe55b6995bb2a9d9138Andreas Huber */
161aec397b1fdea7db4120dbe55b6995bb2a9d9138Andreas Huber
17c9410c7e62a33fd7599b2f3e025093a2d171577eAndreas Huber#ifndef SCOPE_H_
18c9410c7e62a33fd7599b2f3e025093a2d171577eAndreas Huber
19c9410c7e62a33fd7599b2f3e025093a2d171577eAndreas Huber#define SCOPE_H_
20c9410c7e62a33fd7599b2f3e025093a2d171577eAndreas Huber
21c9410c7e62a33fd7599b2f3e025093a2d171577eAndreas Huber#include "NamedType.h"
22c9410c7e62a33fd7599b2f3e025093a2d171577eAndreas Huber
23d537ab02e1707597d70665fd8c75ca1ec9f57326Steven Moreland#include <map>
24458ca365d99ae9f98bd59c0c2ea8b4fa54e2d535Timur Iskhakov#include <unordered_map>
2570a59e1dc3dcf32f791d2dd7966111d4adf32ecaAndreas Huber#include <vector>
26c9410c7e62a33fd7599b2f3e025093a2d171577eAndreas Huber
27c9410c7e62a33fd7599b2f3e025093a2d171577eAndreas Hubernamespace android {
28c9410c7e62a33fd7599b2f3e025093a2d171577eAndreas Huber
29e9ccfa2e9c177ead2b6ccb36dd20de294666abcdTimur Iskhakovstruct Annotation;
307296af19687b1c90dfd238398cd2c8ccb6bcd232Timur Iskhakovstruct ConstantExpression;
31881227d860c59471eee31d39946e96ce2daa35d6Andreas Huberstruct Formatter;
32881227d860c59471eee31d39946e96ce2daa35d6Andreas Huberstruct Interface;
33f24fa85b362d7eb66c7b880f48e1e2e9916bc8a9Yifan Hongstruct LocalIdentifier;
34c9410c7e62a33fd7599b2f3e025093a2d171577eAndreas Huber
35c9410c7e62a33fd7599b2f3e025093a2d171577eAndreas Huberstruct Scope : public NamedType {
36565b0137b2ad0e8e239d38e92c025f096de62f2dTimur Iskhakov    Scope(const char* localName, const FQName& fullName, const Location& location, Scope* parent);
37f24fa85b362d7eb66c7b880f48e1e2e9916bc8a9Yifan Hong    virtual ~Scope();
38c9410c7e62a33fd7599b2f3e025093a2d171577eAndreas Huber
39565b0137b2ad0e8e239d38e92c025f096de62f2dTimur Iskhakov    void addType(NamedType* type);
40565b0137b2ad0e8e239d38e92c025f096de62f2dTimur Iskhakov
41565b0137b2ad0e8e239d38e92c025f096de62f2dTimur Iskhakov    status_t validateUniqueNames() const;
425a545440766886a1bbd09ed7221bb337705e5d14Andreas Huber
43327cfe12d51908469e1544db14a9232e0ddc6855Yifan Hong    // lookup a type given an FQName.
44327cfe12d51908469e1544db14a9232e0ddc6855Yifan Hong    // Assume fqName.package(), fqName.version(), fqName.valueName() is empty.
45ae16eeda35e175dbc3cb0651121854c5788e522eYifan Hong    NamedType *lookupType(const FQName &fqName) const;
46c9410c7e62a33fd7599b2f3e025093a2d171577eAndreas Huber
47f24fa85b362d7eb66c7b880f48e1e2e9916bc8a9Yifan Hong    virtual LocalIdentifier *lookupIdentifier(const std::string &name) const;
48f24fa85b362d7eb66c7b880f48e1e2e9916bc8a9Yifan Hong
495345ec2b1b7e4126b77aa2131b231a9eb5ee811dAndreas Huber    bool isScope() const override;
50881227d860c59471eee31d39946e96ce2daa35d6Andreas Huber
51881227d860c59471eee31d39946e96ce2daa35d6Andreas Huber    // Returns the single interface or NULL.
52881227d860c59471eee31d39946e96ce2daa35d6Andreas Huber    Interface *getInterface() const;
53881227d860c59471eee31d39946e96ce2daa35d6Andreas Huber
547c5ddfb41a806a7bf71581952d06b637a7670cf7Andreas Huber    bool containsInterfaces() const;
55c9410c7e62a33fd7599b2f3e025093a2d171577eAndreas Huber
56e9ccfa2e9c177ead2b6ccb36dd20de294666abcdTimur Iskhakov    const std::vector<Annotation*>& annotations() const;
57e9ccfa2e9c177ead2b6ccb36dd20de294666abcdTimur Iskhakov
58e9ccfa2e9c177ead2b6ccb36dd20de294666abcdTimur Iskhakov    void setAnnotations(std::vector<Annotation*>* annotations);
59e9ccfa2e9c177ead2b6ccb36dd20de294666abcdTimur Iskhakov
60b58f4185934a93fc511c03a8a970b31c5ea0dfcaTimur Iskhakov    std::vector<const Type*> getDefinedTypes() const override;
6133431e6cd425c6cd179080442a8616e2baa20aaeTimur Iskhakov
62b58f4185934a93fc511c03a8a970b31c5ea0dfcaTimur Iskhakov    std::vector<const ConstantExpression*> getConstantExpressions() const override;
63cec46c48853a8c1246656d0095a9faa3fad5c4f9Timur Iskhakov
64458ca365d99ae9f98bd59c0c2ea8b4fa54e2d535Timur Iskhakov    void topologicalReorder(const std::unordered_map<const Type*, size_t>& reversedOrder);
65458ca365d99ae9f98bd59c0c2ea8b4fa54e2d535Timur Iskhakov
66881227d860c59471eee31d39946e96ce2daa35d6Andreas Huber    status_t emitTypeDeclarations(Formatter &out) const override;
678e61c5af41226b0e1e88aa439c1360836f71795bSteven Moreland    void emitGlobalTypeDeclarations(Formatter& out) const override;
684b8f7a11f794d9b4899af92a856b4a03b80b31e8Steven Moreland    status_t emitPackageTypeDeclarations(Formatter& out) const override;
694b8f7a11f794d9b4899af92a856b4a03b80b31e8Steven Moreland    status_t emitPackageHwDeclarations(Formatter& out) const override;
7085eabdbe56720dcdcf130e5ca83129d47b143768Andreas Huber
7185eabdbe56720dcdcf130e5ca83129d47b143768Andreas Huber    status_t emitJavaTypeDeclarations(
7285eabdbe56720dcdcf130e5ca83129d47b143768Andreas Huber            Formatter &out, bool atTopLevel) const override;
73881227d860c59471eee31d39946e96ce2daa35d6Andreas Huber
748c90cc59bf93bd0c08970b4488067a33015d4a1cChih-Hung Hsieh    status_t emitTypeDefinitions(Formatter& out, const std::string& prefix) const override;
75881227d860c59471eee31d39946e96ce2daa35d6Andreas Huber
76d537ab02e1707597d70665fd8c75ca1ec9f57326Steven Moreland    const std::vector<NamedType *> &getSubTypes() const;
775158db484e5ab302368f191d75d5b1334c270e52Zhuoyao Zhang
785158db484e5ab302368f191d75d5b1334c270e52Zhuoyao Zhang    status_t emitVtsTypeDeclarations(Formatter &out) const override;
795158db484e5ab302368f191d75d5b1334c270e52Zhuoyao Zhang
805dc72fe4f6f1d2c03c75307a9bd80f055f752ed3Timur Iskhakov    bool deepIsJavaCompatible(std::unordered_set<const Type*>* visited) const override;
8170a59e1dc3dcf32f791d2dd7966111d4adf32ecaAndreas Huber
82019d21db821ee4ae6dd3858174a0a5cee4d33c25Andreas Huber    void appendToExportedTypesVector(
83019d21db821ee4ae6dd3858174a0a5cee4d33c25Andreas Huber            std::vector<const Type *> *exportedTypes) const override;
84019d21db821ee4ae6dd3858174a0a5cee4d33c25Andreas Huber
85458ca365d99ae9f98bd59c0c2ea8b4fa54e2d535Timur Iskhakov   private:
86d537ab02e1707597d70665fd8c75ca1ec9f57326Steven Moreland    std::vector<NamedType *> mTypes;
87d537ab02e1707597d70665fd8c75ca1ec9f57326Steven Moreland    std::map<std::string, size_t> mTypeIndexByName;
88e9ccfa2e9c177ead2b6ccb36dd20de294666abcdTimur Iskhakov    std::vector<Annotation*> mAnnotations;
89c9410c7e62a33fd7599b2f3e025093a2d171577eAndreas Huber
90458ca365d99ae9f98bd59c0c2ea8b4fa54e2d535Timur Iskhakov    bool mTypeOrderChanged = false;
91458ca365d99ae9f98bd59c0c2ea8b4fa54e2d535Timur Iskhakov
928c90cc59bf93bd0c08970b4488067a33015d4a1cChih-Hung Hsieh    status_t forEachType(const std::function<status_t(Type*)>& func) const;
93244e82d34bf0ef5d8d5b0730a9dea3e3fc4de7c5Yifan Hong
94c9410c7e62a33fd7599b2f3e025093a2d171577eAndreas Huber    DISALLOW_COPY_AND_ASSIGN(Scope);
95c9410c7e62a33fd7599b2f3e025093a2d171577eAndreas Huber};
96c9410c7e62a33fd7599b2f3e025093a2d171577eAndreas Huber
970ecc7b8aca24a5618512610bb6371bdb91b5fdc2Steven Morelandstruct RootScope : public Scope {
98565b0137b2ad0e8e239d38e92c025f096de62f2dTimur Iskhakov    RootScope(const char* localName, const FQName& fullName, const Location& location,
99565b0137b2ad0e8e239d38e92c025f096de62f2dTimur Iskhakov              Scope* parent);
1000ecc7b8aca24a5618512610bb6371bdb91b5fdc2Steven Moreland    virtual ~RootScope();
1010ecc7b8aca24a5618512610bb6371bdb91b5fdc2Steven Moreland
102cec46c48853a8c1246656d0095a9faa3fad5c4f9Timur Iskhakov    virtual status_t validate() const override;
103cec46c48853a8c1246656d0095a9faa3fad5c4f9Timur Iskhakov
1040ecc7b8aca24a5618512610bb6371bdb91b5fdc2Steven Moreland    std::string typeName() const override;
1050ecc7b8aca24a5618512610bb6371bdb91b5fdc2Steven Moreland};
1060ecc7b8aca24a5618512610bb6371bdb91b5fdc2Steven Moreland
107f24fa85b362d7eb66c7b880f48e1e2e9916bc8a9Yifan Hongstruct LocalIdentifier {
108f24fa85b362d7eb66c7b880f48e1e2e9916bc8a9Yifan Hong    LocalIdentifier();
109f24fa85b362d7eb66c7b880f48e1e2e9916bc8a9Yifan Hong    virtual ~LocalIdentifier();
110f24fa85b362d7eb66c7b880f48e1e2e9916bc8a9Yifan Hong    virtual bool isEnumValue() const;
1117296af19687b1c90dfd238398cd2c8ccb6bcd232Timur Iskhakov
112dbaed3317411232962d25666de60799b4dcfce98Timur Iskhakov    const LocalIdentifier* resolve() const;
113dbaed3317411232962d25666de60799b4dcfce98Timur Iskhakov    LocalIdentifier* resolve();
114dbaed3317411232962d25666de60799b4dcfce98Timur Iskhakov
1157296af19687b1c90dfd238398cd2c8ccb6bcd232Timur Iskhakov    virtual ConstantExpression* constExpr() const;
116f24fa85b362d7eb66c7b880f48e1e2e9916bc8a9Yifan Hong};
117f24fa85b362d7eb66c7b880f48e1e2e9916bc8a9Yifan Hong
118c9410c7e62a33fd7599b2f3e025093a2d171577eAndreas Huber}  // namespace android
119c9410c7e62a33fd7599b2f3e025093a2d171577eAndreas Huber
120c9410c7e62a33fd7599b2f3e025093a2d171577eAndreas Huber#endif  // SCOPE_H_
121c9410c7e62a33fd7599b2f3e025093a2d171577eAndreas Huber
122