blob: 55de28b0659b13f1278ec535fb7acaf0c969adf7 [file] [log] [blame]
Will McVickerd7d18df2019-09-12 13:40:50 -07001/*
2 * Copyright (C) 2019, The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Steven Moreland9fccf582018-08-27 20:36:27 -070017#pragma once
Christopher Wileyec31a052016-01-25 07:28:51 -080018
Casey Dahlinbc7a50a2015-09-28 19:20:50 -070019#include <memory>
Jeongik Cha997281d2020-01-16 15:23:59 +090020#include <regex>
Casey Dahlindd691812015-09-09 17:59:06 -070021#include <string>
Jeongik Chadf76dc72019-11-28 00:08:47 +090022#include <unordered_set>
Casey Dahlinbc7a50a2015-09-28 19:20:50 -070023#include <vector>
Casey Dahlindd691812015-09-09 17:59:06 -070024
Elliott Hughes0a620672015-12-04 13:53:18 -080025#include <android-base/strings.h>
Casey Dahlin73d46b02015-09-11 02:47:54 +000026
Jooyung Han888c5bc2020-12-22 17:28:47 +090027#include "aidl_typenames.h"
28#include "code_writer.h"
29#include "diagnostics.h"
30#include "io_delegate.h"
Jooyung Han535c5e82020-12-29 15:16:59 +090031#include "location.h"
32#include "logging.h"
Jooyung Han888c5bc2020-12-22 17:28:47 +090033#include "options.h"
34
Jiyong Parkb034bf02018-07-30 17:44:33 +090035using android::aidl::AidlTypenames;
Jiyong Park02da7422018-07-16 16:00:26 +090036using android::aidl::CodeWriter;
Jooyung Han888c5bc2020-12-22 17:28:47 +090037using android::aidl::DiagnosticsContext;
Jeongik Cha047c5ee2019-08-07 23:16:49 +090038using android::aidl::Options;
Steven Moreland3f658cf2018-08-20 13:40:54 -070039using std::shared_ptr;
Jiyong Park1deecc32018-07-17 01:14:41 +090040using std::string;
41using std::unique_ptr;
42using std::vector;
Andrei Onea8714b022019-02-01 18:55:54 +000043class AidlNode;
44
Jooyung Han535c5e82020-12-29 15:16:59 +090045// helper to see if T is the same to one of Args types.
46template <typename T, typename... Args>
47struct is_one_of : std::false_type {};
48
49template <typename T, typename S, typename... Args>
50struct is_one_of<T, S, Args...> {
51 enum { value = std::is_same_v<T, S> || is_one_of<T, Args...>::value };
52};
53
54// helper to see if T is std::vector of something.
55template <typename>
56struct is_vector : std::false_type {};
57
58template <typename T>
59struct is_vector<std::vector<T>> : std::true_type {};
60
61// helper for static_assert(false)
62template <typename T>
63struct unsupported_type : std::false_type {};
64
Andrei Onea8714b022019-02-01 18:55:54 +000065namespace android {
66namespace aidl {
67namespace mappings {
68std::string dump_location(const AidlNode& method);
69} // namespace mappings
Mathew Inwoodadb74672019-11-29 14:01:53 +000070namespace java {
71std::string dump_location(const AidlNode& method);
72} // namespace java
Andrei Onea8714b022019-02-01 18:55:54 +000073} // namespace aidl
74} // namespace android
75
Jooyung Han535c5e82020-12-29 15:16:59 +090076bool ParseFloating(std::string_view sv, double* parsed);
77bool ParseFloating(std::string_view sv, float* parsed);
Steven Moreland46e9da82018-07-27 15:45:29 -070078
Jooyung Han808a2a02020-12-28 16:46:54 +090079class AidlDocument;
80class AidlInterface;
81class AidlParcelable;
82class AidlStructuredParcelable;
83class AidlEnumDeclaration;
84class AidlUnionDecl;
85class AidlVariableDeclaration;
86class AidlConstantDeclaration;
87class AidlEnumerator;
88class AidlMethod;
89class AidlArgument;
90
Jiyong Park512ed852020-12-30 15:07:23 +090091// Interface for visitors that can traverse AidlTraversable nodes. The contract is that Visit()
92// method returns a bool which controls whether or not the traversal should be continued down to its
93// children.
Jooyung Han808a2a02020-12-28 16:46:54 +090094class AidlVisitor {
95 public:
96 virtual ~AidlVisitor() = default;
Jiyong Park512ed852020-12-30 15:07:23 +090097 virtual bool Visit(const AidlDocument&) { return true; }
98 virtual bool Visit(const AidlInterface&) { return true; }
99 virtual bool Visit(const AidlParcelable&) { return true; }
100 virtual bool Visit(const AidlStructuredParcelable&) { return true; }
101 virtual bool Visit(const AidlUnionDecl&) { return true; }
102 virtual bool Visit(const AidlEnumDeclaration&) { return true; }
103 virtual bool Visit(const AidlEnumerator&) { return true; }
104 virtual bool Visit(const AidlMethod&) { return true; }
105 virtual bool Visit(const AidlVariableDeclaration&) { return true; }
106 virtual bool Visit(const AidlConstantDeclaration&) { return true; }
107 virtual bool Visit(const AidlArgument&) { return true; }
Jooyung Han808a2a02020-12-28 16:46:54 +0900108};
109
Steven Moreland46e9da82018-07-27 15:45:29 -0700110// Anything that is locatable in a .aidl file.
111class AidlNode {
112 public:
113 AidlNode(const AidlLocation& location);
Steven Moreland3f658cf2018-08-20 13:40:54 -0700114
115 AidlNode(const AidlNode&) = default;
Steven Moreland46e9da82018-07-27 15:45:29 -0700116 virtual ~AidlNode() = default;
117
Jiyong Parkd800fef2020-07-22 18:09:43 +0900118 AidlNode(AidlNode&&) = delete;
119 AidlNode& operator=(AidlNode&&) = delete;
120
Devin Mooredf93ebb2020-03-25 14:03:35 -0700121 // To be able to print AidlLocation
Steven Morelandb0d15a52020-03-31 14:03:47 -0700122 friend class AidlErrorLog;
Andrei Onea8714b022019-02-01 18:55:54 +0000123 friend std::string android::aidl::mappings::dump_location(const AidlNode&);
Mathew Inwoodadb74672019-11-29 14:01:53 +0000124 friend std::string android::aidl::java::dump_location(const AidlNode&);
Steven Moreland46e9da82018-07-27 15:45:29 -0700125
Devin Mooredf93ebb2020-03-25 14:03:35 -0700126 const AidlLocation& GetLocation() const { return location_; }
127
Steven Moreland46e9da82018-07-27 15:45:29 -0700128 private:
Mathew Inwoodadb74672019-11-29 14:01:53 +0000129 std::string PrintLine() const;
Andrei Onea8714b022019-02-01 18:55:54 +0000130 std::string PrintLocation() const;
Steven Moreland46e9da82018-07-27 15:45:29 -0700131 const AidlLocation location_;
Casey Dahlinbc7a50a2015-09-28 19:20:50 -0700132};
133
Jiyong Park512ed852020-12-30 15:07:23 +0900134// Anything that is traversable by the AidlVisitor
135class AidlTraversable {
136 public:
137 virtual ~AidlTraversable() = default;
138
139 virtual void TraverseChildren(std::function<void(const AidlTraversable&)> traverse) const = 0;
140 virtual bool DispatchVisit(AidlVisitor&) const = 0;
141};
142
Jeongik Chadf76dc72019-11-28 00:08:47 +0900143// unique_ptr<AidlTypeSpecifier> for type arugment,
144// std::string for type parameter(T, U, and so on).
145template <typename T>
146class AidlParameterizable {
147 public:
148 AidlParameterizable(std::vector<T>* type_params) : type_params_(type_params) {}
149 virtual ~AidlParameterizable() = default;
150 bool IsGeneric() const { return type_params_ != nullptr; }
151 const std::vector<T>& GetTypeParameters() const { return *type_params_; }
152 bool CheckValid() const;
153
Steven Moreland6c07b832020-10-29 23:39:53 +0000154 __attribute__((warn_unused_result)) bool SetTypeParameters(std::vector<T>* type_params) {
155 if (type_params_) return false;
156 type_params_.reset(type_params);
157 return true;
158 }
159
Jeongik Chadf76dc72019-11-28 00:08:47 +0900160 virtual const AidlNode& AsAidlNode() const = 0;
161
162 protected:
163 AidlParameterizable(const AidlParameterizable&);
164
165 private:
Steven Moreland6c07b832020-10-29 23:39:53 +0000166 unique_ptr<std::vector<T>> type_params_;
Jeongik Chadf76dc72019-11-28 00:08:47 +0900167 static_assert(std::is_same<T, unique_ptr<AidlTypeSpecifier>>::value ||
168 std::is_same<T, std::string>::value);
169};
170template <>
171bool AidlParameterizable<std::string>::CheckValid() const;
172
Andrei Onea9445fc62019-06-27 18:11:59 +0100173class AidlConstantValue;
174class AidlConstantDeclaration;
175
176// Transforms a value string into a language specific form. Raw value as produced by
177// AidlConstantValue.
178using ConstantValueDecorator =
179 std::function<std::string(const AidlTypeSpecifier& type, const std::string& raw_value)>;
180
Jiyong Park68bc77a2018-07-19 19:00:45 +0900181class AidlAnnotation : public AidlNode {
182 public:
Steven Moreland0cea4aa2020-04-20 21:06:02 -0700183 enum class Type {
184 BACKING = 1,
185 HIDE,
186 JAVA_STABLE_PARCELABLE,
187 UNSUPPORTED_APP_USAGE,
188 VINTF_STABILITY,
189 NULLABLE,
190 UTF8_IN_CPP,
Steven Morelanda7764e52020-10-27 17:29:29 +0000191 SENSITIVE_DATA,
Jiyong Parkbf5fd5c2020-06-05 19:48:05 +0900192 JAVA_PASSTHROUGH,
Jooyung Han90345002020-10-23 15:28:53 +0900193 JAVA_DERIVE,
Jeongik Chad0a10272020-08-06 16:33:36 +0900194 JAVA_ONLY_IMMUTABLE,
Devin Moorec7e47a32020-08-07 10:55:25 -0700195 FIXED_SIZE,
Jiyong Park27fd7fd2020-08-27 16:25:09 +0900196 DESCRIPTOR,
Andrei Homescue61feb52020-08-18 15:44:24 -0700197 RUST_DERIVE,
Jooyung Hanf8dbbcc2020-12-26 03:05:55 +0900198 SUPPRESS_WARNINGS,
Steven Moreland0cea4aa2020-04-20 21:06:02 -0700199 };
200 static std::string TypeToString(Type type);
201
Andrei Onea9445fc62019-06-27 18:11:59 +0100202 static AidlAnnotation* Parse(
203 const AidlLocation& location, const string& name,
204 std::map<std::string, std::shared_ptr<AidlConstantValue>>* parameter_list);
Steven Moreland46e9da82018-07-27 15:45:29 -0700205
Steven Moreland3f658cf2018-08-20 13:40:54 -0700206 AidlAnnotation(const AidlAnnotation&) = default;
Steven Moreland3be75772018-08-20 13:27:43 -0700207 AidlAnnotation(AidlAnnotation&&) = default;
Jiyong Park68bc77a2018-07-19 19:00:45 +0900208 virtual ~AidlAnnotation() = default;
Andrei Onea9445fc62019-06-27 18:11:59 +0100209 bool CheckValid() const;
Steven Moreland3be75772018-08-20 13:27:43 -0700210
Jooyung Hand902a972020-10-23 17:32:44 +0900211 const string& GetName() const { return schema_.name; }
Steven Moreland0cea4aa2020-04-20 21:06:02 -0700212 const Type& GetType() const { return schema_.type; }
Jooyung Hand902a972020-10-23 17:32:44 +0900213 bool Repeatable() const { return schema_.repeatable; }
Jooyung Han965e31d2020-11-27 12:30:16 +0900214
215 // ToString is for dumping AIDL.
216 // Returns string representation of this annotation.
217 // e.g) "@RustDerive(Clone=true, Copy=true)"
218 string ToString() const;
219
Jooyung Hanb3c77ed2020-12-26 02:02:45 +0900220 template <typename T>
221 std::optional<T> ParamValue(const std::string& param_name) const;
222
Andrei Onea9445fc62019-06-27 18:11:59 +0100223 std::map<std::string, std::string> AnnotationParams(
224 const ConstantValueDecorator& decorator) const;
Jiyong Parka6605ab2018-11-11 14:30:21 +0900225 const string& GetComments() const { return comments_; }
226 void SetComments(const string& comments) { comments_ = comments; }
Jiyong Park68bc77a2018-07-19 19:00:45 +0900227
228 private:
Steven Moreland0cea4aa2020-04-20 21:06:02 -0700229 struct Schema {
230 AidlAnnotation::Type type;
231
232 // text name in .aidl file, e.g. "nullable"
233 std::string name;
234
235 // map from param name -> value type
Jooyung Han5c2fcae2020-12-26 00:04:39 +0900236 std::map<std::string, const AidlTypeSpecifier&> supported_parameters;
Jooyung Hand902a972020-10-23 17:32:44 +0900237
238 bool repeatable;
Jooyung Han5721a232020-12-24 04:34:55 +0900239
240 std::vector<std::string> required_parameters = {};
Steven Moreland0cea4aa2020-04-20 21:06:02 -0700241 };
242 static const std::vector<Schema>& AllSchemas();
243
244 AidlAnnotation(const AidlLocation& location, const Schema& schema,
Andrei Onea9445fc62019-06-27 18:11:59 +0100245 std::map<std::string, std::shared_ptr<AidlConstantValue>>&& parameters);
Steven Moreland0cea4aa2020-04-20 21:06:02 -0700246
247 const Schema& schema_;
Jiyong Parka6605ab2018-11-11 14:30:21 +0900248 string comments_;
Andrei Onea9445fc62019-06-27 18:11:59 +0100249 std::map<std::string, std::shared_ptr<AidlConstantValue>> parameters_;
Jiyong Park68bc77a2018-07-19 19:00:45 +0900250};
251
Steven Moreland3be75772018-08-20 13:27:43 -0700252static inline bool operator<(const AidlAnnotation& lhs, const AidlAnnotation& rhs) {
253 return lhs.GetName() < rhs.GetName();
254}
255static inline bool operator==(const AidlAnnotation& lhs, const AidlAnnotation& rhs) {
256 return lhs.GetName() == rhs.GetName();
257}
Jiyong Park3656c3c2018-08-01 20:02:01 +0900258
Casey Dahline7922932016-02-29 17:23:01 -0800259class AidlAnnotatable : public AidlNode {
Casey Dahlin0ee37582015-09-30 16:31:55 -0700260 public:
Steven Moreland46e9da82018-07-27 15:45:29 -0700261 AidlAnnotatable(const AidlLocation& location);
Steven Moreland3f658cf2018-08-20 13:40:54 -0700262
263 AidlAnnotatable(const AidlAnnotatable&) = default;
264 AidlAnnotatable(AidlAnnotatable&&) = default;
Casey Dahline7922932016-02-29 17:23:01 -0800265 virtual ~AidlAnnotatable() = default;
266
Artur Satayev91fe8712019-07-29 13:06:01 +0100267 void Annotate(vector<AidlAnnotation>&& annotations) {
268 for (auto& annotation : annotations) {
269 annotations_.emplace_back(std::move(annotation));
270 }
271 }
Jiyong Park68bc77a2018-07-19 19:00:45 +0900272 bool IsNullable() const;
Jiyong Park68bc77a2018-07-19 19:00:45 +0900273 bool IsUtf8InCpp() const;
Steven Morelanda7764e52020-10-27 17:29:29 +0000274 bool IsSensitiveData() const;
Steven Morelanda57d0a62019-07-30 09:41:14 -0700275 bool IsVintfStability() const;
Jeongik Chad0a10272020-08-06 16:33:36 +0900276 bool IsJavaOnlyImmutable() const;
Devin Moorec7e47a32020-08-07 10:55:25 -0700277 bool IsFixedSize() const;
Jeongik Cha88f95a82020-01-15 13:02:16 +0900278 bool IsStableApiParcelable(Options::Language lang) const;
Makoto Onuki78a1c1c2020-03-04 16:57:23 -0800279 bool IsHide() const;
Jooyung Han829ec7c2020-12-02 12:07:36 +0900280 bool JavaDerive(const std::string& method) const;
Jiyong Park27fd7fd2020-08-27 16:25:09 +0900281 std::string GetDescriptor() const;
Andrei Onea9445fc62019-06-27 18:11:59 +0100282
Steven Moreland7e4b9502020-02-20 18:10:42 -0800283 void DumpAnnotations(CodeWriter* writer) const;
284
Andrei Onea9445fc62019-06-27 18:11:59 +0100285 const AidlAnnotation* UnsupportedAppUsage() const;
Andrei Homescue61feb52020-08-18 15:44:24 -0700286 const AidlAnnotation* RustDerive() const;
Jooyung Han672557b2020-12-24 05:18:00 +0900287 const AidlAnnotation* BackingType() const;
Jooyung Hanf8dbbcc2020-12-26 03:05:55 +0900288 std::vector<std::string> SuppressWarnings() const;
Jooyung Han965e31d2020-11-27 12:30:16 +0900289
290 // ToString is for dumping AIDL.
291 // Returns string representation of annotations.
292 // e.g) "@JavaDerive(toString=true) @RustDerive(Clone=true, Copy=true)"
Jiyong Park68bc77a2018-07-19 19:00:45 +0900293 std::string ToString() const;
Casey Dahline7922932016-02-29 17:23:01 -0800294
Jiyong Parka6605ab2018-11-11 14:30:21 +0900295 const vector<AidlAnnotation>& GetAnnotations() const { return annotations_; }
Jooyung Han888c5bc2020-12-22 17:28:47 +0900296 bool CheckValid(const AidlTypenames&) const;
Jiyong Park3656c3c2018-08-01 20:02:01 +0900297
Steven Moreland181144c2020-04-20 19:57:56 -0700298 protected:
Steven Moreland0cea4aa2020-04-20 21:06:02 -0700299 virtual std::set<AidlAnnotation::Type> GetSupportedAnnotations() const = 0;
Steven Moreland181144c2020-04-20 19:57:56 -0700300
Casey Dahline7922932016-02-29 17:23:01 -0800301 private:
Jiyong Parka6605ab2018-11-11 14:30:21 +0900302 vector<AidlAnnotation> annotations_;
Casey Dahline7922932016-02-29 17:23:01 -0800303};
304
Jiyong Park1deecc32018-07-17 01:14:41 +0900305// AidlTypeSpecifier represents a reference to either a built-in type,
306// a defined type, or a variant (e.g., array of generic) of a type.
Jeongik Chadf76dc72019-11-28 00:08:47 +0900307class AidlTypeSpecifier final : public AidlAnnotatable,
308 public AidlParameterizable<unique_ptr<AidlTypeSpecifier>> {
Casey Dahline7922932016-02-29 17:23:01 -0800309 public:
Steven Moreland46e9da82018-07-27 15:45:29 -0700310 AidlTypeSpecifier(const AidlLocation& location, const string& unresolved_name, bool is_array,
311 vector<unique_ptr<AidlTypeSpecifier>>* type_params, const string& comments);
Jiyong Parkd59a10d2018-07-18 11:12:55 +0900312 virtual ~AidlTypeSpecifier() = default;
Casey Dahlin0ee37582015-09-30 16:31:55 -0700313
Steven Moreland3f658cf2018-08-20 13:40:54 -0700314 // Copy of this type which is not an array.
Jooyung Hand2fa0232020-10-19 02:51:41 +0900315 const AidlTypeSpecifier& ArrayBase() const;
Steven Moreland3f658cf2018-08-20 13:40:54 -0700316
Jiyong Park1deecc32018-07-17 01:14:41 +0900317 // Returns the full-qualified name of the base type.
318 // int -> int
319 // int[] -> int
320 // List<String> -> List
321 // IFoo -> foo.bar.IFoo (if IFoo is in package foo.bar)
322 const string& GetName() const {
323 if (IsResolved()) {
324 return fully_qualified_name_;
325 } else {
326 return GetUnresolvedName();
327 }
328 }
Casey Dahlin0ee37582015-09-30 16:31:55 -0700329
Jooyung Han965e31d2020-11-27 12:30:16 +0900330 // ToString is for dumping AIDL.
331 // Returns string representation of this type specifier including annotations.
332 // This is "annotations type_name type_params? array_marker?".
333 // e.g) "@utf8InCpp String[]";
334 std::string ToString() const;
Jiyong Park1deecc32018-07-17 01:14:41 +0900335
Jooyung Han965e31d2020-11-27 12:30:16 +0900336 // Signature is for comparing AIDL types.
337 // Returns string representation of this type specifier.
338 // This is "type_name type_params? array_marker?".
339 // e.g.) "String[]" (even if it is annotated with @utf8InCpp)
Jiyong Park02da7422018-07-16 16:00:26 +0900340 std::string Signature() const;
341
Jiyong Park1deecc32018-07-17 01:14:41 +0900342 const string& GetUnresolvedName() const { return unresolved_name_; }
343
Jeongik Cha997281d2020-01-16 15:23:59 +0900344 bool IsHidden() const;
345
Jiyong Park1deecc32018-07-17 01:14:41 +0900346 const string& GetComments() const { return comments_; }
347
Jeongik Cha1a7ab642019-07-29 17:31:02 +0900348 const std::vector<std::string> GetSplitName() const { return split_name_; }
349
Jiyong Parka6605ab2018-11-11 14:30:21 +0900350 void SetComments(const string& comment) { comments_ = comment; }
351
Jiyong Park1deecc32018-07-17 01:14:41 +0900352 bool IsResolved() const { return fully_qualified_name_ != ""; }
353
354 bool IsArray() const { return is_array_; }
355
Steven Moreland6c07b832020-10-29 23:39:53 +0000356 __attribute__((warn_unused_result)) bool SetArray() {
357 if (is_array_) return false;
358 is_array_ = true;
359 return true;
360 }
361
Jiyong Park1deecc32018-07-17 01:14:41 +0900362 // Resolve the base type name to a fully-qualified name. Return false if the
363 // resolution fails.
Daniel Norman716d3112019-09-10 13:11:56 -0700364 bool Resolve(const AidlTypenames& typenames);
Casey Dahlin0ee37582015-09-30 16:31:55 -0700365
Steven Moreland0cea4aa2020-04-20 21:06:02 -0700366 std::set<AidlAnnotation::Type> GetSupportedAnnotations() const override;
Jooyung Han888c5bc2020-12-22 17:28:47 +0900367 bool CheckValid(const AidlTypenames& typenames) const;
Steven Morelandd59e3172020-05-11 16:42:09 -0700368 bool LanguageSpecificCheckValid(const AidlTypenames& typenames, Options::Language lang) const;
Jeongik Chadf76dc72019-11-28 00:08:47 +0900369 const AidlNode& AsAidlNode() const override { return *this; }
Jiyong Park1d2df7d2018-07-23 15:22:50 +0900370
Jooyung Hane9bb9de2020-11-01 22:16:57 +0900371 const AidlDefinedType* GetDefinedType() const;
372
Casey Dahlin0ee37582015-09-30 16:31:55 -0700373 private:
Steven Moreland3f658cf2018-08-20 13:40:54 -0700374 AidlTypeSpecifier(const AidlTypeSpecifier&) = default;
375
Jiyong Park1deecc32018-07-17 01:14:41 +0900376 const string unresolved_name_;
377 string fully_qualified_name_;
Steven Moreland3f658cf2018-08-20 13:40:54 -0700378 bool is_array_;
Jiyong Parka6605ab2018-11-11 14:30:21 +0900379 string comments_;
Jeongik Cha1a7ab642019-07-29 17:31:02 +0900380 vector<string> split_name_;
Jooyung Han690f5842020-12-04 13:02:04 +0900381 const AidlDefinedType* defined_type_ = nullptr; // set when Resolve() for defined types
Jooyung Hand2fa0232020-10-19 02:51:41 +0900382 mutable shared_ptr<AidlTypeSpecifier> array_base_;
Casey Dahlin0ee37582015-09-30 16:31:55 -0700383};
384
Steven Moreland860b1942018-08-16 14:59:28 -0700385// Returns the universal value unaltered.
386std::string AidlConstantValueDecorator(const AidlTypeSpecifier& type, const std::string& raw_value);
387
Steven Moreland9ea10e32018-07-19 15:26:09 -0700388class AidlConstantValue;
Jooyung Han3f347ca2020-12-01 12:41:50 +0900389class AidlMethod;
390class AidlConstantDeclaration;
391class AidlVariableDeclaration;
392
Jiyong Park512ed852020-12-30 15:07:23 +0900393class AidlMember : public AidlNode, public AidlTraversable {
Jooyung Han3f347ca2020-12-01 12:41:50 +0900394 public:
395 AidlMember(const AidlLocation& location);
396 virtual ~AidlMember() = default;
397
398 // non-copyable, non-movable
399 AidlMember(const AidlMember&) = delete;
400 AidlMember(AidlMember&&) = delete;
401 AidlMember& operator=(const AidlMember&) = delete;
402 AidlMember& operator=(AidlMember&&) = delete;
403
Jooyung Han829ec7c2020-12-02 12:07:36 +0900404 virtual const AidlMethod* AsMethod() const { return nullptr; }
405 virtual const AidlConstantDeclaration* AsConstantDeclaration() const { return nullptr; }
406 virtual const AidlVariableDeclaration* AsVariableDeclaration() const { return nullptr; }
407
408 AidlMethod* AsMethod() {
409 return const_cast<AidlMethod*>(const_cast<const AidlMember*>(this)->AsMethod());
410 }
411 AidlConstantDeclaration* AsConstantDeclaration() {
412 return const_cast<AidlConstantDeclaration*>(
413 const_cast<const AidlMember*>(this)->AsConstantDeclaration());
414 }
415 AidlVariableDeclaration* AsVariableDeclaration() {
416 return const_cast<AidlVariableDeclaration*>(
417 const_cast<const AidlMember*>(this)->AsVariableDeclaration());
418 }
Jooyung Han808a2a02020-12-28 16:46:54 +0900419
Jiyong Park512ed852020-12-30 15:07:23 +0900420 void TraverseChildren(std::function<void(const AidlTraversable&)> traverse) const = 0;
421 bool DispatchVisit(AidlVisitor& v) const = 0;
Jooyung Han3f347ca2020-12-01 12:41:50 +0900422};
423
Steven Moreland541788d2020-05-21 22:05:52 +0000424// TODO: This class is used for method arguments and also parcelable fields,
425// and it should be split up since default values don't apply to method
426// arguments
Jooyung Han3f347ca2020-12-01 12:41:50 +0900427class AidlVariableDeclaration : public AidlMember {
Steven Moreland5557f1c2018-07-02 13:50:23 -0700428 public:
Steven Moreland46e9da82018-07-27 15:45:29 -0700429 AidlVariableDeclaration(const AidlLocation& location, AidlTypeSpecifier* type,
430 const std::string& name);
431 AidlVariableDeclaration(const AidlLocation& location, AidlTypeSpecifier* type,
432 const std::string& name, AidlConstantValue* default_value);
Steven Moreland5557f1c2018-07-02 13:50:23 -0700433 virtual ~AidlVariableDeclaration() = default;
434
Jiyong Parkd800fef2020-07-22 18:09:43 +0900435 // non-copyable, non-movable
436 AidlVariableDeclaration(const AidlVariableDeclaration&) = delete;
437 AidlVariableDeclaration(AidlVariableDeclaration&&) = delete;
438 AidlVariableDeclaration& operator=(const AidlVariableDeclaration&) = delete;
439 AidlVariableDeclaration& operator=(AidlVariableDeclaration&&) = delete;
440
Jooyung Han829ec7c2020-12-02 12:07:36 +0900441 const AidlVariableDeclaration* AsVariableDeclaration() const override { return this; }
Jooyung Han3f347ca2020-12-01 12:41:50 +0900442
Steven Moreland5557f1c2018-07-02 13:50:23 -0700443 std::string GetName() const { return name_; }
Jooyung Hanacae85d2020-10-28 16:39:09 +0900444 std::string GetCapitalizedName() const;
Jiyong Parkd59a10d2018-07-18 11:12:55 +0900445 const AidlTypeSpecifier& GetType() const { return *type_; }
Steven Moreland541788d2020-05-21 22:05:52 +0000446 // if this was constructed explicitly with a default value
447 bool IsDefaultUserSpecified() const { return default_user_specified_; }
448 // will return the default value this is constructed with or a default value
449 // if one is available
Steven Moreland9ea10e32018-07-19 15:26:09 -0700450 const AidlConstantValue* GetDefaultValue() const { return default_value_.get(); }
Jooyung Han53fb4242020-12-17 16:03:49 +0900451 bool HasUsefulDefaultValue() const;
Steven Moreland9ea10e32018-07-19 15:26:09 -0700452
Jiyong Parkd59a10d2018-07-18 11:12:55 +0900453 AidlTypeSpecifier* GetMutableType() { return type_.get(); }
Steven Moreland5557f1c2018-07-02 13:50:23 -0700454
Jeongik Chadb0f59e2018-11-01 18:11:21 +0900455 bool CheckValid(const AidlTypenames& typenames) const;
Jooyung Han965e31d2020-11-27 12:30:16 +0900456
457 // ToString is for dumping AIDL.
458 // Returns string representation of this variable decl including default value.
459 // This is "annotations type name default_value?".
460 // e.g) "@utf8InCpp String[] names = {"hello"}"
Steven Moreland5557f1c2018-07-02 13:50:23 -0700461 std::string ToString() const;
Jooyung Han965e31d2020-11-27 12:30:16 +0900462
463 // Signature is for comparing AIDL types.
464 // Returns string representation of this variable decl.
465 // This is "type name".
466 // e.g) "String[] name" (even if it is annotated with @utf8InCpp and has a default value.)
Jiyong Park02da7422018-07-16 16:00:26 +0900467 std::string Signature() const;
Steven Moreland5557f1c2018-07-02 13:50:23 -0700468
Steven Moreland860b1942018-08-16 14:59:28 -0700469 std::string ValueString(const ConstantValueDecorator& decorator) const;
Steven Moreland25294322018-08-07 18:13:55 -0700470
Jiyong Park512ed852020-12-30 15:07:23 +0900471 void TraverseChildren(std::function<void(const AidlTraversable&)>) const override {
472 // no children to visit
473 }
474 bool DispatchVisit(AidlVisitor& v) const override { return v.Visit(*this); }
475
Steven Moreland5557f1c2018-07-02 13:50:23 -0700476 private:
Jiyong Parkd59a10d2018-07-18 11:12:55 +0900477 std::unique_ptr<AidlTypeSpecifier> type_;
Steven Moreland5557f1c2018-07-02 13:50:23 -0700478 std::string name_;
Steven Moreland541788d2020-05-21 22:05:52 +0000479 bool default_user_specified_;
Steven Moreland9ea10e32018-07-19 15:26:09 -0700480 std::unique_ptr<AidlConstantValue> default_value_;
Steven Moreland5557f1c2018-07-02 13:50:23 -0700481};
482
483class AidlArgument : public AidlVariableDeclaration {
Casey Dahlinbc7a50a2015-09-28 19:20:50 -0700484 public:
Casey Dahlinc378c992015-09-29 16:50:40 -0700485 enum Direction { IN_DIR = 1, OUT_DIR = 2, INOUT_DIR = 3 };
486
Steven Moreland46e9da82018-07-27 15:45:29 -0700487 AidlArgument(const AidlLocation& location, AidlArgument::Direction direction,
488 AidlTypeSpecifier* type, const std::string& name);
489 AidlArgument(const AidlLocation& location, AidlTypeSpecifier* type, const std::string& name);
Casey Dahlinbc7a50a2015-09-28 19:20:50 -0700490 virtual ~AidlArgument() = default;
491
Jiyong Parkd800fef2020-07-22 18:09:43 +0900492 // non-copyable, non-movable
493 AidlArgument(const AidlArgument&) = delete;
494 AidlArgument(AidlArgument&&) = delete;
495 AidlArgument& operator=(const AidlArgument&) = delete;
496 AidlArgument& operator=(AidlArgument&&) = delete;
497
Casey Dahlinc378c992015-09-29 16:50:40 -0700498 Direction GetDirection() const { return direction_; }
Christopher Wileyad339272015-10-05 19:11:58 -0700499 bool IsOut() const { return direction_ & OUT_DIR; }
500 bool IsIn() const { return direction_ & IN_DIR; }
Casey Dahlinc378c992015-09-29 16:50:40 -0700501 bool DirectionWasSpecified() const { return direction_specified_; }
Jiyong Park3656c3c2018-08-01 20:02:01 +0900502 string GetDirectionSpecifier() const;
Christopher Wileyad339272015-10-05 19:11:58 -0700503
Jooyung Han965e31d2020-11-27 12:30:16 +0900504 // ToString is for dumping AIDL.
505 // Returns string representation of this argument including direction
506 // This is "direction annotations type name".
507 // e.g) "in @utf8InCpp String[] names"
Casey Dahlinc378c992015-09-29 16:50:40 -0700508 std::string ToString() const;
Jiyong Park512ed852020-12-30 15:07:23 +0900509
510 void TraverseChildren(std::function<void(const AidlTraversable&)>) const override {
511 // no children to visit
512 }
513 bool DispatchVisit(AidlVisitor& v) const override { return v.Visit(*this); }
Casey Dahlinc378c992015-09-29 16:50:40 -0700514
Casey Dahlinbc7a50a2015-09-28 19:20:50 -0700515 private:
Casey Dahlinc378c992015-09-29 16:50:40 -0700516 Direction direction_;
517 bool direction_specified_;
Casey Dahlina834dd42015-09-23 11:52:15 -0700518};
Adam Lesinskiffa16862014-01-23 18:17:42 -0800519
Will McVickerd7d18df2019-09-12 13:40:50 -0700520class AidlUnaryConstExpression;
521class AidlBinaryConstExpression;
Jooyung Han690f5842020-12-04 13:02:04 +0900522class AidlConstantReference;
Will McVickerd7d18df2019-09-12 13:40:50 -0700523
Steven Moreland693640b2018-07-19 13:46:27 -0700524class AidlConstantValue : public AidlNode {
Casey Dahlind40e2fe2015-11-24 14:06:52 -0800525 public:
Will McVickerd7d18df2019-09-12 13:40:50 -0700526 enum class Type {
527 // WARNING: Don't change this order! The order is used to determine type
528 // promotion during a binary expression.
529 BOOLEAN,
530 INT8,
531 INT32,
532 INT64,
533 ARRAY,
534 CHARACTER,
535 STRING,
Jooyung Han690f5842020-12-04 13:02:04 +0900536 REF,
Will McVickerd7d18df2019-09-12 13:40:50 -0700537 FLOATING,
538 UNARY,
539 BINARY,
540 ERROR,
541 };
542
Jooyung Han690f5842020-12-04 13:02:04 +0900543 struct Visitor {
544 virtual ~Visitor() {}
545 virtual void Visit(AidlConstantValue&) = 0;
546 virtual void Visit(AidlConstantReference&) = 0;
547 virtual void Visit(AidlUnaryConstExpression&) = 0;
548 virtual void Visit(AidlBinaryConstExpression&) = 0;
549 };
550
Jooyung Han535c5e82020-12-29 15:16:59 +0900551 // Returns the evaluated value. T> should match to the actual type.
Will McVickerd7d18df2019-09-12 13:40:50 -0700552 template <typename T>
Jooyung Han535c5e82020-12-29 15:16:59 +0900553 T EvaluatedValue() const {
554 is_evaluated_ || (CheckValid() && evaluate());
555 AIDL_FATAL_IF(!is_valid_, this);
556
557 if constexpr (is_vector<T>::value) {
558 AIDL_FATAL_IF(final_type_ != Type::ARRAY, this);
559 T result;
560 for (const auto& v : values_) {
561 result.push_back(v->EvaluatedValue<typename T::value_type>());
562 }
563 return result;
564 } else if constexpr (is_one_of<T, float, double>::value) {
565 AIDL_FATAL_IF(final_type_ != Type::FLOATING, this);
566 T result;
567 AIDL_FATAL_IF(!ParseFloating(value_, &result), this);
568 return result;
569 } else if constexpr (std::is_same<T, std::string>::value) {
570 AIDL_FATAL_IF(final_type_ != Type::STRING, this);
571 return final_string_value_.substr(1, final_string_value_.size() - 2); // unquote "
572 } else if constexpr (is_one_of<T, int8_t, int32_t, int64_t>::value) {
573 AIDL_FATAL_IF(final_type_ < Type::INT8 && final_type_ > Type::INT64, this);
574 return static_cast<T>(final_value_);
575 } else if constexpr (std::is_same<T, char>::value) {
576 AIDL_FATAL_IF(final_type_ != Type::CHARACTER, this);
577 return final_string_value_.at(1); // unquote '
578 } else if constexpr (std::is_same<T, bool>::value) {
579 static_assert(std::is_same<T, bool>::value, "..");
580 AIDL_FATAL_IF(final_type_ != Type::BOOLEAN, this);
581 return final_value_ != 0;
582 } else {
583 static_assert(unsupported_type<T>::value);
584 }
585 }
Casey Dahlind40e2fe2015-11-24 14:06:52 -0800586
Steven Moreland693640b2018-07-19 13:46:27 -0700587 virtual ~AidlConstantValue() = default;
Casey Dahlind40e2fe2015-11-24 14:06:52 -0800588
Jiyong Parkd800fef2020-07-22 18:09:43 +0900589 // non-copyable, non-movable
590 AidlConstantValue(const AidlConstantValue&) = delete;
591 AidlConstantValue(AidlConstantValue&&) = delete;
592 AidlConstantValue& operator=(const AidlConstantValue&) = delete;
593 AidlConstantValue& operator=(AidlConstantValue&&) = delete;
594
Steven Moreland541788d2020-05-21 22:05:52 +0000595 // creates default value, when one isn't specified
596 // nullptr if no default available
597 static AidlConstantValue* Default(const AidlTypeSpecifier& specifier);
598
Steven Moreland25294322018-08-07 18:13:55 -0700599 static AidlConstantValue* Boolean(const AidlLocation& location, bool value);
600 static AidlConstantValue* Character(const AidlLocation& location, char value);
Steven Moreland25294322018-08-07 18:13:55 -0700601 // example: 123, -5498, maybe any size
Will McVickerd7d18df2019-09-12 13:40:50 -0700602 static AidlConstantValue* Integral(const AidlLocation& location, const string& value);
603 static AidlConstantValue* Floating(const AidlLocation& location, const std::string& value);
Steven Moreland860b1942018-08-16 14:59:28 -0700604 static AidlConstantValue* Array(const AidlLocation& location,
Will McVickerd7d18df2019-09-12 13:40:50 -0700605 std::unique_ptr<vector<unique_ptr<AidlConstantValue>>> values);
Steven Moreland693640b2018-07-19 13:46:27 -0700606 // example: "\"asdf\""
Will McVickerd7d18df2019-09-12 13:40:50 -0700607 static AidlConstantValue* String(const AidlLocation& location, const string& value);
Steven Moreland693640b2018-07-19 13:46:27 -0700608
Will McVickerd7d18df2019-09-12 13:40:50 -0700609 Type GetType() const { return final_type_; }
Jooyung Han29813842020-12-08 01:28:03 +0900610 const std::string& Literal() const { return value_; }
Steven Moreland25294322018-08-07 18:13:55 -0700611
Will McVickerd7d18df2019-09-12 13:40:50 -0700612 virtual bool CheckValid() const;
Steven Moreland860b1942018-08-16 14:59:28 -0700613
614 // Raw value of type (currently valid in C++ and Java). Empty string on error.
Steven Moreland4bcb05c2019-11-27 18:57:47 -0800615 string ValueString(const AidlTypeSpecifier& type, const ConstantValueDecorator& decorator) const;
Jooyung Han29813842020-12-08 01:28:03 +0900616 virtual void Accept(Visitor& visitor) {
617 visitor.Visit(*this);
618 if (type_ == Type::ARRAY) {
619 for (const auto& v : values_) {
620 v.get()->Accept(visitor);
621 }
622 }
623 }
Casey Dahlind40e2fe2015-11-24 14:06:52 -0800624
625 private:
Will McVickerd7d18df2019-09-12 13:40:50 -0700626 AidlConstantValue(const AidlLocation& location, Type parsed_type, int64_t parsed_value,
627 const string& checked_value);
628 AidlConstantValue(const AidlLocation& location, Type type, const string& checked_value);
Steven Moreland860b1942018-08-16 14:59:28 -0700629 AidlConstantValue(const AidlLocation& location, Type type,
Jooyung Han29813842020-12-08 01:28:03 +0900630 std::unique_ptr<vector<unique_ptr<AidlConstantValue>>> values,
631 const std::string& value);
Steven Moreland25294322018-08-07 18:13:55 -0700632 static string ToString(Type type);
Will McVickerd7d18df2019-09-12 13:40:50 -0700633 static bool ParseIntegral(const string& value, int64_t* parsed_value, Type* parsed_type);
634 static bool IsHex(const string& value);
Steven Moreland4bcb05c2019-11-27 18:57:47 -0800635
Jooyung Han74675c22020-12-15 08:39:57 +0900636 virtual bool evaluate() const;
Casey Dahlind40e2fe2015-11-24 14:06:52 -0800637
Steven Moreland693640b2018-07-19 13:46:27 -0700638 const Type type_ = Type::ERROR;
Will McVickerd7d18df2019-09-12 13:40:50 -0700639 const vector<unique_ptr<AidlConstantValue>> values_; // if type_ == ARRAY
640 const string value_; // otherwise
641
642 // State for tracking evaluation of expressions
Steven Moreland4bcb05c2019-11-27 18:57:47 -0800643 mutable bool is_valid_ = false; // cache of CheckValid, but may be marked false in evaluate
644 mutable bool is_evaluated_ = false; // whether evaluate has been called
Will McVickerd7d18df2019-09-12 13:40:50 -0700645 mutable Type final_type_;
646 mutable int64_t final_value_;
647 mutable string final_string_value_ = "";
Steven Moreland693640b2018-07-19 13:46:27 -0700648
Will McVickerd7d18df2019-09-12 13:40:50 -0700649 friend AidlUnaryConstExpression;
650 friend AidlBinaryConstExpression;
Jooyung Han690f5842020-12-04 13:02:04 +0900651 friend AidlConstantReference;
652};
653
654// Represents "<type>.<field>" which resolves to a constant which is one of
655// - constant declartion
656// - enumerator
657// When a <type> is missing, <field> is of the enclosing type.
658class AidlConstantReference : public AidlConstantValue {
659 public:
660 AidlConstantReference(const AidlLocation& location, const std::string& value,
661 const std::string& comments);
662
663 const std::unique_ptr<AidlTypeSpecifier>& GetRefType() const { return ref_type_; }
664 void SetRefType(std::unique_ptr<AidlTypeSpecifier> type) { ref_type_ = std::move(type); }
665 const std::string& GetFieldName() const { return field_name_; }
666 const std::string& GetComments() const { return comments_; }
667
668 bool CheckValid() const override;
669 void Accept(Visitor& visitor) override { visitor.Visit(*this); }
Jooyung Han29813842020-12-08 01:28:03 +0900670 const AidlConstantValue* Resolve();
Jooyung Han690f5842020-12-04 13:02:04 +0900671
672 private:
Jooyung Han74675c22020-12-15 08:39:57 +0900673 bool evaluate() const override;
Jooyung Han690f5842020-12-04 13:02:04 +0900674
675 std::unique_ptr<AidlTypeSpecifier> ref_type_;
676 std::string field_name_;
677 const std::string comments_;
Jooyung Han29813842020-12-08 01:28:03 +0900678 const AidlConstantValue* resolved_ = nullptr;
Will McVickerd7d18df2019-09-12 13:40:50 -0700679};
680
681class AidlUnaryConstExpression : public AidlConstantValue {
682 public:
683 AidlUnaryConstExpression(const AidlLocation& location, const string& op,
684 std::unique_ptr<AidlConstantValue> rval);
685
686 static bool IsCompatibleType(Type type, const string& op);
687 bool CheckValid() const override;
Jooyung Han690f5842020-12-04 13:02:04 +0900688 void Accept(Visitor& visitor) override {
689 visitor.Visit(*this);
690 unary_->Accept(visitor);
691 }
692
Will McVickerd7d18df2019-09-12 13:40:50 -0700693 private:
Jooyung Han74675c22020-12-15 08:39:57 +0900694 bool evaluate() const override;
Will McVickerd7d18df2019-09-12 13:40:50 -0700695
696 std::unique_ptr<AidlConstantValue> unary_;
697 const string op_;
698};
699
700class AidlBinaryConstExpression : public AidlConstantValue {
701 public:
702 AidlBinaryConstExpression(const AidlLocation& location, std::unique_ptr<AidlConstantValue> lval,
703 const string& op, std::unique_ptr<AidlConstantValue> rval);
704
705 bool CheckValid() const override;
Will McVickerd7d18df2019-09-12 13:40:50 -0700706
707 static bool AreCompatibleTypes(Type t1, Type t2);
708 // Returns the promoted kind for both operands
709 static Type UsualArithmeticConversion(Type left, Type right);
710 // Returns the promoted integral type where INT32 is the smallest type
711 static Type IntegralPromotion(Type in);
Jooyung Han690f5842020-12-04 13:02:04 +0900712 void Accept(Visitor& visitor) override {
713 visitor.Visit(*this);
714 left_val_->Accept(visitor);
715 right_val_->Accept(visitor);
716 }
Will McVickerd7d18df2019-09-12 13:40:50 -0700717
718 private:
Jooyung Han74675c22020-12-15 08:39:57 +0900719 bool evaluate() const override;
Will McVickerd7d18df2019-09-12 13:40:50 -0700720
721 std::unique_ptr<AidlConstantValue> left_val_;
722 std::unique_ptr<AidlConstantValue> right_val_;
723 const string op_;
Christopher Wileyd6bdd8d2016-05-03 11:23:13 -0700724};
725
Andrei Onea9445fc62019-06-27 18:11:59 +0100726struct AidlAnnotationParameter {
727 std::string name;
728 std::unique_ptr<AidlConstantValue> value;
729};
730
Steven Moreland693640b2018-07-19 13:46:27 -0700731class AidlConstantDeclaration : public AidlMember {
Christopher Wileyd6bdd8d2016-05-03 11:23:13 -0700732 public:
Steven Moreland46e9da82018-07-27 15:45:29 -0700733 AidlConstantDeclaration(const AidlLocation& location, AidlTypeSpecifier* specifier,
Will McVickerd7d18df2019-09-12 13:40:50 -0700734 const string& name, AidlConstantValue* value);
Steven Moreland693640b2018-07-19 13:46:27 -0700735 virtual ~AidlConstantDeclaration() = default;
Christopher Wileyd6bdd8d2016-05-03 11:23:13 -0700736
Jiyong Parkd800fef2020-07-22 18:09:43 +0900737 // non-copyable, non-movable
738 AidlConstantDeclaration(const AidlConstantDeclaration&) = delete;
739 AidlConstantDeclaration(AidlConstantDeclaration&&) = delete;
740 AidlConstantDeclaration& operator=(const AidlConstantDeclaration&) = delete;
741 AidlConstantDeclaration& operator=(AidlConstantDeclaration&&) = delete;
742
Steven Moreland693640b2018-07-19 13:46:27 -0700743 const AidlTypeSpecifier& GetType() const { return *type_; }
Steven Moreland4d12f9a2018-10-31 14:30:55 -0700744 AidlTypeSpecifier* GetMutableType() { return type_.get(); }
Will McVickerd7d18df2019-09-12 13:40:50 -0700745 const string& GetName() const { return name_; }
Steven Moreland693640b2018-07-19 13:46:27 -0700746 const AidlConstantValue& GetValue() const { return *value_; }
Jeongik Chadb0f59e2018-11-01 18:11:21 +0900747 bool CheckValid(const AidlTypenames& typenames) const;
Christopher Wileyd6bdd8d2016-05-03 11:23:13 -0700748
Jooyung Han965e31d2020-11-27 12:30:16 +0900749 // ToString is for dumping AIDL.
750 // Returns string representation of this const decl including a const value.
751 // This is "`const` annotations type name value".
752 // e.g) "const @utf8InCpp String[] names = { "hello" }"
Will McVickerd7d18df2019-09-12 13:40:50 -0700753 string ToString() const;
Jooyung Han965e31d2020-11-27 12:30:16 +0900754
755 // Signature is for comparing types.
756 // Returns string representation of this const decl.
757 // This is "direction annotations type name".
758 // e.g) "String[] names"
Will McVickerd7d18df2019-09-12 13:40:50 -0700759 string Signature() const;
Jooyung Han965e31d2020-11-27 12:30:16 +0900760
Steven Moreland860b1942018-08-16 14:59:28 -0700761 string ValueString(const ConstantValueDecorator& decorator) const {
Will McVickerd7d18df2019-09-12 13:40:50 -0700762 return value_->ValueString(GetType(), decorator);
Steven Moreland860b1942018-08-16 14:59:28 -0700763 }
Steven Moreland25294322018-08-07 18:13:55 -0700764
Jooyung Han829ec7c2020-12-02 12:07:36 +0900765 const AidlConstantDeclaration* AsConstantDeclaration() const override { return this; }
Christopher Wileyd6bdd8d2016-05-03 11:23:13 -0700766
Jiyong Park512ed852020-12-30 15:07:23 +0900767 void TraverseChildren(std::function<void(const AidlTraversable&)>) const override {
768 // no children to traverse
769 }
770 bool DispatchVisit(AidlVisitor& v) const override { return v.Visit(*this); }
771
Christopher Wileyd6bdd8d2016-05-03 11:23:13 -0700772 private:
Steven Moreland693640b2018-07-19 13:46:27 -0700773 const unique_ptr<AidlTypeSpecifier> type_;
Will McVickerd7d18df2019-09-12 13:40:50 -0700774 const string name_;
775 unique_ptr<AidlConstantValue> value_;
Casey Dahlind40e2fe2015-11-24 14:06:52 -0800776};
777
778class AidlMethod : public AidlMember {
Casey Dahlin5c69deb2015-10-01 14:44:12 -0700779 public:
Will McVickerd7d18df2019-09-12 13:40:50 -0700780 AidlMethod(const AidlLocation& location, bool oneway, AidlTypeSpecifier* type, const string& name,
781 vector<unique_ptr<AidlArgument>>* args, const string& comments);
782 AidlMethod(const AidlLocation& location, bool oneway, AidlTypeSpecifier* type, const string& name,
783 vector<unique_ptr<AidlArgument>>* args, const string& comments, int id,
784 bool is_user_defined = true);
Casey Dahlin5c69deb2015-10-01 14:44:12 -0700785 virtual ~AidlMethod() = default;
786
Jiyong Parkd800fef2020-07-22 18:09:43 +0900787 // non-copyable, non-movable
788 AidlMethod(const AidlMethod&) = delete;
789 AidlMethod(AidlMethod&&) = delete;
790 AidlMethod& operator=(const AidlMethod&) = delete;
791 AidlMethod& operator=(AidlMethod&&) = delete;
792
Jooyung Han829ec7c2020-12-02 12:07:36 +0900793 const AidlMethod* AsMethod() const override { return this; }
Jeongik Cha997281d2020-01-16 15:23:59 +0900794 bool IsHidden() const;
Will McVickerd7d18df2019-09-12 13:40:50 -0700795 const string& GetComments() const { return comments_; }
Jiyong Parkd59a10d2018-07-18 11:12:55 +0900796 const AidlTypeSpecifier& GetType() const { return *type_; }
797 AidlTypeSpecifier* GetMutableType() { return type_.get(); }
Steven Morelandacd53472018-12-14 10:17:26 -0800798
Steven Moreland8c70ba92018-12-17 10:20:31 -0800799 // set if this method is part of an interface that is marked oneway
800 void ApplyInterfaceOneway(bool oneway) { oneway_ = oneway_ || oneway; }
Casey Dahlinf4a93112015-10-05 16:58:09 -0700801 bool IsOneway() const { return oneway_; }
Steven Morelandacd53472018-12-14 10:17:26 -0800802
Casey Dahlinf4a93112015-10-05 16:58:09 -0700803 const std::string& GetName() const { return name_; }
Casey Dahlinf4a93112015-10-05 16:58:09 -0700804 bool HasId() const { return has_id_; }
Jiyong Parked65bf42018-08-28 15:43:27 +0900805 int GetId() const { return id_; }
Casey Dahlinf4a93112015-10-05 16:58:09 -0700806 void SetId(unsigned id) { id_ = id; }
Casey Dahlinf2d23f72015-10-02 16:19:19 -0700807
Jiyong Park309668e2018-07-28 16:55:44 +0900808 bool IsUserDefined() const { return is_user_defined_; }
809
Casey Dahlinf4a93112015-10-05 16:58:09 -0700810 const std::vector<std::unique_ptr<AidlArgument>>& GetArguments() const {
Christopher Wileyad339272015-10-05 19:11:58 -0700811 return arguments_;
812 }
813 // An inout parameter will appear in both GetInArguments()
814 // and GetOutArguments(). AidlMethod retains ownership of the argument
815 // pointers returned in this way.
816 const std::vector<const AidlArgument*>& GetInArguments() const {
817 return in_arguments_;
818 }
819 const std::vector<const AidlArgument*>& GetOutArguments() const {
820 return out_arguments_;
Casey Dahlinf4a93112015-10-05 16:58:09 -0700821 }
Casey Dahlin5c69deb2015-10-01 14:44:12 -0700822
Jooyung Han965e31d2020-11-27 12:30:16 +0900823 // ToString is for dumping AIDL.
824 // Returns string representation of this method including everything.
825 // This is "ret_type name ( arg_list ) = id".
826 // e.g) "boolean foo(int, @Nullable String) = 1"
Jiyong Park309668e2018-07-28 16:55:44 +0900827 std::string ToString() const;
828
Jooyung Han965e31d2020-11-27 12:30:16 +0900829 // Signature is for comparing AIDL types.
830 // Returns string representation of this method's name & type.
831 // e.g) "foo(int, String)"
832 std::string Signature() const;
833
Jiyong Park512ed852020-12-30 15:07:23 +0900834 void TraverseChildren(std::function<void(const AidlTraversable&)> traverse) const override {
835 for (const auto& a : GetArguments()) {
836 traverse(*a);
837 }
838 }
839 bool DispatchVisit(AidlVisitor& v) const override { return v.Visit(*this); }
Jooyung Han808a2a02020-12-28 16:46:54 +0900840
Casey Dahlin5c69deb2015-10-01 14:44:12 -0700841 private:
Casey Dahlinf4a93112015-10-05 16:58:09 -0700842 bool oneway_;
Casey Dahlinf2d23f72015-10-02 16:19:19 -0700843 std::string comments_;
Jiyong Parkd59a10d2018-07-18 11:12:55 +0900844 std::unique_ptr<AidlTypeSpecifier> type_;
Casey Dahlinf4a93112015-10-05 16:58:09 -0700845 std::string name_;
Christopher Wileyad339272015-10-05 19:11:58 -0700846 const std::vector<std::unique_ptr<AidlArgument>> arguments_;
847 std::vector<const AidlArgument*> in_arguments_;
848 std::vector<const AidlArgument*> out_arguments_;
Casey Dahlinf4a93112015-10-05 16:58:09 -0700849 bool has_id_;
850 int id_;
Jiyong Park309668e2018-07-28 16:55:44 +0900851 bool is_user_defined_ = true;
Casey Dahlin0a2f8be2015-09-28 16:15:29 -0700852};
Adam Lesinskiffa16862014-01-23 18:17:42 -0800853
Steven Morelandc258abc2018-07-10 14:03:38 -0700854class AidlDefinedType;
Jiyong Parkb034bf02018-07-30 17:44:33 +0900855class AidlInterface;
856class AidlParcelable;
857class AidlStructuredParcelable;
Jooyung Han2946afc2020-10-05 20:29:16 +0900858class AidlUnionDecl;
Jooyung Han3f347ca2020-12-01 12:41:50 +0900859
Daniel Norman85aed542019-08-21 12:01:14 -0700860// AidlDefinedType represents either an interface, parcelable, or enum that is
Jiyong Park1deecc32018-07-17 01:14:41 +0900861// defined in the source file.
Jiyong Park512ed852020-12-30 15:07:23 +0900862class AidlDefinedType : public AidlAnnotatable, public AidlTraversable {
Steven Moreland787b0432018-07-03 09:00:58 -0700863 public:
Steven Moreland46e9da82018-07-27 15:45:29 -0700864 AidlDefinedType(const AidlLocation& location, const std::string& name,
Jooyung Han829ec7c2020-12-02 12:07:36 +0900865 const std::string& comments, const std::string& package,
866 std::vector<std::unique_ptr<AidlMember>>* members);
Steven Moreland787b0432018-07-03 09:00:58 -0700867 virtual ~AidlDefinedType() = default;
868
Jiyong Parkd800fef2020-07-22 18:09:43 +0900869 // non-copyable, non-movable
870 AidlDefinedType(const AidlDefinedType&) = delete;
871 AidlDefinedType(AidlDefinedType&&) = delete;
872 AidlDefinedType& operator=(const AidlDefinedType&) = delete;
873 AidlDefinedType& operator=(AidlDefinedType&&) = delete;
874
Jiyong Park1deecc32018-07-17 01:14:41 +0900875 const std::string& GetName() const { return name_; };
Jeongik Cha997281d2020-01-16 15:23:59 +0900876 bool IsHidden() const;
Jiyong Park1deecc32018-07-17 01:14:41 +0900877 const std::string& GetComments() const { return comments_; }
Jiyong Parka6605ab2018-11-11 14:30:21 +0900878 void SetComments(const std::string comments) { comments_ = comments; }
Jiyong Park1deecc32018-07-17 01:14:41 +0900879
Steven Moreland787b0432018-07-03 09:00:58 -0700880 /* dot joined package, example: "android.package.foo" */
Jiyong Park18132182020-06-08 20:24:40 +0900881 std::string GetPackage() const { return package_; }
Steven Moreland787b0432018-07-03 09:00:58 -0700882 /* dot joined package and name, example: "android.package.foo.IBar" */
883 std::string GetCanonicalName() const;
Jiyong Park18132182020-06-08 20:24:40 +0900884 const std::vector<std::string>& GetSplitPackage() const { return split_package_; }
Steven Moreland787b0432018-07-03 09:00:58 -0700885
Steven Morelanded83a282018-07-17 13:27:29 -0700886 virtual std::string GetPreprocessDeclarationName() const = 0;
Steven Morelandc258abc2018-07-10 14:03:38 -0700887
Steven Moreland5557f1c2018-07-02 13:50:23 -0700888 virtual const AidlStructuredParcelable* AsStructuredParcelable() const { return nullptr; }
Steven Morelandc258abc2018-07-10 14:03:38 -0700889 virtual const AidlParcelable* AsParcelable() const { return nullptr; }
Daniel Norman85aed542019-08-21 12:01:14 -0700890 virtual const AidlEnumDeclaration* AsEnumDeclaration() const { return nullptr; }
Jooyung Han2946afc2020-10-05 20:29:16 +0900891 virtual const AidlUnionDecl* AsUnionDeclaration() const { return nullptr; }
Steven Moreland5557f1c2018-07-02 13:50:23 -0700892 virtual const AidlInterface* AsInterface() const { return nullptr; }
Jeongik Chadf76dc72019-11-28 00:08:47 +0900893 virtual const AidlParameterizable<std::string>* AsParameterizable() const { return nullptr; }
Jooyung Han808a2a02020-12-28 16:46:54 +0900894 virtual bool CheckValid(const AidlTypenames& typenames) const;
Steven Morelandd59e3172020-05-11 16:42:09 -0700895 virtual bool LanguageSpecificCheckValid(const AidlTypenames& typenames,
896 Options::Language lang) const = 0;
Steven Morelandc258abc2018-07-10 14:03:38 -0700897 AidlStructuredParcelable* AsStructuredParcelable() {
898 return const_cast<AidlStructuredParcelable*>(
899 const_cast<const AidlDefinedType*>(this)->AsStructuredParcelable());
900 }
901 AidlParcelable* AsParcelable() {
902 return const_cast<AidlParcelable*>(const_cast<const AidlDefinedType*>(this)->AsParcelable());
903 }
Daniel Norman85aed542019-08-21 12:01:14 -0700904 AidlEnumDeclaration* AsEnumDeclaration() {
905 return const_cast<AidlEnumDeclaration*>(
906 const_cast<const AidlDefinedType*>(this)->AsEnumDeclaration());
907 }
Jooyung Han2946afc2020-10-05 20:29:16 +0900908 AidlUnionDecl* AsUnionDeclaration() {
909 return const_cast<AidlUnionDecl*>(
910 const_cast<const AidlDefinedType*>(this)->AsUnionDeclaration());
911 }
Steven Morelandc258abc2018-07-10 14:03:38 -0700912 AidlInterface* AsInterface() {
913 return const_cast<AidlInterface*>(const_cast<const AidlDefinedType*>(this)->AsInterface());
914 }
915
Jeongik Chadf76dc72019-11-28 00:08:47 +0900916 AidlParameterizable<std::string>* AsParameterizable() {
917 return const_cast<AidlParameterizable<std::string>*>(
918 const_cast<const AidlDefinedType*>(this)->AsParameterizable());
919 }
920
Steven Moreland6cee3482018-07-18 14:39:58 -0700921 const AidlParcelable* AsUnstructuredParcelable() const {
922 if (this->AsStructuredParcelable() != nullptr) return nullptr;
Jooyung Han2946afc2020-10-05 20:29:16 +0900923 if (this->AsUnionDeclaration() != nullptr) return nullptr;
Steven Moreland6cee3482018-07-18 14:39:58 -0700924 return this->AsParcelable();
925 }
926 AidlParcelable* AsUnstructuredParcelable() {
927 return const_cast<AidlParcelable*>(
928 const_cast<const AidlDefinedType*>(this)->AsUnstructuredParcelable());
929 }
930
Jeongik Cha997281d2020-01-16 15:23:59 +0900931 virtual void Dump(CodeWriter* writer) const = 0;
Steven Morelanda5d9c5c2020-02-21 16:01:09 -0800932 void DumpHeader(CodeWriter* writer) const;
Jiyong Park02da7422018-07-16 16:00:26 +0900933
Jooyung Han829ec7c2020-12-02 12:07:36 +0900934 const std::vector<std::unique_ptr<AidlVariableDeclaration>>& GetFields() const {
935 return variables_;
936 }
937 const std::vector<std::unique_ptr<AidlConstantDeclaration>>& GetConstantDeclarations() const {
938 return constants_;
939 }
940 const std::vector<std::unique_ptr<AidlMethod>>& GetMethods() const { return methods_; }
941 void AddMethod(std::unique_ptr<AidlMethod> method) { methods_.push_back(std::move(method)); }
942 const std::vector<const AidlMember*>& GetMembers() const { return members_; }
943
Jiyong Park512ed852020-12-30 15:07:23 +0900944 void TraverseChildren(std::function<void(const AidlTraversable&)>) const = 0;
945 bool DispatchVisit(AidlVisitor& v) const = 0;
946
Jooyung Han829ec7c2020-12-02 12:07:36 +0900947 protected:
948 // utility for subclasses with getter names
949 bool CheckValidForGetterNames() const;
950
Steven Moreland787b0432018-07-03 09:00:58 -0700951 private:
Jooyung Han829ec7c2020-12-02 12:07:36 +0900952 bool CheckValidWithMembers(const AidlTypenames& typenames) const;
953
Jiyong Park1deecc32018-07-17 01:14:41 +0900954 std::string name_;
Jiyong Park1deecc32018-07-17 01:14:41 +0900955 std::string comments_;
Jiyong Park18132182020-06-08 20:24:40 +0900956 const std::string package_;
957 const std::vector<std::string> split_package_;
Jooyung Han829ec7c2020-12-02 12:07:36 +0900958 std::vector<std::unique_ptr<AidlVariableDeclaration>> variables_;
959 std::vector<std::unique_ptr<AidlConstantDeclaration>> constants_;
960 std::vector<std::unique_ptr<AidlMethod>> methods_;
961 std::vector<const AidlMember*> members_; // keep members in order of appearance.
Steven Moreland787b0432018-07-03 09:00:58 -0700962};
963
Jeongik Chadf76dc72019-11-28 00:08:47 +0900964class AidlParcelable : public AidlDefinedType, public AidlParameterizable<std::string> {
Casey Dahlin1ae2bc52015-10-07 18:49:10 -0700965 public:
Jiyong Park18132182020-06-08 20:24:40 +0900966 AidlParcelable(const AidlLocation& location, const std::string& name, const std::string& package,
967 const std::string& comments, const std::string& cpp_header = "",
Jooyung Han829ec7c2020-12-02 12:07:36 +0900968 std::vector<std::string>* type_params = nullptr,
969 std::vector<std::unique_ptr<AidlMember>>* members = nullptr);
Casey Dahlin1ae2bc52015-10-07 18:49:10 -0700970 virtual ~AidlParcelable() = default;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800971
Jiyong Parkd800fef2020-07-22 18:09:43 +0900972 // non-copyable, non-movable
973 AidlParcelable(const AidlParcelable&) = delete;
974 AidlParcelable(AidlParcelable&&) = delete;
975 AidlParcelable& operator=(const AidlParcelable&) = delete;
976 AidlParcelable& operator=(AidlParcelable&&) = delete;
977
Christopher Wiley8aa4d9f2015-11-16 19:10:45 -0800978 std::string GetCppHeader() const { return cpp_header_; }
Christopher Wiley8aa4d9f2015-11-16 19:10:45 -0800979
Steven Moreland0cea4aa2020-04-20 21:06:02 -0700980 std::set<AidlAnnotation::Type> GetSupportedAnnotations() const override;
Jooyung Han808a2a02020-12-28 16:46:54 +0900981 bool CheckValid(const AidlTypenames& typenames) const override;
Steven Morelandd59e3172020-05-11 16:42:09 -0700982 bool LanguageSpecificCheckValid(const AidlTypenames& typenames,
983 Options::Language lang) const override;
Steven Morelandc258abc2018-07-10 14:03:38 -0700984 const AidlParcelable* AsParcelable() const override { return this; }
Jeongik Chadf76dc72019-11-28 00:08:47 +0900985 const AidlParameterizable<std::string>* AsParameterizable() const override { return this; }
986 const AidlNode& AsAidlNode() const override { return *this; }
Steven Morelanded83a282018-07-17 13:27:29 -0700987 std::string GetPreprocessDeclarationName() const override { return "parcelable"; }
Steven Morelandc258abc2018-07-10 14:03:38 -0700988
Jeongik Cha997281d2020-01-16 15:23:59 +0900989 void Dump(CodeWriter* writer) const override;
Jiyong Park02da7422018-07-16 16:00:26 +0900990
Jiyong Park512ed852020-12-30 15:07:23 +0900991 void TraverseChildren(std::function<void(const AidlTraversable&)> traverse) const override {
992 for (const auto c : GetMembers()) {
993 traverse(*c);
994 }
995 }
996 bool DispatchVisit(AidlVisitor& v) const override { return v.Visit(*this); }
997
Casey Dahlin1ae2bc52015-10-07 18:49:10 -0700998 private:
Christopher Wiley8aa4d9f2015-11-16 19:10:45 -0800999 std::string cpp_header_;
Casey Dahlin0a2f8be2015-09-28 16:15:29 -07001000};
Adam Lesinskiffa16862014-01-23 18:17:42 -08001001
Jooyung Han829ec7c2020-12-02 12:07:36 +09001002class AidlStructuredParcelable : public AidlParcelable {
Steven Moreland5557f1c2018-07-02 13:50:23 -07001003 public:
Jiyong Park18132182020-06-08 20:24:40 +09001004 AidlStructuredParcelable(const AidlLocation& location, const std::string& name,
1005 const std::string& package, const std::string& comments,
Jooyung Han829ec7c2020-12-02 12:07:36 +09001006 std::vector<std::string>* type_params,
1007 std::vector<std::unique_ptr<AidlMember>>* members);
Jiyong Parkd800fef2020-07-22 18:09:43 +09001008 virtual ~AidlStructuredParcelable() = default;
1009
1010 // non-copyable, non-movable
1011 AidlStructuredParcelable(const AidlStructuredParcelable&) = delete;
1012 AidlStructuredParcelable(AidlStructuredParcelable&&) = delete;
1013 AidlStructuredParcelable& operator=(const AidlStructuredParcelable&) = delete;
1014 AidlStructuredParcelable& operator=(AidlStructuredParcelable&&) = delete;
Steven Moreland5557f1c2018-07-02 13:50:23 -07001015
Steven Morelandc258abc2018-07-10 14:03:38 -07001016 const AidlStructuredParcelable* AsStructuredParcelable() const override { return this; }
Steven Morelanded83a282018-07-17 13:27:29 -07001017 std::string GetPreprocessDeclarationName() const override { return "structured_parcelable"; }
Steven Moreland5557f1c2018-07-02 13:50:23 -07001018
Jeongik Cha997281d2020-01-16 15:23:59 +09001019 void Dump(CodeWriter* writer) const override;
Jiyong Park02da7422018-07-16 16:00:26 +09001020
Steven Moreland0cea4aa2020-04-20 21:06:02 -07001021 std::set<AidlAnnotation::Type> GetSupportedAnnotations() const override;
Jooyung Han808a2a02020-12-28 16:46:54 +09001022 bool CheckValid(const AidlTypenames& typenames) const override;
Steven Morelandd59e3172020-05-11 16:42:09 -07001023 bool LanguageSpecificCheckValid(const AidlTypenames& typenames,
1024 Options::Language lang) const override;
Jiyong Park512ed852020-12-30 15:07:23 +09001025
1026 void TraverseChildren(std::function<void(const AidlTraversable&)> traverse) const override {
1027 for (const auto c : GetMembers()) {
1028 traverse(*c);
1029 }
1030 }
1031 bool DispatchVisit(AidlVisitor& v) const override { return v.Visit(*this); }
Steven Moreland5557f1c2018-07-02 13:50:23 -07001032};
1033
Jiyong Park512ed852020-12-30 15:07:23 +09001034class AidlEnumerator : public AidlNode, public AidlTraversable {
Daniel Norman85aed542019-08-21 12:01:14 -07001035 public:
Daniel Norman2e4112d2019-10-03 10:22:35 -07001036 AidlEnumerator(const AidlLocation& location, const std::string& name, AidlConstantValue* value,
1037 const std::string& comments);
Daniel Norman85aed542019-08-21 12:01:14 -07001038 virtual ~AidlEnumerator() = default;
1039
Jiyong Parkd800fef2020-07-22 18:09:43 +09001040 // non-copyable, non-movable
1041 AidlEnumerator(const AidlEnumerator&) = delete;
1042 AidlEnumerator(AidlEnumerator&&) = delete;
1043 AidlEnumerator& operator=(const AidlEnumerator&) = delete;
1044 AidlEnumerator& operator=(AidlEnumerator&&) = delete;
1045
Daniel Norman85aed542019-08-21 12:01:14 -07001046 const std::string& GetName() const { return name_; }
Will McVickerd7d18df2019-09-12 13:40:50 -07001047 AidlConstantValue* GetValue() const { return value_.get(); }
Daniel Norman2e4112d2019-10-03 10:22:35 -07001048 const std::string& GetComments() const { return comments_; }
Daniel Norman85aed542019-08-21 12:01:14 -07001049 bool CheckValid(const AidlTypeSpecifier& enum_backing_type) const;
1050
1051 string ValueString(const AidlTypeSpecifier& backing_type,
1052 const ConstantValueDecorator& decorator) const;
1053
Daniel Normanb28684e2019-10-17 15:31:39 -07001054 void SetValue(std::unique_ptr<AidlConstantValue> value) { value_ = std::move(value); }
Jooyung Han29813842020-12-08 01:28:03 +09001055 bool IsValueUserSpecified() const { return value_user_specified_; }
Daniel Normanb28684e2019-10-17 15:31:39 -07001056
Jiyong Park512ed852020-12-30 15:07:23 +09001057 void TraverseChildren(std::function<void(const AidlTraversable&)>) const override {
1058 // no children to traverse
1059 }
1060 bool DispatchVisit(AidlVisitor& v) const override { return v.Visit(*this); }
1061
Daniel Norman85aed542019-08-21 12:01:14 -07001062 private:
1063 const std::string name_;
Will McVickerd7d18df2019-09-12 13:40:50 -07001064 unique_ptr<AidlConstantValue> value_;
Daniel Norman2e4112d2019-10-03 10:22:35 -07001065 const std::string comments_;
Jooyung Han29813842020-12-08 01:28:03 +09001066 const bool value_user_specified_;
Daniel Norman85aed542019-08-21 12:01:14 -07001067};
1068
1069class AidlEnumDeclaration : public AidlDefinedType {
1070 public:
Will McVickerd7d18df2019-09-12 13:40:50 -07001071 AidlEnumDeclaration(const AidlLocation& location, const string& name,
Daniel Norman85aed542019-08-21 12:01:14 -07001072 std::vector<std::unique_ptr<AidlEnumerator>>* enumerators,
Jiyong Park18132182020-06-08 20:24:40 +09001073 const std::string& package, const std::string& comments);
Daniel Norman85aed542019-08-21 12:01:14 -07001074 virtual ~AidlEnumDeclaration() = default;
1075
Jiyong Parkd800fef2020-07-22 18:09:43 +09001076 // non-copyable, non-movable
1077 AidlEnumDeclaration(const AidlEnumDeclaration&) = delete;
1078 AidlEnumDeclaration(AidlEnumDeclaration&&) = delete;
1079 AidlEnumDeclaration& operator=(const AidlEnumDeclaration&) = delete;
1080 AidlEnumDeclaration& operator=(AidlEnumDeclaration&&) = delete;
1081
Jooyung Han672557b2020-12-24 05:18:00 +09001082 bool Autofill(const AidlTypenames&);
Daniel Norman85aed542019-08-21 12:01:14 -07001083 const AidlTypeSpecifier& GetBackingType() const { return *backing_type_; }
1084 const std::vector<std::unique_ptr<AidlEnumerator>>& GetEnumerators() const {
1085 return enumerators_;
1086 }
Steven Moreland0cea4aa2020-04-20 21:06:02 -07001087 std::set<AidlAnnotation::Type> GetSupportedAnnotations() const override;
Jooyung Han808a2a02020-12-28 16:46:54 +09001088 bool CheckValid(const AidlTypenames& typenames) const override;
Steven Morelandd59e3172020-05-11 16:42:09 -07001089 bool LanguageSpecificCheckValid(const AidlTypenames& /*typenames*/,
1090 Options::Language) const override {
1091 return true;
1092 }
Daniel Norman85aed542019-08-21 12:01:14 -07001093 std::string GetPreprocessDeclarationName() const override { return "enum"; }
Jeongik Cha997281d2020-01-16 15:23:59 +09001094 void Dump(CodeWriter* writer) const override;
Daniel Norman85aed542019-08-21 12:01:14 -07001095
1096 const AidlEnumDeclaration* AsEnumDeclaration() const override { return this; }
1097
Jiyong Park512ed852020-12-30 15:07:23 +09001098 void TraverseChildren(std::function<void(const AidlTraversable&)> traverse) const override {
1099 for (const auto& c : GetEnumerators()) {
1100 traverse(*c);
1101 }
1102 }
1103 bool DispatchVisit(AidlVisitor& v) const override { return v.Visit(*this); }
1104
Daniel Norman85aed542019-08-21 12:01:14 -07001105 private:
Jooyung Han29813842020-12-08 01:28:03 +09001106
Daniel Norman85aed542019-08-21 12:01:14 -07001107 const std::string name_;
1108 const std::vector<std::unique_ptr<AidlEnumerator>> enumerators_;
Jooyung Han672557b2020-12-24 05:18:00 +09001109 std::unique_ptr<AidlTypeSpecifier> backing_type_;
Daniel Norman85aed542019-08-21 12:01:14 -07001110};
1111
Jooyung Han829ec7c2020-12-02 12:07:36 +09001112class AidlUnionDecl : public AidlParcelable {
Jooyung Han2946afc2020-10-05 20:29:16 +09001113 public:
1114 AidlUnionDecl(const AidlLocation& location, const std::string& name, const std::string& package,
Jooyung Han829ec7c2020-12-02 12:07:36 +09001115 const std::string& comments, std::vector<std::string>* type_params,
1116 std::vector<std::unique_ptr<AidlMember>>* members);
Jooyung Han2946afc2020-10-05 20:29:16 +09001117 virtual ~AidlUnionDecl() = default;
1118
1119 // non-copyable, non-movable
1120 AidlUnionDecl(const AidlUnionDecl&) = delete;
1121 AidlUnionDecl(AidlUnionDecl&&) = delete;
1122 AidlUnionDecl& operator=(const AidlUnionDecl&) = delete;
1123 AidlUnionDecl& operator=(AidlUnionDecl&&) = delete;
1124
1125 std::set<AidlAnnotation::Type> GetSupportedAnnotations() const override;
1126
1127 const AidlNode& AsAidlNode() const override { return *this; }
Jooyung Han808a2a02020-12-28 16:46:54 +09001128 bool CheckValid(const AidlTypenames& typenames) const override;
Jooyung Hanfe89f122020-10-14 03:49:18 +09001129 bool LanguageSpecificCheckValid(const AidlTypenames& typenames,
1130 Options::Language lang) const override;
Jooyung Han2946afc2020-10-05 20:29:16 +09001131 std::string GetPreprocessDeclarationName() const override { return "union"; }
1132
1133 void Dump(CodeWriter* writer) const override;
1134 const AidlUnionDecl* AsUnionDeclaration() const override { return this; }
Jiyong Park512ed852020-12-30 15:07:23 +09001135
1136 void TraverseChildren(std::function<void(const AidlTraversable&)> traverse) const override {
1137 for (const auto c : GetMembers()) {
1138 traverse(*c);
1139 }
1140 }
1141 bool DispatchVisit(AidlVisitor& v) const override { return v.Visit(*this); }
Jooyung Han2946afc2020-10-05 20:29:16 +09001142};
1143
Jiyong Park1deecc32018-07-17 01:14:41 +09001144class AidlInterface final : public AidlDefinedType {
Casey Dahlin1ae2bc52015-10-07 18:49:10 -07001145 public:
Steven Moreland46e9da82018-07-27 15:45:29 -07001146 AidlInterface(const AidlLocation& location, const std::string& name, const std::string& comments,
Jooyung Han829ec7c2020-12-02 12:07:36 +09001147 bool oneway_, const std::string& package,
1148 std::vector<std::unique_ptr<AidlMember>>* members);
Casey Dahlin1ae2bc52015-10-07 18:49:10 -07001149 virtual ~AidlInterface() = default;
1150
Jiyong Parkd800fef2020-07-22 18:09:43 +09001151 // non-copyable, non-movable
1152 AidlInterface(const AidlInterface&) = delete;
1153 AidlInterface(AidlInterface&&) = delete;
1154 AidlInterface& operator=(const AidlInterface&) = delete;
1155 AidlInterface& operator=(AidlInterface&&) = delete;
1156
Steven Morelandc258abc2018-07-10 14:03:38 -07001157 const AidlInterface* AsInterface() const override { return this; }
Steven Morelanded83a282018-07-17 13:27:29 -07001158 std::string GetPreprocessDeclarationName() const override { return "interface"; }
Steven Moreland5557f1c2018-07-02 13:50:23 -07001159
Jeongik Cha997281d2020-01-16 15:23:59 +09001160 void Dump(CodeWriter* writer) const override;
Jiyong Park02da7422018-07-16 16:00:26 +09001161
Steven Moreland0cea4aa2020-04-20 21:06:02 -07001162 std::set<AidlAnnotation::Type> GetSupportedAnnotations() const override;
Jooyung Han808a2a02020-12-28 16:46:54 +09001163 bool CheckValid(const AidlTypenames& typenames) const override;
Steven Morelandd59e3172020-05-11 16:42:09 -07001164 bool LanguageSpecificCheckValid(const AidlTypenames& typenames,
1165 Options::Language lang) const override;
Jeongik Chadb0f59e2018-11-01 18:11:21 +09001166
Jiyong Park27fd7fd2020-08-27 16:25:09 +09001167 std::string GetDescriptor() const;
Jiyong Park512ed852020-12-30 15:07:23 +09001168
1169 void TraverseChildren(std::function<void(const AidlTraversable&)> traverse) const override {
1170 for (const auto c : GetMembers()) {
1171 traverse(*c);
1172 }
1173 }
1174 bool DispatchVisit(AidlVisitor& v) const override { return v.Visit(*this); }
Casey Dahlin0a2f8be2015-09-28 16:15:29 -07001175};
Adam Lesinskiffa16862014-01-23 18:17:42 -08001176
Casey Dahlin0edf3422015-10-07 12:34:59 -07001177class AidlImport : public AidlNode {
1178 public:
Steven Moreland46e9da82018-07-27 15:45:29 -07001179 AidlImport(const AidlLocation& location, const std::string& needed_class);
Casey Dahlin0edf3422015-10-07 12:34:59 -07001180 virtual ~AidlImport() = default;
1181
Jiyong Parkd800fef2020-07-22 18:09:43 +09001182 // non-copyable, non-movable
1183 AidlImport(const AidlImport&) = delete;
1184 AidlImport(AidlImport&&) = delete;
1185 AidlImport& operator=(const AidlImport&) = delete;
1186 AidlImport& operator=(AidlImport&&) = delete;
1187
Casey Dahlin0edf3422015-10-07 12:34:59 -07001188 const std::string& GetNeededClass() const { return needed_class_; }
Casey Dahlin0edf3422015-10-07 12:34:59 -07001189
1190 private:
Casey Dahlin0edf3422015-10-07 12:34:59 -07001191 std::string needed_class_;
Casey Dahline2507492015-09-14 17:11:20 -07001192};
1193
Jiyong Park62515512020-06-08 15:57:11 +09001194// AidlDocument models an AIDL file
Jiyong Park512ed852020-12-30 15:07:23 +09001195class AidlDocument : public AidlNode, public AidlTraversable {
Jiyong Park62515512020-06-08 15:57:11 +09001196 public:
1197 AidlDocument(const AidlLocation& location, std::vector<std::unique_ptr<AidlImport>>& imports,
Jiyong Park8e79b7f2020-07-20 20:52:38 +09001198 std::vector<std::unique_ptr<AidlDefinedType>>&& defined_types)
1199 : AidlNode(location),
1200 imports_(std::move(imports)),
1201 defined_types_(std::move(defined_types)) {}
Jiyong Parkd800fef2020-07-22 18:09:43 +09001202 ~AidlDocument() = default;
1203
1204 // non-copyable, non-movable
Jiyong Park8e79b7f2020-07-20 20:52:38 +09001205 AidlDocument(const AidlDocument&) = delete;
1206 AidlDocument(AidlDocument&&) = delete;
1207 AidlDocument& operator=(const AidlDocument&) = delete;
1208 AidlDocument& operator=(AidlDocument&&) = delete;
Jiyong Parkd800fef2020-07-22 18:09:43 +09001209
Jooyung Han29813842020-12-08 01:28:03 +09001210 std::optional<std::string> ResolveName(const std::string& unresolved_type) const;
Jiyong Parkd800fef2020-07-22 18:09:43 +09001211 const std::vector<std::unique_ptr<AidlImport>>& Imports() const { return imports_; }
1212 const std::vector<std::unique_ptr<AidlDefinedType>>& DefinedTypes() const {
1213 return defined_types_;
1214 }
Jiyong Park62515512020-06-08 15:57:11 +09001215
Jiyong Park512ed852020-12-30 15:07:23 +09001216 void TraverseChildren(std::function<void(const AidlTraversable&)> traverse) const override {
1217 for (const auto& t : DefinedTypes()) {
1218 traverse(*t);
1219 }
1220 }
1221 bool DispatchVisit(AidlVisitor& v) const override { return v.Visit(*this); }
1222
Jiyong Park62515512020-06-08 15:57:11 +09001223 private:
1224 const std::vector<std::unique_ptr<AidlImport>> imports_;
Jiyong Park8e79b7f2020-07-20 20:52:38 +09001225 const std::vector<std::unique_ptr<AidlDefinedType>> defined_types_;
Jiyong Park62515512020-06-08 15:57:11 +09001226};
Jooyung Hanb3c77ed2020-12-26 02:02:45 +09001227
1228template <typename T>
1229std::optional<T> AidlAnnotation::ParamValue(const std::string& param_name) const {
1230 auto it = parameters_.find(param_name);
1231 if (it == parameters_.end()) {
1232 return std::nullopt;
1233 }
Jooyung Han535c5e82020-12-29 15:16:59 +09001234 return it->second->EvaluatedValue<T>();
Jiyong Park512ed852020-12-30 15:07:23 +09001235}