Jiyong Park | 1deecc3 | 2018-07-17 01:14:41 +0900 | [diff] [blame] | 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 | */ |
Steven Moreland | 9fccf58 | 2018-08-27 20:36:27 -0700 | [diff] [blame] | 16 | #pragma once |
Jiyong Park | 1deecc3 | 2018-07-17 01:14:41 +0900 | [diff] [blame] | 17 | |
Steven Moreland | 6cee348 | 2018-07-18 14:39:58 -0700 | [diff] [blame] | 18 | #include <functional> |
Jiyong Park | 1deecc3 | 2018-07-17 01:14:41 +0900 | [diff] [blame] | 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; |
Daniel Norman | 85aed54 | 2019-08-21 12:01:14 -0700 | [diff] [blame] | 34 | class AidlEnumDeclaration; |
| 35 | class AidlInterface; |
Jiyong Park | 1deecc3 | 2018-07-17 01:14:41 +0900 | [diff] [blame] | 36 | class AidlTypeSpecifier; |
Jiyong Park | 8e79b7f | 2020-07-20 20:52:38 +0900 | [diff] [blame] | 37 | class AidlDocument; |
Jiyong Park | 1deecc3 | 2018-07-17 01:14:41 +0900 | [diff] [blame] | 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++. |
Jiyong Park | ac89604 | 2018-07-23 21:23:05 +0900 | [diff] [blame] | 53 | class AidlTypenames final { |
Jiyong Park | 1deecc3 | 2018-07-17 01:14:41 +0900 | [diff] [blame] | 54 | public: |
| 55 | AidlTypenames() = default; |
Jiyong Park | 8e79b7f | 2020-07-20 20:52:38 +0900 | [diff] [blame] | 56 | bool AddDocument(std::unique_ptr<AidlDocument> doc); |
| 57 | const std::vector<std::unique_ptr<AidlDocument>>& AllDocuments() const { return documents_; } |
| 58 | const AidlDocument& MainDocument() const; |
Jiyong Park | 1deecc3 | 2018-07-17 01:14:41 +0900 | [diff] [blame] | 59 | bool AddPreprocessedType(unique_ptr<AidlDefinedType> type); |
Jiyong Park | ac89604 | 2018-07-23 21:23:05 +0900 | [diff] [blame] | 60 | static bool IsBuiltinTypename(const string& type_name); |
Jeongik Cha | db0f59e | 2018-11-01 18:11:21 +0900 | [diff] [blame] | 61 | static bool IsPrimitiveTypename(const string& type_name); |
Jiyong Park | ac89604 | 2018-07-23 21:23:05 +0900 | [diff] [blame] | 62 | const AidlDefinedType* TryGetDefinedType(const string& type_name) const; |
Jiyong Park | 0cf03b1 | 2020-07-22 19:36:34 +0900 | [diff] [blame] | 63 | std::vector<AidlDefinedType*> AllDefinedTypes() const; |
Steven Moreland | cb1bcd7 | 2020-04-29 16:30:35 -0700 | [diff] [blame] | 64 | |
| 65 | struct ResolvedTypename { |
| 66 | std::string canonical_name; |
| 67 | bool is_resolved; |
| 68 | }; |
| 69 | ResolvedTypename ResolveTypename(const string& type_name) const; |
Jiyong Park | 1d2df7d | 2018-07-23 15:22:50 +0900 | [diff] [blame] | 70 | bool CanBeOutParameter(const AidlTypeSpecifier& type) const; |
Jeongik Cha | d0a1027 | 2020-08-06 16:33:36 +0900 | [diff] [blame] | 71 | bool CanBeJavaOnlyImmutable(const AidlTypeSpecifier& type) const; |
Devin Moore | c7e47a3 | 2020-08-07 10:55:25 -0700 | [diff] [blame] | 72 | bool CanBeFixedSize(const AidlTypeSpecifier& type) const; |
Devin Moore | 693e673 | 2020-08-28 11:07:38 -0700 | [diff] [blame] | 73 | static bool IsList(const AidlTypeSpecifier& type); |
Jeongik Cha | 36f76c3 | 2020-07-28 00:25:52 +0900 | [diff] [blame] | 74 | |
Jeongik Cha | 047c5ee | 2019-08-07 23:16:49 +0900 | [diff] [blame] | 75 | bool IsIgnorableImport(const string& import) const; |
Daniel Norman | 85aed54 | 2019-08-21 12:01:14 -0700 | [diff] [blame] | 76 | // Returns the AidlEnumDeclaration of the given type, or nullptr if the type |
| 77 | // is not an AidlEnumDeclaration; |
| 78 | const AidlEnumDeclaration* GetEnumDeclaration(const AidlTypeSpecifier& type) const; |
| 79 | // Returns the AidlInterface of the given type, or nullptr if the type |
| 80 | // is not an AidlInterface; |
| 81 | const AidlInterface* GetInterface(const AidlTypeSpecifier& type) const; |
Steven Moreland | 6cee348 | 2018-07-18 14:39:58 -0700 | [diff] [blame] | 82 | // Iterates over all defined and then preprocessed types |
| 83 | void IterateTypes(const std::function<void(const AidlDefinedType&)>& body) const; |
| 84 | |
Jiyong Park | 1deecc3 | 2018-07-17 01:14:41 +0900 | [diff] [blame] | 85 | private: |
Jiyong Park | 8f6ec46 | 2020-01-19 20:52:47 +0900 | [diff] [blame] | 86 | struct DefinedImplResult { |
| 87 | DefinedImplResult(const AidlDefinedType* type, const bool from_preprocessed) |
| 88 | : type(type), from_preprocessed(from_preprocessed) {} |
| 89 | const AidlDefinedType* type; |
| 90 | const bool from_preprocessed; |
| 91 | }; |
| 92 | DefinedImplResult TryGetDefinedTypeImpl(const string& type_name) const; |
Jiyong Park | 8e79b7f | 2020-07-20 20:52:38 +0900 | [diff] [blame] | 93 | map<string, const AidlDefinedType*> defined_types_; |
Jiyong Park | 1deecc3 | 2018-07-17 01:14:41 +0900 | [diff] [blame] | 94 | map<string, unique_ptr<AidlDefinedType>> preprocessed_types_; |
Jiyong Park | 8e79b7f | 2020-07-20 20:52:38 +0900 | [diff] [blame] | 95 | std::vector<std::unique_ptr<AidlDocument>> documents_; |
Jiyong Park | 1deecc3 | 2018-07-17 01:14:41 +0900 | [diff] [blame] | 96 | }; |
| 97 | |
| 98 | } // namespace aidl |
| 99 | } // namespace android |