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
66368e46077eb9fbe52242e037a5b9de91693fe70fSteven Moreland    void emitTypeDeclarations(Formatter& out) const override;
678e61c5af41226b0e1e88aa439c1360836f71795bSteven Moreland    void emitGlobalTypeDeclarations(Formatter& out) const override;
68368e46077eb9fbe52242e037a5b9de91693fe70fSteven Moreland    void emitPackageTypeDeclarations(Formatter& out) const override;
69368e46077eb9fbe52242e037a5b9de91693fe70fSteven Moreland    void emitPackageHwDeclarations(Formatter& out) const override;
7085eabdbe56720dcdcf130e5ca83129d47b143768Andreas Huber
71368e46077eb9fbe52242e037a5b9de91693fe70fSteven Moreland    void emitJavaTypeDeclarations(Formatter& out, bool atTopLevel) const override;
72881227d860c59471eee31d39946e96ce2daa35d6Andreas Huber
73368e46077eb9fbe52242e037a5b9de91693fe70fSteven Moreland    void emitTypeDefinitions(Formatter& out, const std::string& prefix) const override;
74881227d860c59471eee31d39946e96ce2daa35d6Andreas Huber
75d537ab02e1707597d70665fd8c75ca1ec9f57326Steven Moreland    const std::vector<NamedType *> &getSubTypes() const;
765158db484e5ab302368f191d75d5b1334c270e52Zhuoyao Zhang
77368e46077eb9fbe52242e037a5b9de91693fe70fSteven Moreland    void emitVtsTypeDeclarations(Formatter& out) const override;
785158db484e5ab302368f191d75d5b1334c270e52Zhuoyao Zhang
795dc72fe4f6f1d2c03c75307a9bd80f055f752ed3Timur Iskhakov    bool deepIsJavaCompatible(std::unordered_set<const Type*>* visited) const override;
8070a59e1dc3dcf32f791d2dd7966111d4adf32ecaAndreas Huber
81019d21db821ee4ae6dd3858174a0a5cee4d33c25Andreas Huber    void appendToExportedTypesVector(
82019d21db821ee4ae6dd3858174a0a5cee4d33c25Andreas Huber            std::vector<const Type *> *exportedTypes) const override;
83019d21db821ee4ae6dd3858174a0a5cee4d33c25Andreas Huber
84458ca365d99ae9f98bd59c0c2ea8b4fa54e2d535Timur Iskhakov   private:
85d537ab02e1707597d70665fd8c75ca1ec9f57326Steven Moreland    std::vector<NamedType *> mTypes;
86d537ab02e1707597d70665fd8c75ca1ec9f57326Steven Moreland    std::map<std::string, size_t> mTypeIndexByName;
87e9ccfa2e9c177ead2b6ccb36dd20de294666abcdTimur Iskhakov    std::vector<Annotation*> mAnnotations;
88c9410c7e62a33fd7599b2f3e025093a2d171577eAndreas Huber
89458ca365d99ae9f98bd59c0c2ea8b4fa54e2d535Timur Iskhakov    bool mTypeOrderChanged = false;
90458ca365d99ae9f98bd59c0c2ea8b4fa54e2d535Timur Iskhakov
91c9410c7e62a33fd7599b2f3e025093a2d171577eAndreas Huber    DISALLOW_COPY_AND_ASSIGN(Scope);
92c9410c7e62a33fd7599b2f3e025093a2d171577eAndreas Huber};
93c9410c7e62a33fd7599b2f3e025093a2d171577eAndreas Huber
940ecc7b8aca24a5618512610bb6371bdb91b5fdc2Steven Morelandstruct RootScope : public Scope {
95565b0137b2ad0e8e239d38e92c025f096de62f2dTimur Iskhakov    RootScope(const char* localName, const FQName& fullName, const Location& location,
96565b0137b2ad0e8e239d38e92c025f096de62f2dTimur Iskhakov              Scope* parent);
970ecc7b8aca24a5618512610bb6371bdb91b5fdc2Steven Moreland    virtual ~RootScope();
980ecc7b8aca24a5618512610bb6371bdb91b5fdc2Steven Moreland
99cec46c48853a8c1246656d0095a9faa3fad5c4f9Timur Iskhakov    virtual status_t validate() const override;
100cec46c48853a8c1246656d0095a9faa3fad5c4f9Timur Iskhakov
1010ecc7b8aca24a5618512610bb6371bdb91b5fdc2Steven Moreland    std::string typeName() const override;
1020ecc7b8aca24a5618512610bb6371bdb91b5fdc2Steven Moreland};
1030ecc7b8aca24a5618512610bb6371bdb91b5fdc2Steven Moreland
104f24fa85b362d7eb66c7b880f48e1e2e9916bc8a9Yifan Hongstruct LocalIdentifier {
105f24fa85b362d7eb66c7b880f48e1e2e9916bc8a9Yifan Hong    LocalIdentifier();
106f24fa85b362d7eb66c7b880f48e1e2e9916bc8a9Yifan Hong    virtual ~LocalIdentifier();
107f24fa85b362d7eb66c7b880f48e1e2e9916bc8a9Yifan Hong    virtual bool isEnumValue() const;
1087296af19687b1c90dfd238398cd2c8ccb6bcd232Timur Iskhakov
109dbaed3317411232962d25666de60799b4dcfce98Timur Iskhakov    const LocalIdentifier* resolve() const;
110dbaed3317411232962d25666de60799b4dcfce98Timur Iskhakov    LocalIdentifier* resolve();
111dbaed3317411232962d25666de60799b4dcfce98Timur Iskhakov
1127296af19687b1c90dfd238398cd2c8ccb6bcd232Timur Iskhakov    virtual ConstantExpression* constExpr() const;
113f24fa85b362d7eb66c7b880f48e1e2e9916bc8a9Yifan Hong};
114f24fa85b362d7eb66c7b880f48e1e2e9916bc8a9Yifan Hong
115c9410c7e62a33fd7599b2f3e025093a2d171577eAndreas Huber}  // namespace android
116c9410c7e62a33fd7599b2f3e025093a2d171577eAndreas Huber
117c9410c7e62a33fd7599b2f3e025093a2d171577eAndreas Huber#endif  // SCOPE_H_
118c9410c7e62a33fd7599b2f3e025093a2d171577eAndreas Huber
119