1 /* 2 * Copyright (C) 2018, 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 #pragma once 17 18 #include <functional> 19 #include <map> 20 #include <memory> 21 #include <set> 22 #include <string> 23 #include <utility> 24 #include <vector> 25 26 using std::map; 27 using std::pair; 28 using std::set; 29 using std::string; 30 using std::unique_ptr; 31 using std::vector; 32 33 class AidlDefinedType; 34 class AidlEnumDeclaration; 35 class AidlInterface; 36 class AidlTypeSpecifier; 37 class AidlDocument; 38 39 namespace android { 40 namespace aidl { 41 42 // AidlTypenames is a collection of AIDL types available to a compilation unit. 43 // 44 // Basic types (such as int, String, etc.) are added by default, while defined 45 // types (such as IFoo, MyParcelable, etc.) and types from preprocessed inputs 46 // are added as they are recognized by the parser. 47 // 48 // When AidlTypeSpecifier is encountered during parsing, parser defers the 49 // resolution of it until the end of the parsing, where it uses AidlTypenames 50 // to resolve type names in AidlTypeSpecifier. 51 // 52 // Note that nothing here is specific to either Java or C++. 53 class AidlTypenames final { 54 public: 55 AidlTypenames() = default; 56 bool AddDocument(std::unique_ptr<AidlDocument> doc); AllDocuments()57 const std::vector<std::unique_ptr<AidlDocument>>& AllDocuments() const { return documents_; } 58 const AidlDocument& MainDocument() const; 59 bool AddPreprocessedType(unique_ptr<AidlDefinedType> type); 60 static bool IsBuiltinTypename(const string& type_name); 61 static bool IsPrimitiveTypename(const string& type_name); 62 const AidlDefinedType* TryGetDefinedType(const string& type_name) const; 63 std::vector<AidlDefinedType*> AllDefinedTypes() const; 64 65 struct ResolvedTypename { 66 std::string canonical_name; 67 bool is_resolved; 68 }; 69 ResolvedTypename ResolveTypename(const string& type_name) const; 70 bool CanBeOutParameter(const AidlTypeSpecifier& type) const; 71 bool CanBeImmutable(const AidlTypeSpecifier& type) const; 72 73 bool IsIgnorableImport(const string& import) const; 74 // Returns the AidlEnumDeclaration of the given type, or nullptr if the type 75 // is not an AidlEnumDeclaration; 76 const AidlEnumDeclaration* GetEnumDeclaration(const AidlTypeSpecifier& type) const; 77 // Returns the AidlInterface of the given type, or nullptr if the type 78 // is not an AidlInterface; 79 const AidlInterface* GetInterface(const AidlTypeSpecifier& type) const; 80 // Iterates over all defined and then preprocessed types 81 void IterateTypes(const std::function<void(const AidlDefinedType&)>& body) const; 82 83 private: 84 struct DefinedImplResult { DefinedImplResultDefinedImplResult85 DefinedImplResult(const AidlDefinedType* type, const bool from_preprocessed) 86 : type(type), from_preprocessed(from_preprocessed) {} 87 const AidlDefinedType* type; 88 const bool from_preprocessed; 89 }; 90 DefinedImplResult TryGetDefinedTypeImpl(const string& type_name) const; 91 map<string, const AidlDefinedType*> defined_types_; 92 map<string, unique_ptr<AidlDefinedType>> preprocessed_types_; 93 std::vector<std::unique_ptr<AidlDocument>> documents_; 94 }; 95 96 } // namespace aidl 97 } // namespace android 98