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