blob: d8bb9d887d618ba29aa6b1f3ac0106f58ad735ca [file] [log] [blame]
Will McVickerd7d18df2019-09-12 13:40:50 -07001/*
2 * Copyright (C) 2019, 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
Steven Moreland9fccf582018-08-27 20:36:27 -070017#pragma once
Christopher Wileyec31a052016-01-25 07:28:51 -080018
Casey Dahlinbc7a50a2015-09-28 19:20:50 -070019#include <memory>
Jeongik Cha997281d2020-01-16 15:23:59 +090020#include <regex>
Casey Dahlindd691812015-09-09 17:59:06 -070021#include <string>
Jeongik Chadf76dc72019-11-28 00:08:47 +090022#include <unordered_set>
Casey Dahlinbc7a50a2015-09-28 19:20:50 -070023#include <vector>
Casey Dahlindd691812015-09-09 17:59:06 -070024
Elliott Hughes0a620672015-12-04 13:53:18 -080025#include <android-base/strings.h>
Casey Dahlin73d46b02015-09-11 02:47:54 +000026
Jooyung Han888c5bc2020-12-22 17:28:47 +090027#include "aidl_typenames.h"
28#include "code_writer.h"
29#include "diagnostics.h"
30#include "io_delegate.h"
31#include "options.h"
32
Jiyong Parkb034bf02018-07-30 17:44:33 +090033using android::aidl::AidlTypenames;
Jiyong Park02da7422018-07-16 16:00:26 +090034using android::aidl::CodeWriter;
Jooyung Han888c5bc2020-12-22 17:28:47 +090035using android::aidl::DiagnosticsContext;
Jeongik Cha047c5ee2019-08-07 23:16:49 +090036using android::aidl::Options;
Steven Moreland3f658cf2018-08-20 13:40:54 -070037using std::shared_ptr;
Jiyong Park1deecc32018-07-17 01:14:41 +090038using std::string;
39using std::unique_ptr;
40using std::vector;
Andrei Onea8714b022019-02-01 18:55:54 +000041class AidlNode;
42
43namespace android {
44namespace aidl {
45namespace mappings {
46std::string dump_location(const AidlNode& method);
47} // namespace mappings
Mathew Inwoodadb74672019-11-29 14:01:53 +000048namespace java {
49std::string dump_location(const AidlNode& method);
50} // namespace java
Andrei Onea8714b022019-02-01 18:55:54 +000051} // namespace aidl
52} // namespace android
53
Steven Moreland46e9da82018-07-27 15:45:29 -070054class AidlLocation {
Casey Dahlinbc7a50a2015-09-28 19:20:50 -070055 public:
Steven Moreland46e9da82018-07-27 15:45:29 -070056 struct Point {
Dan Willemsen609ba6d2019-12-30 10:44:00 -080057 int line;
58 int column;
Steven Moreland46e9da82018-07-27 15:45:29 -070059 };
60
Devin Mooredf93ebb2020-03-25 14:03:35 -070061 enum class Source {
62 // From internal aidl source code
63 INTERNAL = 0,
64 // From a parsed file
65 EXTERNAL = 1
66 };
67
68 AidlLocation(const std::string& file, Point begin, Point end, Source source);
Devin Moore5de18ed2020-04-02 13:52:29 -070069 AidlLocation(const std::string& file, Source source)
70 : AidlLocation(file, {0, 0}, {0, 0}, source) {}
Devin Mooredf93ebb2020-03-25 14:03:35 -070071
72 bool IsInternal() const { return source_ == Source::INTERNAL; }
Steven Moreland46e9da82018-07-27 15:45:29 -070073
Devin Moore5de18ed2020-04-02 13:52:29 -070074 // The first line of a file is line 1.
75 bool LocationKnown() const { return begin_.line != 0; }
76
Steven Moreland46e9da82018-07-27 15:45:29 -070077 friend std::ostream& operator<<(std::ostream& os, const AidlLocation& l);
Andrei Onea8714b022019-02-01 18:55:54 +000078 friend class AidlNode;
Casey Dahlinbc7a50a2015-09-28 19:20:50 -070079
80 private:
Steven Moreland541788d2020-05-21 22:05:52 +000081 // INTENTIONALLY HIDDEN: only operator<< should access details here.
82 // Otherwise, locations should only ever be copied around to construct new
83 // objects.
Steven Moreland46e9da82018-07-27 15:45:29 -070084 const std::string file_;
85 Point begin_;
86 Point end_;
Devin Mooredf93ebb2020-03-25 14:03:35 -070087 Source source_;
Steven Moreland46e9da82018-07-27 15:45:29 -070088};
89
Devin Mooredf93ebb2020-03-25 14:03:35 -070090#define AIDL_LOCATION_HERE \
Steven Moreland21780812020-09-11 01:29:45 +000091 (AidlLocation{__FILE__, {__LINE__, 0}, {__LINE__, 0}, AidlLocation::Source::INTERNAL})
Steven Moreland02e012e2018-08-02 14:58:10 -070092
Steven Moreland46e9da82018-07-27 15:45:29 -070093std::ostream& operator<<(std::ostream& os, const AidlLocation& l);
94
95// Anything that is locatable in a .aidl file.
96class AidlNode {
97 public:
98 AidlNode(const AidlLocation& location);
Steven Moreland3f658cf2018-08-20 13:40:54 -070099
100 AidlNode(const AidlNode&) = default;
Steven Moreland46e9da82018-07-27 15:45:29 -0700101 virtual ~AidlNode() = default;
102
Jiyong Parkd800fef2020-07-22 18:09:43 +0900103 AidlNode(AidlNode&&) = delete;
104 AidlNode& operator=(AidlNode&&) = delete;
105
Devin Mooredf93ebb2020-03-25 14:03:35 -0700106 // To be able to print AidlLocation
Steven Morelandb0d15a52020-03-31 14:03:47 -0700107 friend class AidlErrorLog;
Andrei Onea8714b022019-02-01 18:55:54 +0000108 friend std::string android::aidl::mappings::dump_location(const AidlNode&);
Mathew Inwoodadb74672019-11-29 14:01:53 +0000109 friend std::string android::aidl::java::dump_location(const AidlNode&);
Steven Moreland46e9da82018-07-27 15:45:29 -0700110
Devin Mooredf93ebb2020-03-25 14:03:35 -0700111 const AidlLocation& GetLocation() const { return location_; }
112
Steven Moreland46e9da82018-07-27 15:45:29 -0700113 private:
Mathew Inwoodadb74672019-11-29 14:01:53 +0000114 std::string PrintLine() const;
Andrei Onea8714b022019-02-01 18:55:54 +0000115 std::string PrintLocation() const;
Steven Moreland46e9da82018-07-27 15:45:29 -0700116 const AidlLocation location_;
Casey Dahlinbc7a50a2015-09-28 19:20:50 -0700117};
118
Jeongik Chadf76dc72019-11-28 00:08:47 +0900119// unique_ptr<AidlTypeSpecifier> for type arugment,
120// std::string for type parameter(T, U, and so on).
121template <typename T>
122class AidlParameterizable {
123 public:
124 AidlParameterizable(std::vector<T>* type_params) : type_params_(type_params) {}
125 virtual ~AidlParameterizable() = default;
126 bool IsGeneric() const { return type_params_ != nullptr; }
127 const std::vector<T>& GetTypeParameters() const { return *type_params_; }
128 bool CheckValid() const;
129
Steven Moreland6c07b832020-10-29 23:39:53 +0000130 __attribute__((warn_unused_result)) bool SetTypeParameters(std::vector<T>* type_params) {
131 if (type_params_) return false;
132 type_params_.reset(type_params);
133 return true;
134 }
135
Jeongik Chadf76dc72019-11-28 00:08:47 +0900136 virtual const AidlNode& AsAidlNode() const = 0;
137
138 protected:
139 AidlParameterizable(const AidlParameterizable&);
140
141 private:
Steven Moreland6c07b832020-10-29 23:39:53 +0000142 unique_ptr<std::vector<T>> type_params_;
Jeongik Chadf76dc72019-11-28 00:08:47 +0900143 static_assert(std::is_same<T, unique_ptr<AidlTypeSpecifier>>::value ||
144 std::is_same<T, std::string>::value);
145};
146template <>
147bool AidlParameterizable<std::string>::CheckValid() const;
148
Andrei Onea9445fc62019-06-27 18:11:59 +0100149class AidlConstantValue;
150class AidlConstantDeclaration;
151
152// Transforms a value string into a language specific form. Raw value as produced by
153// AidlConstantValue.
154using ConstantValueDecorator =
155 std::function<std::string(const AidlTypeSpecifier& type, const std::string& raw_value)>;
156
Jiyong Park68bc77a2018-07-19 19:00:45 +0900157class AidlAnnotation : public AidlNode {
158 public:
Steven Moreland0cea4aa2020-04-20 21:06:02 -0700159 enum class Type {
160 BACKING = 1,
161 HIDE,
162 JAVA_STABLE_PARCELABLE,
163 UNSUPPORTED_APP_USAGE,
164 VINTF_STABILITY,
165 NULLABLE,
166 UTF8_IN_CPP,
Steven Morelanda7764e52020-10-27 17:29:29 +0000167 SENSITIVE_DATA,
Jiyong Parkbf5fd5c2020-06-05 19:48:05 +0900168 JAVA_PASSTHROUGH,
Jooyung Han90345002020-10-23 15:28:53 +0900169 JAVA_DERIVE,
Jeongik Chad0a10272020-08-06 16:33:36 +0900170 JAVA_ONLY_IMMUTABLE,
Devin Moorec7e47a32020-08-07 10:55:25 -0700171 FIXED_SIZE,
Jiyong Park27fd7fd2020-08-27 16:25:09 +0900172 DESCRIPTOR,
Andrei Homescue61feb52020-08-18 15:44:24 -0700173 RUST_DERIVE,
Steven Moreland0cea4aa2020-04-20 21:06:02 -0700174 };
175 static std::string TypeToString(Type type);
176
Andrei Onea9445fc62019-06-27 18:11:59 +0100177 static AidlAnnotation* Parse(
178 const AidlLocation& location, const string& name,
179 std::map<std::string, std::shared_ptr<AidlConstantValue>>* parameter_list);
Steven Moreland46e9da82018-07-27 15:45:29 -0700180
Steven Moreland3f658cf2018-08-20 13:40:54 -0700181 AidlAnnotation(const AidlAnnotation&) = default;
Steven Moreland3be75772018-08-20 13:27:43 -0700182 AidlAnnotation(AidlAnnotation&&) = default;
Jiyong Park68bc77a2018-07-19 19:00:45 +0900183 virtual ~AidlAnnotation() = default;
Andrei Onea9445fc62019-06-27 18:11:59 +0100184 bool CheckValid() const;
Steven Moreland3be75772018-08-20 13:27:43 -0700185
Jooyung Hand902a972020-10-23 17:32:44 +0900186 const string& GetName() const { return schema_.name; }
Steven Moreland0cea4aa2020-04-20 21:06:02 -0700187 const Type& GetType() const { return schema_.type; }
Jooyung Hand902a972020-10-23 17:32:44 +0900188 bool Repeatable() const { return schema_.repeatable; }
Jooyung Han965e31d2020-11-27 12:30:16 +0900189
190 // ToString is for dumping AIDL.
191 // Returns string representation of this annotation.
192 // e.g) "@RustDerive(Clone=true, Copy=true)"
193 string ToString() const;
194
Andrei Onea9445fc62019-06-27 18:11:59 +0100195 std::map<std::string, std::string> AnnotationParams(
196 const ConstantValueDecorator& decorator) const;
Jiyong Parka6605ab2018-11-11 14:30:21 +0900197 const string& GetComments() const { return comments_; }
198 void SetComments(const string& comments) { comments_ = comments; }
Jiyong Park68bc77a2018-07-19 19:00:45 +0900199
200 private:
Steven Moreland0cea4aa2020-04-20 21:06:02 -0700201 struct Schema {
202 AidlAnnotation::Type type;
203
204 // text name in .aidl file, e.g. "nullable"
205 std::string name;
206
207 // map from param name -> value type
208 std::map<std::string, std::string> supported_parameters;
Jooyung Hand902a972020-10-23 17:32:44 +0900209
210 bool repeatable;
Steven Moreland0cea4aa2020-04-20 21:06:02 -0700211 };
212 static const std::vector<Schema>& AllSchemas();
213
214 AidlAnnotation(const AidlLocation& location, const Schema& schema,
Andrei Onea9445fc62019-06-27 18:11:59 +0100215 std::map<std::string, std::shared_ptr<AidlConstantValue>>&& parameters);
Steven Moreland0cea4aa2020-04-20 21:06:02 -0700216
217 const Schema& schema_;
Jiyong Parka6605ab2018-11-11 14:30:21 +0900218 string comments_;
Andrei Onea9445fc62019-06-27 18:11:59 +0100219 std::map<std::string, std::shared_ptr<AidlConstantValue>> parameters_;
Jiyong Park68bc77a2018-07-19 19:00:45 +0900220};
221
Steven Moreland3be75772018-08-20 13:27:43 -0700222static inline bool operator<(const AidlAnnotation& lhs, const AidlAnnotation& rhs) {
223 return lhs.GetName() < rhs.GetName();
224}
225static inline bool operator==(const AidlAnnotation& lhs, const AidlAnnotation& rhs) {
226 return lhs.GetName() == rhs.GetName();
227}
Jiyong Park3656c3c2018-08-01 20:02:01 +0900228
Casey Dahline7922932016-02-29 17:23:01 -0800229class AidlAnnotatable : public AidlNode {
Casey Dahlin0ee37582015-09-30 16:31:55 -0700230 public:
Steven Moreland46e9da82018-07-27 15:45:29 -0700231 AidlAnnotatable(const AidlLocation& location);
Steven Moreland3f658cf2018-08-20 13:40:54 -0700232
233 AidlAnnotatable(const AidlAnnotatable&) = default;
234 AidlAnnotatable(AidlAnnotatable&&) = default;
Casey Dahline7922932016-02-29 17:23:01 -0800235 virtual ~AidlAnnotatable() = default;
236
Artur Satayev91fe8712019-07-29 13:06:01 +0100237 void Annotate(vector<AidlAnnotation>&& annotations) {
238 for (auto& annotation : annotations) {
239 annotations_.emplace_back(std::move(annotation));
240 }
241 }
Jiyong Park68bc77a2018-07-19 19:00:45 +0900242 bool IsNullable() const;
Jiyong Park68bc77a2018-07-19 19:00:45 +0900243 bool IsUtf8InCpp() const;
Steven Morelanda7764e52020-10-27 17:29:29 +0000244 bool IsSensitiveData() const;
Steven Morelanda57d0a62019-07-30 09:41:14 -0700245 bool IsVintfStability() const;
Jeongik Chad0a10272020-08-06 16:33:36 +0900246 bool IsJavaOnlyImmutable() const;
Devin Moorec7e47a32020-08-07 10:55:25 -0700247 bool IsFixedSize() const;
Jeongik Cha88f95a82020-01-15 13:02:16 +0900248 bool IsStableApiParcelable(Options::Language lang) const;
Makoto Onuki78a1c1c2020-03-04 16:57:23 -0800249 bool IsHide() const;
Jooyung Han829ec7c2020-12-02 12:07:36 +0900250 bool JavaDerive(const std::string& method) const;
Jiyong Park27fd7fd2020-08-27 16:25:09 +0900251 std::string GetDescriptor() const;
Andrei Onea9445fc62019-06-27 18:11:59 +0100252
Steven Moreland7e4b9502020-02-20 18:10:42 -0800253 void DumpAnnotations(CodeWriter* writer) const;
254
Andrei Onea9445fc62019-06-27 18:11:59 +0100255 const AidlAnnotation* UnsupportedAppUsage() const;
Andrei Homescue61feb52020-08-18 15:44:24 -0700256 const AidlAnnotation* RustDerive() const;
Daniel Norman716d3112019-09-10 13:11:56 -0700257 const AidlTypeSpecifier* BackingType(const AidlTypenames& typenames) const;
Jooyung Han965e31d2020-11-27 12:30:16 +0900258
259 // ToString is for dumping AIDL.
260 // Returns string representation of annotations.
261 // e.g) "@JavaDerive(toString=true) @RustDerive(Clone=true, Copy=true)"
Jiyong Park68bc77a2018-07-19 19:00:45 +0900262 std::string ToString() const;
Casey Dahline7922932016-02-29 17:23:01 -0800263
Jiyong Parka6605ab2018-11-11 14:30:21 +0900264 const vector<AidlAnnotation>& GetAnnotations() const { return annotations_; }
Jooyung Han888c5bc2020-12-22 17:28:47 +0900265 bool CheckValid(const AidlTypenames&) const;
Jiyong Park3656c3c2018-08-01 20:02:01 +0900266
Steven Moreland181144c2020-04-20 19:57:56 -0700267 protected:
Steven Moreland0cea4aa2020-04-20 21:06:02 -0700268 virtual std::set<AidlAnnotation::Type> GetSupportedAnnotations() const = 0;
Steven Moreland181144c2020-04-20 19:57:56 -0700269
Casey Dahline7922932016-02-29 17:23:01 -0800270 private:
Jiyong Parka6605ab2018-11-11 14:30:21 +0900271 vector<AidlAnnotation> annotations_;
Casey Dahline7922932016-02-29 17:23:01 -0800272};
273
Jiyong Park1deecc32018-07-17 01:14:41 +0900274// AidlTypeSpecifier represents a reference to either a built-in type,
275// a defined type, or a variant (e.g., array of generic) of a type.
Jeongik Chadf76dc72019-11-28 00:08:47 +0900276class AidlTypeSpecifier final : public AidlAnnotatable,
277 public AidlParameterizable<unique_ptr<AidlTypeSpecifier>> {
Casey Dahline7922932016-02-29 17:23:01 -0800278 public:
Steven Moreland46e9da82018-07-27 15:45:29 -0700279 AidlTypeSpecifier(const AidlLocation& location, const string& unresolved_name, bool is_array,
280 vector<unique_ptr<AidlTypeSpecifier>>* type_params, const string& comments);
Jiyong Parkd59a10d2018-07-18 11:12:55 +0900281 virtual ~AidlTypeSpecifier() = default;
Casey Dahlin0ee37582015-09-30 16:31:55 -0700282
Steven Moreland3f658cf2018-08-20 13:40:54 -0700283 // Copy of this type which is not an array.
Jooyung Hand2fa0232020-10-19 02:51:41 +0900284 const AidlTypeSpecifier& ArrayBase() const;
Steven Moreland3f658cf2018-08-20 13:40:54 -0700285
Jiyong Park1deecc32018-07-17 01:14:41 +0900286 // Returns the full-qualified name of the base type.
287 // int -> int
288 // int[] -> int
289 // List<String> -> List
290 // IFoo -> foo.bar.IFoo (if IFoo is in package foo.bar)
291 const string& GetName() const {
292 if (IsResolved()) {
293 return fully_qualified_name_;
294 } else {
295 return GetUnresolvedName();
296 }
297 }
Casey Dahlin0ee37582015-09-30 16:31:55 -0700298
Jooyung Han965e31d2020-11-27 12:30:16 +0900299 // ToString is for dumping AIDL.
300 // Returns string representation of this type specifier including annotations.
301 // This is "annotations type_name type_params? array_marker?".
302 // e.g) "@utf8InCpp String[]";
303 std::string ToString() const;
Jiyong Park1deecc32018-07-17 01:14:41 +0900304
Jooyung Han965e31d2020-11-27 12:30:16 +0900305 // Signature is for comparing AIDL types.
306 // Returns string representation of this type specifier.
307 // This is "type_name type_params? array_marker?".
308 // e.g.) "String[]" (even if it is annotated with @utf8InCpp)
Jiyong Park02da7422018-07-16 16:00:26 +0900309 std::string Signature() const;
310
Jiyong Park1deecc32018-07-17 01:14:41 +0900311 const string& GetUnresolvedName() const { return unresolved_name_; }
312
Jeongik Cha997281d2020-01-16 15:23:59 +0900313 bool IsHidden() const;
314
Jiyong Park1deecc32018-07-17 01:14:41 +0900315 const string& GetComments() const { return comments_; }
316
Jeongik Cha1a7ab642019-07-29 17:31:02 +0900317 const std::vector<std::string> GetSplitName() const { return split_name_; }
318
Jiyong Parka6605ab2018-11-11 14:30:21 +0900319 void SetComments(const string& comment) { comments_ = comment; }
320
Jiyong Park1deecc32018-07-17 01:14:41 +0900321 bool IsResolved() const { return fully_qualified_name_ != ""; }
322
323 bool IsArray() const { return is_array_; }
324
Steven Moreland6c07b832020-10-29 23:39:53 +0000325 __attribute__((warn_unused_result)) bool SetArray() {
326 if (is_array_) return false;
327 is_array_ = true;
328 return true;
329 }
330
Jiyong Park1deecc32018-07-17 01:14:41 +0900331 // Resolve the base type name to a fully-qualified name. Return false if the
332 // resolution fails.
Daniel Norman716d3112019-09-10 13:11:56 -0700333 bool Resolve(const AidlTypenames& typenames);
Casey Dahlin0ee37582015-09-30 16:31:55 -0700334
Steven Moreland0cea4aa2020-04-20 21:06:02 -0700335 std::set<AidlAnnotation::Type> GetSupportedAnnotations() const override;
Jooyung Han888c5bc2020-12-22 17:28:47 +0900336 bool CheckValid(const AidlTypenames& typenames) const;
Steven Morelandd59e3172020-05-11 16:42:09 -0700337 bool LanguageSpecificCheckValid(const AidlTypenames& typenames, Options::Language lang) const;
Jeongik Chadf76dc72019-11-28 00:08:47 +0900338 const AidlNode& AsAidlNode() const override { return *this; }
Jiyong Park1d2df7d2018-07-23 15:22:50 +0900339
Jooyung Hane9bb9de2020-11-01 22:16:57 +0900340 const AidlDefinedType* GetDefinedType() const;
341
Casey Dahlin0ee37582015-09-30 16:31:55 -0700342 private:
Steven Moreland3f658cf2018-08-20 13:40:54 -0700343 AidlTypeSpecifier(const AidlTypeSpecifier&) = default;
344
Jiyong Park1deecc32018-07-17 01:14:41 +0900345 const string unresolved_name_;
346 string fully_qualified_name_;
Steven Moreland3f658cf2018-08-20 13:40:54 -0700347 bool is_array_;
Jiyong Parka6605ab2018-11-11 14:30:21 +0900348 string comments_;
Jeongik Cha1a7ab642019-07-29 17:31:02 +0900349 vector<string> split_name_;
Jooyung Han690f5842020-12-04 13:02:04 +0900350 const AidlDefinedType* defined_type_ = nullptr; // set when Resolve() for defined types
Jooyung Hand2fa0232020-10-19 02:51:41 +0900351 mutable shared_ptr<AidlTypeSpecifier> array_base_;
Casey Dahlin0ee37582015-09-30 16:31:55 -0700352};
353
Steven Moreland860b1942018-08-16 14:59:28 -0700354// Returns the universal value unaltered.
355std::string AidlConstantValueDecorator(const AidlTypeSpecifier& type, const std::string& raw_value);
356
Steven Moreland9ea10e32018-07-19 15:26:09 -0700357class AidlConstantValue;
Jooyung Han3f347ca2020-12-01 12:41:50 +0900358class AidlMethod;
359class AidlConstantDeclaration;
360class AidlVariableDeclaration;
361
362class AidlMember : public AidlNode {
363 public:
364 AidlMember(const AidlLocation& location);
365 virtual ~AidlMember() = default;
366
367 // non-copyable, non-movable
368 AidlMember(const AidlMember&) = delete;
369 AidlMember(AidlMember&&) = delete;
370 AidlMember& operator=(const AidlMember&) = delete;
371 AidlMember& operator=(AidlMember&&) = delete;
372
Jooyung Han829ec7c2020-12-02 12:07:36 +0900373 virtual const AidlMethod* AsMethod() const { return nullptr; }
374 virtual const AidlConstantDeclaration* AsConstantDeclaration() const { return nullptr; }
375 virtual const AidlVariableDeclaration* AsVariableDeclaration() const { return nullptr; }
376
377 AidlMethod* AsMethod() {
378 return const_cast<AidlMethod*>(const_cast<const AidlMember*>(this)->AsMethod());
379 }
380 AidlConstantDeclaration* AsConstantDeclaration() {
381 return const_cast<AidlConstantDeclaration*>(
382 const_cast<const AidlMember*>(this)->AsConstantDeclaration());
383 }
384 AidlVariableDeclaration* AsVariableDeclaration() {
385 return const_cast<AidlVariableDeclaration*>(
386 const_cast<const AidlMember*>(this)->AsVariableDeclaration());
387 }
Jooyung Han3f347ca2020-12-01 12:41:50 +0900388};
389
Steven Moreland541788d2020-05-21 22:05:52 +0000390// TODO: This class is used for method arguments and also parcelable fields,
391// and it should be split up since default values don't apply to method
392// arguments
Jooyung Han3f347ca2020-12-01 12:41:50 +0900393class AidlVariableDeclaration : public AidlMember {
Steven Moreland5557f1c2018-07-02 13:50:23 -0700394 public:
Steven Moreland46e9da82018-07-27 15:45:29 -0700395 AidlVariableDeclaration(const AidlLocation& location, AidlTypeSpecifier* type,
396 const std::string& name);
397 AidlVariableDeclaration(const AidlLocation& location, AidlTypeSpecifier* type,
398 const std::string& name, AidlConstantValue* default_value);
Steven Moreland5557f1c2018-07-02 13:50:23 -0700399 virtual ~AidlVariableDeclaration() = default;
400
Jiyong Parkd800fef2020-07-22 18:09:43 +0900401 // non-copyable, non-movable
402 AidlVariableDeclaration(const AidlVariableDeclaration&) = delete;
403 AidlVariableDeclaration(AidlVariableDeclaration&&) = delete;
404 AidlVariableDeclaration& operator=(const AidlVariableDeclaration&) = delete;
405 AidlVariableDeclaration& operator=(AidlVariableDeclaration&&) = delete;
406
Jooyung Han829ec7c2020-12-02 12:07:36 +0900407 const AidlVariableDeclaration* AsVariableDeclaration() const override { return this; }
Jooyung Han3f347ca2020-12-01 12:41:50 +0900408
Steven Moreland5557f1c2018-07-02 13:50:23 -0700409 std::string GetName() const { return name_; }
Jooyung Hanacae85d2020-10-28 16:39:09 +0900410 std::string GetCapitalizedName() const;
Jiyong Parkd59a10d2018-07-18 11:12:55 +0900411 const AidlTypeSpecifier& GetType() const { return *type_; }
Steven Moreland541788d2020-05-21 22:05:52 +0000412 // if this was constructed explicitly with a default value
413 bool IsDefaultUserSpecified() const { return default_user_specified_; }
414 // will return the default value this is constructed with or a default value
415 // if one is available
Steven Moreland9ea10e32018-07-19 15:26:09 -0700416 const AidlConstantValue* GetDefaultValue() const { return default_value_.get(); }
Jooyung Han53fb4242020-12-17 16:03:49 +0900417 bool HasUsefulDefaultValue() const;
Steven Moreland9ea10e32018-07-19 15:26:09 -0700418
Jiyong Parkd59a10d2018-07-18 11:12:55 +0900419 AidlTypeSpecifier* GetMutableType() { return type_.get(); }
Steven Moreland5557f1c2018-07-02 13:50:23 -0700420
Jeongik Chadb0f59e2018-11-01 18:11:21 +0900421 bool CheckValid(const AidlTypenames& typenames) const;
Jooyung Han965e31d2020-11-27 12:30:16 +0900422
423 // ToString is for dumping AIDL.
424 // Returns string representation of this variable decl including default value.
425 // This is "annotations type name default_value?".
426 // e.g) "@utf8InCpp String[] names = {"hello"}"
Steven Moreland5557f1c2018-07-02 13:50:23 -0700427 std::string ToString() const;
Jooyung Han965e31d2020-11-27 12:30:16 +0900428
429 // Signature is for comparing AIDL types.
430 // Returns string representation of this variable decl.
431 // This is "type name".
432 // e.g) "String[] name" (even if it is annotated with @utf8InCpp and has a default value.)
Jiyong Park02da7422018-07-16 16:00:26 +0900433 std::string Signature() const;
Steven Moreland5557f1c2018-07-02 13:50:23 -0700434
Steven Moreland860b1942018-08-16 14:59:28 -0700435 std::string ValueString(const ConstantValueDecorator& decorator) const;
Steven Moreland25294322018-08-07 18:13:55 -0700436
Steven Moreland5557f1c2018-07-02 13:50:23 -0700437 private:
Jiyong Parkd59a10d2018-07-18 11:12:55 +0900438 std::unique_ptr<AidlTypeSpecifier> type_;
Steven Moreland5557f1c2018-07-02 13:50:23 -0700439 std::string name_;
Steven Moreland541788d2020-05-21 22:05:52 +0000440 bool default_user_specified_;
Steven Moreland9ea10e32018-07-19 15:26:09 -0700441 std::unique_ptr<AidlConstantValue> default_value_;
Steven Moreland5557f1c2018-07-02 13:50:23 -0700442};
443
444class AidlArgument : public AidlVariableDeclaration {
Casey Dahlinbc7a50a2015-09-28 19:20:50 -0700445 public:
Casey Dahlinc378c992015-09-29 16:50:40 -0700446 enum Direction { IN_DIR = 1, OUT_DIR = 2, INOUT_DIR = 3 };
447
Steven Moreland46e9da82018-07-27 15:45:29 -0700448 AidlArgument(const AidlLocation& location, AidlArgument::Direction direction,
449 AidlTypeSpecifier* type, const std::string& name);
450 AidlArgument(const AidlLocation& location, AidlTypeSpecifier* type, const std::string& name);
Casey Dahlinbc7a50a2015-09-28 19:20:50 -0700451 virtual ~AidlArgument() = default;
452
Jiyong Parkd800fef2020-07-22 18:09:43 +0900453 // non-copyable, non-movable
454 AidlArgument(const AidlArgument&) = delete;
455 AidlArgument(AidlArgument&&) = delete;
456 AidlArgument& operator=(const AidlArgument&) = delete;
457 AidlArgument& operator=(AidlArgument&&) = delete;
458
Casey Dahlinc378c992015-09-29 16:50:40 -0700459 Direction GetDirection() const { return direction_; }
Christopher Wileyad339272015-10-05 19:11:58 -0700460 bool IsOut() const { return direction_ & OUT_DIR; }
461 bool IsIn() const { return direction_ & IN_DIR; }
Casey Dahlinc378c992015-09-29 16:50:40 -0700462 bool DirectionWasSpecified() const { return direction_specified_; }
Jiyong Park3656c3c2018-08-01 20:02:01 +0900463 string GetDirectionSpecifier() const;
Christopher Wileyad339272015-10-05 19:11:58 -0700464
Jooyung Han965e31d2020-11-27 12:30:16 +0900465 // ToString is for dumping AIDL.
466 // Returns string representation of this argument including direction
467 // This is "direction annotations type name".
468 // e.g) "in @utf8InCpp String[] names"
Casey Dahlinc378c992015-09-29 16:50:40 -0700469 std::string ToString() const;
470
Casey Dahlinbc7a50a2015-09-28 19:20:50 -0700471 private:
Casey Dahlinc378c992015-09-29 16:50:40 -0700472 Direction direction_;
473 bool direction_specified_;
Casey Dahlina834dd42015-09-23 11:52:15 -0700474};
Adam Lesinskiffa16862014-01-23 18:17:42 -0800475
Will McVickerd7d18df2019-09-12 13:40:50 -0700476class AidlUnaryConstExpression;
477class AidlBinaryConstExpression;
Jooyung Han690f5842020-12-04 13:02:04 +0900478class AidlConstantReference;
Will McVickerd7d18df2019-09-12 13:40:50 -0700479
Steven Moreland693640b2018-07-19 13:46:27 -0700480class AidlConstantValue : public AidlNode {
Casey Dahlind40e2fe2015-11-24 14:06:52 -0800481 public:
Will McVickerd7d18df2019-09-12 13:40:50 -0700482 enum class Type {
483 // WARNING: Don't change this order! The order is used to determine type
484 // promotion during a binary expression.
485 BOOLEAN,
486 INT8,
487 INT32,
488 INT64,
489 ARRAY,
490 CHARACTER,
491 STRING,
Jooyung Han690f5842020-12-04 13:02:04 +0900492 REF,
Will McVickerd7d18df2019-09-12 13:40:50 -0700493 FLOATING,
494 UNARY,
495 BINARY,
496 ERROR,
497 };
498
Jooyung Han690f5842020-12-04 13:02:04 +0900499 struct Visitor {
500 virtual ~Visitor() {}
501 virtual void Visit(AidlConstantValue&) = 0;
502 virtual void Visit(AidlConstantReference&) = 0;
503 virtual void Visit(AidlUnaryConstExpression&) = 0;
504 virtual void Visit(AidlBinaryConstExpression&) = 0;
505 };
506
Will McVickerd7d18df2019-09-12 13:40:50 -0700507 /*
508 * Return the value casted to the given type.
509 */
510 template <typename T>
511 T cast() const;
Casey Dahlind40e2fe2015-11-24 14:06:52 -0800512
Steven Moreland693640b2018-07-19 13:46:27 -0700513 virtual ~AidlConstantValue() = default;
Casey Dahlind40e2fe2015-11-24 14:06:52 -0800514
Jiyong Parkd800fef2020-07-22 18:09:43 +0900515 // non-copyable, non-movable
516 AidlConstantValue(const AidlConstantValue&) = delete;
517 AidlConstantValue(AidlConstantValue&&) = delete;
518 AidlConstantValue& operator=(const AidlConstantValue&) = delete;
519 AidlConstantValue& operator=(AidlConstantValue&&) = delete;
520
Steven Moreland541788d2020-05-21 22:05:52 +0000521 // creates default value, when one isn't specified
522 // nullptr if no default available
523 static AidlConstantValue* Default(const AidlTypeSpecifier& specifier);
524
Steven Moreland25294322018-08-07 18:13:55 -0700525 static AidlConstantValue* Boolean(const AidlLocation& location, bool value);
526 static AidlConstantValue* Character(const AidlLocation& location, char value);
Steven Moreland25294322018-08-07 18:13:55 -0700527 // example: 123, -5498, maybe any size
Will McVickerd7d18df2019-09-12 13:40:50 -0700528 static AidlConstantValue* Integral(const AidlLocation& location, const string& value);
529 static AidlConstantValue* Floating(const AidlLocation& location, const std::string& value);
Steven Moreland860b1942018-08-16 14:59:28 -0700530 static AidlConstantValue* Array(const AidlLocation& location,
Will McVickerd7d18df2019-09-12 13:40:50 -0700531 std::unique_ptr<vector<unique_ptr<AidlConstantValue>>> values);
Steven Moreland693640b2018-07-19 13:46:27 -0700532 // example: "\"asdf\""
Will McVickerd7d18df2019-09-12 13:40:50 -0700533 static AidlConstantValue* String(const AidlLocation& location, const string& value);
Steven Moreland693640b2018-07-19 13:46:27 -0700534
Will McVickerd7d18df2019-09-12 13:40:50 -0700535 Type GetType() const { return final_type_; }
Jooyung Han29813842020-12-08 01:28:03 +0900536 const std::string& Literal() const { return value_; }
Steven Moreland25294322018-08-07 18:13:55 -0700537
Will McVickerd7d18df2019-09-12 13:40:50 -0700538 virtual bool CheckValid() const;
Steven Moreland860b1942018-08-16 14:59:28 -0700539
540 // Raw value of type (currently valid in C++ and Java). Empty string on error.
Steven Moreland4bcb05c2019-11-27 18:57:47 -0800541 string ValueString(const AidlTypeSpecifier& type, const ConstantValueDecorator& decorator) const;
Jooyung Han29813842020-12-08 01:28:03 +0900542 virtual void Accept(Visitor& visitor) {
543 visitor.Visit(*this);
544 if (type_ == Type::ARRAY) {
545 for (const auto& v : values_) {
546 v.get()->Accept(visitor);
547 }
548 }
549 }
Casey Dahlind40e2fe2015-11-24 14:06:52 -0800550
551 private:
Will McVickerd7d18df2019-09-12 13:40:50 -0700552 AidlConstantValue(const AidlLocation& location, Type parsed_type, int64_t parsed_value,
553 const string& checked_value);
554 AidlConstantValue(const AidlLocation& location, Type type, const string& checked_value);
Steven Moreland860b1942018-08-16 14:59:28 -0700555 AidlConstantValue(const AidlLocation& location, Type type,
Jooyung Han29813842020-12-08 01:28:03 +0900556 std::unique_ptr<vector<unique_ptr<AidlConstantValue>>> values,
557 const std::string& value);
Steven Moreland25294322018-08-07 18:13:55 -0700558 static string ToString(Type type);
Will McVickerd7d18df2019-09-12 13:40:50 -0700559 static bool ParseIntegral(const string& value, int64_t* parsed_value, Type* parsed_type);
560 static bool IsHex(const string& value);
Steven Moreland4bcb05c2019-11-27 18:57:47 -0800561
Jooyung Han74675c22020-12-15 08:39:57 +0900562 virtual bool evaluate() const;
Casey Dahlind40e2fe2015-11-24 14:06:52 -0800563
Steven Moreland693640b2018-07-19 13:46:27 -0700564 const Type type_ = Type::ERROR;
Will McVickerd7d18df2019-09-12 13:40:50 -0700565 const vector<unique_ptr<AidlConstantValue>> values_; // if type_ == ARRAY
566 const string value_; // otherwise
567
568 // State for tracking evaluation of expressions
Steven Moreland4bcb05c2019-11-27 18:57:47 -0800569 mutable bool is_valid_ = false; // cache of CheckValid, but may be marked false in evaluate
570 mutable bool is_evaluated_ = false; // whether evaluate has been called
Will McVickerd7d18df2019-09-12 13:40:50 -0700571 mutable Type final_type_;
572 mutable int64_t final_value_;
573 mutable string final_string_value_ = "";
Steven Moreland693640b2018-07-19 13:46:27 -0700574
Will McVickerd7d18df2019-09-12 13:40:50 -0700575 friend AidlUnaryConstExpression;
576 friend AidlBinaryConstExpression;
Jooyung Han690f5842020-12-04 13:02:04 +0900577 friend AidlConstantReference;
578};
579
580// Represents "<type>.<field>" which resolves to a constant which is one of
581// - constant declartion
582// - enumerator
583// When a <type> is missing, <field> is of the enclosing type.
584class AidlConstantReference : public AidlConstantValue {
585 public:
586 AidlConstantReference(const AidlLocation& location, const std::string& value,
587 const std::string& comments);
588
589 const std::unique_ptr<AidlTypeSpecifier>& GetRefType() const { return ref_type_; }
590 void SetRefType(std::unique_ptr<AidlTypeSpecifier> type) { ref_type_ = std::move(type); }
591 const std::string& GetFieldName() const { return field_name_; }
592 const std::string& GetComments() const { return comments_; }
593
594 bool CheckValid() const override;
595 void Accept(Visitor& visitor) override { visitor.Visit(*this); }
Jooyung Han29813842020-12-08 01:28:03 +0900596 const AidlConstantValue* Resolve();
Jooyung Han690f5842020-12-04 13:02:04 +0900597
598 private:
Jooyung Han74675c22020-12-15 08:39:57 +0900599 bool evaluate() const override;
Jooyung Han690f5842020-12-04 13:02:04 +0900600
601 std::unique_ptr<AidlTypeSpecifier> ref_type_;
602 std::string field_name_;
603 const std::string comments_;
Jooyung Han29813842020-12-08 01:28:03 +0900604 const AidlConstantValue* resolved_ = nullptr;
Will McVickerd7d18df2019-09-12 13:40:50 -0700605};
606
607class AidlUnaryConstExpression : public AidlConstantValue {
608 public:
609 AidlUnaryConstExpression(const AidlLocation& location, const string& op,
610 std::unique_ptr<AidlConstantValue> rval);
611
612 static bool IsCompatibleType(Type type, const string& op);
613 bool CheckValid() const override;
Jooyung Han690f5842020-12-04 13:02:04 +0900614 void Accept(Visitor& visitor) override {
615 visitor.Visit(*this);
616 unary_->Accept(visitor);
617 }
618
Will McVickerd7d18df2019-09-12 13:40:50 -0700619 private:
Jooyung Han74675c22020-12-15 08:39:57 +0900620 bool evaluate() const override;
Will McVickerd7d18df2019-09-12 13:40:50 -0700621
622 std::unique_ptr<AidlConstantValue> unary_;
623 const string op_;
624};
625
626class AidlBinaryConstExpression : public AidlConstantValue {
627 public:
628 AidlBinaryConstExpression(const AidlLocation& location, std::unique_ptr<AidlConstantValue> lval,
629 const string& op, std::unique_ptr<AidlConstantValue> rval);
630
631 bool CheckValid() const override;
Will McVickerd7d18df2019-09-12 13:40:50 -0700632
633 static bool AreCompatibleTypes(Type t1, Type t2);
634 // Returns the promoted kind for both operands
635 static Type UsualArithmeticConversion(Type left, Type right);
636 // Returns the promoted integral type where INT32 is the smallest type
637 static Type IntegralPromotion(Type in);
Jooyung Han690f5842020-12-04 13:02:04 +0900638 void Accept(Visitor& visitor) override {
639 visitor.Visit(*this);
640 left_val_->Accept(visitor);
641 right_val_->Accept(visitor);
642 }
Will McVickerd7d18df2019-09-12 13:40:50 -0700643
644 private:
Jooyung Han74675c22020-12-15 08:39:57 +0900645 bool evaluate() const override;
Will McVickerd7d18df2019-09-12 13:40:50 -0700646
647 std::unique_ptr<AidlConstantValue> left_val_;
648 std::unique_ptr<AidlConstantValue> right_val_;
649 const string op_;
Christopher Wileyd6bdd8d2016-05-03 11:23:13 -0700650};
651
Andrei Onea9445fc62019-06-27 18:11:59 +0100652struct AidlAnnotationParameter {
653 std::string name;
654 std::unique_ptr<AidlConstantValue> value;
655};
656
Steven Moreland693640b2018-07-19 13:46:27 -0700657class AidlConstantDeclaration : public AidlMember {
Christopher Wileyd6bdd8d2016-05-03 11:23:13 -0700658 public:
Steven Moreland46e9da82018-07-27 15:45:29 -0700659 AidlConstantDeclaration(const AidlLocation& location, AidlTypeSpecifier* specifier,
Will McVickerd7d18df2019-09-12 13:40:50 -0700660 const string& name, AidlConstantValue* value);
Steven Moreland693640b2018-07-19 13:46:27 -0700661 virtual ~AidlConstantDeclaration() = default;
Christopher Wileyd6bdd8d2016-05-03 11:23:13 -0700662
Jiyong Parkd800fef2020-07-22 18:09:43 +0900663 // non-copyable, non-movable
664 AidlConstantDeclaration(const AidlConstantDeclaration&) = delete;
665 AidlConstantDeclaration(AidlConstantDeclaration&&) = delete;
666 AidlConstantDeclaration& operator=(const AidlConstantDeclaration&) = delete;
667 AidlConstantDeclaration& operator=(AidlConstantDeclaration&&) = delete;
668
Steven Moreland693640b2018-07-19 13:46:27 -0700669 const AidlTypeSpecifier& GetType() const { return *type_; }
Steven Moreland4d12f9a2018-10-31 14:30:55 -0700670 AidlTypeSpecifier* GetMutableType() { return type_.get(); }
Will McVickerd7d18df2019-09-12 13:40:50 -0700671 const string& GetName() const { return name_; }
Steven Moreland693640b2018-07-19 13:46:27 -0700672 const AidlConstantValue& GetValue() const { return *value_; }
Jeongik Chadb0f59e2018-11-01 18:11:21 +0900673 bool CheckValid(const AidlTypenames& typenames) const;
Christopher Wileyd6bdd8d2016-05-03 11:23:13 -0700674
Jooyung Han965e31d2020-11-27 12:30:16 +0900675 // ToString is for dumping AIDL.
676 // Returns string representation of this const decl including a const value.
677 // This is "`const` annotations type name value".
678 // e.g) "const @utf8InCpp String[] names = { "hello" }"
Will McVickerd7d18df2019-09-12 13:40:50 -0700679 string ToString() const;
Jooyung Han965e31d2020-11-27 12:30:16 +0900680
681 // Signature is for comparing types.
682 // Returns string representation of this const decl.
683 // This is "direction annotations type name".
684 // e.g) "String[] names"
Will McVickerd7d18df2019-09-12 13:40:50 -0700685 string Signature() const;
Jooyung Han965e31d2020-11-27 12:30:16 +0900686
Steven Moreland860b1942018-08-16 14:59:28 -0700687 string ValueString(const ConstantValueDecorator& decorator) const {
Will McVickerd7d18df2019-09-12 13:40:50 -0700688 return value_->ValueString(GetType(), decorator);
Steven Moreland860b1942018-08-16 14:59:28 -0700689 }
Steven Moreland25294322018-08-07 18:13:55 -0700690
Jooyung Han829ec7c2020-12-02 12:07:36 +0900691 const AidlConstantDeclaration* AsConstantDeclaration() const override { return this; }
Christopher Wileyd6bdd8d2016-05-03 11:23:13 -0700692
693 private:
Steven Moreland693640b2018-07-19 13:46:27 -0700694 const unique_ptr<AidlTypeSpecifier> type_;
Will McVickerd7d18df2019-09-12 13:40:50 -0700695 const string name_;
696 unique_ptr<AidlConstantValue> value_;
Casey Dahlind40e2fe2015-11-24 14:06:52 -0800697};
698
699class AidlMethod : public AidlMember {
Casey Dahlin5c69deb2015-10-01 14:44:12 -0700700 public:
Will McVickerd7d18df2019-09-12 13:40:50 -0700701 AidlMethod(const AidlLocation& location, bool oneway, AidlTypeSpecifier* type, const string& name,
702 vector<unique_ptr<AidlArgument>>* args, const string& comments);
703 AidlMethod(const AidlLocation& location, bool oneway, AidlTypeSpecifier* type, const string& name,
704 vector<unique_ptr<AidlArgument>>* args, const string& comments, int id,
705 bool is_user_defined = true);
Casey Dahlin5c69deb2015-10-01 14:44:12 -0700706 virtual ~AidlMethod() = default;
707
Jiyong Parkd800fef2020-07-22 18:09:43 +0900708 // non-copyable, non-movable
709 AidlMethod(const AidlMethod&) = delete;
710 AidlMethod(AidlMethod&&) = delete;
711 AidlMethod& operator=(const AidlMethod&) = delete;
712 AidlMethod& operator=(AidlMethod&&) = delete;
713
Jooyung Han829ec7c2020-12-02 12:07:36 +0900714 const AidlMethod* AsMethod() const override { return this; }
Jeongik Cha997281d2020-01-16 15:23:59 +0900715 bool IsHidden() const;
Will McVickerd7d18df2019-09-12 13:40:50 -0700716 const string& GetComments() const { return comments_; }
Jiyong Parkd59a10d2018-07-18 11:12:55 +0900717 const AidlTypeSpecifier& GetType() const { return *type_; }
718 AidlTypeSpecifier* GetMutableType() { return type_.get(); }
Steven Morelandacd53472018-12-14 10:17:26 -0800719
Steven Moreland8c70ba92018-12-17 10:20:31 -0800720 // set if this method is part of an interface that is marked oneway
721 void ApplyInterfaceOneway(bool oneway) { oneway_ = oneway_ || oneway; }
Casey Dahlinf4a93112015-10-05 16:58:09 -0700722 bool IsOneway() const { return oneway_; }
Steven Morelandacd53472018-12-14 10:17:26 -0800723
Casey Dahlinf4a93112015-10-05 16:58:09 -0700724 const std::string& GetName() const { return name_; }
Casey Dahlinf4a93112015-10-05 16:58:09 -0700725 bool HasId() const { return has_id_; }
Jiyong Parked65bf42018-08-28 15:43:27 +0900726 int GetId() const { return id_; }
Casey Dahlinf4a93112015-10-05 16:58:09 -0700727 void SetId(unsigned id) { id_ = id; }
Casey Dahlinf2d23f72015-10-02 16:19:19 -0700728
Jiyong Park309668e2018-07-28 16:55:44 +0900729 bool IsUserDefined() const { return is_user_defined_; }
730
Casey Dahlinf4a93112015-10-05 16:58:09 -0700731 const std::vector<std::unique_ptr<AidlArgument>>& GetArguments() const {
Christopher Wileyad339272015-10-05 19:11:58 -0700732 return arguments_;
733 }
734 // An inout parameter will appear in both GetInArguments()
735 // and GetOutArguments(). AidlMethod retains ownership of the argument
736 // pointers returned in this way.
737 const std::vector<const AidlArgument*>& GetInArguments() const {
738 return in_arguments_;
739 }
740 const std::vector<const AidlArgument*>& GetOutArguments() const {
741 return out_arguments_;
Casey Dahlinf4a93112015-10-05 16:58:09 -0700742 }
Casey Dahlin5c69deb2015-10-01 14:44:12 -0700743
Jooyung Han965e31d2020-11-27 12:30:16 +0900744 // ToString is for dumping AIDL.
745 // Returns string representation of this method including everything.
746 // This is "ret_type name ( arg_list ) = id".
747 // e.g) "boolean foo(int, @Nullable String) = 1"
Jiyong Park309668e2018-07-28 16:55:44 +0900748 std::string ToString() const;
749
Jooyung Han965e31d2020-11-27 12:30:16 +0900750 // Signature is for comparing AIDL types.
751 // Returns string representation of this method's name & type.
752 // e.g) "foo(int, String)"
753 std::string Signature() const;
754
Casey Dahlin5c69deb2015-10-01 14:44:12 -0700755 private:
Casey Dahlinf4a93112015-10-05 16:58:09 -0700756 bool oneway_;
Casey Dahlinf2d23f72015-10-02 16:19:19 -0700757 std::string comments_;
Jiyong Parkd59a10d2018-07-18 11:12:55 +0900758 std::unique_ptr<AidlTypeSpecifier> type_;
Casey Dahlinf4a93112015-10-05 16:58:09 -0700759 std::string name_;
Christopher Wileyad339272015-10-05 19:11:58 -0700760 const std::vector<std::unique_ptr<AidlArgument>> arguments_;
761 std::vector<const AidlArgument*> in_arguments_;
762 std::vector<const AidlArgument*> out_arguments_;
Casey Dahlinf4a93112015-10-05 16:58:09 -0700763 bool has_id_;
764 int id_;
Jiyong Park309668e2018-07-28 16:55:44 +0900765 bool is_user_defined_ = true;
Casey Dahlin0a2f8be2015-09-28 16:15:29 -0700766};
Adam Lesinskiffa16862014-01-23 18:17:42 -0800767
Steven Morelandc258abc2018-07-10 14:03:38 -0700768class AidlDefinedType;
Jiyong Parkb034bf02018-07-30 17:44:33 +0900769class AidlInterface;
770class AidlParcelable;
771class AidlStructuredParcelable;
Jooyung Han2946afc2020-10-05 20:29:16 +0900772class AidlUnionDecl;
Jooyung Han3f347ca2020-12-01 12:41:50 +0900773
Daniel Norman85aed542019-08-21 12:01:14 -0700774// AidlDefinedType represents either an interface, parcelable, or enum that is
Jiyong Park1deecc32018-07-17 01:14:41 +0900775// defined in the source file.
776class AidlDefinedType : public AidlAnnotatable {
Steven Moreland787b0432018-07-03 09:00:58 -0700777 public:
Steven Moreland46e9da82018-07-27 15:45:29 -0700778 AidlDefinedType(const AidlLocation& location, const std::string& name,
Jooyung Han829ec7c2020-12-02 12:07:36 +0900779 const std::string& comments, const std::string& package,
780 std::vector<std::unique_ptr<AidlMember>>* members);
Steven Moreland787b0432018-07-03 09:00:58 -0700781 virtual ~AidlDefinedType() = default;
782
Jiyong Parkd800fef2020-07-22 18:09:43 +0900783 // non-copyable, non-movable
784 AidlDefinedType(const AidlDefinedType&) = delete;
785 AidlDefinedType(AidlDefinedType&&) = delete;
786 AidlDefinedType& operator=(const AidlDefinedType&) = delete;
787 AidlDefinedType& operator=(AidlDefinedType&&) = delete;
788
Jiyong Park1deecc32018-07-17 01:14:41 +0900789 const std::string& GetName() const { return name_; };
Jeongik Cha997281d2020-01-16 15:23:59 +0900790 bool IsHidden() const;
Jiyong Park1deecc32018-07-17 01:14:41 +0900791 const std::string& GetComments() const { return comments_; }
Jiyong Parka6605ab2018-11-11 14:30:21 +0900792 void SetComments(const std::string comments) { comments_ = comments; }
Jiyong Park1deecc32018-07-17 01:14:41 +0900793
Steven Moreland787b0432018-07-03 09:00:58 -0700794 /* dot joined package, example: "android.package.foo" */
Jiyong Park18132182020-06-08 20:24:40 +0900795 std::string GetPackage() const { return package_; }
Steven Moreland787b0432018-07-03 09:00:58 -0700796 /* dot joined package and name, example: "android.package.foo.IBar" */
797 std::string GetCanonicalName() const;
Jiyong Park18132182020-06-08 20:24:40 +0900798 const std::vector<std::string>& GetSplitPackage() const { return split_package_; }
Steven Moreland787b0432018-07-03 09:00:58 -0700799
Steven Morelanded83a282018-07-17 13:27:29 -0700800 virtual std::string GetPreprocessDeclarationName() const = 0;
Steven Morelandc258abc2018-07-10 14:03:38 -0700801
Steven Moreland5557f1c2018-07-02 13:50:23 -0700802 virtual const AidlStructuredParcelable* AsStructuredParcelable() const { return nullptr; }
Steven Morelandc258abc2018-07-10 14:03:38 -0700803 virtual const AidlParcelable* AsParcelable() const { return nullptr; }
Daniel Norman85aed542019-08-21 12:01:14 -0700804 virtual const AidlEnumDeclaration* AsEnumDeclaration() const { return nullptr; }
Jooyung Han2946afc2020-10-05 20:29:16 +0900805 virtual const AidlUnionDecl* AsUnionDeclaration() const { return nullptr; }
Steven Moreland5557f1c2018-07-02 13:50:23 -0700806 virtual const AidlInterface* AsInterface() const { return nullptr; }
Jeongik Chadf76dc72019-11-28 00:08:47 +0900807 virtual const AidlParameterizable<std::string>* AsParameterizable() const { return nullptr; }
Jooyung Han888c5bc2020-12-22 17:28:47 +0900808 virtual bool CheckValid(const AidlTypenames& typenames, DiagnosticsContext& context) const;
Steven Morelandd59e3172020-05-11 16:42:09 -0700809 virtual bool LanguageSpecificCheckValid(const AidlTypenames& typenames,
810 Options::Language lang) const = 0;
Steven Morelandc258abc2018-07-10 14:03:38 -0700811 AidlStructuredParcelable* AsStructuredParcelable() {
812 return const_cast<AidlStructuredParcelable*>(
813 const_cast<const AidlDefinedType*>(this)->AsStructuredParcelable());
814 }
815 AidlParcelable* AsParcelable() {
816 return const_cast<AidlParcelable*>(const_cast<const AidlDefinedType*>(this)->AsParcelable());
817 }
Daniel Norman85aed542019-08-21 12:01:14 -0700818 AidlEnumDeclaration* AsEnumDeclaration() {
819 return const_cast<AidlEnumDeclaration*>(
820 const_cast<const AidlDefinedType*>(this)->AsEnumDeclaration());
821 }
Jooyung Han2946afc2020-10-05 20:29:16 +0900822 AidlUnionDecl* AsUnionDeclaration() {
823 return const_cast<AidlUnionDecl*>(
824 const_cast<const AidlDefinedType*>(this)->AsUnionDeclaration());
825 }
Steven Morelandc258abc2018-07-10 14:03:38 -0700826 AidlInterface* AsInterface() {
827 return const_cast<AidlInterface*>(const_cast<const AidlDefinedType*>(this)->AsInterface());
828 }
829
Jeongik Chadf76dc72019-11-28 00:08:47 +0900830 AidlParameterizable<std::string>* AsParameterizable() {
831 return const_cast<AidlParameterizable<std::string>*>(
832 const_cast<const AidlDefinedType*>(this)->AsParameterizable());
833 }
834
Steven Moreland6cee3482018-07-18 14:39:58 -0700835 const AidlParcelable* AsUnstructuredParcelable() const {
836 if (this->AsStructuredParcelable() != nullptr) return nullptr;
Jooyung Han2946afc2020-10-05 20:29:16 +0900837 if (this->AsUnionDeclaration() != nullptr) return nullptr;
Steven Moreland6cee3482018-07-18 14:39:58 -0700838 return this->AsParcelable();
839 }
840 AidlParcelable* AsUnstructuredParcelable() {
841 return const_cast<AidlParcelable*>(
842 const_cast<const AidlDefinedType*>(this)->AsUnstructuredParcelable());
843 }
844
Jeongik Cha997281d2020-01-16 15:23:59 +0900845 virtual void Dump(CodeWriter* writer) const = 0;
Steven Morelanda5d9c5c2020-02-21 16:01:09 -0800846 void DumpHeader(CodeWriter* writer) const;
Jiyong Park02da7422018-07-16 16:00:26 +0900847
Jooyung Han829ec7c2020-12-02 12:07:36 +0900848 const std::vector<std::unique_ptr<AidlVariableDeclaration>>& GetFields() const {
849 return variables_;
850 }
851 const std::vector<std::unique_ptr<AidlConstantDeclaration>>& GetConstantDeclarations() const {
852 return constants_;
853 }
854 const std::vector<std::unique_ptr<AidlMethod>>& GetMethods() const { return methods_; }
855 void AddMethod(std::unique_ptr<AidlMethod> method) { methods_.push_back(std::move(method)); }
856 const std::vector<const AidlMember*>& GetMembers() const { return members_; }
857
858 protected:
859 // utility for subclasses with getter names
860 bool CheckValidForGetterNames() const;
861
Steven Moreland787b0432018-07-03 09:00:58 -0700862 private:
Jooyung Han829ec7c2020-12-02 12:07:36 +0900863 bool CheckValidWithMembers(const AidlTypenames& typenames) const;
864
Jiyong Park1deecc32018-07-17 01:14:41 +0900865 std::string name_;
Jiyong Park1deecc32018-07-17 01:14:41 +0900866 std::string comments_;
Jiyong Park18132182020-06-08 20:24:40 +0900867 const std::string package_;
868 const std::vector<std::string> split_package_;
Jooyung Han829ec7c2020-12-02 12:07:36 +0900869 std::vector<std::unique_ptr<AidlVariableDeclaration>> variables_;
870 std::vector<std::unique_ptr<AidlConstantDeclaration>> constants_;
871 std::vector<std::unique_ptr<AidlMethod>> methods_;
872 std::vector<const AidlMember*> members_; // keep members in order of appearance.
Steven Moreland787b0432018-07-03 09:00:58 -0700873};
874
Jeongik Chadf76dc72019-11-28 00:08:47 +0900875class AidlParcelable : public AidlDefinedType, public AidlParameterizable<std::string> {
Casey Dahlin1ae2bc52015-10-07 18:49:10 -0700876 public:
Jiyong Park18132182020-06-08 20:24:40 +0900877 AidlParcelable(const AidlLocation& location, const std::string& name, const std::string& package,
878 const std::string& comments, const std::string& cpp_header = "",
Jooyung Han829ec7c2020-12-02 12:07:36 +0900879 std::vector<std::string>* type_params = nullptr,
880 std::vector<std::unique_ptr<AidlMember>>* members = nullptr);
Casey Dahlin1ae2bc52015-10-07 18:49:10 -0700881 virtual ~AidlParcelable() = default;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800882
Jiyong Parkd800fef2020-07-22 18:09:43 +0900883 // non-copyable, non-movable
884 AidlParcelable(const AidlParcelable&) = delete;
885 AidlParcelable(AidlParcelable&&) = delete;
886 AidlParcelable& operator=(const AidlParcelable&) = delete;
887 AidlParcelable& operator=(AidlParcelable&&) = delete;
888
Christopher Wiley8aa4d9f2015-11-16 19:10:45 -0800889 std::string GetCppHeader() const { return cpp_header_; }
Christopher Wiley8aa4d9f2015-11-16 19:10:45 -0800890
Steven Moreland0cea4aa2020-04-20 21:06:02 -0700891 std::set<AidlAnnotation::Type> GetSupportedAnnotations() const override;
Jooyung Han888c5bc2020-12-22 17:28:47 +0900892 bool CheckValid(const AidlTypenames& typenames, DiagnosticsContext& context) const override;
Steven Morelandd59e3172020-05-11 16:42:09 -0700893 bool LanguageSpecificCheckValid(const AidlTypenames& typenames,
894 Options::Language lang) const override;
Steven Morelandc258abc2018-07-10 14:03:38 -0700895 const AidlParcelable* AsParcelable() const override { return this; }
Jeongik Chadf76dc72019-11-28 00:08:47 +0900896 const AidlParameterizable<std::string>* AsParameterizable() const override { return this; }
897 const AidlNode& AsAidlNode() const override { return *this; }
Steven Morelanded83a282018-07-17 13:27:29 -0700898 std::string GetPreprocessDeclarationName() const override { return "parcelable"; }
Steven Morelandc258abc2018-07-10 14:03:38 -0700899
Jeongik Cha997281d2020-01-16 15:23:59 +0900900 void Dump(CodeWriter* writer) const override;
Jiyong Park02da7422018-07-16 16:00:26 +0900901
Casey Dahlin1ae2bc52015-10-07 18:49:10 -0700902 private:
Christopher Wiley8aa4d9f2015-11-16 19:10:45 -0800903 std::string cpp_header_;
Casey Dahlin0a2f8be2015-09-28 16:15:29 -0700904};
Adam Lesinskiffa16862014-01-23 18:17:42 -0800905
Jooyung Han829ec7c2020-12-02 12:07:36 +0900906class AidlStructuredParcelable : public AidlParcelable {
Steven Moreland5557f1c2018-07-02 13:50:23 -0700907 public:
Jiyong Park18132182020-06-08 20:24:40 +0900908 AidlStructuredParcelable(const AidlLocation& location, const std::string& name,
909 const std::string& package, const std::string& comments,
Jooyung Han829ec7c2020-12-02 12:07:36 +0900910 std::vector<std::string>* type_params,
911 std::vector<std::unique_ptr<AidlMember>>* members);
Jiyong Parkd800fef2020-07-22 18:09:43 +0900912 virtual ~AidlStructuredParcelable() = default;
913
914 // non-copyable, non-movable
915 AidlStructuredParcelable(const AidlStructuredParcelable&) = delete;
916 AidlStructuredParcelable(AidlStructuredParcelable&&) = delete;
917 AidlStructuredParcelable& operator=(const AidlStructuredParcelable&) = delete;
918 AidlStructuredParcelable& operator=(AidlStructuredParcelable&&) = delete;
Steven Moreland5557f1c2018-07-02 13:50:23 -0700919
Steven Morelandc258abc2018-07-10 14:03:38 -0700920 const AidlStructuredParcelable* AsStructuredParcelable() const override { return this; }
Steven Morelanded83a282018-07-17 13:27:29 -0700921 std::string GetPreprocessDeclarationName() const override { return "structured_parcelable"; }
Steven Moreland5557f1c2018-07-02 13:50:23 -0700922
Jeongik Cha997281d2020-01-16 15:23:59 +0900923 void Dump(CodeWriter* writer) const override;
Jiyong Park02da7422018-07-16 16:00:26 +0900924
Steven Moreland0cea4aa2020-04-20 21:06:02 -0700925 std::set<AidlAnnotation::Type> GetSupportedAnnotations() const override;
Jooyung Han888c5bc2020-12-22 17:28:47 +0900926 bool CheckValid(const AidlTypenames& typenames, DiagnosticsContext& context) const override;
Steven Morelandd59e3172020-05-11 16:42:09 -0700927 bool LanguageSpecificCheckValid(const AidlTypenames& typenames,
928 Options::Language lang) const override;
Steven Moreland5557f1c2018-07-02 13:50:23 -0700929};
930
Daniel Norman85aed542019-08-21 12:01:14 -0700931class AidlEnumerator : public AidlNode {
932 public:
Daniel Norman2e4112d2019-10-03 10:22:35 -0700933 AidlEnumerator(const AidlLocation& location, const std::string& name, AidlConstantValue* value,
934 const std::string& comments);
Daniel Norman85aed542019-08-21 12:01:14 -0700935 virtual ~AidlEnumerator() = default;
936
Jiyong Parkd800fef2020-07-22 18:09:43 +0900937 // non-copyable, non-movable
938 AidlEnumerator(const AidlEnumerator&) = delete;
939 AidlEnumerator(AidlEnumerator&&) = delete;
940 AidlEnumerator& operator=(const AidlEnumerator&) = delete;
941 AidlEnumerator& operator=(AidlEnumerator&&) = delete;
942
Daniel Norman85aed542019-08-21 12:01:14 -0700943 const std::string& GetName() const { return name_; }
Will McVickerd7d18df2019-09-12 13:40:50 -0700944 AidlConstantValue* GetValue() const { return value_.get(); }
Daniel Norman2e4112d2019-10-03 10:22:35 -0700945 const std::string& GetComments() const { return comments_; }
Daniel Norman85aed542019-08-21 12:01:14 -0700946 bool CheckValid(const AidlTypeSpecifier& enum_backing_type) const;
947
948 string ValueString(const AidlTypeSpecifier& backing_type,
949 const ConstantValueDecorator& decorator) const;
950
Daniel Normanb28684e2019-10-17 15:31:39 -0700951 void SetValue(std::unique_ptr<AidlConstantValue> value) { value_ = std::move(value); }
Jooyung Han29813842020-12-08 01:28:03 +0900952 bool IsValueUserSpecified() const { return value_user_specified_; }
Daniel Normanb28684e2019-10-17 15:31:39 -0700953
Daniel Norman85aed542019-08-21 12:01:14 -0700954 private:
955 const std::string name_;
Will McVickerd7d18df2019-09-12 13:40:50 -0700956 unique_ptr<AidlConstantValue> value_;
Daniel Norman2e4112d2019-10-03 10:22:35 -0700957 const std::string comments_;
Jooyung Han29813842020-12-08 01:28:03 +0900958 const bool value_user_specified_;
Daniel Norman85aed542019-08-21 12:01:14 -0700959};
960
961class AidlEnumDeclaration : public AidlDefinedType {
962 public:
Will McVickerd7d18df2019-09-12 13:40:50 -0700963 AidlEnumDeclaration(const AidlLocation& location, const string& name,
Daniel Norman85aed542019-08-21 12:01:14 -0700964 std::vector<std::unique_ptr<AidlEnumerator>>* enumerators,
Jiyong Park18132182020-06-08 20:24:40 +0900965 const std::string& package, const std::string& comments);
Daniel Norman85aed542019-08-21 12:01:14 -0700966 virtual ~AidlEnumDeclaration() = default;
967
Jiyong Parkd800fef2020-07-22 18:09:43 +0900968 // non-copyable, non-movable
969 AidlEnumDeclaration(const AidlEnumDeclaration&) = delete;
970 AidlEnumDeclaration(AidlEnumDeclaration&&) = delete;
971 AidlEnumDeclaration& operator=(const AidlEnumDeclaration&) = delete;
972 AidlEnumDeclaration& operator=(AidlEnumDeclaration&&) = delete;
973
Daniel Norman85aed542019-08-21 12:01:14 -0700974 void SetBackingType(std::unique_ptr<const AidlTypeSpecifier> type);
975 const AidlTypeSpecifier& GetBackingType() const { return *backing_type_; }
976 const std::vector<std::unique_ptr<AidlEnumerator>>& GetEnumerators() const {
977 return enumerators_;
978 }
Steven Moreland0cea4aa2020-04-20 21:06:02 -0700979 std::set<AidlAnnotation::Type> GetSupportedAnnotations() const override;
Jooyung Han888c5bc2020-12-22 17:28:47 +0900980 bool CheckValid(const AidlTypenames& typenames, DiagnosticsContext& context) const override;
Steven Morelandd59e3172020-05-11 16:42:09 -0700981 bool LanguageSpecificCheckValid(const AidlTypenames& /*typenames*/,
982 Options::Language) const override {
983 return true;
984 }
Daniel Norman85aed542019-08-21 12:01:14 -0700985 std::string GetPreprocessDeclarationName() const override { return "enum"; }
Jeongik Cha997281d2020-01-16 15:23:59 +0900986 void Dump(CodeWriter* writer) const override;
Daniel Norman85aed542019-08-21 12:01:14 -0700987
988 const AidlEnumDeclaration* AsEnumDeclaration() const override { return this; }
989
990 private:
Jooyung Han29813842020-12-08 01:28:03 +0900991 void Autofill();
992
Daniel Norman85aed542019-08-21 12:01:14 -0700993 const std::string name_;
994 const std::vector<std::unique_ptr<AidlEnumerator>> enumerators_;
995 std::unique_ptr<const AidlTypeSpecifier> backing_type_;
Daniel Norman85aed542019-08-21 12:01:14 -0700996};
997
Jooyung Han829ec7c2020-12-02 12:07:36 +0900998class AidlUnionDecl : public AidlParcelable {
Jooyung Han2946afc2020-10-05 20:29:16 +0900999 public:
1000 AidlUnionDecl(const AidlLocation& location, const std::string& name, const std::string& package,
Jooyung Han829ec7c2020-12-02 12:07:36 +09001001 const std::string& comments, std::vector<std::string>* type_params,
1002 std::vector<std::unique_ptr<AidlMember>>* members);
Jooyung Han2946afc2020-10-05 20:29:16 +09001003 virtual ~AidlUnionDecl() = default;
1004
1005 // non-copyable, non-movable
1006 AidlUnionDecl(const AidlUnionDecl&) = delete;
1007 AidlUnionDecl(AidlUnionDecl&&) = delete;
1008 AidlUnionDecl& operator=(const AidlUnionDecl&) = delete;
1009 AidlUnionDecl& operator=(AidlUnionDecl&&) = delete;
1010
1011 std::set<AidlAnnotation::Type> GetSupportedAnnotations() const override;
1012
1013 const AidlNode& AsAidlNode() const override { return *this; }
1014
Jooyung Han888c5bc2020-12-22 17:28:47 +09001015 bool CheckValid(const AidlTypenames& typenames, DiagnosticsContext& context) const override;
Jooyung Hanfe89f122020-10-14 03:49:18 +09001016 bool LanguageSpecificCheckValid(const AidlTypenames& typenames,
1017 Options::Language lang) const override;
Jooyung Han2946afc2020-10-05 20:29:16 +09001018 std::string GetPreprocessDeclarationName() const override { return "union"; }
1019
1020 void Dump(CodeWriter* writer) const override;
1021 const AidlUnionDecl* AsUnionDeclaration() const override { return this; }
Jooyung Han2946afc2020-10-05 20:29:16 +09001022};
1023
Jiyong Park1deecc32018-07-17 01:14:41 +09001024class AidlInterface final : public AidlDefinedType {
Casey Dahlin1ae2bc52015-10-07 18:49:10 -07001025 public:
Steven Moreland46e9da82018-07-27 15:45:29 -07001026 AidlInterface(const AidlLocation& location, const std::string& name, const std::string& comments,
Jooyung Han829ec7c2020-12-02 12:07:36 +09001027 bool oneway_, const std::string& package,
1028 std::vector<std::unique_ptr<AidlMember>>* members);
Casey Dahlin1ae2bc52015-10-07 18:49:10 -07001029 virtual ~AidlInterface() = default;
1030
Jiyong Parkd800fef2020-07-22 18:09:43 +09001031 // non-copyable, non-movable
1032 AidlInterface(const AidlInterface&) = delete;
1033 AidlInterface(AidlInterface&&) = delete;
1034 AidlInterface& operator=(const AidlInterface&) = delete;
1035 AidlInterface& operator=(AidlInterface&&) = delete;
1036
Steven Morelandc258abc2018-07-10 14:03:38 -07001037 const AidlInterface* AsInterface() const override { return this; }
Steven Morelanded83a282018-07-17 13:27:29 -07001038 std::string GetPreprocessDeclarationName() const override { return "interface"; }
Steven Moreland5557f1c2018-07-02 13:50:23 -07001039
Jeongik Cha997281d2020-01-16 15:23:59 +09001040 void Dump(CodeWriter* writer) const override;
Jiyong Park02da7422018-07-16 16:00:26 +09001041
Steven Moreland0cea4aa2020-04-20 21:06:02 -07001042 std::set<AidlAnnotation::Type> GetSupportedAnnotations() const override;
Jooyung Han888c5bc2020-12-22 17:28:47 +09001043 bool CheckValid(const AidlTypenames& typenames, DiagnosticsContext& context) const override;
Steven Morelandd59e3172020-05-11 16:42:09 -07001044 bool LanguageSpecificCheckValid(const AidlTypenames& typenames,
1045 Options::Language lang) const override;
Jeongik Chadb0f59e2018-11-01 18:11:21 +09001046
Jiyong Park27fd7fd2020-08-27 16:25:09 +09001047 std::string GetDescriptor() const;
Casey Dahlin0a2f8be2015-09-28 16:15:29 -07001048};
Adam Lesinskiffa16862014-01-23 18:17:42 -08001049
Casey Dahlin0edf3422015-10-07 12:34:59 -07001050class AidlImport : public AidlNode {
1051 public:
Steven Moreland46e9da82018-07-27 15:45:29 -07001052 AidlImport(const AidlLocation& location, const std::string& needed_class);
Casey Dahlin0edf3422015-10-07 12:34:59 -07001053 virtual ~AidlImport() = default;
1054
Jiyong Parkd800fef2020-07-22 18:09:43 +09001055 // non-copyable, non-movable
1056 AidlImport(const AidlImport&) = delete;
1057 AidlImport(AidlImport&&) = delete;
1058 AidlImport& operator=(const AidlImport&) = delete;
1059 AidlImport& operator=(AidlImport&&) = delete;
1060
Casey Dahlin0edf3422015-10-07 12:34:59 -07001061 const std::string& GetNeededClass() const { return needed_class_; }
Casey Dahlin0edf3422015-10-07 12:34:59 -07001062
1063 private:
Casey Dahlin0edf3422015-10-07 12:34:59 -07001064 std::string needed_class_;
Casey Dahline2507492015-09-14 17:11:20 -07001065};
1066
Jiyong Park62515512020-06-08 15:57:11 +09001067// AidlDocument models an AIDL file
1068class AidlDocument : public AidlNode {
1069 public:
1070 AidlDocument(const AidlLocation& location, std::vector<std::unique_ptr<AidlImport>>& imports,
Jiyong Park8e79b7f2020-07-20 20:52:38 +09001071 std::vector<std::unique_ptr<AidlDefinedType>>&& defined_types)
1072 : AidlNode(location),
1073 imports_(std::move(imports)),
1074 defined_types_(std::move(defined_types)) {}
Jiyong Parkd800fef2020-07-22 18:09:43 +09001075 ~AidlDocument() = default;
1076
1077 // non-copyable, non-movable
Jiyong Park8e79b7f2020-07-20 20:52:38 +09001078 AidlDocument(const AidlDocument&) = delete;
1079 AidlDocument(AidlDocument&&) = delete;
1080 AidlDocument& operator=(const AidlDocument&) = delete;
1081 AidlDocument& operator=(AidlDocument&&) = delete;
Jiyong Parkd800fef2020-07-22 18:09:43 +09001082
Jooyung Han29813842020-12-08 01:28:03 +09001083 std::optional<std::string> ResolveName(const std::string& unresolved_type) const;
Jiyong Parkd800fef2020-07-22 18:09:43 +09001084 const std::vector<std::unique_ptr<AidlImport>>& Imports() const { return imports_; }
1085 const std::vector<std::unique_ptr<AidlDefinedType>>& DefinedTypes() const {
1086 return defined_types_;
1087 }
Jiyong Park62515512020-06-08 15:57:11 +09001088
1089 private:
1090 const std::vector<std::unique_ptr<AidlImport>> imports_;
Jiyong Park8e79b7f2020-07-20 20:52:38 +09001091 const std::vector<std::unique_ptr<AidlDefinedType>> defined_types_;
Jiyong Park62515512020-06-08 15:57:11 +09001092};