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