blob: 78b996f317c964625155aa751694cc14dc7e325b [file] [log] [blame]
Jiyong Park1deecc32018-07-17 01:14:41 +09001/*
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 Moreland9fccf582018-08-27 20:36:27 -070016#pragma once
Jiyong Park1deecc32018-07-17 01:14:41 +090017
Steven Moreland6cee3482018-07-18 14:39:58 -070018#include <functional>
Jiyong Park1deecc32018-07-17 01:14:41 +090019#include <map>
20#include <memory>
21#include <set>
22#include <string>
23#include <utility>
24#include <vector>
25
26using std::map;
27using std::pair;
28using std::set;
29using std::string;
30using std::unique_ptr;
31using std::vector;
32
33class AidlDefinedType;
Daniel Norman85aed542019-08-21 12:01:14 -070034class AidlEnumDeclaration;
35class AidlInterface;
Jiyong Park1deecc32018-07-17 01:14:41 +090036class AidlTypeSpecifier;
37
38namespace android {
39namespace aidl {
40
41// AidlTypenames is a collection of AIDL types available to a compilation unit.
42//
43// Basic types (such as int, String, etc.) are added by default, while defined
44// types (such as IFoo, MyParcelable, etc.) and types from preprocessed inputs
45// are added as they are recognized by the parser.
46//
47// When AidlTypeSpecifier is encountered during parsing, parser defers the
48// resolution of it until the end of the parsing, where it uses AidlTypenames
49// to resolve type names in AidlTypeSpecifier.
50//
51// Note that nothing here is specific to either Java or C++.
Jiyong Parkac896042018-07-23 21:23:05 +090052class AidlTypenames final {
Jiyong Park1deecc32018-07-17 01:14:41 +090053 public:
54 AidlTypenames() = default;
Jiyong Parkb034bf02018-07-30 17:44:33 +090055 void Reset();
56 bool AddDefinedType(unique_ptr<AidlDefinedType> type);
Jiyong Park1deecc32018-07-17 01:14:41 +090057 bool AddPreprocessedType(unique_ptr<AidlDefinedType> type);
Jiyong Parkac896042018-07-23 21:23:05 +090058 static bool IsBuiltinTypename(const string& type_name);
Jeongik Chadb0f59e2018-11-01 18:11:21 +090059 static bool IsPrimitiveTypename(const string& type_name);
Jiyong Parkac896042018-07-23 21:23:05 +090060 const AidlDefinedType* TryGetDefinedType(const string& type_name) const;
61 pair<string, bool> ResolveTypename(const string& type_name) const;
Jiyong Park1d2df7d2018-07-23 15:22:50 +090062 bool CanBeOutParameter(const AidlTypeSpecifier& type) const;
Jeongik Cha047c5ee2019-08-07 23:16:49 +090063 bool IsIgnorableImport(const string& import) const;
Daniel Norman85aed542019-08-21 12:01:14 -070064 // Returns the AidlEnumDeclaration of the given type, or nullptr if the type
65 // is not an AidlEnumDeclaration;
66 const AidlEnumDeclaration* GetEnumDeclaration(const AidlTypeSpecifier& type) const;
67 // Returns the AidlInterface of the given type, or nullptr if the type
68 // is not an AidlInterface;
69 const AidlInterface* GetInterface(const AidlTypeSpecifier& type) const;
Steven Moreland6cee3482018-07-18 14:39:58 -070070 // Iterates over all defined and then preprocessed types
71 void IterateTypes(const std::function<void(const AidlDefinedType&)>& body) const;
72
Jiyong Park1deecc32018-07-17 01:14:41 +090073 private:
Jiyong Park8f6ec462020-01-19 20:52:47 +090074 struct DefinedImplResult {
75 DefinedImplResult(const AidlDefinedType* type, const bool from_preprocessed)
76 : type(type), from_preprocessed(from_preprocessed) {}
77 const AidlDefinedType* type;
78 const bool from_preprocessed;
79 };
80 DefinedImplResult TryGetDefinedTypeImpl(const string& type_name) const;
Jiyong Parkb034bf02018-07-30 17:44:33 +090081 map<string, unique_ptr<AidlDefinedType>> defined_types_;
Jiyong Park1deecc32018-07-17 01:14:41 +090082 map<string, unique_ptr<AidlDefinedType>> preprocessed_types_;
83};
84
85} // namespace aidl
86} // namespace android