blob: c4a61fcfe10b7cf847e857904f18cab26d12237b [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;
Jooyung Han5721a232020-12-24 04:34:55 +0900211
212 std::vector<std::string> required_parameters = {};
Steven Moreland0cea4aa2020-04-20 21:06:02 -0700213 };
214 static const std::vector<Schema>& AllSchemas();
215
216 AidlAnnotation(const AidlLocation& location, const Schema& schema,
Andrei Onea9445fc62019-06-27 18:11:59 +0100217 std::map<std::string, std::shared_ptr<AidlConstantValue>>&& parameters);
Steven Moreland0cea4aa2020-04-20 21:06:02 -0700218
219 const Schema& schema_;
Jiyong Parka6605ab2018-11-11 14:30:21 +0900220 string comments_;
Andrei Onea9445fc62019-06-27 18:11:59 +0100221 std::map<std::string, std::shared_ptr<AidlConstantValue>> parameters_;
Jiyong Park68bc77a2018-07-19 19:00:45 +0900222};
223
Steven Moreland3be75772018-08-20 13:27:43 -0700224static inline bool operator<(const AidlAnnotation& lhs, const AidlAnnotation& rhs) {
225 return lhs.GetName() < rhs.GetName();
226}
227static inline bool operator==(const AidlAnnotation& lhs, const AidlAnnotation& rhs) {
228 return lhs.GetName() == rhs.GetName();
229}
Jiyong Park3656c3c2018-08-01 20:02:01 +0900230
Casey Dahline7922932016-02-29 17:23:01 -0800231class AidlAnnotatable : public AidlNode {
Casey Dahlin0ee37582015-09-30 16:31:55 -0700232 public:
Steven Moreland46e9da82018-07-27 15:45:29 -0700233 AidlAnnotatable(const AidlLocation& location);
Steven Moreland3f658cf2018-08-20 13:40:54 -0700234
235 AidlAnnotatable(const AidlAnnotatable&) = default;
236 AidlAnnotatable(AidlAnnotatable&&) = default;
Casey Dahline7922932016-02-29 17:23:01 -0800237 virtual ~AidlAnnotatable() = default;
238
Artur Satayev91fe8712019-07-29 13:06:01 +0100239 void Annotate(vector<AidlAnnotation>&& annotations) {
240 for (auto& annotation : annotations) {
241 annotations_.emplace_back(std::move(annotation));
242 }
243 }
Jiyong Park68bc77a2018-07-19 19:00:45 +0900244 bool IsNullable() const;
Jiyong Park68bc77a2018-07-19 19:00:45 +0900245 bool IsUtf8InCpp() const;
Steven Morelanda7764e52020-10-27 17:29:29 +0000246 bool IsSensitiveData() const;
Steven Morelanda57d0a62019-07-30 09:41:14 -0700247 bool IsVintfStability() const;
Jeongik Chad0a10272020-08-06 16:33:36 +0900248 bool IsJavaOnlyImmutable() const;
Devin Moorec7e47a32020-08-07 10:55:25 -0700249 bool IsFixedSize() const;
Jeongik Cha88f95a82020-01-15 13:02:16 +0900250 bool IsStableApiParcelable(Options::Language lang) const;
Makoto Onuki78a1c1c2020-03-04 16:57:23 -0800251 bool IsHide() const;
Jooyung Han829ec7c2020-12-02 12:07:36 +0900252 bool JavaDerive(const std::string& method) const;
Jiyong Park27fd7fd2020-08-27 16:25:09 +0900253 std::string GetDescriptor() const;
Andrei Onea9445fc62019-06-27 18:11:59 +0100254
Steven Moreland7e4b9502020-02-20 18:10:42 -0800255 void DumpAnnotations(CodeWriter* writer) const;
256
Andrei Onea9445fc62019-06-27 18:11:59 +0100257 const AidlAnnotation* UnsupportedAppUsage() const;
Andrei Homescue61feb52020-08-18 15:44:24 -0700258 const AidlAnnotation* RustDerive() const;
Jooyung Han672557b2020-12-24 05:18:00 +0900259 const AidlAnnotation* BackingType() const;
Jooyung Han965e31d2020-11-27 12:30:16 +0900260
261 // ToString is for dumping AIDL.
262 // Returns string representation of annotations.
263 // e.g) "@JavaDerive(toString=true) @RustDerive(Clone=true, Copy=true)"
Jiyong Park68bc77a2018-07-19 19:00:45 +0900264 std::string ToString() const;
Casey Dahline7922932016-02-29 17:23:01 -0800265
Jiyong Parka6605ab2018-11-11 14:30:21 +0900266 const vector<AidlAnnotation>& GetAnnotations() const { return annotations_; }
Jooyung Han888c5bc2020-12-22 17:28:47 +0900267 bool CheckValid(const AidlTypenames&) const;
Jiyong Park3656c3c2018-08-01 20:02:01 +0900268
Steven Moreland181144c2020-04-20 19:57:56 -0700269 protected:
Steven Moreland0cea4aa2020-04-20 21:06:02 -0700270 virtual std::set<AidlAnnotation::Type> GetSupportedAnnotations() const = 0;
Steven Moreland181144c2020-04-20 19:57:56 -0700271
Casey Dahline7922932016-02-29 17:23:01 -0800272 private:
Jiyong Parka6605ab2018-11-11 14:30:21 +0900273 vector<AidlAnnotation> annotations_;
Casey Dahline7922932016-02-29 17:23:01 -0800274};
275
Jiyong Park1deecc32018-07-17 01:14:41 +0900276// AidlTypeSpecifier represents a reference to either a built-in type,
277// a defined type, or a variant (e.g., array of generic) of a type.
Jeongik Chadf76dc72019-11-28 00:08:47 +0900278class AidlTypeSpecifier final : public AidlAnnotatable,
279 public AidlParameterizable<unique_ptr<AidlTypeSpecifier>> {
Casey Dahline7922932016-02-29 17:23:01 -0800280 public:
Steven Moreland46e9da82018-07-27 15:45:29 -0700281 AidlTypeSpecifier(const AidlLocation& location, const string& unresolved_name, bool is_array,
282 vector<unique_ptr<AidlTypeSpecifier>>* type_params, const string& comments);
Jiyong Parkd59a10d2018-07-18 11:12:55 +0900283 virtual ~AidlTypeSpecifier() = default;
Casey Dahlin0ee37582015-09-30 16:31:55 -0700284
Steven Moreland3f658cf2018-08-20 13:40:54 -0700285 // Copy of this type which is not an array.
Jooyung Hand2fa0232020-10-19 02:51:41 +0900286 const AidlTypeSpecifier& ArrayBase() const;
Steven Moreland3f658cf2018-08-20 13:40:54 -0700287
Jiyong Park1deecc32018-07-17 01:14:41 +0900288 // Returns the full-qualified name of the base type.
289 // int -> int
290 // int[] -> int
291 // List<String> -> List
292 // IFoo -> foo.bar.IFoo (if IFoo is in package foo.bar)
293 const string& GetName() const {
294 if (IsResolved()) {
295 return fully_qualified_name_;
296 } else {
297 return GetUnresolvedName();
298 }
299 }
Casey Dahlin0ee37582015-09-30 16:31:55 -0700300
Jooyung Han965e31d2020-11-27 12:30:16 +0900301 // ToString is for dumping AIDL.
302 // Returns string representation of this type specifier including annotations.
303 // This is "annotations type_name type_params? array_marker?".
304 // e.g) "@utf8InCpp String[]";
305 std::string ToString() const;
Jiyong Park1deecc32018-07-17 01:14:41 +0900306
Jooyung Han965e31d2020-11-27 12:30:16 +0900307 // Signature is for comparing AIDL types.
308 // Returns string representation of this type specifier.
309 // This is "type_name type_params? array_marker?".
310 // e.g.) "String[]" (even if it is annotated with @utf8InCpp)
Jiyong Park02da7422018-07-16 16:00:26 +0900311 std::string Signature() const;
312
Jiyong Park1deecc32018-07-17 01:14:41 +0900313 const string& GetUnresolvedName() const { return unresolved_name_; }
314
Jeongik Cha997281d2020-01-16 15:23:59 +0900315 bool IsHidden() const;
316
Jiyong Park1deecc32018-07-17 01:14:41 +0900317 const string& GetComments() const { return comments_; }
318
Jeongik Cha1a7ab642019-07-29 17:31:02 +0900319 const std::vector<std::string> GetSplitName() const { return split_name_; }
320
Jiyong Parka6605ab2018-11-11 14:30:21 +0900321 void SetComments(const string& comment) { comments_ = comment; }
322
Jiyong Park1deecc32018-07-17 01:14:41 +0900323 bool IsResolved() const { return fully_qualified_name_ != ""; }
324
325 bool IsArray() const { return is_array_; }
326
Steven Moreland6c07b832020-10-29 23:39:53 +0000327 __attribute__((warn_unused_result)) bool SetArray() {
328 if (is_array_) return false;
329 is_array_ = true;
330 return true;
331 }
332
Jiyong Park1deecc32018-07-17 01:14:41 +0900333 // Resolve the base type name to a fully-qualified name. Return false if the
334 // resolution fails.
Daniel Norman716d3112019-09-10 13:11:56 -0700335 bool Resolve(const AidlTypenames& typenames);
Casey Dahlin0ee37582015-09-30 16:31:55 -0700336
Steven Moreland0cea4aa2020-04-20 21:06:02 -0700337 std::set<AidlAnnotation::Type> GetSupportedAnnotations() const override;
Jooyung Han888c5bc2020-12-22 17:28:47 +0900338 bool CheckValid(const AidlTypenames& typenames) const;
Steven Morelandd59e3172020-05-11 16:42:09 -0700339 bool LanguageSpecificCheckValid(const AidlTypenames& typenames, Options::Language lang) const;
Jeongik Chadf76dc72019-11-28 00:08:47 +0900340 const AidlNode& AsAidlNode() const override { return *this; }
Jiyong Park1d2df7d2018-07-23 15:22:50 +0900341
Jooyung Hane9bb9de2020-11-01 22:16:57 +0900342 const AidlDefinedType* GetDefinedType() const;
343
Casey Dahlin0ee37582015-09-30 16:31:55 -0700344 private:
Steven Moreland3f658cf2018-08-20 13:40:54 -0700345 AidlTypeSpecifier(const AidlTypeSpecifier&) = default;
346
Jiyong Park1deecc32018-07-17 01:14:41 +0900347 const string unresolved_name_;
348 string fully_qualified_name_;
Steven Moreland3f658cf2018-08-20 13:40:54 -0700349 bool is_array_;
Jiyong Parka6605ab2018-11-11 14:30:21 +0900350 string comments_;
Jeongik Cha1a7ab642019-07-29 17:31:02 +0900351 vector<string> split_name_;
Jooyung Han690f5842020-12-04 13:02:04 +0900352 const AidlDefinedType* defined_type_ = nullptr; // set when Resolve() for defined types
Jooyung Hand2fa0232020-10-19 02:51:41 +0900353 mutable shared_ptr<AidlTypeSpecifier> array_base_;
Casey Dahlin0ee37582015-09-30 16:31:55 -0700354};
355
Steven Moreland860b1942018-08-16 14:59:28 -0700356// Returns the universal value unaltered.
357std::string AidlConstantValueDecorator(const AidlTypeSpecifier& type, const std::string& raw_value);
358
Steven Moreland9ea10e32018-07-19 15:26:09 -0700359class AidlConstantValue;
Jooyung Han3f347ca2020-12-01 12:41:50 +0900360class AidlMethod;
361class AidlConstantDeclaration;
362class AidlVariableDeclaration;
363
364class AidlMember : public AidlNode {
365 public:
366 AidlMember(const AidlLocation& location);
367 virtual ~AidlMember() = default;
368
369 // non-copyable, non-movable
370 AidlMember(const AidlMember&) = delete;
371 AidlMember(AidlMember&&) = delete;
372 AidlMember& operator=(const AidlMember&) = delete;
373 AidlMember& operator=(AidlMember&&) = delete;
374
Jooyung Han829ec7c2020-12-02 12:07:36 +0900375 virtual const AidlMethod* AsMethod() const { return nullptr; }
376 virtual const AidlConstantDeclaration* AsConstantDeclaration() const { return nullptr; }
377 virtual const AidlVariableDeclaration* AsVariableDeclaration() const { return nullptr; }
378
379 AidlMethod* AsMethod() {
380 return const_cast<AidlMethod*>(const_cast<const AidlMember*>(this)->AsMethod());
381 }
382 AidlConstantDeclaration* AsConstantDeclaration() {
383 return const_cast<AidlConstantDeclaration*>(
384 const_cast<const AidlMember*>(this)->AsConstantDeclaration());
385 }
386 AidlVariableDeclaration* AsVariableDeclaration() {
387 return const_cast<AidlVariableDeclaration*>(
388 const_cast<const AidlMember*>(this)->AsVariableDeclaration());
389 }
Jooyung Han3f347ca2020-12-01 12:41:50 +0900390};
391
Steven Moreland541788d2020-05-21 22:05:52 +0000392// TODO: This class is used for method arguments and also parcelable fields,
393// and it should be split up since default values don't apply to method
394// arguments
Jooyung Han3f347ca2020-12-01 12:41:50 +0900395class AidlVariableDeclaration : public AidlMember {
Steven Moreland5557f1c2018-07-02 13:50:23 -0700396 public:
Steven Moreland46e9da82018-07-27 15:45:29 -0700397 AidlVariableDeclaration(const AidlLocation& location, AidlTypeSpecifier* type,
398 const std::string& name);
399 AidlVariableDeclaration(const AidlLocation& location, AidlTypeSpecifier* type,
400 const std::string& name, AidlConstantValue* default_value);
Steven Moreland5557f1c2018-07-02 13:50:23 -0700401 virtual ~AidlVariableDeclaration() = default;
402
Jiyong Parkd800fef2020-07-22 18:09:43 +0900403 // non-copyable, non-movable
404 AidlVariableDeclaration(const AidlVariableDeclaration&) = delete;
405 AidlVariableDeclaration(AidlVariableDeclaration&&) = delete;
406 AidlVariableDeclaration& operator=(const AidlVariableDeclaration&) = delete;
407 AidlVariableDeclaration& operator=(AidlVariableDeclaration&&) = delete;
408
Jooyung Han829ec7c2020-12-02 12:07:36 +0900409 const AidlVariableDeclaration* AsVariableDeclaration() const override { return this; }
Jooyung Han3f347ca2020-12-01 12:41:50 +0900410
Steven Moreland5557f1c2018-07-02 13:50:23 -0700411 std::string GetName() const { return name_; }
Jooyung Hanacae85d2020-10-28 16:39:09 +0900412 std::string GetCapitalizedName() const;
Jiyong Parkd59a10d2018-07-18 11:12:55 +0900413 const AidlTypeSpecifier& GetType() const { return *type_; }
Steven Moreland541788d2020-05-21 22:05:52 +0000414 // if this was constructed explicitly with a default value
415 bool IsDefaultUserSpecified() const { return default_user_specified_; }
416 // will return the default value this is constructed with or a default value
417 // if one is available
Steven Moreland9ea10e32018-07-19 15:26:09 -0700418 const AidlConstantValue* GetDefaultValue() const { return default_value_.get(); }
Jooyung Han53fb4242020-12-17 16:03:49 +0900419 bool HasUsefulDefaultValue() const;
Steven Moreland9ea10e32018-07-19 15:26:09 -0700420
Jiyong Parkd59a10d2018-07-18 11:12:55 +0900421 AidlTypeSpecifier* GetMutableType() { return type_.get(); }
Steven Moreland5557f1c2018-07-02 13:50:23 -0700422
Jeongik Chadb0f59e2018-11-01 18:11:21 +0900423 bool CheckValid(const AidlTypenames& typenames) const;
Jooyung Han965e31d2020-11-27 12:30:16 +0900424
425 // ToString is for dumping AIDL.
426 // Returns string representation of this variable decl including default value.
427 // This is "annotations type name default_value?".
428 // e.g) "@utf8InCpp String[] names = {"hello"}"
Steven Moreland5557f1c2018-07-02 13:50:23 -0700429 std::string ToString() const;
Jooyung Han965e31d2020-11-27 12:30:16 +0900430
431 // Signature is for comparing AIDL types.
432 // Returns string representation of this variable decl.
433 // This is "type name".
434 // e.g) "String[] name" (even if it is annotated with @utf8InCpp and has a default value.)
Jiyong Park02da7422018-07-16 16:00:26 +0900435 std::string Signature() const;
Steven Moreland5557f1c2018-07-02 13:50:23 -0700436
Steven Moreland860b1942018-08-16 14:59:28 -0700437 std::string ValueString(const ConstantValueDecorator& decorator) const;
Steven Moreland25294322018-08-07 18:13:55 -0700438
Steven Moreland5557f1c2018-07-02 13:50:23 -0700439 private:
Jiyong Parkd59a10d2018-07-18 11:12:55 +0900440 std::unique_ptr<AidlTypeSpecifier> type_;
Steven Moreland5557f1c2018-07-02 13:50:23 -0700441 std::string name_;
Steven Moreland541788d2020-05-21 22:05:52 +0000442 bool default_user_specified_;
Steven Moreland9ea10e32018-07-19 15:26:09 -0700443 std::unique_ptr<AidlConstantValue> default_value_;
Steven Moreland5557f1c2018-07-02 13:50:23 -0700444};
445
446class AidlArgument : public AidlVariableDeclaration {
Casey Dahlinbc7a50a2015-09-28 19:20:50 -0700447 public:
Casey Dahlinc378c992015-09-29 16:50:40 -0700448 enum Direction { IN_DIR = 1, OUT_DIR = 2, INOUT_DIR = 3 };
449
Steven Moreland46e9da82018-07-27 15:45:29 -0700450 AidlArgument(const AidlLocation& location, AidlArgument::Direction direction,
451 AidlTypeSpecifier* type, const std::string& name);
452 AidlArgument(const AidlLocation& location, AidlTypeSpecifier* type, const std::string& name);
Casey Dahlinbc7a50a2015-09-28 19:20:50 -0700453 virtual ~AidlArgument() = default;
454
Jiyong Parkd800fef2020-07-22 18:09:43 +0900455 // non-copyable, non-movable
456 AidlArgument(const AidlArgument&) = delete;
457 AidlArgument(AidlArgument&&) = delete;
458 AidlArgument& operator=(const AidlArgument&) = delete;
459 AidlArgument& operator=(AidlArgument&&) = delete;
460
Casey Dahlinc378c992015-09-29 16:50:40 -0700461 Direction GetDirection() const { return direction_; }
Christopher Wileyad339272015-10-05 19:11:58 -0700462 bool IsOut() const { return direction_ & OUT_DIR; }
463 bool IsIn() const { return direction_ & IN_DIR; }
Casey Dahlinc378c992015-09-29 16:50:40 -0700464 bool DirectionWasSpecified() const { return direction_specified_; }
Jiyong Park3656c3c2018-08-01 20:02:01 +0900465 string GetDirectionSpecifier() const;
Christopher Wileyad339272015-10-05 19:11:58 -0700466
Jooyung Han965e31d2020-11-27 12:30:16 +0900467 // ToString is for dumping AIDL.
468 // Returns string representation of this argument including direction
469 // This is "direction annotations type name".
470 // e.g) "in @utf8InCpp String[] names"
Casey Dahlinc378c992015-09-29 16:50:40 -0700471 std::string ToString() const;
472
Casey Dahlinbc7a50a2015-09-28 19:20:50 -0700473 private:
Casey Dahlinc378c992015-09-29 16:50:40 -0700474 Direction direction_;
475 bool direction_specified_;
Casey Dahlina834dd42015-09-23 11:52:15 -0700476};
Adam Lesinskiffa16862014-01-23 18:17:42 -0800477
Will McVickerd7d18df2019-09-12 13:40:50 -0700478class AidlUnaryConstExpression;
479class AidlBinaryConstExpression;
Jooyung Han690f5842020-12-04 13:02:04 +0900480class AidlConstantReference;
Will McVickerd7d18df2019-09-12 13:40:50 -0700481
Steven Moreland693640b2018-07-19 13:46:27 -0700482class AidlConstantValue : public AidlNode {
Casey Dahlind40e2fe2015-11-24 14:06:52 -0800483 public:
Will McVickerd7d18df2019-09-12 13:40:50 -0700484 enum class Type {
485 // WARNING: Don't change this order! The order is used to determine type
486 // promotion during a binary expression.
487 BOOLEAN,
488 INT8,
489 INT32,
490 INT64,
491 ARRAY,
492 CHARACTER,
493 STRING,
Jooyung Han690f5842020-12-04 13:02:04 +0900494 REF,
Will McVickerd7d18df2019-09-12 13:40:50 -0700495 FLOATING,
496 UNARY,
497 BINARY,
498 ERROR,
499 };
500
Jooyung Han690f5842020-12-04 13:02:04 +0900501 struct Visitor {
502 virtual ~Visitor() {}
503 virtual void Visit(AidlConstantValue&) = 0;
504 virtual void Visit(AidlConstantReference&) = 0;
505 virtual void Visit(AidlUnaryConstExpression&) = 0;
506 virtual void Visit(AidlBinaryConstExpression&) = 0;
507 };
508
Will McVickerd7d18df2019-09-12 13:40:50 -0700509 /*
510 * Return the value casted to the given type.
511 */
512 template <typename T>
513 T cast() const;
Casey Dahlind40e2fe2015-11-24 14:06:52 -0800514
Steven Moreland693640b2018-07-19 13:46:27 -0700515 virtual ~AidlConstantValue() = default;
Casey Dahlind40e2fe2015-11-24 14:06:52 -0800516
Jiyong Parkd800fef2020-07-22 18:09:43 +0900517 // non-copyable, non-movable
518 AidlConstantValue(const AidlConstantValue&) = delete;
519 AidlConstantValue(AidlConstantValue&&) = delete;
520 AidlConstantValue& operator=(const AidlConstantValue&) = delete;
521 AidlConstantValue& operator=(AidlConstantValue&&) = delete;
522
Steven Moreland541788d2020-05-21 22:05:52 +0000523 // creates default value, when one isn't specified
524 // nullptr if no default available
525 static AidlConstantValue* Default(const AidlTypeSpecifier& specifier);
526
Steven Moreland25294322018-08-07 18:13:55 -0700527 static AidlConstantValue* Boolean(const AidlLocation& location, bool value);
528 static AidlConstantValue* Character(const AidlLocation& location, char value);
Steven Moreland25294322018-08-07 18:13:55 -0700529 // example: 123, -5498, maybe any size
Will McVickerd7d18df2019-09-12 13:40:50 -0700530 static AidlConstantValue* Integral(const AidlLocation& location, const string& value);
531 static AidlConstantValue* Floating(const AidlLocation& location, const std::string& value);
Steven Moreland860b1942018-08-16 14:59:28 -0700532 static AidlConstantValue* Array(const AidlLocation& location,
Will McVickerd7d18df2019-09-12 13:40:50 -0700533 std::unique_ptr<vector<unique_ptr<AidlConstantValue>>> values);
Steven Moreland693640b2018-07-19 13:46:27 -0700534 // example: "\"asdf\""
Will McVickerd7d18df2019-09-12 13:40:50 -0700535 static AidlConstantValue* String(const AidlLocation& location, const string& value);
Steven Moreland693640b2018-07-19 13:46:27 -0700536
Will McVickerd7d18df2019-09-12 13:40:50 -0700537 Type GetType() const { return final_type_; }
Jooyung Han29813842020-12-08 01:28:03 +0900538 const std::string& Literal() const { return value_; }
Steven Moreland25294322018-08-07 18:13:55 -0700539
Will McVickerd7d18df2019-09-12 13:40:50 -0700540 virtual bool CheckValid() const;
Steven Moreland860b1942018-08-16 14:59:28 -0700541
542 // Raw value of type (currently valid in C++ and Java). Empty string on error.
Steven Moreland4bcb05c2019-11-27 18:57:47 -0800543 string ValueString(const AidlTypeSpecifier& type, const ConstantValueDecorator& decorator) const;
Jooyung Han29813842020-12-08 01:28:03 +0900544 virtual void Accept(Visitor& visitor) {
545 visitor.Visit(*this);
546 if (type_ == Type::ARRAY) {
547 for (const auto& v : values_) {
548 v.get()->Accept(visitor);
549 }
550 }
551 }
Casey Dahlind40e2fe2015-11-24 14:06:52 -0800552
553 private:
Will McVickerd7d18df2019-09-12 13:40:50 -0700554 AidlConstantValue(const AidlLocation& location, Type parsed_type, int64_t parsed_value,
555 const string& checked_value);
556 AidlConstantValue(const AidlLocation& location, Type type, const string& checked_value);
Steven Moreland860b1942018-08-16 14:59:28 -0700557 AidlConstantValue(const AidlLocation& location, Type type,
Jooyung Han29813842020-12-08 01:28:03 +0900558 std::unique_ptr<vector<unique_ptr<AidlConstantValue>>> values,
559 const std::string& value);
Steven Moreland25294322018-08-07 18:13:55 -0700560 static string ToString(Type type);
Will McVickerd7d18df2019-09-12 13:40:50 -0700561 static bool ParseIntegral(const string& value, int64_t* parsed_value, Type* parsed_type);
562 static bool IsHex(const string& value);
Steven Moreland4bcb05c2019-11-27 18:57:47 -0800563
Jooyung Han74675c22020-12-15 08:39:57 +0900564 virtual bool evaluate() const;
Casey Dahlind40e2fe2015-11-24 14:06:52 -0800565
Steven Moreland693640b2018-07-19 13:46:27 -0700566 const Type type_ = Type::ERROR;
Will McVickerd7d18df2019-09-12 13:40:50 -0700567 const vector<unique_ptr<AidlConstantValue>> values_; // if type_ == ARRAY
568 const string value_; // otherwise
569
570 // State for tracking evaluation of expressions
Steven Moreland4bcb05c2019-11-27 18:57:47 -0800571 mutable bool is_valid_ = false; // cache of CheckValid, but may be marked false in evaluate
572 mutable bool is_evaluated_ = false; // whether evaluate has been called
Will McVickerd7d18df2019-09-12 13:40:50 -0700573 mutable Type final_type_;
574 mutable int64_t final_value_;
575 mutable string final_string_value_ = "";
Steven Moreland693640b2018-07-19 13:46:27 -0700576
Will McVickerd7d18df2019-09-12 13:40:50 -0700577 friend AidlUnaryConstExpression;
578 friend AidlBinaryConstExpression;
Jooyung Han690f5842020-12-04 13:02:04 +0900579 friend AidlConstantReference;
580};
581
582// Represents "<type>.<field>" which resolves to a constant which is one of
583// - constant declartion
584// - enumerator
585// When a <type> is missing, <field> is of the enclosing type.
586class AidlConstantReference : public AidlConstantValue {
587 public:
588 AidlConstantReference(const AidlLocation& location, const std::string& value,
589 const std::string& comments);
590
591 const std::unique_ptr<AidlTypeSpecifier>& GetRefType() const { return ref_type_; }
592 void SetRefType(std::unique_ptr<AidlTypeSpecifier> type) { ref_type_ = std::move(type); }
593 const std::string& GetFieldName() const { return field_name_; }
594 const std::string& GetComments() const { return comments_; }
595
596 bool CheckValid() const override;
597 void Accept(Visitor& visitor) override { visitor.Visit(*this); }
Jooyung Han29813842020-12-08 01:28:03 +0900598 const AidlConstantValue* Resolve();
Jooyung Han690f5842020-12-04 13:02:04 +0900599
600 private:
Jooyung Han74675c22020-12-15 08:39:57 +0900601 bool evaluate() const override;
Jooyung Han690f5842020-12-04 13:02:04 +0900602
603 std::unique_ptr<AidlTypeSpecifier> ref_type_;
604 std::string field_name_;
605 const std::string comments_;
Jooyung Han29813842020-12-08 01:28:03 +0900606 const AidlConstantValue* resolved_ = nullptr;
Will McVickerd7d18df2019-09-12 13:40:50 -0700607};
608
609class AidlUnaryConstExpression : public AidlConstantValue {
610 public:
611 AidlUnaryConstExpression(const AidlLocation& location, const string& op,
612 std::unique_ptr<AidlConstantValue> rval);
613
614 static bool IsCompatibleType(Type type, const string& op);
615 bool CheckValid() const override;
Jooyung Han690f5842020-12-04 13:02:04 +0900616 void Accept(Visitor& visitor) override {
617 visitor.Visit(*this);
618 unary_->Accept(visitor);
619 }
620
Will McVickerd7d18df2019-09-12 13:40:50 -0700621 private:
Jooyung Han74675c22020-12-15 08:39:57 +0900622 bool evaluate() const override;
Will McVickerd7d18df2019-09-12 13:40:50 -0700623
624 std::unique_ptr<AidlConstantValue> unary_;
625 const string op_;
626};
627
628class AidlBinaryConstExpression : public AidlConstantValue {
629 public:
630 AidlBinaryConstExpression(const AidlLocation& location, std::unique_ptr<AidlConstantValue> lval,
631 const string& op, std::unique_ptr<AidlConstantValue> rval);
632
633 bool CheckValid() const override;
Will McVickerd7d18df2019-09-12 13:40:50 -0700634
635 static bool AreCompatibleTypes(Type t1, Type t2);
636 // Returns the promoted kind for both operands
637 static Type UsualArithmeticConversion(Type left, Type right);
638 // Returns the promoted integral type where INT32 is the smallest type
639 static Type IntegralPromotion(Type in);
Jooyung Han690f5842020-12-04 13:02:04 +0900640 void Accept(Visitor& visitor) override {
641 visitor.Visit(*this);
642 left_val_->Accept(visitor);
643 right_val_->Accept(visitor);
644 }
Will McVickerd7d18df2019-09-12 13:40:50 -0700645
646 private:
Jooyung Han74675c22020-12-15 08:39:57 +0900647 bool evaluate() const override;
Will McVickerd7d18df2019-09-12 13:40:50 -0700648
649 std::unique_ptr<AidlConstantValue> left_val_;
650 std::unique_ptr<AidlConstantValue> right_val_;
651 const string op_;
Christopher Wileyd6bdd8d2016-05-03 11:23:13 -0700652};
653
Andrei Onea9445fc62019-06-27 18:11:59 +0100654struct AidlAnnotationParameter {
655 std::string name;
656 std::unique_ptr<AidlConstantValue> value;
657};
658
Steven Moreland693640b2018-07-19 13:46:27 -0700659class AidlConstantDeclaration : public AidlMember {
Christopher Wileyd6bdd8d2016-05-03 11:23:13 -0700660 public:
Steven Moreland46e9da82018-07-27 15:45:29 -0700661 AidlConstantDeclaration(const AidlLocation& location, AidlTypeSpecifier* specifier,
Will McVickerd7d18df2019-09-12 13:40:50 -0700662 const string& name, AidlConstantValue* value);
Steven Moreland693640b2018-07-19 13:46:27 -0700663 virtual ~AidlConstantDeclaration() = default;
Christopher Wileyd6bdd8d2016-05-03 11:23:13 -0700664
Jiyong Parkd800fef2020-07-22 18:09:43 +0900665 // non-copyable, non-movable
666 AidlConstantDeclaration(const AidlConstantDeclaration&) = delete;
667 AidlConstantDeclaration(AidlConstantDeclaration&&) = delete;
668 AidlConstantDeclaration& operator=(const AidlConstantDeclaration&) = delete;
669 AidlConstantDeclaration& operator=(AidlConstantDeclaration&&) = delete;
670
Steven Moreland693640b2018-07-19 13:46:27 -0700671 const AidlTypeSpecifier& GetType() const { return *type_; }
Steven Moreland4d12f9a2018-10-31 14:30:55 -0700672 AidlTypeSpecifier* GetMutableType() { return type_.get(); }
Will McVickerd7d18df2019-09-12 13:40:50 -0700673 const string& GetName() const { return name_; }
Steven Moreland693640b2018-07-19 13:46:27 -0700674 const AidlConstantValue& GetValue() const { return *value_; }
Jeongik Chadb0f59e2018-11-01 18:11:21 +0900675 bool CheckValid(const AidlTypenames& typenames) const;
Christopher Wileyd6bdd8d2016-05-03 11:23:13 -0700676
Jooyung Han965e31d2020-11-27 12:30:16 +0900677 // ToString is for dumping AIDL.
678 // Returns string representation of this const decl including a const value.
679 // This is "`const` annotations type name value".
680 // e.g) "const @utf8InCpp String[] names = { "hello" }"
Will McVickerd7d18df2019-09-12 13:40:50 -0700681 string ToString() const;
Jooyung Han965e31d2020-11-27 12:30:16 +0900682
683 // Signature is for comparing types.
684 // Returns string representation of this const decl.
685 // This is "direction annotations type name".
686 // e.g) "String[] names"
Will McVickerd7d18df2019-09-12 13:40:50 -0700687 string Signature() const;
Jooyung Han965e31d2020-11-27 12:30:16 +0900688
Steven Moreland860b1942018-08-16 14:59:28 -0700689 string ValueString(const ConstantValueDecorator& decorator) const {
Will McVickerd7d18df2019-09-12 13:40:50 -0700690 return value_->ValueString(GetType(), decorator);
Steven Moreland860b1942018-08-16 14:59:28 -0700691 }
Steven Moreland25294322018-08-07 18:13:55 -0700692
Jooyung Han829ec7c2020-12-02 12:07:36 +0900693 const AidlConstantDeclaration* AsConstantDeclaration() const override { return this; }
Christopher Wileyd6bdd8d2016-05-03 11:23:13 -0700694
695 private:
Steven Moreland693640b2018-07-19 13:46:27 -0700696 const unique_ptr<AidlTypeSpecifier> type_;
Will McVickerd7d18df2019-09-12 13:40:50 -0700697 const string name_;
698 unique_ptr<AidlConstantValue> value_;
Casey Dahlind40e2fe2015-11-24 14:06:52 -0800699};
700
701class AidlMethod : public AidlMember {
Casey Dahlin5c69deb2015-10-01 14:44:12 -0700702 public:
Will McVickerd7d18df2019-09-12 13:40:50 -0700703 AidlMethod(const AidlLocation& location, bool oneway, AidlTypeSpecifier* type, const string& name,
704 vector<unique_ptr<AidlArgument>>* args, const string& comments);
705 AidlMethod(const AidlLocation& location, bool oneway, AidlTypeSpecifier* type, const string& name,
706 vector<unique_ptr<AidlArgument>>* args, const string& comments, int id,
707 bool is_user_defined = true);
Casey Dahlin5c69deb2015-10-01 14:44:12 -0700708 virtual ~AidlMethod() = default;
709
Jiyong Parkd800fef2020-07-22 18:09:43 +0900710 // non-copyable, non-movable
711 AidlMethod(const AidlMethod&) = delete;
712 AidlMethod(AidlMethod&&) = delete;
713 AidlMethod& operator=(const AidlMethod&) = delete;
714 AidlMethod& operator=(AidlMethod&&) = delete;
715
Jooyung Han829ec7c2020-12-02 12:07:36 +0900716 const AidlMethod* AsMethod() const override { return this; }
Jeongik Cha997281d2020-01-16 15:23:59 +0900717 bool IsHidden() const;
Will McVickerd7d18df2019-09-12 13:40:50 -0700718 const string& GetComments() const { return comments_; }
Jiyong Parkd59a10d2018-07-18 11:12:55 +0900719 const AidlTypeSpecifier& GetType() const { return *type_; }
720 AidlTypeSpecifier* GetMutableType() { return type_.get(); }
Steven Morelandacd53472018-12-14 10:17:26 -0800721
Steven Moreland8c70ba92018-12-17 10:20:31 -0800722 // set if this method is part of an interface that is marked oneway
723 void ApplyInterfaceOneway(bool oneway) { oneway_ = oneway_ || oneway; }
Casey Dahlinf4a93112015-10-05 16:58:09 -0700724 bool IsOneway() const { return oneway_; }
Steven Morelandacd53472018-12-14 10:17:26 -0800725
Casey Dahlinf4a93112015-10-05 16:58:09 -0700726 const std::string& GetName() const { return name_; }
Casey Dahlinf4a93112015-10-05 16:58:09 -0700727 bool HasId() const { return has_id_; }
Jiyong Parked65bf42018-08-28 15:43:27 +0900728 int GetId() const { return id_; }
Casey Dahlinf4a93112015-10-05 16:58:09 -0700729 void SetId(unsigned id) { id_ = id; }
Casey Dahlinf2d23f72015-10-02 16:19:19 -0700730
Jiyong Park309668e2018-07-28 16:55:44 +0900731 bool IsUserDefined() const { return is_user_defined_; }
732
Casey Dahlinf4a93112015-10-05 16:58:09 -0700733 const std::vector<std::unique_ptr<AidlArgument>>& GetArguments() const {
Christopher Wileyad339272015-10-05 19:11:58 -0700734 return arguments_;
735 }
736 // An inout parameter will appear in both GetInArguments()
737 // and GetOutArguments(). AidlMethod retains ownership of the argument
738 // pointers returned in this way.
739 const std::vector<const AidlArgument*>& GetInArguments() const {
740 return in_arguments_;
741 }
742 const std::vector<const AidlArgument*>& GetOutArguments() const {
743 return out_arguments_;
Casey Dahlinf4a93112015-10-05 16:58:09 -0700744 }
Casey Dahlin5c69deb2015-10-01 14:44:12 -0700745
Jooyung Han965e31d2020-11-27 12:30:16 +0900746 // ToString is for dumping AIDL.
747 // Returns string representation of this method including everything.
748 // This is "ret_type name ( arg_list ) = id".
749 // e.g) "boolean foo(int, @Nullable String) = 1"
Jiyong Park309668e2018-07-28 16:55:44 +0900750 std::string ToString() const;
751
Jooyung Han965e31d2020-11-27 12:30:16 +0900752 // Signature is for comparing AIDL types.
753 // Returns string representation of this method's name & type.
754 // e.g) "foo(int, String)"
755 std::string Signature() const;
756
Casey Dahlin5c69deb2015-10-01 14:44:12 -0700757 private:
Casey Dahlinf4a93112015-10-05 16:58:09 -0700758 bool oneway_;
Casey Dahlinf2d23f72015-10-02 16:19:19 -0700759 std::string comments_;
Jiyong Parkd59a10d2018-07-18 11:12:55 +0900760 std::unique_ptr<AidlTypeSpecifier> type_;
Casey Dahlinf4a93112015-10-05 16:58:09 -0700761 std::string name_;
Christopher Wileyad339272015-10-05 19:11:58 -0700762 const std::vector<std::unique_ptr<AidlArgument>> arguments_;
763 std::vector<const AidlArgument*> in_arguments_;
764 std::vector<const AidlArgument*> out_arguments_;
Casey Dahlinf4a93112015-10-05 16:58:09 -0700765 bool has_id_;
766 int id_;
Jiyong Park309668e2018-07-28 16:55:44 +0900767 bool is_user_defined_ = true;
Casey Dahlin0a2f8be2015-09-28 16:15:29 -0700768};
Adam Lesinskiffa16862014-01-23 18:17:42 -0800769
Steven Morelandc258abc2018-07-10 14:03:38 -0700770class AidlDefinedType;
Jiyong Parkb034bf02018-07-30 17:44:33 +0900771class AidlInterface;
772class AidlParcelable;
773class AidlStructuredParcelable;
Jooyung Han2946afc2020-10-05 20:29:16 +0900774class AidlUnionDecl;
Jooyung Han3f347ca2020-12-01 12:41:50 +0900775
Daniel Norman85aed542019-08-21 12:01:14 -0700776// AidlDefinedType represents either an interface, parcelable, or enum that is
Jiyong Park1deecc32018-07-17 01:14:41 +0900777// defined in the source file.
778class AidlDefinedType : public AidlAnnotatable {
Steven Moreland787b0432018-07-03 09:00:58 -0700779 public:
Steven Moreland46e9da82018-07-27 15:45:29 -0700780 AidlDefinedType(const AidlLocation& location, const std::string& name,
Jooyung Han829ec7c2020-12-02 12:07:36 +0900781 const std::string& comments, const std::string& package,
782 std::vector<std::unique_ptr<AidlMember>>* members);
Steven Moreland787b0432018-07-03 09:00:58 -0700783 virtual ~AidlDefinedType() = default;
784
Jiyong Parkd800fef2020-07-22 18:09:43 +0900785 // non-copyable, non-movable
786 AidlDefinedType(const AidlDefinedType&) = delete;
787 AidlDefinedType(AidlDefinedType&&) = delete;
788 AidlDefinedType& operator=(const AidlDefinedType&) = delete;
789 AidlDefinedType& operator=(AidlDefinedType&&) = delete;
790
Jiyong Park1deecc32018-07-17 01:14:41 +0900791 const std::string& GetName() const { return name_; };
Jeongik Cha997281d2020-01-16 15:23:59 +0900792 bool IsHidden() const;
Jiyong Park1deecc32018-07-17 01:14:41 +0900793 const std::string& GetComments() const { return comments_; }
Jiyong Parka6605ab2018-11-11 14:30:21 +0900794 void SetComments(const std::string comments) { comments_ = comments; }
Jiyong Park1deecc32018-07-17 01:14:41 +0900795
Steven Moreland787b0432018-07-03 09:00:58 -0700796 /* dot joined package, example: "android.package.foo" */
Jiyong Park18132182020-06-08 20:24:40 +0900797 std::string GetPackage() const { return package_; }
Steven Moreland787b0432018-07-03 09:00:58 -0700798 /* dot joined package and name, example: "android.package.foo.IBar" */
799 std::string GetCanonicalName() const;
Jiyong Park18132182020-06-08 20:24:40 +0900800 const std::vector<std::string>& GetSplitPackage() const { return split_package_; }
Steven Moreland787b0432018-07-03 09:00:58 -0700801
Steven Morelanded83a282018-07-17 13:27:29 -0700802 virtual std::string GetPreprocessDeclarationName() const = 0;
Steven Morelandc258abc2018-07-10 14:03:38 -0700803
Steven Moreland5557f1c2018-07-02 13:50:23 -0700804 virtual const AidlStructuredParcelable* AsStructuredParcelable() const { return nullptr; }
Steven Morelandc258abc2018-07-10 14:03:38 -0700805 virtual const AidlParcelable* AsParcelable() const { return nullptr; }
Daniel Norman85aed542019-08-21 12:01:14 -0700806 virtual const AidlEnumDeclaration* AsEnumDeclaration() const { return nullptr; }
Jooyung Han2946afc2020-10-05 20:29:16 +0900807 virtual const AidlUnionDecl* AsUnionDeclaration() const { return nullptr; }
Steven Moreland5557f1c2018-07-02 13:50:23 -0700808 virtual const AidlInterface* AsInterface() const { return nullptr; }
Jeongik Chadf76dc72019-11-28 00:08:47 +0900809 virtual const AidlParameterizable<std::string>* AsParameterizable() const { return nullptr; }
Jooyung Han888c5bc2020-12-22 17:28:47 +0900810 virtual bool CheckValid(const AidlTypenames& typenames, DiagnosticsContext& context) const;
Steven Morelandd59e3172020-05-11 16:42:09 -0700811 virtual bool LanguageSpecificCheckValid(const AidlTypenames& typenames,
812 Options::Language lang) const = 0;
Steven Morelandc258abc2018-07-10 14:03:38 -0700813 AidlStructuredParcelable* AsStructuredParcelable() {
814 return const_cast<AidlStructuredParcelable*>(
815 const_cast<const AidlDefinedType*>(this)->AsStructuredParcelable());
816 }
817 AidlParcelable* AsParcelable() {
818 return const_cast<AidlParcelable*>(const_cast<const AidlDefinedType*>(this)->AsParcelable());
819 }
Daniel Norman85aed542019-08-21 12:01:14 -0700820 AidlEnumDeclaration* AsEnumDeclaration() {
821 return const_cast<AidlEnumDeclaration*>(
822 const_cast<const AidlDefinedType*>(this)->AsEnumDeclaration());
823 }
Jooyung Han2946afc2020-10-05 20:29:16 +0900824 AidlUnionDecl* AsUnionDeclaration() {
825 return const_cast<AidlUnionDecl*>(
826 const_cast<const AidlDefinedType*>(this)->AsUnionDeclaration());
827 }
Steven Morelandc258abc2018-07-10 14:03:38 -0700828 AidlInterface* AsInterface() {
829 return const_cast<AidlInterface*>(const_cast<const AidlDefinedType*>(this)->AsInterface());
830 }
831
Jeongik Chadf76dc72019-11-28 00:08:47 +0900832 AidlParameterizable<std::string>* AsParameterizable() {
833 return const_cast<AidlParameterizable<std::string>*>(
834 const_cast<const AidlDefinedType*>(this)->AsParameterizable());
835 }
836
Steven Moreland6cee3482018-07-18 14:39:58 -0700837 const AidlParcelable* AsUnstructuredParcelable() const {
838 if (this->AsStructuredParcelable() != nullptr) return nullptr;
Jooyung Han2946afc2020-10-05 20:29:16 +0900839 if (this->AsUnionDeclaration() != nullptr) return nullptr;
Steven Moreland6cee3482018-07-18 14:39:58 -0700840 return this->AsParcelable();
841 }
842 AidlParcelable* AsUnstructuredParcelable() {
843 return const_cast<AidlParcelable*>(
844 const_cast<const AidlDefinedType*>(this)->AsUnstructuredParcelable());
845 }
846
Jeongik Cha997281d2020-01-16 15:23:59 +0900847 virtual void Dump(CodeWriter* writer) const = 0;
Steven Morelanda5d9c5c2020-02-21 16:01:09 -0800848 void DumpHeader(CodeWriter* writer) const;
Jiyong Park02da7422018-07-16 16:00:26 +0900849
Jooyung Han829ec7c2020-12-02 12:07:36 +0900850 const std::vector<std::unique_ptr<AidlVariableDeclaration>>& GetFields() const {
851 return variables_;
852 }
853 const std::vector<std::unique_ptr<AidlConstantDeclaration>>& GetConstantDeclarations() const {
854 return constants_;
855 }
856 const std::vector<std::unique_ptr<AidlMethod>>& GetMethods() const { return methods_; }
857 void AddMethod(std::unique_ptr<AidlMethod> method) { methods_.push_back(std::move(method)); }
858 const std::vector<const AidlMember*>& GetMembers() const { return members_; }
859
860 protected:
861 // utility for subclasses with getter names
862 bool CheckValidForGetterNames() const;
863
Steven Moreland787b0432018-07-03 09:00:58 -0700864 private:
Jooyung Han829ec7c2020-12-02 12:07:36 +0900865 bool CheckValidWithMembers(const AidlTypenames& typenames) const;
866
Jiyong Park1deecc32018-07-17 01:14:41 +0900867 std::string name_;
Jiyong Park1deecc32018-07-17 01:14:41 +0900868 std::string comments_;
Jiyong Park18132182020-06-08 20:24:40 +0900869 const std::string package_;
870 const std::vector<std::string> split_package_;
Jooyung Han829ec7c2020-12-02 12:07:36 +0900871 std::vector<std::unique_ptr<AidlVariableDeclaration>> variables_;
872 std::vector<std::unique_ptr<AidlConstantDeclaration>> constants_;
873 std::vector<std::unique_ptr<AidlMethod>> methods_;
874 std::vector<const AidlMember*> members_; // keep members in order of appearance.
Steven Moreland787b0432018-07-03 09:00:58 -0700875};
876
Jeongik Chadf76dc72019-11-28 00:08:47 +0900877class AidlParcelable : public AidlDefinedType, public AidlParameterizable<std::string> {
Casey Dahlin1ae2bc52015-10-07 18:49:10 -0700878 public:
Jiyong Park18132182020-06-08 20:24:40 +0900879 AidlParcelable(const AidlLocation& location, const std::string& name, const std::string& package,
880 const std::string& comments, const std::string& cpp_header = "",
Jooyung Han829ec7c2020-12-02 12:07:36 +0900881 std::vector<std::string>* type_params = nullptr,
882 std::vector<std::unique_ptr<AidlMember>>* members = nullptr);
Casey Dahlin1ae2bc52015-10-07 18:49:10 -0700883 virtual ~AidlParcelable() = default;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800884
Jiyong Parkd800fef2020-07-22 18:09:43 +0900885 // non-copyable, non-movable
886 AidlParcelable(const AidlParcelable&) = delete;
887 AidlParcelable(AidlParcelable&&) = delete;
888 AidlParcelable& operator=(const AidlParcelable&) = delete;
889 AidlParcelable& operator=(AidlParcelable&&) = delete;
890
Christopher Wiley8aa4d9f2015-11-16 19:10:45 -0800891 std::string GetCppHeader() const { return cpp_header_; }
Christopher Wiley8aa4d9f2015-11-16 19:10:45 -0800892
Steven Moreland0cea4aa2020-04-20 21:06:02 -0700893 std::set<AidlAnnotation::Type> GetSupportedAnnotations() const override;
Jooyung Han888c5bc2020-12-22 17:28:47 +0900894 bool CheckValid(const AidlTypenames& typenames, DiagnosticsContext& context) const override;
Steven Morelandd59e3172020-05-11 16:42:09 -0700895 bool LanguageSpecificCheckValid(const AidlTypenames& typenames,
896 Options::Language lang) const override;
Steven Morelandc258abc2018-07-10 14:03:38 -0700897 const AidlParcelable* AsParcelable() const override { return this; }
Jeongik Chadf76dc72019-11-28 00:08:47 +0900898 const AidlParameterizable<std::string>* AsParameterizable() const override { return this; }
899 const AidlNode& AsAidlNode() const override { return *this; }
Steven Morelanded83a282018-07-17 13:27:29 -0700900 std::string GetPreprocessDeclarationName() const override { return "parcelable"; }
Steven Morelandc258abc2018-07-10 14:03:38 -0700901
Jeongik Cha997281d2020-01-16 15:23:59 +0900902 void Dump(CodeWriter* writer) const override;
Jiyong Park02da7422018-07-16 16:00:26 +0900903
Casey Dahlin1ae2bc52015-10-07 18:49:10 -0700904 private:
Christopher Wiley8aa4d9f2015-11-16 19:10:45 -0800905 std::string cpp_header_;
Casey Dahlin0a2f8be2015-09-28 16:15:29 -0700906};
Adam Lesinskiffa16862014-01-23 18:17:42 -0800907
Jooyung Han829ec7c2020-12-02 12:07:36 +0900908class AidlStructuredParcelable : public AidlParcelable {
Steven Moreland5557f1c2018-07-02 13:50:23 -0700909 public:
Jiyong Park18132182020-06-08 20:24:40 +0900910 AidlStructuredParcelable(const AidlLocation& location, const std::string& name,
911 const std::string& package, const std::string& comments,
Jooyung Han829ec7c2020-12-02 12:07:36 +0900912 std::vector<std::string>* type_params,
913 std::vector<std::unique_ptr<AidlMember>>* members);
Jiyong Parkd800fef2020-07-22 18:09:43 +0900914 virtual ~AidlStructuredParcelable() = default;
915
916 // non-copyable, non-movable
917 AidlStructuredParcelable(const AidlStructuredParcelable&) = delete;
918 AidlStructuredParcelable(AidlStructuredParcelable&&) = delete;
919 AidlStructuredParcelable& operator=(const AidlStructuredParcelable&) = delete;
920 AidlStructuredParcelable& operator=(AidlStructuredParcelable&&) = delete;
Steven Moreland5557f1c2018-07-02 13:50:23 -0700921
Steven Morelandc258abc2018-07-10 14:03:38 -0700922 const AidlStructuredParcelable* AsStructuredParcelable() const override { return this; }
Steven Morelanded83a282018-07-17 13:27:29 -0700923 std::string GetPreprocessDeclarationName() const override { return "structured_parcelable"; }
Steven Moreland5557f1c2018-07-02 13:50:23 -0700924
Jeongik Cha997281d2020-01-16 15:23:59 +0900925 void Dump(CodeWriter* writer) const override;
Jiyong Park02da7422018-07-16 16:00:26 +0900926
Steven Moreland0cea4aa2020-04-20 21:06:02 -0700927 std::set<AidlAnnotation::Type> GetSupportedAnnotations() const override;
Jooyung Han888c5bc2020-12-22 17:28:47 +0900928 bool CheckValid(const AidlTypenames& typenames, DiagnosticsContext& context) const override;
Steven Morelandd59e3172020-05-11 16:42:09 -0700929 bool LanguageSpecificCheckValid(const AidlTypenames& typenames,
930 Options::Language lang) const override;
Steven Moreland5557f1c2018-07-02 13:50:23 -0700931};
932
Daniel Norman85aed542019-08-21 12:01:14 -0700933class AidlEnumerator : public AidlNode {
934 public:
Daniel Norman2e4112d2019-10-03 10:22:35 -0700935 AidlEnumerator(const AidlLocation& location, const std::string& name, AidlConstantValue* value,
936 const std::string& comments);
Daniel Norman85aed542019-08-21 12:01:14 -0700937 virtual ~AidlEnumerator() = default;
938
Jiyong Parkd800fef2020-07-22 18:09:43 +0900939 // non-copyable, non-movable
940 AidlEnumerator(const AidlEnumerator&) = delete;
941 AidlEnumerator(AidlEnumerator&&) = delete;
942 AidlEnumerator& operator=(const AidlEnumerator&) = delete;
943 AidlEnumerator& operator=(AidlEnumerator&&) = delete;
944
Daniel Norman85aed542019-08-21 12:01:14 -0700945 const std::string& GetName() const { return name_; }
Will McVickerd7d18df2019-09-12 13:40:50 -0700946 AidlConstantValue* GetValue() const { return value_.get(); }
Daniel Norman2e4112d2019-10-03 10:22:35 -0700947 const std::string& GetComments() const { return comments_; }
Daniel Norman85aed542019-08-21 12:01:14 -0700948 bool CheckValid(const AidlTypeSpecifier& enum_backing_type) const;
949
950 string ValueString(const AidlTypeSpecifier& backing_type,
951 const ConstantValueDecorator& decorator) const;
952
Daniel Normanb28684e2019-10-17 15:31:39 -0700953 void SetValue(std::unique_ptr<AidlConstantValue> value) { value_ = std::move(value); }
Jooyung Han29813842020-12-08 01:28:03 +0900954 bool IsValueUserSpecified() const { return value_user_specified_; }
Daniel Normanb28684e2019-10-17 15:31:39 -0700955
Daniel Norman85aed542019-08-21 12:01:14 -0700956 private:
957 const std::string name_;
Will McVickerd7d18df2019-09-12 13:40:50 -0700958 unique_ptr<AidlConstantValue> value_;
Daniel Norman2e4112d2019-10-03 10:22:35 -0700959 const std::string comments_;
Jooyung Han29813842020-12-08 01:28:03 +0900960 const bool value_user_specified_;
Daniel Norman85aed542019-08-21 12:01:14 -0700961};
962
963class AidlEnumDeclaration : public AidlDefinedType {
964 public:
Will McVickerd7d18df2019-09-12 13:40:50 -0700965 AidlEnumDeclaration(const AidlLocation& location, const string& name,
Daniel Norman85aed542019-08-21 12:01:14 -0700966 std::vector<std::unique_ptr<AidlEnumerator>>* enumerators,
Jiyong Park18132182020-06-08 20:24:40 +0900967 const std::string& package, const std::string& comments);
Daniel Norman85aed542019-08-21 12:01:14 -0700968 virtual ~AidlEnumDeclaration() = default;
969
Jiyong Parkd800fef2020-07-22 18:09:43 +0900970 // non-copyable, non-movable
971 AidlEnumDeclaration(const AidlEnumDeclaration&) = delete;
972 AidlEnumDeclaration(AidlEnumDeclaration&&) = delete;
973 AidlEnumDeclaration& operator=(const AidlEnumDeclaration&) = delete;
974 AidlEnumDeclaration& operator=(AidlEnumDeclaration&&) = delete;
975
Jooyung Han672557b2020-12-24 05:18:00 +0900976 bool Autofill(const AidlTypenames&);
Daniel Norman85aed542019-08-21 12:01:14 -0700977 const AidlTypeSpecifier& GetBackingType() const { return *backing_type_; }
978 const std::vector<std::unique_ptr<AidlEnumerator>>& GetEnumerators() const {
979 return enumerators_;
980 }
Steven Moreland0cea4aa2020-04-20 21:06:02 -0700981 std::set<AidlAnnotation::Type> GetSupportedAnnotations() const override;
Jooyung Han888c5bc2020-12-22 17:28:47 +0900982 bool CheckValid(const AidlTypenames& typenames, DiagnosticsContext& context) const override;
Steven Morelandd59e3172020-05-11 16:42:09 -0700983 bool LanguageSpecificCheckValid(const AidlTypenames& /*typenames*/,
984 Options::Language) const override {
985 return true;
986 }
Daniel Norman85aed542019-08-21 12:01:14 -0700987 std::string GetPreprocessDeclarationName() const override { return "enum"; }
Jeongik Cha997281d2020-01-16 15:23:59 +0900988 void Dump(CodeWriter* writer) const override;
Daniel Norman85aed542019-08-21 12:01:14 -0700989
990 const AidlEnumDeclaration* AsEnumDeclaration() const override { return this; }
991
992 private:
Jooyung Han29813842020-12-08 01:28:03 +0900993
Daniel Norman85aed542019-08-21 12:01:14 -0700994 const std::string name_;
995 const std::vector<std::unique_ptr<AidlEnumerator>> enumerators_;
Jooyung Han672557b2020-12-24 05:18:00 +0900996 std::unique_ptr<AidlTypeSpecifier> backing_type_;
Daniel Norman85aed542019-08-21 12:01:14 -0700997};
998
Jooyung Han829ec7c2020-12-02 12:07:36 +0900999class AidlUnionDecl : public AidlParcelable {
Jooyung Han2946afc2020-10-05 20:29:16 +09001000 public:
1001 AidlUnionDecl(const AidlLocation& location, const std::string& name, const std::string& package,
Jooyung Han829ec7c2020-12-02 12:07:36 +09001002 const std::string& comments, std::vector<std::string>* type_params,
1003 std::vector<std::unique_ptr<AidlMember>>* members);
Jooyung Han2946afc2020-10-05 20:29:16 +09001004 virtual ~AidlUnionDecl() = default;
1005
1006 // non-copyable, non-movable
1007 AidlUnionDecl(const AidlUnionDecl&) = delete;
1008 AidlUnionDecl(AidlUnionDecl&&) = delete;
1009 AidlUnionDecl& operator=(const AidlUnionDecl&) = delete;
1010 AidlUnionDecl& operator=(AidlUnionDecl&&) = delete;
1011
1012 std::set<AidlAnnotation::Type> GetSupportedAnnotations() const override;
1013
1014 const AidlNode& AsAidlNode() const override { return *this; }
1015
Jooyung Han888c5bc2020-12-22 17:28:47 +09001016 bool CheckValid(const AidlTypenames& typenames, DiagnosticsContext& context) const override;
Jooyung Hanfe89f122020-10-14 03:49:18 +09001017 bool LanguageSpecificCheckValid(const AidlTypenames& typenames,
1018 Options::Language lang) const override;
Jooyung Han2946afc2020-10-05 20:29:16 +09001019 std::string GetPreprocessDeclarationName() const override { return "union"; }
1020
1021 void Dump(CodeWriter* writer) const override;
1022 const AidlUnionDecl* AsUnionDeclaration() const override { return this; }
Jooyung Han2946afc2020-10-05 20:29:16 +09001023};
1024
Jiyong Park1deecc32018-07-17 01:14:41 +09001025class AidlInterface final : public AidlDefinedType {
Casey Dahlin1ae2bc52015-10-07 18:49:10 -07001026 public:
Steven Moreland46e9da82018-07-27 15:45:29 -07001027 AidlInterface(const AidlLocation& location, const std::string& name, const std::string& comments,
Jooyung Han829ec7c2020-12-02 12:07:36 +09001028 bool oneway_, const std::string& package,
1029 std::vector<std::unique_ptr<AidlMember>>* members);
Casey Dahlin1ae2bc52015-10-07 18:49:10 -07001030 virtual ~AidlInterface() = default;
1031
Jiyong Parkd800fef2020-07-22 18:09:43 +09001032 // non-copyable, non-movable
1033 AidlInterface(const AidlInterface&) = delete;
1034 AidlInterface(AidlInterface&&) = delete;
1035 AidlInterface& operator=(const AidlInterface&) = delete;
1036 AidlInterface& operator=(AidlInterface&&) = delete;
1037
Steven Morelandc258abc2018-07-10 14:03:38 -07001038 const AidlInterface* AsInterface() const override { return this; }
Steven Morelanded83a282018-07-17 13:27:29 -07001039 std::string GetPreprocessDeclarationName() const override { return "interface"; }
Steven Moreland5557f1c2018-07-02 13:50:23 -07001040
Jeongik Cha997281d2020-01-16 15:23:59 +09001041 void Dump(CodeWriter* writer) const override;
Jiyong Park02da7422018-07-16 16:00:26 +09001042
Steven Moreland0cea4aa2020-04-20 21:06:02 -07001043 std::set<AidlAnnotation::Type> GetSupportedAnnotations() const override;
Jooyung Han888c5bc2020-12-22 17:28:47 +09001044 bool CheckValid(const AidlTypenames& typenames, DiagnosticsContext& context) const override;
Steven Morelandd59e3172020-05-11 16:42:09 -07001045 bool LanguageSpecificCheckValid(const AidlTypenames& typenames,
1046 Options::Language lang) const override;
Jeongik Chadb0f59e2018-11-01 18:11:21 +09001047
Jiyong Park27fd7fd2020-08-27 16:25:09 +09001048 std::string GetDescriptor() const;
Casey Dahlin0a2f8be2015-09-28 16:15:29 -07001049};
Adam Lesinskiffa16862014-01-23 18:17:42 -08001050
Casey Dahlin0edf3422015-10-07 12:34:59 -07001051class AidlImport : public AidlNode {
1052 public:
Steven Moreland46e9da82018-07-27 15:45:29 -07001053 AidlImport(const AidlLocation& location, const std::string& needed_class);
Casey Dahlin0edf3422015-10-07 12:34:59 -07001054 virtual ~AidlImport() = default;
1055
Jiyong Parkd800fef2020-07-22 18:09:43 +09001056 // non-copyable, non-movable
1057 AidlImport(const AidlImport&) = delete;
1058 AidlImport(AidlImport&&) = delete;
1059 AidlImport& operator=(const AidlImport&) = delete;
1060 AidlImport& operator=(AidlImport&&) = delete;
1061
Casey Dahlin0edf3422015-10-07 12:34:59 -07001062 const std::string& GetNeededClass() const { return needed_class_; }
Casey Dahlin0edf3422015-10-07 12:34:59 -07001063
1064 private:
Casey Dahlin0edf3422015-10-07 12:34:59 -07001065 std::string needed_class_;
Casey Dahline2507492015-09-14 17:11:20 -07001066};
1067
Jiyong Park62515512020-06-08 15:57:11 +09001068// AidlDocument models an AIDL file
1069class AidlDocument : public AidlNode {
1070 public:
1071 AidlDocument(const AidlLocation& location, std::vector<std::unique_ptr<AidlImport>>& imports,
Jiyong Park8e79b7f2020-07-20 20:52:38 +09001072 std::vector<std::unique_ptr<AidlDefinedType>>&& defined_types)
1073 : AidlNode(location),
1074 imports_(std::move(imports)),
1075 defined_types_(std::move(defined_types)) {}
Jiyong Parkd800fef2020-07-22 18:09:43 +09001076 ~AidlDocument() = default;
1077
1078 // non-copyable, non-movable
Jiyong Park8e79b7f2020-07-20 20:52:38 +09001079 AidlDocument(const AidlDocument&) = delete;
1080 AidlDocument(AidlDocument&&) = delete;
1081 AidlDocument& operator=(const AidlDocument&) = delete;
1082 AidlDocument& operator=(AidlDocument&&) = delete;
Jiyong Parkd800fef2020-07-22 18:09:43 +09001083
Jooyung Han29813842020-12-08 01:28:03 +09001084 std::optional<std::string> ResolveName(const std::string& unresolved_type) const;
Jiyong Parkd800fef2020-07-22 18:09:43 +09001085 const std::vector<std::unique_ptr<AidlImport>>& Imports() const { return imports_; }
1086 const std::vector<std::unique_ptr<AidlDefinedType>>& DefinedTypes() const {
1087 return defined_types_;
1088 }
Jiyong Park62515512020-06-08 15:57:11 +09001089
1090 private:
1091 const std::vector<std::unique_ptr<AidlImport>> imports_;
Jiyong Park8e79b7f2020-07-20 20:52:38 +09001092 const std::vector<std::unique_ptr<AidlDefinedType>> defined_types_;
Jiyong Park62515512020-06-08 15:57:11 +09001093};