blob: 53616b7f2a9e855eabc5f6cf6040809c79300484 [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
Jiyong Park1deecc32018-07-17 01:14:41 +090019#include "aidl_typenames.h"
Jiyong Park02da7422018-07-16 16:00:26 +090020#include "code_writer.h"
Jiyong Park1deecc32018-07-17 01:14:41 +090021#include "io_delegate.h"
Jeongik Cha047c5ee2019-08-07 23:16:49 +090022#include "options.h"
Jiyong Park1deecc32018-07-17 01:14:41 +090023
Casey Dahlinbc7a50a2015-09-28 19:20:50 -070024#include <memory>
Jeongik Cha997281d2020-01-16 15:23:59 +090025#include <regex>
Casey Dahlindd691812015-09-09 17:59:06 -070026#include <string>
Jeongik Chadf76dc72019-11-28 00:08:47 +090027#include <unordered_set>
Casey Dahlinbc7a50a2015-09-28 19:20:50 -070028#include <vector>
Casey Dahlindd691812015-09-09 17:59:06 -070029
Elliott Hughes0a620672015-12-04 13:53:18 -080030#include <android-base/strings.h>
Casey Dahlin73d46b02015-09-11 02:47:54 +000031
Jiyong Parkb034bf02018-07-30 17:44:33 +090032using android::aidl::AidlTypenames;
Jiyong Park02da7422018-07-16 16:00:26 +090033using android::aidl::CodeWriter;
Jeongik Cha047c5ee2019-08-07 23:16:49 +090034using android::aidl::Options;
Steven Moreland3f658cf2018-08-20 13:40:54 -070035using std::shared_ptr;
Jiyong Park1deecc32018-07-17 01:14:41 +090036using std::string;
37using std::unique_ptr;
38using std::vector;
Andrei Onea8714b022019-02-01 18:55:54 +000039class AidlNode;
40
41namespace android {
42namespace aidl {
43namespace mappings {
44std::string dump_location(const AidlNode& method);
45} // namespace mappings
Mathew Inwoodadb74672019-11-29 14:01:53 +000046namespace java {
47std::string dump_location(const AidlNode& method);
48} // namespace java
Andrei Onea8714b022019-02-01 18:55:54 +000049} // namespace aidl
50} // namespace android
51
Steven Moreland46e9da82018-07-27 15:45:29 -070052class AidlLocation {
Casey Dahlinbc7a50a2015-09-28 19:20:50 -070053 public:
Steven Moreland46e9da82018-07-27 15:45:29 -070054 struct Point {
Dan Willemsen609ba6d2019-12-30 10:44:00 -080055 int line;
56 int column;
Steven Moreland46e9da82018-07-27 15:45:29 -070057 };
58
Devin Mooredf93ebb2020-03-25 14:03:35 -070059 enum class Source {
60 // From internal aidl source code
61 INTERNAL = 0,
62 // From a parsed file
63 EXTERNAL = 1
64 };
65
66 AidlLocation(const std::string& file, Point begin, Point end, Source source);
Devin Moore5de18ed2020-04-02 13:52:29 -070067 AidlLocation(const std::string& file, Source source)
68 : AidlLocation(file, {0, 0}, {0, 0}, source) {}
Devin Mooredf93ebb2020-03-25 14:03:35 -070069
70 bool IsInternal() const { return source_ == Source::INTERNAL; }
Steven Moreland46e9da82018-07-27 15:45:29 -070071
Devin Moore5de18ed2020-04-02 13:52:29 -070072 // The first line of a file is line 1.
73 bool LocationKnown() const { return begin_.line != 0; }
74
Steven Moreland46e9da82018-07-27 15:45:29 -070075 friend std::ostream& operator<<(std::ostream& os, const AidlLocation& l);
Andrei Onea8714b022019-02-01 18:55:54 +000076 friend class AidlNode;
Casey Dahlinbc7a50a2015-09-28 19:20:50 -070077
78 private:
Steven Moreland541788d2020-05-21 22:05:52 +000079 // INTENTIONALLY HIDDEN: only operator<< should access details here.
80 // Otherwise, locations should only ever be copied around to construct new
81 // objects.
Steven Moreland46e9da82018-07-27 15:45:29 -070082 const std::string file_;
83 Point begin_;
84 Point end_;
Devin Mooredf93ebb2020-03-25 14:03:35 -070085 Source source_;
Steven Moreland46e9da82018-07-27 15:45:29 -070086};
87
Devin Mooredf93ebb2020-03-25 14:03:35 -070088#define AIDL_LOCATION_HERE \
Steven Moreland21780812020-09-11 01:29:45 +000089 (AidlLocation{__FILE__, {__LINE__, 0}, {__LINE__, 0}, AidlLocation::Source::INTERNAL})
Steven Moreland02e012e2018-08-02 14:58:10 -070090
Steven Moreland46e9da82018-07-27 15:45:29 -070091std::ostream& operator<<(std::ostream& os, const AidlLocation& l);
92
93// Anything that is locatable in a .aidl file.
94class AidlNode {
95 public:
96 AidlNode(const AidlLocation& location);
Steven Moreland3f658cf2018-08-20 13:40:54 -070097
98 AidlNode(const AidlNode&) = default;
Steven Moreland46e9da82018-07-27 15:45:29 -070099 virtual ~AidlNode() = default;
100
Jiyong Parkd800fef2020-07-22 18:09:43 +0900101 AidlNode(AidlNode&&) = delete;
102 AidlNode& operator=(AidlNode&&) = delete;
103
Devin Mooredf93ebb2020-03-25 14:03:35 -0700104 // To be able to print AidlLocation
Steven Morelandb0d15a52020-03-31 14:03:47 -0700105 friend class AidlErrorLog;
Andrei Onea8714b022019-02-01 18:55:54 +0000106 friend std::string android::aidl::mappings::dump_location(const AidlNode&);
Mathew Inwoodadb74672019-11-29 14:01:53 +0000107 friend std::string android::aidl::java::dump_location(const AidlNode&);
Steven Moreland46e9da82018-07-27 15:45:29 -0700108
Devin Mooredf93ebb2020-03-25 14:03:35 -0700109 const AidlLocation& GetLocation() const { return location_; }
110
Steven Moreland46e9da82018-07-27 15:45:29 -0700111 private:
Mathew Inwoodadb74672019-11-29 14:01:53 +0000112 std::string PrintLine() const;
Andrei Onea8714b022019-02-01 18:55:54 +0000113 std::string PrintLocation() const;
Steven Moreland46e9da82018-07-27 15:45:29 -0700114 const AidlLocation location_;
Casey Dahlinbc7a50a2015-09-28 19:20:50 -0700115};
116
Casey Dahlina2f77c42015-12-01 18:26:02 -0800117namespace android {
118namespace aidl {
119
Jiyong Park1deecc32018-07-17 01:14:41 +0900120class AidlTypenames;
Casey Dahlina2f77c42015-12-01 18:26:02 -0800121
122} // namespace aidl
123} // namespace android
124
Jeongik Chadf76dc72019-11-28 00:08:47 +0900125// unique_ptr<AidlTypeSpecifier> for type arugment,
126// std::string for type parameter(T, U, and so on).
127template <typename T>
128class AidlParameterizable {
129 public:
130 AidlParameterizable(std::vector<T>* type_params) : type_params_(type_params) {}
131 virtual ~AidlParameterizable() = default;
132 bool IsGeneric() const { return type_params_ != nullptr; }
133 const std::vector<T>& GetTypeParameters() const { return *type_params_; }
134 bool CheckValid() const;
135
136 virtual const AidlNode& AsAidlNode() const = 0;
137
138 protected:
139 AidlParameterizable(const AidlParameterizable&);
140
141 private:
142 const unique_ptr<std::vector<T>> type_params_;
143 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,
Jiyong Parkbf5fd5c2020-06-05 19:48:05 +0900167 JAVA_PASSTHROUGH,
Jooyung Han90345002020-10-23 15:28:53 +0900168 JAVA_DERIVE,
Jeongik Chad0a10272020-08-06 16:33:36 +0900169 JAVA_ONLY_IMMUTABLE,
Devin Moorec7e47a32020-08-07 10:55:25 -0700170 FIXED_SIZE,
Jiyong Park27fd7fd2020-08-27 16:25:09 +0900171 DESCRIPTOR,
Andrei Homescue61feb52020-08-18 15:44:24 -0700172 RUST_DERIVE,
Steven Moreland0cea4aa2020-04-20 21:06:02 -0700173 };
174 static std::string TypeToString(Type type);
175
Andrei Onea9445fc62019-06-27 18:11:59 +0100176 static AidlAnnotation* Parse(
177 const AidlLocation& location, const string& name,
178 std::map<std::string, std::shared_ptr<AidlConstantValue>>* parameter_list);
Steven Moreland46e9da82018-07-27 15:45:29 -0700179
Steven Moreland3f658cf2018-08-20 13:40:54 -0700180 AidlAnnotation(const AidlAnnotation&) = default;
Steven Moreland3be75772018-08-20 13:27:43 -0700181 AidlAnnotation(AidlAnnotation&&) = default;
Jiyong Park68bc77a2018-07-19 19:00:45 +0900182 virtual ~AidlAnnotation() = default;
Andrei Onea9445fc62019-06-27 18:11:59 +0100183 bool CheckValid() const;
Steven Moreland3be75772018-08-20 13:27:43 -0700184
Jooyung Hand902a972020-10-23 17:32:44 +0900185 const string& GetName() const { return schema_.name; }
Steven Moreland0cea4aa2020-04-20 21:06:02 -0700186 const Type& GetType() const { return schema_.type; }
Jooyung Hand902a972020-10-23 17:32:44 +0900187 bool Repeatable() const { return schema_.repeatable; }
Daniel Norman37d43dd2019-09-09 17:22:34 -0700188 string ToString(const ConstantValueDecorator& decorator) const;
Andrei Onea9445fc62019-06-27 18:11:59 +0100189 std::map<std::string, std::string> AnnotationParams(
190 const ConstantValueDecorator& decorator) const;
Jiyong Parka6605ab2018-11-11 14:30:21 +0900191 const string& GetComments() const { return comments_; }
192 void SetComments(const string& comments) { comments_ = comments; }
Jiyong Park68bc77a2018-07-19 19:00:45 +0900193
194 private:
Steven Moreland0cea4aa2020-04-20 21:06:02 -0700195 struct Schema {
196 AidlAnnotation::Type type;
197
198 // text name in .aidl file, e.g. "nullable"
199 std::string name;
200
201 // map from param name -> value type
202 std::map<std::string, std::string> supported_parameters;
Jooyung Hand902a972020-10-23 17:32:44 +0900203
204 bool repeatable;
Steven Moreland0cea4aa2020-04-20 21:06:02 -0700205 };
206 static const std::vector<Schema>& AllSchemas();
207
208 AidlAnnotation(const AidlLocation& location, const Schema& schema,
Andrei Onea9445fc62019-06-27 18:11:59 +0100209 std::map<std::string, std::shared_ptr<AidlConstantValue>>&& parameters);
Steven Moreland0cea4aa2020-04-20 21:06:02 -0700210
211 const Schema& schema_;
Jiyong Parka6605ab2018-11-11 14:30:21 +0900212 string comments_;
Andrei Onea9445fc62019-06-27 18:11:59 +0100213 std::map<std::string, std::shared_ptr<AidlConstantValue>> parameters_;
Jiyong Park68bc77a2018-07-19 19:00:45 +0900214};
215
Steven Moreland3be75772018-08-20 13:27:43 -0700216static inline bool operator<(const AidlAnnotation& lhs, const AidlAnnotation& rhs) {
217 return lhs.GetName() < rhs.GetName();
218}
219static inline bool operator==(const AidlAnnotation& lhs, const AidlAnnotation& rhs) {
220 return lhs.GetName() == rhs.GetName();
221}
Jiyong Park3656c3c2018-08-01 20:02:01 +0900222
Casey Dahline7922932016-02-29 17:23:01 -0800223class AidlAnnotatable : public AidlNode {
Casey Dahlin0ee37582015-09-30 16:31:55 -0700224 public:
Steven Moreland46e9da82018-07-27 15:45:29 -0700225 AidlAnnotatable(const AidlLocation& location);
Steven Moreland3f658cf2018-08-20 13:40:54 -0700226
227 AidlAnnotatable(const AidlAnnotatable&) = default;
228 AidlAnnotatable(AidlAnnotatable&&) = default;
Casey Dahline7922932016-02-29 17:23:01 -0800229 virtual ~AidlAnnotatable() = default;
230
Artur Satayev91fe8712019-07-29 13:06:01 +0100231 void Annotate(vector<AidlAnnotation>&& annotations) {
232 for (auto& annotation : annotations) {
233 annotations_.emplace_back(std::move(annotation));
234 }
235 }
Jiyong Park68bc77a2018-07-19 19:00:45 +0900236 bool IsNullable() const;
Jiyong Park68bc77a2018-07-19 19:00:45 +0900237 bool IsUtf8InCpp() const;
Steven Morelanda57d0a62019-07-30 09:41:14 -0700238 bool IsVintfStability() const;
Jeongik Chad0a10272020-08-06 16:33:36 +0900239 bool IsJavaOnlyImmutable() const;
Devin Moorec7e47a32020-08-07 10:55:25 -0700240 bool IsFixedSize() const;
Jeongik Cha88f95a82020-01-15 13:02:16 +0900241 bool IsStableApiParcelable(Options::Language lang) const;
Makoto Onuki78a1c1c2020-03-04 16:57:23 -0800242 bool IsHide() const;
Jooyung Han90345002020-10-23 15:28:53 +0900243 const AidlAnnotation* JavaDerive() const;
Jiyong Park27fd7fd2020-08-27 16:25:09 +0900244 std::string GetDescriptor() const;
Andrei Onea9445fc62019-06-27 18:11:59 +0100245
Steven Moreland7e4b9502020-02-20 18:10:42 -0800246 void DumpAnnotations(CodeWriter* writer) const;
247
Andrei Onea9445fc62019-06-27 18:11:59 +0100248 const AidlAnnotation* UnsupportedAppUsage() const;
Andrei Homescue61feb52020-08-18 15:44:24 -0700249 const AidlAnnotation* RustDerive() const;
Daniel Norman716d3112019-09-10 13:11:56 -0700250 const AidlTypeSpecifier* BackingType(const AidlTypenames& typenames) const;
Jiyong Park68bc77a2018-07-19 19:00:45 +0900251 std::string ToString() const;
Casey Dahline7922932016-02-29 17:23:01 -0800252
Jiyong Parka6605ab2018-11-11 14:30:21 +0900253 const vector<AidlAnnotation>& GetAnnotations() const { return annotations_; }
Devin Moore24f68572020-02-26 13:20:59 -0800254 virtual bool CheckValid(const AidlTypenames&) const;
Jiyong Park3656c3c2018-08-01 20:02:01 +0900255
Steven Moreland181144c2020-04-20 19:57:56 -0700256 protected:
Steven Moreland0cea4aa2020-04-20 21:06:02 -0700257 virtual std::set<AidlAnnotation::Type> GetSupportedAnnotations() const = 0;
Steven Moreland181144c2020-04-20 19:57:56 -0700258
Casey Dahline7922932016-02-29 17:23:01 -0800259 private:
Jiyong Parka6605ab2018-11-11 14:30:21 +0900260 vector<AidlAnnotation> annotations_;
Casey Dahline7922932016-02-29 17:23:01 -0800261};
262
Jiyong Park1deecc32018-07-17 01:14:41 +0900263// AidlTypeSpecifier represents a reference to either a built-in type,
264// a defined type, or a variant (e.g., array of generic) of a type.
Jeongik Chadf76dc72019-11-28 00:08:47 +0900265class AidlTypeSpecifier final : public AidlAnnotatable,
266 public AidlParameterizable<unique_ptr<AidlTypeSpecifier>> {
Casey Dahline7922932016-02-29 17:23:01 -0800267 public:
Steven Moreland46e9da82018-07-27 15:45:29 -0700268 AidlTypeSpecifier(const AidlLocation& location, const string& unresolved_name, bool is_array,
269 vector<unique_ptr<AidlTypeSpecifier>>* type_params, const string& comments);
Jiyong Parkd59a10d2018-07-18 11:12:55 +0900270 virtual ~AidlTypeSpecifier() = default;
Casey Dahlin0ee37582015-09-30 16:31:55 -0700271
Steven Moreland3f658cf2018-08-20 13:40:54 -0700272 // Copy of this type which is not an array.
Jooyung Hand2fa0232020-10-19 02:51:41 +0900273 const AidlTypeSpecifier& ArrayBase() const;
Steven Moreland3f658cf2018-08-20 13:40:54 -0700274
Jiyong Park1deecc32018-07-17 01:14:41 +0900275 // Returns the full-qualified name of the base type.
276 // int -> int
277 // int[] -> int
278 // List<String> -> List
279 // IFoo -> foo.bar.IFoo (if IFoo is in package foo.bar)
280 const string& GetName() const {
281 if (IsResolved()) {
282 return fully_qualified_name_;
283 } else {
284 return GetUnresolvedName();
285 }
286 }
Casey Dahlin0ee37582015-09-30 16:31:55 -0700287
Jiyong Park1deecc32018-07-17 01:14:41 +0900288 // Returns string representation of this type specifier.
Artur Satayev91fe8712019-07-29 13:06:01 +0100289 // This is GetBaseTypeName() + array modifier or generic type parameters
Jiyong Park1deecc32018-07-17 01:14:41 +0900290 string ToString() const;
291
Jiyong Park02da7422018-07-16 16:00:26 +0900292 std::string Signature() const;
293
Jiyong Park1deecc32018-07-17 01:14:41 +0900294 const string& GetUnresolvedName() const { return unresolved_name_; }
295
Jeongik Cha997281d2020-01-16 15:23:59 +0900296 bool IsHidden() const;
297
Jiyong Park1deecc32018-07-17 01:14:41 +0900298 const string& GetComments() const { return comments_; }
299
Jeongik Cha1a7ab642019-07-29 17:31:02 +0900300 const std::vector<std::string> GetSplitName() const { return split_name_; }
301
Jiyong Parka6605ab2018-11-11 14:30:21 +0900302 void SetComments(const string& comment) { comments_ = comment; }
303
Jiyong Park1deecc32018-07-17 01:14:41 +0900304 bool IsResolved() const { return fully_qualified_name_ != ""; }
305
306 bool IsArray() const { return is_array_; }
307
Jiyong Park1deecc32018-07-17 01:14:41 +0900308 // Resolve the base type name to a fully-qualified name. Return false if the
309 // resolution fails.
Daniel Norman716d3112019-09-10 13:11:56 -0700310 bool Resolve(const AidlTypenames& typenames);
Casey Dahlin0ee37582015-09-30 16:31:55 -0700311
Steven Moreland0cea4aa2020-04-20 21:06:02 -0700312 std::set<AidlAnnotation::Type> GetSupportedAnnotations() const override;
Devin Moore24f68572020-02-26 13:20:59 -0800313 bool CheckValid(const AidlTypenames& typenames) const override;
Steven Morelandd59e3172020-05-11 16:42:09 -0700314 bool LanguageSpecificCheckValid(const AidlTypenames& typenames, Options::Language lang) const;
Jeongik Chadf76dc72019-11-28 00:08:47 +0900315 const AidlNode& AsAidlNode() const override { return *this; }
Jiyong Park1d2df7d2018-07-23 15:22:50 +0900316
Jooyung Hane9bb9de2020-11-01 22:16:57 +0900317 const AidlDefinedType* GetDefinedType() const;
318
Casey Dahlin0ee37582015-09-30 16:31:55 -0700319 private:
Steven Moreland3f658cf2018-08-20 13:40:54 -0700320 AidlTypeSpecifier(const AidlTypeSpecifier&) = default;
321
Jiyong Park1deecc32018-07-17 01:14:41 +0900322 const string unresolved_name_;
323 string fully_qualified_name_;
Steven Moreland3f658cf2018-08-20 13:40:54 -0700324 bool is_array_;
Jiyong Parka6605ab2018-11-11 14:30:21 +0900325 string comments_;
Jeongik Cha1a7ab642019-07-29 17:31:02 +0900326 vector<string> split_name_;
Jooyung Hane9bb9de2020-11-01 22:16:57 +0900327 const AidlDefinedType* defined_type_; // set when Resolve() for defined types
Jooyung Hand2fa0232020-10-19 02:51:41 +0900328 mutable shared_ptr<AidlTypeSpecifier> array_base_;
Casey Dahlin0ee37582015-09-30 16:31:55 -0700329};
330
Steven Moreland860b1942018-08-16 14:59:28 -0700331// Returns the universal value unaltered.
332std::string AidlConstantValueDecorator(const AidlTypeSpecifier& type, const std::string& raw_value);
333
Steven Moreland9ea10e32018-07-19 15:26:09 -0700334class AidlConstantValue;
Steven Moreland541788d2020-05-21 22:05:52 +0000335// TODO: This class is used for method arguments and also parcelable fields,
336// and it should be split up since default values don't apply to method
337// arguments
Steven Moreland5557f1c2018-07-02 13:50:23 -0700338class AidlVariableDeclaration : public AidlNode {
339 public:
Steven Moreland46e9da82018-07-27 15:45:29 -0700340 AidlVariableDeclaration(const AidlLocation& location, AidlTypeSpecifier* type,
341 const std::string& name);
342 AidlVariableDeclaration(const AidlLocation& location, AidlTypeSpecifier* type,
343 const std::string& name, AidlConstantValue* default_value);
Steven Moreland5557f1c2018-07-02 13:50:23 -0700344 virtual ~AidlVariableDeclaration() = default;
345
Jiyong Parkd800fef2020-07-22 18:09:43 +0900346 // non-copyable, non-movable
347 AidlVariableDeclaration(const AidlVariableDeclaration&) = delete;
348 AidlVariableDeclaration(AidlVariableDeclaration&&) = delete;
349 AidlVariableDeclaration& operator=(const AidlVariableDeclaration&) = delete;
350 AidlVariableDeclaration& operator=(AidlVariableDeclaration&&) = delete;
351
Steven Moreland5557f1c2018-07-02 13:50:23 -0700352 std::string GetName() const { return name_; }
Jooyung Hanacae85d2020-10-28 16:39:09 +0900353 std::string GetCapitalizedName() const;
Jiyong Parkd59a10d2018-07-18 11:12:55 +0900354 const AidlTypeSpecifier& GetType() const { return *type_; }
Steven Moreland541788d2020-05-21 22:05:52 +0000355 // if this was constructed explicitly with a default value
356 bool IsDefaultUserSpecified() const { return default_user_specified_; }
357 // will return the default value this is constructed with or a default value
358 // if one is available
Steven Moreland9ea10e32018-07-19 15:26:09 -0700359 const AidlConstantValue* GetDefaultValue() const { return default_value_.get(); }
360
Jiyong Parkd59a10d2018-07-18 11:12:55 +0900361 AidlTypeSpecifier* GetMutableType() { return type_.get(); }
Steven Moreland5557f1c2018-07-02 13:50:23 -0700362
Jeongik Chadb0f59e2018-11-01 18:11:21 +0900363 bool CheckValid(const AidlTypenames& typenames) const;
Steven Moreland5557f1c2018-07-02 13:50:23 -0700364 std::string ToString() const;
Jiyong Park02da7422018-07-16 16:00:26 +0900365 std::string Signature() const;
Steven Moreland5557f1c2018-07-02 13:50:23 -0700366
Steven Moreland860b1942018-08-16 14:59:28 -0700367 std::string ValueString(const ConstantValueDecorator& decorator) const;
Steven Moreland25294322018-08-07 18:13:55 -0700368
Steven Moreland5557f1c2018-07-02 13:50:23 -0700369 private:
Jiyong Parkd59a10d2018-07-18 11:12:55 +0900370 std::unique_ptr<AidlTypeSpecifier> type_;
Steven Moreland5557f1c2018-07-02 13:50:23 -0700371 std::string name_;
Steven Moreland541788d2020-05-21 22:05:52 +0000372 bool default_user_specified_;
Steven Moreland9ea10e32018-07-19 15:26:09 -0700373 std::unique_ptr<AidlConstantValue> default_value_;
Steven Moreland5557f1c2018-07-02 13:50:23 -0700374};
375
376class AidlArgument : public AidlVariableDeclaration {
Casey Dahlinbc7a50a2015-09-28 19:20:50 -0700377 public:
Casey Dahlinc378c992015-09-29 16:50:40 -0700378 enum Direction { IN_DIR = 1, OUT_DIR = 2, INOUT_DIR = 3 };
379
Steven Moreland46e9da82018-07-27 15:45:29 -0700380 AidlArgument(const AidlLocation& location, AidlArgument::Direction direction,
381 AidlTypeSpecifier* type, const std::string& name);
382 AidlArgument(const AidlLocation& location, AidlTypeSpecifier* type, const std::string& name);
Casey Dahlinbc7a50a2015-09-28 19:20:50 -0700383 virtual ~AidlArgument() = default;
384
Jiyong Parkd800fef2020-07-22 18:09:43 +0900385 // non-copyable, non-movable
386 AidlArgument(const AidlArgument&) = delete;
387 AidlArgument(AidlArgument&&) = delete;
388 AidlArgument& operator=(const AidlArgument&) = delete;
389 AidlArgument& operator=(AidlArgument&&) = delete;
390
Casey Dahlinc378c992015-09-29 16:50:40 -0700391 Direction GetDirection() const { return direction_; }
Christopher Wileyad339272015-10-05 19:11:58 -0700392 bool IsOut() const { return direction_ & OUT_DIR; }
393 bool IsIn() const { return direction_ & IN_DIR; }
Casey Dahlinc378c992015-09-29 16:50:40 -0700394 bool DirectionWasSpecified() const { return direction_specified_; }
Jiyong Park3656c3c2018-08-01 20:02:01 +0900395 string GetDirectionSpecifier() const;
Christopher Wileyad339272015-10-05 19:11:58 -0700396
Casey Dahlinc378c992015-09-29 16:50:40 -0700397 std::string ToString() const;
Jiyong Park02da7422018-07-16 16:00:26 +0900398 std::string Signature() const;
Casey Dahlinc378c992015-09-29 16:50:40 -0700399
Casey Dahlinbc7a50a2015-09-28 19:20:50 -0700400 private:
Casey Dahlinc378c992015-09-29 16:50:40 -0700401 Direction direction_;
402 bool direction_specified_;
Casey Dahlina834dd42015-09-23 11:52:15 -0700403};
Adam Lesinskiffa16862014-01-23 18:17:42 -0800404
Casey Dahlind40e2fe2015-11-24 14:06:52 -0800405class AidlMethod;
Steven Moreland693640b2018-07-19 13:46:27 -0700406class AidlConstantDeclaration;
Daniel Norman85aed542019-08-21 12:01:14 -0700407class AidlEnumDeclaration;
Casey Dahlind40e2fe2015-11-24 14:06:52 -0800408class AidlMember : public AidlNode {
409 public:
Steven Moreland46e9da82018-07-27 15:45:29 -0700410 AidlMember(const AidlLocation& location);
Casey Dahlind40e2fe2015-11-24 14:06:52 -0800411 virtual ~AidlMember() = default;
412
Jiyong Parkd800fef2020-07-22 18:09:43 +0900413 // non-copyable, non-movable
414 AidlMember(const AidlMember&) = delete;
415 AidlMember(AidlMember&&) = delete;
416 AidlMember& operator=(const AidlMember&) = delete;
417 AidlMember& operator=(AidlMember&&) = delete;
418
Casey Dahlind40e2fe2015-11-24 14:06:52 -0800419 virtual AidlMethod* AsMethod() { return nullptr; }
Steven Moreland693640b2018-07-19 13:46:27 -0700420 virtual AidlConstantDeclaration* AsConstantDeclaration() { return nullptr; }
Casey Dahlind40e2fe2015-11-24 14:06:52 -0800421};
422
Will McVickerd7d18df2019-09-12 13:40:50 -0700423class AidlUnaryConstExpression;
424class AidlBinaryConstExpression;
425
Steven Moreland693640b2018-07-19 13:46:27 -0700426class AidlConstantValue : public AidlNode {
Casey Dahlind40e2fe2015-11-24 14:06:52 -0800427 public:
Will McVickerd7d18df2019-09-12 13:40:50 -0700428 enum class Type {
429 // WARNING: Don't change this order! The order is used to determine type
430 // promotion during a binary expression.
431 BOOLEAN,
432 INT8,
433 INT32,
434 INT64,
435 ARRAY,
436 CHARACTER,
437 STRING,
438 FLOATING,
439 UNARY,
440 BINARY,
441 ERROR,
442 };
443
444 /*
445 * Return the value casted to the given type.
446 */
447 template <typename T>
448 T cast() const;
Casey Dahlind40e2fe2015-11-24 14:06:52 -0800449
Steven Moreland693640b2018-07-19 13:46:27 -0700450 virtual ~AidlConstantValue() = default;
Casey Dahlind40e2fe2015-11-24 14:06:52 -0800451
Jiyong Parkd800fef2020-07-22 18:09:43 +0900452 // non-copyable, non-movable
453 AidlConstantValue(const AidlConstantValue&) = delete;
454 AidlConstantValue(AidlConstantValue&&) = delete;
455 AidlConstantValue& operator=(const AidlConstantValue&) = delete;
456 AidlConstantValue& operator=(AidlConstantValue&&) = delete;
457
Steven Moreland541788d2020-05-21 22:05:52 +0000458 // creates default value, when one isn't specified
459 // nullptr if no default available
460 static AidlConstantValue* Default(const AidlTypeSpecifier& specifier);
461
Steven Moreland25294322018-08-07 18:13:55 -0700462 static AidlConstantValue* Boolean(const AidlLocation& location, bool value);
463 static AidlConstantValue* Character(const AidlLocation& location, char value);
Steven Moreland25294322018-08-07 18:13:55 -0700464 // example: 123, -5498, maybe any size
Will McVickerd7d18df2019-09-12 13:40:50 -0700465 static AidlConstantValue* Integral(const AidlLocation& location, const string& value);
466 static AidlConstantValue* Floating(const AidlLocation& location, const std::string& value);
Steven Moreland860b1942018-08-16 14:59:28 -0700467 static AidlConstantValue* Array(const AidlLocation& location,
Will McVickerd7d18df2019-09-12 13:40:50 -0700468 std::unique_ptr<vector<unique_ptr<AidlConstantValue>>> values);
Steven Moreland693640b2018-07-19 13:46:27 -0700469 // example: "\"asdf\""
Will McVickerd7d18df2019-09-12 13:40:50 -0700470 static AidlConstantValue* String(const AidlLocation& location, const string& value);
Steven Moreland693640b2018-07-19 13:46:27 -0700471
Daniel Normanf0ca44f2019-10-25 09:59:44 -0700472 // Construct an AidlConstantValue by evaluating the other integral constant's
473 // value string. This does not preserve the structure of the copied constant.
Steven Moreland59e53e42019-11-26 20:38:08 -0800474 // Returns nullptr and logs if value cannot be copied.
Daniel Normanf0ca44f2019-10-25 09:59:44 -0700475 static AidlConstantValue* ShallowIntegralCopy(const AidlConstantValue& other);
Daniel Normanb28684e2019-10-17 15:31:39 -0700476
Will McVickerd7d18df2019-09-12 13:40:50 -0700477 Type GetType() const { return final_type_; }
Steven Moreland25294322018-08-07 18:13:55 -0700478
Will McVickerd7d18df2019-09-12 13:40:50 -0700479 virtual bool CheckValid() const;
Steven Moreland860b1942018-08-16 14:59:28 -0700480
481 // Raw value of type (currently valid in C++ and Java). Empty string on error.
Steven Moreland4bcb05c2019-11-27 18:57:47 -0800482 string ValueString(const AidlTypeSpecifier& type, const ConstantValueDecorator& decorator) const;
Casey Dahlind40e2fe2015-11-24 14:06:52 -0800483
484 private:
Will McVickerd7d18df2019-09-12 13:40:50 -0700485 AidlConstantValue(const AidlLocation& location, Type parsed_type, int64_t parsed_value,
486 const string& checked_value);
487 AidlConstantValue(const AidlLocation& location, Type type, const string& checked_value);
Steven Moreland860b1942018-08-16 14:59:28 -0700488 AidlConstantValue(const AidlLocation& location, Type type,
Will McVickerd7d18df2019-09-12 13:40:50 -0700489 std::unique_ptr<vector<unique_ptr<AidlConstantValue>>> values);
Steven Moreland25294322018-08-07 18:13:55 -0700490 static string ToString(Type type);
Will McVickerd7d18df2019-09-12 13:40:50 -0700491 static bool ParseIntegral(const string& value, int64_t* parsed_value, Type* parsed_type);
492 static bool IsHex(const string& value);
Steven Moreland4bcb05c2019-11-27 18:57:47 -0800493
Will McVickerd7d18df2019-09-12 13:40:50 -0700494 virtual bool evaluate(const AidlTypeSpecifier& type) const;
Casey Dahlind40e2fe2015-11-24 14:06:52 -0800495
Steven Moreland693640b2018-07-19 13:46:27 -0700496 const Type type_ = Type::ERROR;
Will McVickerd7d18df2019-09-12 13:40:50 -0700497 const vector<unique_ptr<AidlConstantValue>> values_; // if type_ == ARRAY
498 const string value_; // otherwise
499
500 // State for tracking evaluation of expressions
Steven Moreland4bcb05c2019-11-27 18:57:47 -0800501 mutable bool is_valid_ = false; // cache of CheckValid, but may be marked false in evaluate
502 mutable bool is_evaluated_ = false; // whether evaluate has been called
Will McVickerd7d18df2019-09-12 13:40:50 -0700503 mutable Type final_type_;
504 mutable int64_t final_value_;
505 mutable string final_string_value_ = "";
Steven Moreland693640b2018-07-19 13:46:27 -0700506
Will McVickerd7d18df2019-09-12 13:40:50 -0700507 friend AidlUnaryConstExpression;
508 friend AidlBinaryConstExpression;
509};
510
511class AidlUnaryConstExpression : public AidlConstantValue {
512 public:
513 AidlUnaryConstExpression(const AidlLocation& location, const string& op,
514 std::unique_ptr<AidlConstantValue> rval);
515
516 static bool IsCompatibleType(Type type, const string& op);
517 bool CheckValid() const override;
Will McVickerd7d18df2019-09-12 13:40:50 -0700518 private:
519 bool evaluate(const AidlTypeSpecifier& type) const override;
520
521 std::unique_ptr<AidlConstantValue> unary_;
522 const string op_;
523};
524
525class AidlBinaryConstExpression : public AidlConstantValue {
526 public:
527 AidlBinaryConstExpression(const AidlLocation& location, std::unique_ptr<AidlConstantValue> lval,
528 const string& op, std::unique_ptr<AidlConstantValue> rval);
529
530 bool CheckValid() const override;
Will McVickerd7d18df2019-09-12 13:40:50 -0700531
532 static bool AreCompatibleTypes(Type t1, Type t2);
533 // Returns the promoted kind for both operands
534 static Type UsualArithmeticConversion(Type left, Type right);
535 // Returns the promoted integral type where INT32 is the smallest type
536 static Type IntegralPromotion(Type in);
537
538 private:
539 bool evaluate(const AidlTypeSpecifier& type) const override;
540
541 std::unique_ptr<AidlConstantValue> left_val_;
542 std::unique_ptr<AidlConstantValue> right_val_;
543 const string op_;
Christopher Wileyd6bdd8d2016-05-03 11:23:13 -0700544};
545
Andrei Onea9445fc62019-06-27 18:11:59 +0100546struct AidlAnnotationParameter {
547 std::string name;
548 std::unique_ptr<AidlConstantValue> value;
549};
550
Steven Moreland693640b2018-07-19 13:46:27 -0700551class AidlConstantDeclaration : public AidlMember {
Christopher Wileyd6bdd8d2016-05-03 11:23:13 -0700552 public:
Steven Moreland46e9da82018-07-27 15:45:29 -0700553 AidlConstantDeclaration(const AidlLocation& location, AidlTypeSpecifier* specifier,
Will McVickerd7d18df2019-09-12 13:40:50 -0700554 const string& name, AidlConstantValue* value);
Steven Moreland693640b2018-07-19 13:46:27 -0700555 virtual ~AidlConstantDeclaration() = default;
Christopher Wileyd6bdd8d2016-05-03 11:23:13 -0700556
Jiyong Parkd800fef2020-07-22 18:09:43 +0900557 // non-copyable, non-movable
558 AidlConstantDeclaration(const AidlConstantDeclaration&) = delete;
559 AidlConstantDeclaration(AidlConstantDeclaration&&) = delete;
560 AidlConstantDeclaration& operator=(const AidlConstantDeclaration&) = delete;
561 AidlConstantDeclaration& operator=(AidlConstantDeclaration&&) = delete;
562
Steven Moreland693640b2018-07-19 13:46:27 -0700563 const AidlTypeSpecifier& GetType() const { return *type_; }
Steven Moreland4d12f9a2018-10-31 14:30:55 -0700564 AidlTypeSpecifier* GetMutableType() { return type_.get(); }
Will McVickerd7d18df2019-09-12 13:40:50 -0700565 const string& GetName() const { return name_; }
Steven Moreland693640b2018-07-19 13:46:27 -0700566 const AidlConstantValue& GetValue() const { return *value_; }
Jeongik Chadb0f59e2018-11-01 18:11:21 +0900567 bool CheckValid(const AidlTypenames& typenames) const;
Christopher Wileyd6bdd8d2016-05-03 11:23:13 -0700568
Will McVickerd7d18df2019-09-12 13:40:50 -0700569 string ToString() const;
570 string Signature() const;
Steven Moreland860b1942018-08-16 14:59:28 -0700571 string ValueString(const ConstantValueDecorator& decorator) const {
Will McVickerd7d18df2019-09-12 13:40:50 -0700572 return value_->ValueString(GetType(), decorator);
Steven Moreland860b1942018-08-16 14:59:28 -0700573 }
Steven Moreland25294322018-08-07 18:13:55 -0700574
Steven Moreland693640b2018-07-19 13:46:27 -0700575 AidlConstantDeclaration* AsConstantDeclaration() override { return this; }
Christopher Wileyd6bdd8d2016-05-03 11:23:13 -0700576
577 private:
Steven Moreland693640b2018-07-19 13:46:27 -0700578 const unique_ptr<AidlTypeSpecifier> type_;
Will McVickerd7d18df2019-09-12 13:40:50 -0700579 const string name_;
580 unique_ptr<AidlConstantValue> value_;
Casey Dahlind40e2fe2015-11-24 14:06:52 -0800581};
582
583class AidlMethod : public AidlMember {
Casey Dahlin5c69deb2015-10-01 14:44:12 -0700584 public:
Will McVickerd7d18df2019-09-12 13:40:50 -0700585 AidlMethod(const AidlLocation& location, bool oneway, AidlTypeSpecifier* type, const string& name,
586 vector<unique_ptr<AidlArgument>>* args, const string& comments);
587 AidlMethod(const AidlLocation& location, bool oneway, AidlTypeSpecifier* type, const string& name,
588 vector<unique_ptr<AidlArgument>>* args, const string& comments, int id,
589 bool is_user_defined = true);
Casey Dahlin5c69deb2015-10-01 14:44:12 -0700590 virtual ~AidlMethod() = default;
591
Jiyong Parkd800fef2020-07-22 18:09:43 +0900592 // non-copyable, non-movable
593 AidlMethod(const AidlMethod&) = delete;
594 AidlMethod(AidlMethod&&) = delete;
595 AidlMethod& operator=(const AidlMethod&) = delete;
596 AidlMethod& operator=(AidlMethod&&) = delete;
597
Casey Dahlind40e2fe2015-11-24 14:06:52 -0800598 AidlMethod* AsMethod() override { return this; }
Jeongik Cha997281d2020-01-16 15:23:59 +0900599 bool IsHidden() const;
Will McVickerd7d18df2019-09-12 13:40:50 -0700600 const string& GetComments() const { return comments_; }
Jiyong Parkd59a10d2018-07-18 11:12:55 +0900601 const AidlTypeSpecifier& GetType() const { return *type_; }
602 AidlTypeSpecifier* GetMutableType() { return type_.get(); }
Steven Morelandacd53472018-12-14 10:17:26 -0800603
Steven Moreland8c70ba92018-12-17 10:20:31 -0800604 // set if this method is part of an interface that is marked oneway
605 void ApplyInterfaceOneway(bool oneway) { oneway_ = oneway_ || oneway; }
Casey Dahlinf4a93112015-10-05 16:58:09 -0700606 bool IsOneway() const { return oneway_; }
Steven Morelandacd53472018-12-14 10:17:26 -0800607
Casey Dahlinf4a93112015-10-05 16:58:09 -0700608 const std::string& GetName() const { return name_; }
Casey Dahlinf4a93112015-10-05 16:58:09 -0700609 bool HasId() const { return has_id_; }
Jiyong Parked65bf42018-08-28 15:43:27 +0900610 int GetId() const { return id_; }
Casey Dahlinf4a93112015-10-05 16:58:09 -0700611 void SetId(unsigned id) { id_ = id; }
Casey Dahlinf2d23f72015-10-02 16:19:19 -0700612
Jiyong Park309668e2018-07-28 16:55:44 +0900613 bool IsUserDefined() const { return is_user_defined_; }
614
Casey Dahlinf4a93112015-10-05 16:58:09 -0700615 const std::vector<std::unique_ptr<AidlArgument>>& GetArguments() const {
Christopher Wileyad339272015-10-05 19:11:58 -0700616 return arguments_;
617 }
618 // An inout parameter will appear in both GetInArguments()
619 // and GetOutArguments(). AidlMethod retains ownership of the argument
620 // pointers returned in this way.
621 const std::vector<const AidlArgument*>& GetInArguments() const {
622 return in_arguments_;
623 }
624 const std::vector<const AidlArgument*>& GetOutArguments() const {
625 return out_arguments_;
Casey Dahlinf4a93112015-10-05 16:58:09 -0700626 }
Casey Dahlin5c69deb2015-10-01 14:44:12 -0700627
Jiyong Park309668e2018-07-28 16:55:44 +0900628 // name + type parameter types
629 // i.e, foo(int, String)
Jiyong Park02da7422018-07-16 16:00:26 +0900630 std::string Signature() const;
631
Jiyong Park309668e2018-07-28 16:55:44 +0900632 // return type + name + type parameter types + annotations
633 // i.e, boolean foo(int, @Nullable String)
634 std::string ToString() const;
635
Casey Dahlin5c69deb2015-10-01 14:44:12 -0700636 private:
Casey Dahlinf4a93112015-10-05 16:58:09 -0700637 bool oneway_;
Casey Dahlinf2d23f72015-10-02 16:19:19 -0700638 std::string comments_;
Jiyong Parkd59a10d2018-07-18 11:12:55 +0900639 std::unique_ptr<AidlTypeSpecifier> type_;
Casey Dahlinf4a93112015-10-05 16:58:09 -0700640 std::string name_;
Christopher Wileyad339272015-10-05 19:11:58 -0700641 const std::vector<std::unique_ptr<AidlArgument>> arguments_;
642 std::vector<const AidlArgument*> in_arguments_;
643 std::vector<const AidlArgument*> out_arguments_;
Casey Dahlinf4a93112015-10-05 16:58:09 -0700644 bool has_id_;
645 int id_;
Jiyong Park309668e2018-07-28 16:55:44 +0900646 bool is_user_defined_ = true;
Casey Dahlin0a2f8be2015-09-28 16:15:29 -0700647};
Adam Lesinskiffa16862014-01-23 18:17:42 -0800648
Steven Morelandc258abc2018-07-10 14:03:38 -0700649class AidlDefinedType;
Jiyong Parkb034bf02018-07-30 17:44:33 +0900650class AidlInterface;
651class AidlParcelable;
652class AidlStructuredParcelable;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800653
Steven Moreland46e9da82018-07-27 15:45:29 -0700654class AidlInterface;
655class AidlParcelable;
656class AidlStructuredParcelable;
Jooyung Han2946afc2020-10-05 20:29:16 +0900657
658class AidlUnionDecl;
Daniel Norman85aed542019-08-21 12:01:14 -0700659// AidlDefinedType represents either an interface, parcelable, or enum that is
Jiyong Park1deecc32018-07-17 01:14:41 +0900660// defined in the source file.
661class AidlDefinedType : public AidlAnnotatable {
Steven Moreland787b0432018-07-03 09:00:58 -0700662 public:
Steven Moreland46e9da82018-07-27 15:45:29 -0700663 AidlDefinedType(const AidlLocation& location, const std::string& name,
Jiyong Park18132182020-06-08 20:24:40 +0900664 const std::string& comments, const std::string& package);
Steven Moreland787b0432018-07-03 09:00:58 -0700665 virtual ~AidlDefinedType() = default;
666
Jiyong Parkd800fef2020-07-22 18:09:43 +0900667 // non-copyable, non-movable
668 AidlDefinedType(const AidlDefinedType&) = delete;
669 AidlDefinedType(AidlDefinedType&&) = delete;
670 AidlDefinedType& operator=(const AidlDefinedType&) = delete;
671 AidlDefinedType& operator=(AidlDefinedType&&) = delete;
672
Jiyong Park1deecc32018-07-17 01:14:41 +0900673 const std::string& GetName() const { return name_; };
Jeongik Cha997281d2020-01-16 15:23:59 +0900674 bool IsHidden() const;
Jiyong Park1deecc32018-07-17 01:14:41 +0900675 const std::string& GetComments() const { return comments_; }
Jiyong Parka6605ab2018-11-11 14:30:21 +0900676 void SetComments(const std::string comments) { comments_ = comments; }
Jiyong Park1deecc32018-07-17 01:14:41 +0900677
Steven Moreland787b0432018-07-03 09:00:58 -0700678 /* dot joined package, example: "android.package.foo" */
Jiyong Park18132182020-06-08 20:24:40 +0900679 std::string GetPackage() const { return package_; }
Steven Moreland787b0432018-07-03 09:00:58 -0700680 /* dot joined package and name, example: "android.package.foo.IBar" */
681 std::string GetCanonicalName() const;
Jiyong Park18132182020-06-08 20:24:40 +0900682 const std::vector<std::string>& GetSplitPackage() const { return split_package_; }
Steven Moreland787b0432018-07-03 09:00:58 -0700683
Steven Morelanded83a282018-07-17 13:27:29 -0700684 virtual std::string GetPreprocessDeclarationName() const = 0;
Steven Morelandc258abc2018-07-10 14:03:38 -0700685
Steven Moreland5557f1c2018-07-02 13:50:23 -0700686 virtual const AidlStructuredParcelable* AsStructuredParcelable() const { return nullptr; }
Steven Morelandc258abc2018-07-10 14:03:38 -0700687 virtual const AidlParcelable* AsParcelable() const { return nullptr; }
Daniel Norman85aed542019-08-21 12:01:14 -0700688 virtual const AidlEnumDeclaration* AsEnumDeclaration() const { return nullptr; }
Jooyung Han2946afc2020-10-05 20:29:16 +0900689 virtual const AidlUnionDecl* AsUnionDeclaration() const { return nullptr; }
Steven Moreland5557f1c2018-07-02 13:50:23 -0700690 virtual const AidlInterface* AsInterface() const { return nullptr; }
Jeongik Chadf76dc72019-11-28 00:08:47 +0900691 virtual const AidlParameterizable<std::string>* AsParameterizable() const { return nullptr; }
Devin Moore24f68572020-02-26 13:20:59 -0800692 bool CheckValid(const AidlTypenames& typenames) const override;
Steven Morelandd59e3172020-05-11 16:42:09 -0700693 virtual bool LanguageSpecificCheckValid(const AidlTypenames& typenames,
694 Options::Language lang) const = 0;
Steven Morelandc258abc2018-07-10 14:03:38 -0700695 AidlStructuredParcelable* AsStructuredParcelable() {
696 return const_cast<AidlStructuredParcelable*>(
697 const_cast<const AidlDefinedType*>(this)->AsStructuredParcelable());
698 }
699 AidlParcelable* AsParcelable() {
700 return const_cast<AidlParcelable*>(const_cast<const AidlDefinedType*>(this)->AsParcelable());
701 }
Daniel Norman85aed542019-08-21 12:01:14 -0700702 AidlEnumDeclaration* AsEnumDeclaration() {
703 return const_cast<AidlEnumDeclaration*>(
704 const_cast<const AidlDefinedType*>(this)->AsEnumDeclaration());
705 }
Jooyung Han2946afc2020-10-05 20:29:16 +0900706 AidlUnionDecl* AsUnionDeclaration() {
707 return const_cast<AidlUnionDecl*>(
708 const_cast<const AidlDefinedType*>(this)->AsUnionDeclaration());
709 }
Steven Morelandc258abc2018-07-10 14:03:38 -0700710 AidlInterface* AsInterface() {
711 return const_cast<AidlInterface*>(const_cast<const AidlDefinedType*>(this)->AsInterface());
712 }
713
Jeongik Chadf76dc72019-11-28 00:08:47 +0900714 AidlParameterizable<std::string>* AsParameterizable() {
715 return const_cast<AidlParameterizable<std::string>*>(
716 const_cast<const AidlDefinedType*>(this)->AsParameterizable());
717 }
718
Steven Moreland6cee3482018-07-18 14:39:58 -0700719 const AidlParcelable* AsUnstructuredParcelable() const {
720 if (this->AsStructuredParcelable() != nullptr) return nullptr;
Jooyung Han2946afc2020-10-05 20:29:16 +0900721 if (this->AsUnionDeclaration() != nullptr) return nullptr;
Steven Moreland6cee3482018-07-18 14:39:58 -0700722 return this->AsParcelable();
723 }
724 AidlParcelable* AsUnstructuredParcelable() {
725 return const_cast<AidlParcelable*>(
726 const_cast<const AidlDefinedType*>(this)->AsUnstructuredParcelable());
727 }
728
Jeongik Cha997281d2020-01-16 15:23:59 +0900729 virtual void Dump(CodeWriter* writer) const = 0;
Steven Morelanda5d9c5c2020-02-21 16:01:09 -0800730 void DumpHeader(CodeWriter* writer) const;
Jiyong Park02da7422018-07-16 16:00:26 +0900731
Steven Moreland787b0432018-07-03 09:00:58 -0700732 private:
Jiyong Park1deecc32018-07-17 01:14:41 +0900733 std::string name_;
Jiyong Park1deecc32018-07-17 01:14:41 +0900734 std::string comments_;
Jiyong Park18132182020-06-08 20:24:40 +0900735 const std::string package_;
736 const std::vector<std::string> split_package_;
Steven Moreland787b0432018-07-03 09:00:58 -0700737};
738
Jeongik Chadf76dc72019-11-28 00:08:47 +0900739class AidlParcelable : public AidlDefinedType, public AidlParameterizable<std::string> {
Casey Dahlin1ae2bc52015-10-07 18:49:10 -0700740 public:
Jiyong Park18132182020-06-08 20:24:40 +0900741 AidlParcelable(const AidlLocation& location, const std::string& name, const std::string& package,
742 const std::string& comments, const std::string& cpp_header = "",
Jeongik Chadf76dc72019-11-28 00:08:47 +0900743 std::vector<std::string>* type_params = nullptr);
Casey Dahlin1ae2bc52015-10-07 18:49:10 -0700744 virtual ~AidlParcelable() = default;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800745
Jiyong Parkd800fef2020-07-22 18:09:43 +0900746 // non-copyable, non-movable
747 AidlParcelable(const AidlParcelable&) = delete;
748 AidlParcelable(AidlParcelable&&) = delete;
749 AidlParcelable& operator=(const AidlParcelable&) = delete;
750 AidlParcelable& operator=(AidlParcelable&&) = delete;
751
Christopher Wiley8aa4d9f2015-11-16 19:10:45 -0800752 std::string GetCppHeader() const { return cpp_header_; }
Christopher Wiley8aa4d9f2015-11-16 19:10:45 -0800753
Steven Moreland0cea4aa2020-04-20 21:06:02 -0700754 std::set<AidlAnnotation::Type> GetSupportedAnnotations() const override;
Jeongik Cha82317dd2019-02-27 20:26:11 +0900755 bool CheckValid(const AidlTypenames& typenames) const override;
Steven Morelandd59e3172020-05-11 16:42:09 -0700756 bool LanguageSpecificCheckValid(const AidlTypenames& typenames,
757 Options::Language lang) const override;
Steven Morelandc258abc2018-07-10 14:03:38 -0700758 const AidlParcelable* AsParcelable() const override { return this; }
Jeongik Chadf76dc72019-11-28 00:08:47 +0900759 const AidlParameterizable<std::string>* AsParameterizable() const override { return this; }
760 const AidlNode& AsAidlNode() const override { return *this; }
Steven Morelanded83a282018-07-17 13:27:29 -0700761 std::string GetPreprocessDeclarationName() const override { return "parcelable"; }
Steven Morelandc258abc2018-07-10 14:03:38 -0700762
Jeongik Cha997281d2020-01-16 15:23:59 +0900763 void Dump(CodeWriter* writer) const override;
Jiyong Park02da7422018-07-16 16:00:26 +0900764
Casey Dahlin1ae2bc52015-10-07 18:49:10 -0700765 private:
Christopher Wiley8aa4d9f2015-11-16 19:10:45 -0800766 std::string cpp_header_;
Casey Dahlin0a2f8be2015-09-28 16:15:29 -0700767};
Adam Lesinskiffa16862014-01-23 18:17:42 -0800768
Jooyung Han59af9cc2020-10-25 21:44:14 +0900769class AidlWithFields {
770 public:
771 AidlWithFields(std::vector<std::unique_ptr<AidlVariableDeclaration>>* variables)
772 : variables_(std::move(*variables)) {}
773
774 const std::vector<std::unique_ptr<AidlVariableDeclaration>>& GetFields() const {
775 return variables_;
776 }
777
778 protected:
779 bool CheckValid(const AidlParcelable& parcel, const AidlTypenames& typenames) const;
780 bool CheckValidForGetterNames(const AidlParcelable& parcel) const;
781
782 private:
783 const std::vector<std::unique_ptr<AidlVariableDeclaration>> variables_;
784};
785
786class AidlStructuredParcelable : public AidlParcelable, public AidlWithFields {
Steven Moreland5557f1c2018-07-02 13:50:23 -0700787 public:
Jiyong Park18132182020-06-08 20:24:40 +0900788 AidlStructuredParcelable(const AidlLocation& location, const std::string& name,
789 const std::string& package, const std::string& comments,
Devin Moore53fc99c2020-08-12 08:07:52 -0700790 std::vector<std::unique_ptr<AidlVariableDeclaration>>* variables,
791 std::vector<std::string>* type_params);
Jiyong Parkd800fef2020-07-22 18:09:43 +0900792 virtual ~AidlStructuredParcelable() = default;
793
794 // non-copyable, non-movable
795 AidlStructuredParcelable(const AidlStructuredParcelable&) = delete;
796 AidlStructuredParcelable(AidlStructuredParcelable&&) = delete;
797 AidlStructuredParcelable& operator=(const AidlStructuredParcelable&) = delete;
798 AidlStructuredParcelable& operator=(AidlStructuredParcelable&&) = delete;
Steven Moreland5557f1c2018-07-02 13:50:23 -0700799
Steven Morelandc258abc2018-07-10 14:03:38 -0700800 const AidlStructuredParcelable* AsStructuredParcelable() const override { return this; }
Steven Morelanded83a282018-07-17 13:27:29 -0700801 std::string GetPreprocessDeclarationName() const override { return "structured_parcelable"; }
Steven Moreland5557f1c2018-07-02 13:50:23 -0700802
Jeongik Cha997281d2020-01-16 15:23:59 +0900803 void Dump(CodeWriter* writer) const override;
Jiyong Park02da7422018-07-16 16:00:26 +0900804
Steven Moreland0cea4aa2020-04-20 21:06:02 -0700805 std::set<AidlAnnotation::Type> GetSupportedAnnotations() const override;
Jeongik Chadb0f59e2018-11-01 18:11:21 +0900806 bool CheckValid(const AidlTypenames& typenames) const override;
Steven Morelandd59e3172020-05-11 16:42:09 -0700807 bool LanguageSpecificCheckValid(const AidlTypenames& typenames,
808 Options::Language lang) const override;
Steven Moreland5557f1c2018-07-02 13:50:23 -0700809};
810
Daniel Norman85aed542019-08-21 12:01:14 -0700811class AidlEnumerator : public AidlNode {
812 public:
Daniel Norman2e4112d2019-10-03 10:22:35 -0700813 AidlEnumerator(const AidlLocation& location, const std::string& name, AidlConstantValue* value,
814 const std::string& comments);
Daniel Norman85aed542019-08-21 12:01:14 -0700815 virtual ~AidlEnumerator() = default;
816
Jiyong Parkd800fef2020-07-22 18:09:43 +0900817 // non-copyable, non-movable
818 AidlEnumerator(const AidlEnumerator&) = delete;
819 AidlEnumerator(AidlEnumerator&&) = delete;
820 AidlEnumerator& operator=(const AidlEnumerator&) = delete;
821 AidlEnumerator& operator=(AidlEnumerator&&) = delete;
822
Daniel Norman85aed542019-08-21 12:01:14 -0700823 const std::string& GetName() const { return name_; }
Will McVickerd7d18df2019-09-12 13:40:50 -0700824 AidlConstantValue* GetValue() const { return value_.get(); }
Daniel Norman2e4112d2019-10-03 10:22:35 -0700825 const std::string& GetComments() const { return comments_; }
Daniel Norman85aed542019-08-21 12:01:14 -0700826 bool CheckValid(const AidlTypeSpecifier& enum_backing_type) const;
827
828 string ValueString(const AidlTypeSpecifier& backing_type,
829 const ConstantValueDecorator& decorator) const;
830
Daniel Normanb28684e2019-10-17 15:31:39 -0700831 void SetValue(std::unique_ptr<AidlConstantValue> value) { value_ = std::move(value); }
832
Daniel Norman85aed542019-08-21 12:01:14 -0700833 private:
834 const std::string name_;
Will McVickerd7d18df2019-09-12 13:40:50 -0700835 unique_ptr<AidlConstantValue> value_;
Daniel Norman2e4112d2019-10-03 10:22:35 -0700836 const std::string comments_;
Daniel Norman85aed542019-08-21 12:01:14 -0700837};
838
839class AidlEnumDeclaration : public AidlDefinedType {
840 public:
Will McVickerd7d18df2019-09-12 13:40:50 -0700841 AidlEnumDeclaration(const AidlLocation& location, const string& name,
Daniel Norman85aed542019-08-21 12:01:14 -0700842 std::vector<std::unique_ptr<AidlEnumerator>>* enumerators,
Jiyong Park18132182020-06-08 20:24:40 +0900843 const std::string& package, const std::string& comments);
Daniel Norman85aed542019-08-21 12:01:14 -0700844 virtual ~AidlEnumDeclaration() = default;
845
Jiyong Parkd800fef2020-07-22 18:09:43 +0900846 // non-copyable, non-movable
847 AidlEnumDeclaration(const AidlEnumDeclaration&) = delete;
848 AidlEnumDeclaration(AidlEnumDeclaration&&) = delete;
849 AidlEnumDeclaration& operator=(const AidlEnumDeclaration&) = delete;
850 AidlEnumDeclaration& operator=(AidlEnumDeclaration&&) = delete;
851
Daniel Norman85aed542019-08-21 12:01:14 -0700852 void SetBackingType(std::unique_ptr<const AidlTypeSpecifier> type);
853 const AidlTypeSpecifier& GetBackingType() const { return *backing_type_; }
854 const std::vector<std::unique_ptr<AidlEnumerator>>& GetEnumerators() const {
855 return enumerators_;
856 }
Steven Moreland59e53e42019-11-26 20:38:08 -0800857 bool Autofill();
Steven Moreland0cea4aa2020-04-20 21:06:02 -0700858 std::set<AidlAnnotation::Type> GetSupportedAnnotations() const override;
Daniel Norman85aed542019-08-21 12:01:14 -0700859 bool CheckValid(const AidlTypenames& typenames) const override;
Steven Morelandd59e3172020-05-11 16:42:09 -0700860 bool LanguageSpecificCheckValid(const AidlTypenames& /*typenames*/,
861 Options::Language) const override {
862 return true;
863 }
Daniel Norman85aed542019-08-21 12:01:14 -0700864 std::string GetPreprocessDeclarationName() const override { return "enum"; }
Jeongik Cha997281d2020-01-16 15:23:59 +0900865 void Dump(CodeWriter* writer) const override;
Daniel Norman85aed542019-08-21 12:01:14 -0700866
867 const AidlEnumDeclaration* AsEnumDeclaration() const override { return this; }
868
869 private:
870 const std::string name_;
871 const std::vector<std::unique_ptr<AidlEnumerator>> enumerators_;
872 std::unique_ptr<const AidlTypeSpecifier> backing_type_;
Daniel Norman85aed542019-08-21 12:01:14 -0700873};
874
Jooyung Han59af9cc2020-10-25 21:44:14 +0900875class AidlUnionDecl : public AidlParcelable, public AidlWithFields {
Jooyung Han2946afc2020-10-05 20:29:16 +0900876 public:
877 AidlUnionDecl(const AidlLocation& location, const std::string& name, const std::string& package,
878 const std::string& comments,
879 std::vector<std::unique_ptr<AidlVariableDeclaration>>* variables,
880 std::vector<std::string>* type_params);
881 virtual ~AidlUnionDecl() = default;
882
883 // non-copyable, non-movable
884 AidlUnionDecl(const AidlUnionDecl&) = delete;
885 AidlUnionDecl(AidlUnionDecl&&) = delete;
886 AidlUnionDecl& operator=(const AidlUnionDecl&) = delete;
887 AidlUnionDecl& operator=(AidlUnionDecl&&) = delete;
888
889 std::set<AidlAnnotation::Type> GetSupportedAnnotations() const override;
890
891 const AidlNode& AsAidlNode() const override { return *this; }
892
Jooyung Hanfe89f122020-10-14 03:49:18 +0900893 bool CheckValid(const AidlTypenames& typenames) const override;
894 bool LanguageSpecificCheckValid(const AidlTypenames& typenames,
895 Options::Language lang) const override;
Jooyung Han2946afc2020-10-05 20:29:16 +0900896 std::string GetPreprocessDeclarationName() const override { return "union"; }
897
898 void Dump(CodeWriter* writer) const override;
899 const AidlUnionDecl* AsUnionDeclaration() const override { return this; }
Jooyung Han2946afc2020-10-05 20:29:16 +0900900};
901
Jiyong Park1deecc32018-07-17 01:14:41 +0900902class AidlInterface final : public AidlDefinedType {
Casey Dahlin1ae2bc52015-10-07 18:49:10 -0700903 public:
Steven Moreland46e9da82018-07-27 15:45:29 -0700904 AidlInterface(const AidlLocation& location, const std::string& name, const std::string& comments,
905 bool oneway_, std::vector<std::unique_ptr<AidlMember>>* members,
Jiyong Park18132182020-06-08 20:24:40 +0900906 const std::string& package);
Casey Dahlin1ae2bc52015-10-07 18:49:10 -0700907 virtual ~AidlInterface() = default;
908
Jiyong Parkd800fef2020-07-22 18:09:43 +0900909 // non-copyable, non-movable
910 AidlInterface(const AidlInterface&) = delete;
911 AidlInterface(AidlInterface&&) = delete;
912 AidlInterface& operator=(const AidlInterface&) = delete;
913 AidlInterface& operator=(AidlInterface&&) = delete;
914
Casey Dahlinfb7da2e2015-10-08 17:26:09 -0700915 const std::vector<std::unique_ptr<AidlMethod>>& GetMethods() const
916 { return methods_; }
Jiyong Park309668e2018-07-28 16:55:44 +0900917 std::vector<std::unique_ptr<AidlMethod>>& GetMutableMethods() { return methods_; }
Steven Moreland693640b2018-07-19 13:46:27 -0700918 const std::vector<std::unique_ptr<AidlConstantDeclaration>>& GetConstantDeclarations() const {
919 return constants_;
920 }
Casey Dahlina2f77c42015-12-01 18:26:02 -0800921
Steven Morelandc258abc2018-07-10 14:03:38 -0700922 const AidlInterface* AsInterface() const override { return this; }
Steven Morelanded83a282018-07-17 13:27:29 -0700923 std::string GetPreprocessDeclarationName() const override { return "interface"; }
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;
Jeongik Chadb0f59e2018-11-01 18:11:21 +0900928 bool CheckValid(const AidlTypenames& typenames) const override;
Steven Morelandd59e3172020-05-11 16:42:09 -0700929 bool LanguageSpecificCheckValid(const AidlTypenames& typenames,
930 Options::Language lang) const override;
Jeongik Chadb0f59e2018-11-01 18:11:21 +0900931
Jiyong Park27fd7fd2020-08-27 16:25:09 +0900932 std::string GetDescriptor() const;
933
Casey Dahlin1ae2bc52015-10-07 18:49:10 -0700934 private:
Casey Dahlinfb7da2e2015-10-08 17:26:09 -0700935 std::vector<std::unique_ptr<AidlMethod>> methods_;
Steven Moreland693640b2018-07-19 13:46:27 -0700936 std::vector<std::unique_ptr<AidlConstantDeclaration>> constants_;
Casey Dahlin0a2f8be2015-09-28 16:15:29 -0700937};
Adam Lesinskiffa16862014-01-23 18:17:42 -0800938
Casey Dahlin0edf3422015-10-07 12:34:59 -0700939class AidlImport : public AidlNode {
940 public:
Steven Moreland46e9da82018-07-27 15:45:29 -0700941 AidlImport(const AidlLocation& location, const std::string& needed_class);
Casey Dahlin0edf3422015-10-07 12:34:59 -0700942 virtual ~AidlImport() = default;
943
Jiyong Parkd800fef2020-07-22 18:09:43 +0900944 // non-copyable, non-movable
945 AidlImport(const AidlImport&) = delete;
946 AidlImport(AidlImport&&) = delete;
947 AidlImport& operator=(const AidlImport&) = delete;
948 AidlImport& operator=(AidlImport&&) = delete;
949
Casey Dahlin0edf3422015-10-07 12:34:59 -0700950 const std::string& GetNeededClass() const { return needed_class_; }
Casey Dahlin0edf3422015-10-07 12:34:59 -0700951
952 private:
Casey Dahlin0edf3422015-10-07 12:34:59 -0700953 std::string needed_class_;
Casey Dahline2507492015-09-14 17:11:20 -0700954};
955
Jiyong Park62515512020-06-08 15:57:11 +0900956// AidlDocument models an AIDL file
957class AidlDocument : public AidlNode {
958 public:
959 AidlDocument(const AidlLocation& location, std::vector<std::unique_ptr<AidlImport>>& imports,
Jiyong Park8e79b7f2020-07-20 20:52:38 +0900960 std::vector<std::unique_ptr<AidlDefinedType>>&& defined_types)
961 : AidlNode(location),
962 imports_(std::move(imports)),
963 defined_types_(std::move(defined_types)) {}
Jiyong Parkd800fef2020-07-22 18:09:43 +0900964 ~AidlDocument() = default;
965
966 // non-copyable, non-movable
Jiyong Park8e79b7f2020-07-20 20:52:38 +0900967 AidlDocument(const AidlDocument&) = delete;
968 AidlDocument(AidlDocument&&) = delete;
969 AidlDocument& operator=(const AidlDocument&) = delete;
970 AidlDocument& operator=(AidlDocument&&) = delete;
Jiyong Parkd800fef2020-07-22 18:09:43 +0900971
972 const std::vector<std::unique_ptr<AidlImport>>& Imports() const { return imports_; }
973 const std::vector<std::unique_ptr<AidlDefinedType>>& DefinedTypes() const {
974 return defined_types_;
975 }
Jiyong Park62515512020-06-08 15:57:11 +0900976
977 private:
978 const std::vector<std::unique_ptr<AidlImport>> imports_;
Jiyong Park8e79b7f2020-07-20 20:52:38 +0900979 const std::vector<std::unique_ptr<AidlDefinedType>> defined_types_;
Jiyong Park62515512020-06-08 15:57:11 +0900980};