blob: 74ef07d58d56f0381202710d62ef9abdffefb4fa [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 \
89 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,
Jiyong Park43113fb2020-07-20 16:26:19 +0900168 JAVA_DEBUG,
Jeongik Chad0a10272020-08-06 16:33:36 +0900169 JAVA_ONLY_IMMUTABLE,
Devin Moorec7e47a32020-08-07 10:55:25 -0700170 FIXED_SIZE,
Steven Moreland0cea4aa2020-04-20 21:06:02 -0700171 };
172 static std::string TypeToString(Type type);
173
Andrei Onea9445fc62019-06-27 18:11:59 +0100174 static AidlAnnotation* Parse(
175 const AidlLocation& location, const string& name,
176 std::map<std::string, std::shared_ptr<AidlConstantValue>>* parameter_list);
Steven Moreland46e9da82018-07-27 15:45:29 -0700177
Steven Moreland3f658cf2018-08-20 13:40:54 -0700178 AidlAnnotation(const AidlAnnotation&) = default;
Steven Moreland3be75772018-08-20 13:27:43 -0700179 AidlAnnotation(AidlAnnotation&&) = default;
Jiyong Park68bc77a2018-07-19 19:00:45 +0900180 virtual ~AidlAnnotation() = default;
Andrei Onea9445fc62019-06-27 18:11:59 +0100181 bool CheckValid() const;
Steven Moreland3be75772018-08-20 13:27:43 -0700182
Steven Moreland0cea4aa2020-04-20 21:06:02 -0700183 const string& GetName() const { return schema_.name; };
184 const Type& GetType() const { return schema_.type; }
Daniel Norman37d43dd2019-09-09 17:22:34 -0700185 string ToString(const ConstantValueDecorator& decorator) const;
Andrei Onea9445fc62019-06-27 18:11:59 +0100186 std::map<std::string, std::string> AnnotationParams(
187 const ConstantValueDecorator& decorator) const;
Jiyong Parka6605ab2018-11-11 14:30:21 +0900188 const string& GetComments() const { return comments_; }
189 void SetComments(const string& comments) { comments_ = comments; }
Jiyong Park68bc77a2018-07-19 19:00:45 +0900190
191 private:
Steven Moreland0cea4aa2020-04-20 21:06:02 -0700192 struct Schema {
193 AidlAnnotation::Type type;
194
195 // text name in .aidl file, e.g. "nullable"
196 std::string name;
197
198 // map from param name -> value type
199 std::map<std::string, std::string> supported_parameters;
200 };
201 static const std::vector<Schema>& AllSchemas();
202
203 AidlAnnotation(const AidlLocation& location, const Schema& schema,
Andrei Onea9445fc62019-06-27 18:11:59 +0100204 std::map<std::string, std::shared_ptr<AidlConstantValue>>&& parameters);
Steven Moreland0cea4aa2020-04-20 21:06:02 -0700205
206 const Schema& schema_;
Jiyong Parka6605ab2018-11-11 14:30:21 +0900207 string comments_;
Andrei Onea9445fc62019-06-27 18:11:59 +0100208 std::map<std::string, std::shared_ptr<AidlConstantValue>> parameters_;
Jiyong Park68bc77a2018-07-19 19:00:45 +0900209};
210
Steven Moreland3be75772018-08-20 13:27:43 -0700211static inline bool operator<(const AidlAnnotation& lhs, const AidlAnnotation& rhs) {
212 return lhs.GetName() < rhs.GetName();
213}
214static inline bool operator==(const AidlAnnotation& lhs, const AidlAnnotation& rhs) {
215 return lhs.GetName() == rhs.GetName();
216}
Jiyong Park3656c3c2018-08-01 20:02:01 +0900217
Casey Dahline7922932016-02-29 17:23:01 -0800218class AidlAnnotatable : public AidlNode {
Casey Dahlin0ee37582015-09-30 16:31:55 -0700219 public:
Steven Moreland46e9da82018-07-27 15:45:29 -0700220 AidlAnnotatable(const AidlLocation& location);
Steven Moreland3f658cf2018-08-20 13:40:54 -0700221
222 AidlAnnotatable(const AidlAnnotatable&) = default;
223 AidlAnnotatable(AidlAnnotatable&&) = default;
Casey Dahline7922932016-02-29 17:23:01 -0800224 virtual ~AidlAnnotatable() = default;
225
Artur Satayev91fe8712019-07-29 13:06:01 +0100226 void Annotate(vector<AidlAnnotation>&& annotations) {
227 for (auto& annotation : annotations) {
228 annotations_.emplace_back(std::move(annotation));
229 }
230 }
Jiyong Park68bc77a2018-07-19 19:00:45 +0900231 bool IsNullable() const;
Jiyong Park68bc77a2018-07-19 19:00:45 +0900232 bool IsUtf8InCpp() const;
Steven Morelanda57d0a62019-07-30 09:41:14 -0700233 bool IsVintfStability() const;
Jeongik Chad0a10272020-08-06 16:33:36 +0900234 bool IsJavaOnlyImmutable() const;
Devin Moorec7e47a32020-08-07 10:55:25 -0700235 bool IsFixedSize() const;
Jeongik Cha88f95a82020-01-15 13:02:16 +0900236 bool IsStableApiParcelable(Options::Language lang) const;
Makoto Onuki78a1c1c2020-03-04 16:57:23 -0800237 bool IsHide() const;
Jiyong Park43113fb2020-07-20 16:26:19 +0900238 bool IsJavaDebug() const;
Andrei Onea9445fc62019-06-27 18:11:59 +0100239
Steven Moreland7e4b9502020-02-20 18:10:42 -0800240 void DumpAnnotations(CodeWriter* writer) const;
241
Andrei Onea9445fc62019-06-27 18:11:59 +0100242 const AidlAnnotation* UnsupportedAppUsage() const;
Jiyong Parkbf5fd5c2020-06-05 19:48:05 +0900243 const AidlAnnotation* JavaPassthrough() const;
Daniel Norman716d3112019-09-10 13:11:56 -0700244 const AidlTypeSpecifier* BackingType(const AidlTypenames& typenames) const;
Jiyong Park68bc77a2018-07-19 19:00:45 +0900245 std::string ToString() const;
Casey Dahline7922932016-02-29 17:23:01 -0800246
Jiyong Parka6605ab2018-11-11 14:30:21 +0900247 const vector<AidlAnnotation>& GetAnnotations() const { return annotations_; }
Devin Moore24f68572020-02-26 13:20:59 -0800248 virtual bool CheckValid(const AidlTypenames&) const;
Jiyong Park3656c3c2018-08-01 20:02:01 +0900249
Steven Moreland181144c2020-04-20 19:57:56 -0700250 protected:
Steven Moreland0cea4aa2020-04-20 21:06:02 -0700251 virtual std::set<AidlAnnotation::Type> GetSupportedAnnotations() const = 0;
Steven Moreland181144c2020-04-20 19:57:56 -0700252
Casey Dahline7922932016-02-29 17:23:01 -0800253 private:
Jiyong Parka6605ab2018-11-11 14:30:21 +0900254 vector<AidlAnnotation> annotations_;
Casey Dahline7922932016-02-29 17:23:01 -0800255};
256
Jiyong Park1deecc32018-07-17 01:14:41 +0900257// AidlTypeSpecifier represents a reference to either a built-in type,
258// a defined type, or a variant (e.g., array of generic) of a type.
Jeongik Chadf76dc72019-11-28 00:08:47 +0900259class AidlTypeSpecifier final : public AidlAnnotatable,
260 public AidlParameterizable<unique_ptr<AidlTypeSpecifier>> {
Casey Dahline7922932016-02-29 17:23:01 -0800261 public:
Steven Moreland46e9da82018-07-27 15:45:29 -0700262 AidlTypeSpecifier(const AidlLocation& location, const string& unresolved_name, bool is_array,
263 vector<unique_ptr<AidlTypeSpecifier>>* type_params, const string& comments);
Jiyong Parkd59a10d2018-07-18 11:12:55 +0900264 virtual ~AidlTypeSpecifier() = default;
Casey Dahlin0ee37582015-09-30 16:31:55 -0700265
Steven Moreland3f658cf2018-08-20 13:40:54 -0700266 // Copy of this type which is not an array.
267 AidlTypeSpecifier ArrayBase() const;
268
Jiyong Park1deecc32018-07-17 01:14:41 +0900269 // Returns the full-qualified name of the base type.
270 // int -> int
271 // int[] -> int
272 // List<String> -> List
273 // IFoo -> foo.bar.IFoo (if IFoo is in package foo.bar)
274 const string& GetName() const {
275 if (IsResolved()) {
276 return fully_qualified_name_;
277 } else {
278 return GetUnresolvedName();
279 }
280 }
Casey Dahlin0ee37582015-09-30 16:31:55 -0700281
Jiyong Park1deecc32018-07-17 01:14:41 +0900282 // Returns string representation of this type specifier.
Artur Satayev91fe8712019-07-29 13:06:01 +0100283 // This is GetBaseTypeName() + array modifier or generic type parameters
Jiyong Park1deecc32018-07-17 01:14:41 +0900284 string ToString() const;
285
Jiyong Park02da7422018-07-16 16:00:26 +0900286 std::string Signature() const;
287
Jiyong Park1deecc32018-07-17 01:14:41 +0900288 const string& GetUnresolvedName() const { return unresolved_name_; }
289
Jeongik Cha997281d2020-01-16 15:23:59 +0900290 bool IsHidden() const;
291
Jiyong Park1deecc32018-07-17 01:14:41 +0900292 const string& GetComments() const { return comments_; }
293
Jeongik Cha1a7ab642019-07-29 17:31:02 +0900294 const std::vector<std::string> GetSplitName() const { return split_name_; }
295
Jiyong Parka6605ab2018-11-11 14:30:21 +0900296 void SetComments(const string& comment) { comments_ = comment; }
297
Jiyong Park1deecc32018-07-17 01:14:41 +0900298 bool IsResolved() const { return fully_qualified_name_ != ""; }
299
300 bool IsArray() const { return is_array_; }
301
Jiyong Park1deecc32018-07-17 01:14:41 +0900302 // Resolve the base type name to a fully-qualified name. Return false if the
303 // resolution fails.
Daniel Norman716d3112019-09-10 13:11:56 -0700304 bool Resolve(const AidlTypenames& typenames);
Casey Dahlin0ee37582015-09-30 16:31:55 -0700305
Steven Moreland0cea4aa2020-04-20 21:06:02 -0700306 std::set<AidlAnnotation::Type> GetSupportedAnnotations() const override;
Devin Moore24f68572020-02-26 13:20:59 -0800307 bool CheckValid(const AidlTypenames& typenames) const override;
Steven Morelandd59e3172020-05-11 16:42:09 -0700308 bool LanguageSpecificCheckValid(const AidlTypenames& typenames, Options::Language lang) const;
Jeongik Chadf76dc72019-11-28 00:08:47 +0900309 const AidlNode& AsAidlNode() const override { return *this; }
Jiyong Park1d2df7d2018-07-23 15:22:50 +0900310
Casey Dahlin0ee37582015-09-30 16:31:55 -0700311 private:
Steven Moreland3f658cf2018-08-20 13:40:54 -0700312 AidlTypeSpecifier(const AidlTypeSpecifier&) = default;
313
Jiyong Park1deecc32018-07-17 01:14:41 +0900314 const string unresolved_name_;
315 string fully_qualified_name_;
Steven Moreland3f658cf2018-08-20 13:40:54 -0700316 bool is_array_;
Jiyong Parka6605ab2018-11-11 14:30:21 +0900317 string comments_;
Jeongik Cha1a7ab642019-07-29 17:31:02 +0900318 vector<string> split_name_;
Casey Dahlin0ee37582015-09-30 16:31:55 -0700319};
320
Steven Moreland860b1942018-08-16 14:59:28 -0700321// Returns the universal value unaltered.
322std::string AidlConstantValueDecorator(const AidlTypeSpecifier& type, const std::string& raw_value);
323
Steven Moreland9ea10e32018-07-19 15:26:09 -0700324class AidlConstantValue;
Steven Moreland541788d2020-05-21 22:05:52 +0000325// TODO: This class is used for method arguments and also parcelable fields,
326// and it should be split up since default values don't apply to method
327// arguments
Steven Moreland5557f1c2018-07-02 13:50:23 -0700328class AidlVariableDeclaration : public AidlNode {
329 public:
Steven Moreland46e9da82018-07-27 15:45:29 -0700330 AidlVariableDeclaration(const AidlLocation& location, AidlTypeSpecifier* type,
331 const std::string& name);
332 AidlVariableDeclaration(const AidlLocation& location, AidlTypeSpecifier* type,
333 const std::string& name, AidlConstantValue* default_value);
Steven Moreland5557f1c2018-07-02 13:50:23 -0700334 virtual ~AidlVariableDeclaration() = default;
335
Jiyong Parkd800fef2020-07-22 18:09:43 +0900336 // non-copyable, non-movable
337 AidlVariableDeclaration(const AidlVariableDeclaration&) = delete;
338 AidlVariableDeclaration(AidlVariableDeclaration&&) = delete;
339 AidlVariableDeclaration& operator=(const AidlVariableDeclaration&) = delete;
340 AidlVariableDeclaration& operator=(AidlVariableDeclaration&&) = delete;
341
Steven Moreland5557f1c2018-07-02 13:50:23 -0700342 std::string GetName() const { return name_; }
Jiyong Parkd59a10d2018-07-18 11:12:55 +0900343 const AidlTypeSpecifier& GetType() const { return *type_; }
Steven Moreland541788d2020-05-21 22:05:52 +0000344 // if this was constructed explicitly with a default value
345 bool IsDefaultUserSpecified() const { return default_user_specified_; }
346 // will return the default value this is constructed with or a default value
347 // if one is available
Steven Moreland9ea10e32018-07-19 15:26:09 -0700348 const AidlConstantValue* GetDefaultValue() const { return default_value_.get(); }
349
Jiyong Parkd59a10d2018-07-18 11:12:55 +0900350 AidlTypeSpecifier* GetMutableType() { return type_.get(); }
Steven Moreland5557f1c2018-07-02 13:50:23 -0700351
Jeongik Chadb0f59e2018-11-01 18:11:21 +0900352 bool CheckValid(const AidlTypenames& typenames) const;
Steven Moreland5557f1c2018-07-02 13:50:23 -0700353 std::string ToString() const;
Jiyong Park02da7422018-07-16 16:00:26 +0900354 std::string Signature() const;
Steven Moreland5557f1c2018-07-02 13:50:23 -0700355
Steven Moreland860b1942018-08-16 14:59:28 -0700356 std::string ValueString(const ConstantValueDecorator& decorator) const;
Steven Moreland25294322018-08-07 18:13:55 -0700357
Steven Moreland5557f1c2018-07-02 13:50:23 -0700358 private:
Jiyong Parkd59a10d2018-07-18 11:12:55 +0900359 std::unique_ptr<AidlTypeSpecifier> type_;
Steven Moreland5557f1c2018-07-02 13:50:23 -0700360 std::string name_;
Steven Moreland541788d2020-05-21 22:05:52 +0000361 bool default_user_specified_;
Steven Moreland9ea10e32018-07-19 15:26:09 -0700362 std::unique_ptr<AidlConstantValue> default_value_;
Steven Moreland5557f1c2018-07-02 13:50:23 -0700363};
364
365class AidlArgument : public AidlVariableDeclaration {
Casey Dahlinbc7a50a2015-09-28 19:20:50 -0700366 public:
Casey Dahlinc378c992015-09-29 16:50:40 -0700367 enum Direction { IN_DIR = 1, OUT_DIR = 2, INOUT_DIR = 3 };
368
Steven Moreland46e9da82018-07-27 15:45:29 -0700369 AidlArgument(const AidlLocation& location, AidlArgument::Direction direction,
370 AidlTypeSpecifier* type, const std::string& name);
371 AidlArgument(const AidlLocation& location, AidlTypeSpecifier* type, const std::string& name);
Casey Dahlinbc7a50a2015-09-28 19:20:50 -0700372 virtual ~AidlArgument() = default;
373
Jiyong Parkd800fef2020-07-22 18:09:43 +0900374 // non-copyable, non-movable
375 AidlArgument(const AidlArgument&) = delete;
376 AidlArgument(AidlArgument&&) = delete;
377 AidlArgument& operator=(const AidlArgument&) = delete;
378 AidlArgument& operator=(AidlArgument&&) = delete;
379
Casey Dahlinc378c992015-09-29 16:50:40 -0700380 Direction GetDirection() const { return direction_; }
Christopher Wileyad339272015-10-05 19:11:58 -0700381 bool IsOut() const { return direction_ & OUT_DIR; }
382 bool IsIn() const { return direction_ & IN_DIR; }
Casey Dahlinc378c992015-09-29 16:50:40 -0700383 bool DirectionWasSpecified() const { return direction_specified_; }
Jiyong Park3656c3c2018-08-01 20:02:01 +0900384 string GetDirectionSpecifier() const;
Christopher Wileyad339272015-10-05 19:11:58 -0700385
Casey Dahlinc378c992015-09-29 16:50:40 -0700386 std::string ToString() const;
Jiyong Park02da7422018-07-16 16:00:26 +0900387 std::string Signature() const;
Casey Dahlinc378c992015-09-29 16:50:40 -0700388
Casey Dahlinbc7a50a2015-09-28 19:20:50 -0700389 private:
Casey Dahlinc378c992015-09-29 16:50:40 -0700390 Direction direction_;
391 bool direction_specified_;
Casey Dahlina834dd42015-09-23 11:52:15 -0700392};
Adam Lesinskiffa16862014-01-23 18:17:42 -0800393
Casey Dahlind40e2fe2015-11-24 14:06:52 -0800394class AidlMethod;
Steven Moreland693640b2018-07-19 13:46:27 -0700395class AidlConstantDeclaration;
Daniel Norman85aed542019-08-21 12:01:14 -0700396class AidlEnumDeclaration;
Casey Dahlind40e2fe2015-11-24 14:06:52 -0800397class AidlMember : public AidlNode {
398 public:
Steven Moreland46e9da82018-07-27 15:45:29 -0700399 AidlMember(const AidlLocation& location);
Casey Dahlind40e2fe2015-11-24 14:06:52 -0800400 virtual ~AidlMember() = default;
401
Jiyong Parkd800fef2020-07-22 18:09:43 +0900402 // non-copyable, non-movable
403 AidlMember(const AidlMember&) = delete;
404 AidlMember(AidlMember&&) = delete;
405 AidlMember& operator=(const AidlMember&) = delete;
406 AidlMember& operator=(AidlMember&&) = delete;
407
Casey Dahlind40e2fe2015-11-24 14:06:52 -0800408 virtual AidlMethod* AsMethod() { return nullptr; }
Steven Moreland693640b2018-07-19 13:46:27 -0700409 virtual AidlConstantDeclaration* AsConstantDeclaration() { return nullptr; }
Casey Dahlind40e2fe2015-11-24 14:06:52 -0800410};
411
Will McVickerd7d18df2019-09-12 13:40:50 -0700412class AidlUnaryConstExpression;
413class AidlBinaryConstExpression;
414
Steven Moreland693640b2018-07-19 13:46:27 -0700415class AidlConstantValue : public AidlNode {
Casey Dahlind40e2fe2015-11-24 14:06:52 -0800416 public:
Will McVickerd7d18df2019-09-12 13:40:50 -0700417 enum class Type {
418 // WARNING: Don't change this order! The order is used to determine type
419 // promotion during a binary expression.
420 BOOLEAN,
421 INT8,
422 INT32,
423 INT64,
424 ARRAY,
425 CHARACTER,
426 STRING,
427 FLOATING,
428 UNARY,
429 BINARY,
430 ERROR,
431 };
432
433 /*
434 * Return the value casted to the given type.
435 */
436 template <typename T>
437 T cast() const;
Casey Dahlind40e2fe2015-11-24 14:06:52 -0800438
Steven Moreland693640b2018-07-19 13:46:27 -0700439 virtual ~AidlConstantValue() = default;
Casey Dahlind40e2fe2015-11-24 14:06:52 -0800440
Jiyong Parkd800fef2020-07-22 18:09:43 +0900441 // non-copyable, non-movable
442 AidlConstantValue(const AidlConstantValue&) = delete;
443 AidlConstantValue(AidlConstantValue&&) = delete;
444 AidlConstantValue& operator=(const AidlConstantValue&) = delete;
445 AidlConstantValue& operator=(AidlConstantValue&&) = delete;
446
Steven Moreland541788d2020-05-21 22:05:52 +0000447 // creates default value, when one isn't specified
448 // nullptr if no default available
449 static AidlConstantValue* Default(const AidlTypeSpecifier& specifier);
450
Steven Moreland25294322018-08-07 18:13:55 -0700451 static AidlConstantValue* Boolean(const AidlLocation& location, bool value);
452 static AidlConstantValue* Character(const AidlLocation& location, char value);
Steven Moreland25294322018-08-07 18:13:55 -0700453 // example: 123, -5498, maybe any size
Will McVickerd7d18df2019-09-12 13:40:50 -0700454 static AidlConstantValue* Integral(const AidlLocation& location, const string& value);
455 static AidlConstantValue* Floating(const AidlLocation& location, const std::string& value);
Steven Moreland860b1942018-08-16 14:59:28 -0700456 static AidlConstantValue* Array(const AidlLocation& location,
Will McVickerd7d18df2019-09-12 13:40:50 -0700457 std::unique_ptr<vector<unique_ptr<AidlConstantValue>>> values);
Steven Moreland693640b2018-07-19 13:46:27 -0700458 // example: "\"asdf\""
Will McVickerd7d18df2019-09-12 13:40:50 -0700459 static AidlConstantValue* String(const AidlLocation& location, const string& value);
Steven Moreland693640b2018-07-19 13:46:27 -0700460
Daniel Normanf0ca44f2019-10-25 09:59:44 -0700461 // Construct an AidlConstantValue by evaluating the other integral constant's
462 // value string. This does not preserve the structure of the copied constant.
Steven Moreland59e53e42019-11-26 20:38:08 -0800463 // Returns nullptr and logs if value cannot be copied.
Daniel Normanf0ca44f2019-10-25 09:59:44 -0700464 static AidlConstantValue* ShallowIntegralCopy(const AidlConstantValue& other);
Daniel Normanb28684e2019-10-17 15:31:39 -0700465
Will McVickerd7d18df2019-09-12 13:40:50 -0700466 Type GetType() const { return final_type_; }
Steven Moreland25294322018-08-07 18:13:55 -0700467
Will McVickerd7d18df2019-09-12 13:40:50 -0700468 virtual bool CheckValid() const;
Steven Moreland860b1942018-08-16 14:59:28 -0700469
470 // Raw value of type (currently valid in C++ and Java). Empty string on error.
Steven Moreland4bcb05c2019-11-27 18:57:47 -0800471 string ValueString(const AidlTypeSpecifier& type, const ConstantValueDecorator& decorator) const;
Casey Dahlind40e2fe2015-11-24 14:06:52 -0800472
473 private:
Will McVickerd7d18df2019-09-12 13:40:50 -0700474 AidlConstantValue(const AidlLocation& location, Type parsed_type, int64_t parsed_value,
475 const string& checked_value);
476 AidlConstantValue(const AidlLocation& location, Type type, const string& checked_value);
Steven Moreland860b1942018-08-16 14:59:28 -0700477 AidlConstantValue(const AidlLocation& location, Type type,
Will McVickerd7d18df2019-09-12 13:40:50 -0700478 std::unique_ptr<vector<unique_ptr<AidlConstantValue>>> values);
Steven Moreland25294322018-08-07 18:13:55 -0700479 static string ToString(Type type);
Will McVickerd7d18df2019-09-12 13:40:50 -0700480 static bool ParseIntegral(const string& value, int64_t* parsed_value, Type* parsed_type);
481 static bool IsHex(const string& value);
Steven Moreland4bcb05c2019-11-27 18:57:47 -0800482
Will McVickerd7d18df2019-09-12 13:40:50 -0700483 virtual bool evaluate(const AidlTypeSpecifier& type) const;
Casey Dahlind40e2fe2015-11-24 14:06:52 -0800484
Steven Moreland693640b2018-07-19 13:46:27 -0700485 const Type type_ = Type::ERROR;
Will McVickerd7d18df2019-09-12 13:40:50 -0700486 const vector<unique_ptr<AidlConstantValue>> values_; // if type_ == ARRAY
487 const string value_; // otherwise
488
489 // State for tracking evaluation of expressions
Steven Moreland4bcb05c2019-11-27 18:57:47 -0800490 mutable bool is_valid_ = false; // cache of CheckValid, but may be marked false in evaluate
491 mutable bool is_evaluated_ = false; // whether evaluate has been called
Will McVickerd7d18df2019-09-12 13:40:50 -0700492 mutable Type final_type_;
493 mutable int64_t final_value_;
494 mutable string final_string_value_ = "";
Steven Moreland693640b2018-07-19 13:46:27 -0700495
Will McVickerd7d18df2019-09-12 13:40:50 -0700496 friend AidlUnaryConstExpression;
497 friend AidlBinaryConstExpression;
498};
499
500class AidlUnaryConstExpression : public AidlConstantValue {
501 public:
502 AidlUnaryConstExpression(const AidlLocation& location, const string& op,
503 std::unique_ptr<AidlConstantValue> rval);
504
505 static bool IsCompatibleType(Type type, const string& op);
506 bool CheckValid() const override;
Will McVickerd7d18df2019-09-12 13:40:50 -0700507 private:
508 bool evaluate(const AidlTypeSpecifier& type) const override;
509
510 std::unique_ptr<AidlConstantValue> unary_;
511 const string op_;
512};
513
514class AidlBinaryConstExpression : public AidlConstantValue {
515 public:
516 AidlBinaryConstExpression(const AidlLocation& location, std::unique_ptr<AidlConstantValue> lval,
517 const string& op, std::unique_ptr<AidlConstantValue> rval);
518
519 bool CheckValid() const override;
Will McVickerd7d18df2019-09-12 13:40:50 -0700520
521 static bool AreCompatibleTypes(Type t1, Type t2);
522 // Returns the promoted kind for both operands
523 static Type UsualArithmeticConversion(Type left, Type right);
524 // Returns the promoted integral type where INT32 is the smallest type
525 static Type IntegralPromotion(Type in);
526
527 private:
528 bool evaluate(const AidlTypeSpecifier& type) const override;
529
530 std::unique_ptr<AidlConstantValue> left_val_;
531 std::unique_ptr<AidlConstantValue> right_val_;
532 const string op_;
Christopher Wileyd6bdd8d2016-05-03 11:23:13 -0700533};
534
Andrei Onea9445fc62019-06-27 18:11:59 +0100535struct AidlAnnotationParameter {
536 std::string name;
537 std::unique_ptr<AidlConstantValue> value;
538};
539
Steven Moreland693640b2018-07-19 13:46:27 -0700540class AidlConstantDeclaration : public AidlMember {
Christopher Wileyd6bdd8d2016-05-03 11:23:13 -0700541 public:
Steven Moreland46e9da82018-07-27 15:45:29 -0700542 AidlConstantDeclaration(const AidlLocation& location, AidlTypeSpecifier* specifier,
Will McVickerd7d18df2019-09-12 13:40:50 -0700543 const string& name, AidlConstantValue* value);
Steven Moreland693640b2018-07-19 13:46:27 -0700544 virtual ~AidlConstantDeclaration() = default;
Christopher Wileyd6bdd8d2016-05-03 11:23:13 -0700545
Jiyong Parkd800fef2020-07-22 18:09:43 +0900546 // non-copyable, non-movable
547 AidlConstantDeclaration(const AidlConstantDeclaration&) = delete;
548 AidlConstantDeclaration(AidlConstantDeclaration&&) = delete;
549 AidlConstantDeclaration& operator=(const AidlConstantDeclaration&) = delete;
550 AidlConstantDeclaration& operator=(AidlConstantDeclaration&&) = delete;
551
Steven Moreland693640b2018-07-19 13:46:27 -0700552 const AidlTypeSpecifier& GetType() const { return *type_; }
Steven Moreland4d12f9a2018-10-31 14:30:55 -0700553 AidlTypeSpecifier* GetMutableType() { return type_.get(); }
Will McVickerd7d18df2019-09-12 13:40:50 -0700554 const string& GetName() const { return name_; }
Steven Moreland693640b2018-07-19 13:46:27 -0700555 const AidlConstantValue& GetValue() const { return *value_; }
Jeongik Chadb0f59e2018-11-01 18:11:21 +0900556 bool CheckValid(const AidlTypenames& typenames) const;
Christopher Wileyd6bdd8d2016-05-03 11:23:13 -0700557
Will McVickerd7d18df2019-09-12 13:40:50 -0700558 string ToString() const;
559 string Signature() const;
Steven Moreland860b1942018-08-16 14:59:28 -0700560 string ValueString(const ConstantValueDecorator& decorator) const {
Will McVickerd7d18df2019-09-12 13:40:50 -0700561 return value_->ValueString(GetType(), decorator);
Steven Moreland860b1942018-08-16 14:59:28 -0700562 }
Steven Moreland25294322018-08-07 18:13:55 -0700563
Steven Moreland693640b2018-07-19 13:46:27 -0700564 AidlConstantDeclaration* AsConstantDeclaration() override { return this; }
Christopher Wileyd6bdd8d2016-05-03 11:23:13 -0700565
566 private:
Steven Moreland693640b2018-07-19 13:46:27 -0700567 const unique_ptr<AidlTypeSpecifier> type_;
Will McVickerd7d18df2019-09-12 13:40:50 -0700568 const string name_;
569 unique_ptr<AidlConstantValue> value_;
Casey Dahlind40e2fe2015-11-24 14:06:52 -0800570};
571
572class AidlMethod : public AidlMember {
Casey Dahlin5c69deb2015-10-01 14:44:12 -0700573 public:
Will McVickerd7d18df2019-09-12 13:40:50 -0700574 AidlMethod(const AidlLocation& location, bool oneway, AidlTypeSpecifier* type, const string& name,
575 vector<unique_ptr<AidlArgument>>* args, const string& comments);
576 AidlMethod(const AidlLocation& location, bool oneway, AidlTypeSpecifier* type, const string& name,
577 vector<unique_ptr<AidlArgument>>* args, const string& comments, int id,
578 bool is_user_defined = true);
Casey Dahlin5c69deb2015-10-01 14:44:12 -0700579 virtual ~AidlMethod() = default;
580
Jiyong Parkd800fef2020-07-22 18:09:43 +0900581 // non-copyable, non-movable
582 AidlMethod(const AidlMethod&) = delete;
583 AidlMethod(AidlMethod&&) = delete;
584 AidlMethod& operator=(const AidlMethod&) = delete;
585 AidlMethod& operator=(AidlMethod&&) = delete;
586
Casey Dahlind40e2fe2015-11-24 14:06:52 -0800587 AidlMethod* AsMethod() override { return this; }
Jeongik Cha997281d2020-01-16 15:23:59 +0900588 bool IsHidden() const;
Will McVickerd7d18df2019-09-12 13:40:50 -0700589 const string& GetComments() const { return comments_; }
Jiyong Parkd59a10d2018-07-18 11:12:55 +0900590 const AidlTypeSpecifier& GetType() const { return *type_; }
591 AidlTypeSpecifier* GetMutableType() { return type_.get(); }
Steven Morelandacd53472018-12-14 10:17:26 -0800592
Steven Moreland8c70ba92018-12-17 10:20:31 -0800593 // set if this method is part of an interface that is marked oneway
594 void ApplyInterfaceOneway(bool oneway) { oneway_ = oneway_ || oneway; }
Casey Dahlinf4a93112015-10-05 16:58:09 -0700595 bool IsOneway() const { return oneway_; }
Steven Morelandacd53472018-12-14 10:17:26 -0800596
Casey Dahlinf4a93112015-10-05 16:58:09 -0700597 const std::string& GetName() const { return name_; }
Casey Dahlinf4a93112015-10-05 16:58:09 -0700598 bool HasId() const { return has_id_; }
Jiyong Parked65bf42018-08-28 15:43:27 +0900599 int GetId() const { return id_; }
Casey Dahlinf4a93112015-10-05 16:58:09 -0700600 void SetId(unsigned id) { id_ = id; }
Casey Dahlinf2d23f72015-10-02 16:19:19 -0700601
Jiyong Park309668e2018-07-28 16:55:44 +0900602 bool IsUserDefined() const { return is_user_defined_; }
603
Casey Dahlinf4a93112015-10-05 16:58:09 -0700604 const std::vector<std::unique_ptr<AidlArgument>>& GetArguments() const {
Christopher Wileyad339272015-10-05 19:11:58 -0700605 return arguments_;
606 }
607 // An inout parameter will appear in both GetInArguments()
608 // and GetOutArguments(). AidlMethod retains ownership of the argument
609 // pointers returned in this way.
610 const std::vector<const AidlArgument*>& GetInArguments() const {
611 return in_arguments_;
612 }
613 const std::vector<const AidlArgument*>& GetOutArguments() const {
614 return out_arguments_;
Casey Dahlinf4a93112015-10-05 16:58:09 -0700615 }
Casey Dahlin5c69deb2015-10-01 14:44:12 -0700616
Jiyong Park309668e2018-07-28 16:55:44 +0900617 // name + type parameter types
618 // i.e, foo(int, String)
Jiyong Park02da7422018-07-16 16:00:26 +0900619 std::string Signature() const;
620
Jiyong Park309668e2018-07-28 16:55:44 +0900621 // return type + name + type parameter types + annotations
622 // i.e, boolean foo(int, @Nullable String)
623 std::string ToString() const;
624
Casey Dahlin5c69deb2015-10-01 14:44:12 -0700625 private:
Casey Dahlinf4a93112015-10-05 16:58:09 -0700626 bool oneway_;
Casey Dahlinf2d23f72015-10-02 16:19:19 -0700627 std::string comments_;
Jiyong Parkd59a10d2018-07-18 11:12:55 +0900628 std::unique_ptr<AidlTypeSpecifier> type_;
Casey Dahlinf4a93112015-10-05 16:58:09 -0700629 std::string name_;
Christopher Wileyad339272015-10-05 19:11:58 -0700630 const std::vector<std::unique_ptr<AidlArgument>> arguments_;
631 std::vector<const AidlArgument*> in_arguments_;
632 std::vector<const AidlArgument*> out_arguments_;
Casey Dahlinf4a93112015-10-05 16:58:09 -0700633 bool has_id_;
634 int id_;
Jiyong Park309668e2018-07-28 16:55:44 +0900635 bool is_user_defined_ = true;
Casey Dahlin0a2f8be2015-09-28 16:15:29 -0700636};
Adam Lesinskiffa16862014-01-23 18:17:42 -0800637
Steven Morelandc258abc2018-07-10 14:03:38 -0700638class AidlDefinedType;
Jiyong Parkb034bf02018-07-30 17:44:33 +0900639class AidlInterface;
640class AidlParcelable;
641class AidlStructuredParcelable;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800642
Steven Moreland46e9da82018-07-27 15:45:29 -0700643class AidlInterface;
644class AidlParcelable;
645class AidlStructuredParcelable;
Daniel Norman85aed542019-08-21 12:01:14 -0700646// AidlDefinedType represents either an interface, parcelable, or enum that is
Jiyong Park1deecc32018-07-17 01:14:41 +0900647// defined in the source file.
648class AidlDefinedType : public AidlAnnotatable {
Steven Moreland787b0432018-07-03 09:00:58 -0700649 public:
Steven Moreland46e9da82018-07-27 15:45:29 -0700650 AidlDefinedType(const AidlLocation& location, const std::string& name,
Jiyong Park18132182020-06-08 20:24:40 +0900651 const std::string& comments, const std::string& package);
Steven Moreland787b0432018-07-03 09:00:58 -0700652 virtual ~AidlDefinedType() = default;
653
Jiyong Parkd800fef2020-07-22 18:09:43 +0900654 // non-copyable, non-movable
655 AidlDefinedType(const AidlDefinedType&) = delete;
656 AidlDefinedType(AidlDefinedType&&) = delete;
657 AidlDefinedType& operator=(const AidlDefinedType&) = delete;
658 AidlDefinedType& operator=(AidlDefinedType&&) = delete;
659
Jiyong Park1deecc32018-07-17 01:14:41 +0900660 const std::string& GetName() const { return name_; };
Jeongik Cha997281d2020-01-16 15:23:59 +0900661 bool IsHidden() const;
Jiyong Park1deecc32018-07-17 01:14:41 +0900662 const std::string& GetComments() const { return comments_; }
Jiyong Parka6605ab2018-11-11 14:30:21 +0900663 void SetComments(const std::string comments) { comments_ = comments; }
Jiyong Park1deecc32018-07-17 01:14:41 +0900664
Steven Moreland787b0432018-07-03 09:00:58 -0700665 /* dot joined package, example: "android.package.foo" */
Jiyong Park18132182020-06-08 20:24:40 +0900666 std::string GetPackage() const { return package_; }
Steven Moreland787b0432018-07-03 09:00:58 -0700667 /* dot joined package and name, example: "android.package.foo.IBar" */
668 std::string GetCanonicalName() const;
Jiyong Park18132182020-06-08 20:24:40 +0900669 const std::vector<std::string>& GetSplitPackage() const { return split_package_; }
Steven Moreland787b0432018-07-03 09:00:58 -0700670
Steven Morelanded83a282018-07-17 13:27:29 -0700671 virtual std::string GetPreprocessDeclarationName() const = 0;
Steven Morelandc258abc2018-07-10 14:03:38 -0700672
Steven Moreland5557f1c2018-07-02 13:50:23 -0700673 virtual const AidlStructuredParcelable* AsStructuredParcelable() const { return nullptr; }
Steven Morelandc258abc2018-07-10 14:03:38 -0700674 virtual const AidlParcelable* AsParcelable() const { return nullptr; }
Daniel Norman85aed542019-08-21 12:01:14 -0700675 virtual const AidlEnumDeclaration* AsEnumDeclaration() const { return nullptr; }
Steven Moreland5557f1c2018-07-02 13:50:23 -0700676 virtual const AidlInterface* AsInterface() const { return nullptr; }
Jeongik Chadf76dc72019-11-28 00:08:47 +0900677 virtual const AidlParameterizable<std::string>* AsParameterizable() const { return nullptr; }
Devin Moore24f68572020-02-26 13:20:59 -0800678 bool CheckValid(const AidlTypenames& typenames) const override;
Steven Morelandd59e3172020-05-11 16:42:09 -0700679 virtual bool LanguageSpecificCheckValid(const AidlTypenames& typenames,
680 Options::Language lang) const = 0;
Steven Morelandc258abc2018-07-10 14:03:38 -0700681 AidlStructuredParcelable* AsStructuredParcelable() {
682 return const_cast<AidlStructuredParcelable*>(
683 const_cast<const AidlDefinedType*>(this)->AsStructuredParcelable());
684 }
685 AidlParcelable* AsParcelable() {
686 return const_cast<AidlParcelable*>(const_cast<const AidlDefinedType*>(this)->AsParcelable());
687 }
Daniel Norman85aed542019-08-21 12:01:14 -0700688 AidlEnumDeclaration* AsEnumDeclaration() {
689 return const_cast<AidlEnumDeclaration*>(
690 const_cast<const AidlDefinedType*>(this)->AsEnumDeclaration());
691 }
Steven Morelandc258abc2018-07-10 14:03:38 -0700692 AidlInterface* AsInterface() {
693 return const_cast<AidlInterface*>(const_cast<const AidlDefinedType*>(this)->AsInterface());
694 }
695
Jeongik Chadf76dc72019-11-28 00:08:47 +0900696 AidlParameterizable<std::string>* AsParameterizable() {
697 return const_cast<AidlParameterizable<std::string>*>(
698 const_cast<const AidlDefinedType*>(this)->AsParameterizable());
699 }
700
Steven Moreland6cee3482018-07-18 14:39:58 -0700701 const AidlParcelable* AsUnstructuredParcelable() const {
702 if (this->AsStructuredParcelable() != nullptr) return nullptr;
703 return this->AsParcelable();
704 }
705 AidlParcelable* AsUnstructuredParcelable() {
706 return const_cast<AidlParcelable*>(
707 const_cast<const AidlDefinedType*>(this)->AsUnstructuredParcelable());
708 }
709
Jeongik Cha997281d2020-01-16 15:23:59 +0900710 virtual void Dump(CodeWriter* writer) const = 0;
Steven Morelanda5d9c5c2020-02-21 16:01:09 -0800711 void DumpHeader(CodeWriter* writer) const;
Jiyong Park02da7422018-07-16 16:00:26 +0900712
Steven Moreland787b0432018-07-03 09:00:58 -0700713 private:
Jiyong Park1deecc32018-07-17 01:14:41 +0900714 std::string name_;
Jiyong Park1deecc32018-07-17 01:14:41 +0900715 std::string comments_;
Jiyong Park18132182020-06-08 20:24:40 +0900716 const std::string package_;
717 const std::vector<std::string> split_package_;
Steven Moreland787b0432018-07-03 09:00:58 -0700718};
719
Jeongik Chadf76dc72019-11-28 00:08:47 +0900720class AidlParcelable : public AidlDefinedType, public AidlParameterizable<std::string> {
Casey Dahlin1ae2bc52015-10-07 18:49:10 -0700721 public:
Jiyong Park18132182020-06-08 20:24:40 +0900722 AidlParcelable(const AidlLocation& location, const std::string& name, const std::string& package,
723 const std::string& comments, const std::string& cpp_header = "",
Jeongik Chadf76dc72019-11-28 00:08:47 +0900724 std::vector<std::string>* type_params = nullptr);
Casey Dahlin1ae2bc52015-10-07 18:49:10 -0700725 virtual ~AidlParcelable() = default;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800726
Jiyong Parkd800fef2020-07-22 18:09:43 +0900727 // non-copyable, non-movable
728 AidlParcelable(const AidlParcelable&) = delete;
729 AidlParcelable(AidlParcelable&&) = delete;
730 AidlParcelable& operator=(const AidlParcelable&) = delete;
731 AidlParcelable& operator=(AidlParcelable&&) = delete;
732
Christopher Wiley8aa4d9f2015-11-16 19:10:45 -0800733 std::string GetCppHeader() const { return cpp_header_; }
Christopher Wiley8aa4d9f2015-11-16 19:10:45 -0800734
Steven Moreland0cea4aa2020-04-20 21:06:02 -0700735 std::set<AidlAnnotation::Type> GetSupportedAnnotations() const override;
Jeongik Cha82317dd2019-02-27 20:26:11 +0900736 bool CheckValid(const AidlTypenames& typenames) const override;
Steven Morelandd59e3172020-05-11 16:42:09 -0700737 bool LanguageSpecificCheckValid(const AidlTypenames& typenames,
738 Options::Language lang) const override;
Steven Morelandc258abc2018-07-10 14:03:38 -0700739 const AidlParcelable* AsParcelable() const override { return this; }
Jeongik Chadf76dc72019-11-28 00:08:47 +0900740 const AidlParameterizable<std::string>* AsParameterizable() const override { return this; }
741 const AidlNode& AsAidlNode() const override { return *this; }
Steven Morelanded83a282018-07-17 13:27:29 -0700742 std::string GetPreprocessDeclarationName() const override { return "parcelable"; }
Steven Morelandc258abc2018-07-10 14:03:38 -0700743
Jeongik Cha997281d2020-01-16 15:23:59 +0900744 void Dump(CodeWriter* writer) const override;
Jiyong Park02da7422018-07-16 16:00:26 +0900745
Casey Dahlin1ae2bc52015-10-07 18:49:10 -0700746 private:
Christopher Wiley8aa4d9f2015-11-16 19:10:45 -0800747 std::string cpp_header_;
Casey Dahlin0a2f8be2015-09-28 16:15:29 -0700748};
Adam Lesinskiffa16862014-01-23 18:17:42 -0800749
Steven Moreland5557f1c2018-07-02 13:50:23 -0700750class AidlStructuredParcelable : public AidlParcelable {
751 public:
Jiyong Park18132182020-06-08 20:24:40 +0900752 AidlStructuredParcelable(const AidlLocation& location, const std::string& name,
753 const std::string& package, const std::string& comments,
Steven Moreland5557f1c2018-07-02 13:50:23 -0700754 std::vector<std::unique_ptr<AidlVariableDeclaration>>* variables);
Jiyong Parkd800fef2020-07-22 18:09:43 +0900755 virtual ~AidlStructuredParcelable() = default;
756
757 // non-copyable, non-movable
758 AidlStructuredParcelable(const AidlStructuredParcelable&) = delete;
759 AidlStructuredParcelable(AidlStructuredParcelable&&) = delete;
760 AidlStructuredParcelable& operator=(const AidlStructuredParcelable&) = delete;
761 AidlStructuredParcelable& operator=(AidlStructuredParcelable&&) = delete;
Steven Moreland5557f1c2018-07-02 13:50:23 -0700762
763 const std::vector<std::unique_ptr<AidlVariableDeclaration>>& GetFields() const {
764 return variables_;
765 }
766
Steven Morelandc258abc2018-07-10 14:03:38 -0700767 const AidlStructuredParcelable* AsStructuredParcelable() const override { return this; }
Steven Morelanded83a282018-07-17 13:27:29 -0700768 std::string GetPreprocessDeclarationName() const override { return "structured_parcelable"; }
Steven Moreland5557f1c2018-07-02 13:50:23 -0700769
Jeongik Cha997281d2020-01-16 15:23:59 +0900770 void Dump(CodeWriter* writer) const override;
Jiyong Park02da7422018-07-16 16:00:26 +0900771
Steven Moreland0cea4aa2020-04-20 21:06:02 -0700772 std::set<AidlAnnotation::Type> GetSupportedAnnotations() const override;
Jeongik Chadb0f59e2018-11-01 18:11:21 +0900773 bool CheckValid(const AidlTypenames& typenames) const override;
Steven Morelandd59e3172020-05-11 16:42:09 -0700774 bool LanguageSpecificCheckValid(const AidlTypenames& typenames,
775 Options::Language lang) const override;
Jeongik Chadb0f59e2018-11-01 18:11:21 +0900776
Steven Moreland5557f1c2018-07-02 13:50:23 -0700777 private:
778 const std::vector<std::unique_ptr<AidlVariableDeclaration>> variables_;
Steven Moreland5557f1c2018-07-02 13:50:23 -0700779};
780
Daniel Norman85aed542019-08-21 12:01:14 -0700781class AidlEnumerator : public AidlNode {
782 public:
Daniel Norman2e4112d2019-10-03 10:22:35 -0700783 AidlEnumerator(const AidlLocation& location, const std::string& name, AidlConstantValue* value,
784 const std::string& comments);
Daniel Norman85aed542019-08-21 12:01:14 -0700785 virtual ~AidlEnumerator() = default;
786
Jiyong Parkd800fef2020-07-22 18:09:43 +0900787 // non-copyable, non-movable
788 AidlEnumerator(const AidlEnumerator&) = delete;
789 AidlEnumerator(AidlEnumerator&&) = delete;
790 AidlEnumerator& operator=(const AidlEnumerator&) = delete;
791 AidlEnumerator& operator=(AidlEnumerator&&) = delete;
792
Daniel Norman85aed542019-08-21 12:01:14 -0700793 const std::string& GetName() const { return name_; }
Will McVickerd7d18df2019-09-12 13:40:50 -0700794 AidlConstantValue* GetValue() const { return value_.get(); }
Daniel Norman2e4112d2019-10-03 10:22:35 -0700795 const std::string& GetComments() const { return comments_; }
Daniel Norman85aed542019-08-21 12:01:14 -0700796 bool CheckValid(const AidlTypeSpecifier& enum_backing_type) const;
797
798 string ValueString(const AidlTypeSpecifier& backing_type,
799 const ConstantValueDecorator& decorator) const;
800
Daniel Normanb28684e2019-10-17 15:31:39 -0700801 void SetValue(std::unique_ptr<AidlConstantValue> value) { value_ = std::move(value); }
802
Daniel Norman85aed542019-08-21 12:01:14 -0700803 private:
804 const std::string name_;
Will McVickerd7d18df2019-09-12 13:40:50 -0700805 unique_ptr<AidlConstantValue> value_;
Daniel Norman2e4112d2019-10-03 10:22:35 -0700806 const std::string comments_;
Daniel Norman85aed542019-08-21 12:01:14 -0700807};
808
809class AidlEnumDeclaration : public AidlDefinedType {
810 public:
Will McVickerd7d18df2019-09-12 13:40:50 -0700811 AidlEnumDeclaration(const AidlLocation& location, const string& name,
Daniel Norman85aed542019-08-21 12:01:14 -0700812 std::vector<std::unique_ptr<AidlEnumerator>>* enumerators,
Jiyong Park18132182020-06-08 20:24:40 +0900813 const std::string& package, const std::string& comments);
Daniel Norman85aed542019-08-21 12:01:14 -0700814 virtual ~AidlEnumDeclaration() = default;
815
Jiyong Parkd800fef2020-07-22 18:09:43 +0900816 // non-copyable, non-movable
817 AidlEnumDeclaration(const AidlEnumDeclaration&) = delete;
818 AidlEnumDeclaration(AidlEnumDeclaration&&) = delete;
819 AidlEnumDeclaration& operator=(const AidlEnumDeclaration&) = delete;
820 AidlEnumDeclaration& operator=(AidlEnumDeclaration&&) = delete;
821
Daniel Norman85aed542019-08-21 12:01:14 -0700822 void SetBackingType(std::unique_ptr<const AidlTypeSpecifier> type);
823 const AidlTypeSpecifier& GetBackingType() const { return *backing_type_; }
824 const std::vector<std::unique_ptr<AidlEnumerator>>& GetEnumerators() const {
825 return enumerators_;
826 }
Steven Moreland59e53e42019-11-26 20:38:08 -0800827 bool Autofill();
Steven Moreland0cea4aa2020-04-20 21:06:02 -0700828 std::set<AidlAnnotation::Type> GetSupportedAnnotations() const override;
Daniel Norman85aed542019-08-21 12:01:14 -0700829 bool CheckValid(const AidlTypenames& typenames) const override;
Steven Morelandd59e3172020-05-11 16:42:09 -0700830 bool LanguageSpecificCheckValid(const AidlTypenames& /*typenames*/,
831 Options::Language) const override {
832 return true;
833 }
Daniel Norman85aed542019-08-21 12:01:14 -0700834 std::string GetPreprocessDeclarationName() const override { return "enum"; }
Jeongik Cha997281d2020-01-16 15:23:59 +0900835 void Dump(CodeWriter* writer) const override;
Daniel Norman85aed542019-08-21 12:01:14 -0700836
837 const AidlEnumDeclaration* AsEnumDeclaration() const override { return this; }
838
839 private:
840 const std::string name_;
841 const std::vector<std::unique_ptr<AidlEnumerator>> enumerators_;
842 std::unique_ptr<const AidlTypeSpecifier> backing_type_;
Daniel Norman85aed542019-08-21 12:01:14 -0700843};
844
Jiyong Park1deecc32018-07-17 01:14:41 +0900845class AidlInterface final : public AidlDefinedType {
Casey Dahlin1ae2bc52015-10-07 18:49:10 -0700846 public:
Steven Moreland46e9da82018-07-27 15:45:29 -0700847 AidlInterface(const AidlLocation& location, const std::string& name, const std::string& comments,
848 bool oneway_, std::vector<std::unique_ptr<AidlMember>>* members,
Jiyong Park18132182020-06-08 20:24:40 +0900849 const std::string& package);
Casey Dahlin1ae2bc52015-10-07 18:49:10 -0700850 virtual ~AidlInterface() = default;
851
Jiyong Parkd800fef2020-07-22 18:09:43 +0900852 // non-copyable, non-movable
853 AidlInterface(const AidlInterface&) = delete;
854 AidlInterface(AidlInterface&&) = delete;
855 AidlInterface& operator=(const AidlInterface&) = delete;
856 AidlInterface& operator=(AidlInterface&&) = delete;
857
Casey Dahlinfb7da2e2015-10-08 17:26:09 -0700858 const std::vector<std::unique_ptr<AidlMethod>>& GetMethods() const
859 { return methods_; }
Jiyong Park309668e2018-07-28 16:55:44 +0900860 std::vector<std::unique_ptr<AidlMethod>>& GetMutableMethods() { return methods_; }
Steven Moreland693640b2018-07-19 13:46:27 -0700861 const std::vector<std::unique_ptr<AidlConstantDeclaration>>& GetConstantDeclarations() const {
862 return constants_;
863 }
Casey Dahlina2f77c42015-12-01 18:26:02 -0800864
Steven Morelandc258abc2018-07-10 14:03:38 -0700865 const AidlInterface* AsInterface() const override { return this; }
Steven Morelanded83a282018-07-17 13:27:29 -0700866 std::string GetPreprocessDeclarationName() const override { return "interface"; }
Steven Moreland5557f1c2018-07-02 13:50:23 -0700867
Jeongik Cha997281d2020-01-16 15:23:59 +0900868 void Dump(CodeWriter* writer) const override;
Jiyong Park02da7422018-07-16 16:00:26 +0900869
Steven Moreland0cea4aa2020-04-20 21:06:02 -0700870 std::set<AidlAnnotation::Type> GetSupportedAnnotations() const override;
Jeongik Chadb0f59e2018-11-01 18:11:21 +0900871 bool CheckValid(const AidlTypenames& typenames) const override;
Steven Morelandd59e3172020-05-11 16:42:09 -0700872 bool LanguageSpecificCheckValid(const AidlTypenames& typenames,
873 Options::Language lang) const override;
Jeongik Chadb0f59e2018-11-01 18:11:21 +0900874
Casey Dahlin1ae2bc52015-10-07 18:49:10 -0700875 private:
Casey Dahlinfb7da2e2015-10-08 17:26:09 -0700876 std::vector<std::unique_ptr<AidlMethod>> methods_;
Steven Moreland693640b2018-07-19 13:46:27 -0700877 std::vector<std::unique_ptr<AidlConstantDeclaration>> constants_;
Casey Dahlin0a2f8be2015-09-28 16:15:29 -0700878};
Adam Lesinskiffa16862014-01-23 18:17:42 -0800879
Casey Dahlin0edf3422015-10-07 12:34:59 -0700880class AidlImport : public AidlNode {
881 public:
Steven Moreland46e9da82018-07-27 15:45:29 -0700882 AidlImport(const AidlLocation& location, const std::string& needed_class);
Casey Dahlin0edf3422015-10-07 12:34:59 -0700883 virtual ~AidlImport() = default;
884
Jiyong Parkd800fef2020-07-22 18:09:43 +0900885 // non-copyable, non-movable
886 AidlImport(const AidlImport&) = delete;
887 AidlImport(AidlImport&&) = delete;
888 AidlImport& operator=(const AidlImport&) = delete;
889 AidlImport& operator=(AidlImport&&) = delete;
890
Casey Dahlin0edf3422015-10-07 12:34:59 -0700891 const std::string& GetFilename() const { return filename_; }
892 const std::string& GetNeededClass() const { return needed_class_; }
Casey Dahlin0edf3422015-10-07 12:34:59 -0700893
894 private:
Casey Dahlin0edf3422015-10-07 12:34:59 -0700895 std::string filename_;
896 std::string needed_class_;
Casey Dahline2507492015-09-14 17:11:20 -0700897};
898
Jiyong Park62515512020-06-08 15:57:11 +0900899// AidlDocument models an AIDL file
900class AidlDocument : public AidlNode {
901 public:
902 AidlDocument(const AidlLocation& location, std::vector<std::unique_ptr<AidlImport>>& imports,
Jiyong Park8e79b7f2020-07-20 20:52:38 +0900903 std::vector<std::unique_ptr<AidlDefinedType>>&& defined_types)
904 : AidlNode(location),
905 imports_(std::move(imports)),
906 defined_types_(std::move(defined_types)) {}
Jiyong Parkd800fef2020-07-22 18:09:43 +0900907 ~AidlDocument() = default;
908
909 // non-copyable, non-movable
Jiyong Park8e79b7f2020-07-20 20:52:38 +0900910 AidlDocument(const AidlDocument&) = delete;
911 AidlDocument(AidlDocument&&) = delete;
912 AidlDocument& operator=(const AidlDocument&) = delete;
913 AidlDocument& operator=(AidlDocument&&) = delete;
Jiyong Parkd800fef2020-07-22 18:09:43 +0900914
915 const std::vector<std::unique_ptr<AidlImport>>& Imports() const { return imports_; }
916 const std::vector<std::unique_ptr<AidlDefinedType>>& DefinedTypes() const {
917 return defined_types_;
918 }
Jiyong Park62515512020-06-08 15:57:11 +0900919
920 private:
921 const std::vector<std::unique_ptr<AidlImport>> imports_;
Jiyong Park8e79b7f2020-07-20 20:52:38 +0900922 const std::vector<std::unique_ptr<AidlDefinedType>> defined_types_;
Jiyong Park62515512020-06-08 15:57:11 +0900923};