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