Scope.h revision 4b8f7a11f794d9b4899af92a856b4a03b80b31e8
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 status_t emitTypeDeclarations(Formatter &out) const override; 67 status_t emitPackageTypeDeclarations(Formatter& out) const override; 68 status_t emitPackageHwDeclarations(Formatter& out) const override; 69 70 status_t emitJavaTypeDeclarations( 71 Formatter &out, bool atTopLevel) const override; 72 73 status_t emitTypeDefinitions(Formatter& out, const std::string& prefix) const override; 74 75 const std::vector<NamedType *> &getSubTypes() const; 76 77 status_t 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 status_t forEachType(const std::function<status_t(Type*)>& func) const; 92 93 DISALLOW_COPY_AND_ASSIGN(Scope); 94}; 95 96struct RootScope : public Scope { 97 RootScope(const char* localName, const FQName& fullName, const Location& location, 98 Scope* parent); 99 virtual ~RootScope(); 100 101 virtual status_t validate() const override; 102 103 std::string typeName() const override; 104}; 105 106struct LocalIdentifier { 107 LocalIdentifier(); 108 virtual ~LocalIdentifier(); 109 virtual bool isEnumValue() const; 110 111 const LocalIdentifier* resolve() const; 112 LocalIdentifier* resolve(); 113 114 virtual ConstantExpression* constExpr() const; 115}; 116 117} // namespace android 118 119#endif // SCOPE_H_ 120 121