blob: 1492aa3dc674c47986eac9767f2bf4462f697892 [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;
Jeongik Cha047c5ee2019-08-07 23:16:49 +090037using android::aidl::Options;
Steven Moreland3f658cf2018-08-20 13:40:54 -070038using std::shared_ptr;
Jiyong Park1deecc32018-07-17 01:14:41 +090039using std::string;
40using std::unique_ptr;
41using std::vector;
Andrei Onea8714b022019-02-01 18:55:54 +000042class AidlNode;
43
Jooyung Han535c5e82020-12-29 15:16:59 +090044// helper to see if T is the same to one of Args types.
45template <typename T, typename... Args>
46struct is_one_of : std::false_type {};
47
48template <typename T, typename S, typename... Args>
49struct is_one_of<T, S, Args...> {
50 enum { value = std::is_same_v<T, S> || is_one_of<T, Args...>::value };
51};
52
53// helper to see if T is std::vector of something.
54template <typename>
55struct is_vector : std::false_type {};
56
57template <typename T>
58struct is_vector<std::vector<T>> : std::true_type {};
59
60// helper for static_assert(false)
61template <typename T>
62struct unsupported_type : std::false_type {};
63
Andrei Onea8714b022019-02-01 18:55:54 +000064namespace android {
65namespace aidl {
66namespace mappings {
67std::string dump_location(const AidlNode& method);
68} // namespace mappings
Mathew Inwoodadb74672019-11-29 14:01:53 +000069namespace java {
70std::string dump_location(const AidlNode& method);
71} // namespace java
Andrei Onea8714b022019-02-01 18:55:54 +000072} // namespace aidl
73} // namespace android
74
Jooyung Han535c5e82020-12-29 15:16:59 +090075bool ParseFloating(std::string_view sv, double* parsed);
76bool ParseFloating(std::string_view sv, float* parsed);
Steven Moreland46e9da82018-07-27 15:45:29 -070077
Jooyung Han808a2a02020-12-28 16:46:54 +090078class AidlDocument;
79class AidlInterface;
80class AidlParcelable;
81class AidlStructuredParcelable;
82class AidlEnumDeclaration;
83class AidlUnionDecl;
84class AidlVariableDeclaration;
85class AidlConstantDeclaration;
86class AidlEnumerator;
87class AidlMethod;
88class AidlArgument;
89
Jiyong Park512ed852020-12-30 15:07:23 +090090// Interface for visitors that can traverse AidlTraversable nodes. The contract is that Visit()
91// method returns a bool which controls whether or not the traversal should be continued down to its
92// children.
Jooyung Han808a2a02020-12-28 16:46:54 +090093class AidlVisitor {
94 public:
95 virtual ~AidlVisitor() = default;
Jiyong Park512ed852020-12-30 15:07:23 +090096 virtual bool Visit(const AidlDocument&) { return true; }
97 virtual bool Visit(const AidlInterface&) { return true; }
98 virtual bool Visit(const AidlParcelable&) { return true; }
99 virtual bool Visit(const AidlStructuredParcelable&) { return true; }
100 virtual bool Visit(const AidlUnionDecl&) { return true; }
101 virtual bool Visit(const AidlEnumDeclaration&) { return true; }
102 virtual bool Visit(const AidlEnumerator&) { return true; }
103 virtual bool Visit(const AidlMethod&) { return true; }
104 virtual bool Visit(const AidlVariableDeclaration&) { return true; }
105 virtual bool Visit(const AidlConstantDeclaration&) { return true; }
106 virtual bool Visit(const AidlArgument&) { return true; }
Jooyung Han808a2a02020-12-28 16:46:54 +0900107};
108
Steven Moreland46e9da82018-07-27 15:45:29 -0700109// Anything that is locatable in a .aidl file.
110class AidlNode {
111 public:
112 AidlNode(const AidlLocation& location);
Steven Moreland3f658cf2018-08-20 13:40:54 -0700113
114 AidlNode(const AidlNode&) = default;
Steven Moreland46e9da82018-07-27 15:45:29 -0700115 virtual ~AidlNode() = default;
116
Jiyong Parkd800fef2020-07-22 18:09:43 +0900117 AidlNode(AidlNode&&) = delete;
118 AidlNode& operator=(AidlNode&&) = delete;
119
Devin Mooredf93ebb2020-03-25 14:03:35 -0700120 // To be able to print AidlLocation
Steven Morelandb0d15a52020-03-31 14:03:47 -0700121 friend class AidlErrorLog;
Andrei Onea8714b022019-02-01 18:55:54 +0000122 friend std::string android::aidl::mappings::dump_location(const AidlNode&);
Mathew Inwoodadb74672019-11-29 14:01:53 +0000123 friend std::string android::aidl::java::dump_location(const AidlNode&);
Steven Moreland46e9da82018-07-27 15:45:29 -0700124
Devin Mooredf93ebb2020-03-25 14:03:35 -0700125 const AidlLocation& GetLocation() const { return location_; }
126
Steven Moreland46e9da82018-07-27 15:45:29 -0700127 private:
Mathew Inwoodadb74672019-11-29 14:01:53 +0000128 std::string PrintLine() const;
Andrei Onea8714b022019-02-01 18:55:54 +0000129 std::string PrintLocation() const;
Steven Moreland46e9da82018-07-27 15:45:29 -0700130 const AidlLocation location_;
Casey Dahlinbc7a50a2015-09-28 19:20:50 -0700131};
132
Jiyong Park512ed852020-12-30 15:07:23 +0900133// Anything that is traversable by the AidlVisitor
134class AidlTraversable {
135 public:
136 virtual ~AidlTraversable() = default;
137
138 virtual void TraverseChildren(std::function<void(const AidlTraversable&)> traverse) const = 0;
139 virtual bool DispatchVisit(AidlVisitor&) const = 0;
140};
141
Jeongik Chadf76dc72019-11-28 00:08:47 +0900142// unique_ptr<AidlTypeSpecifier> for type arugment,
143// std::string for type parameter(T, U, and so on).
144template <typename T>
145class AidlParameterizable {
146 public:
147 AidlParameterizable(std::vector<T>* type_params) : type_params_(type_params) {}
148 virtual ~AidlParameterizable() = default;
149 bool IsGeneric() const { return type_params_ != nullptr; }
150 const std::vector<T>& GetTypeParameters() const { return *type_params_; }
151 bool CheckValid() const;
152
Steven Moreland6c07b832020-10-29 23:39:53 +0000153 __attribute__((warn_unused_result)) bool SetTypeParameters(std::vector<T>* type_params) {
154 if (type_params_) return false;
155 type_params_.reset(type_params);
156 return true;
157 }
158
Jeongik Chadf76dc72019-11-28 00:08:47 +0900159 virtual const AidlNode& AsAidlNode() const = 0;
160
161 protected:
162 AidlParameterizable(const AidlParameterizable&);
163
164 private:
Steven Moreland6c07b832020-10-29 23:39:53 +0000165 unique_ptr<std::vector<T>> type_params_;
Jeongik Chadf76dc72019-11-28 00:08:47 +0900166 static_assert(std::is_same<T, unique_ptr<AidlTypeSpecifier>>::value ||
167 std::is_same<T, std::string>::value);
168};
169template <>
170bool AidlParameterizable<std::string>::CheckValid() const;
171
Andrei Onea9445fc62019-06-27 18:11:59 +0100172class AidlConstantValue;
173class AidlConstantDeclaration;
174
175// Transforms a value string into a language specific form. Raw value as produced by
176// AidlConstantValue.
177using ConstantValueDecorator =
178 std::function<std::string(const AidlTypeSpecifier& type, const std::string& raw_value)>;
179
Jiyong Park68bc77a2018-07-19 19:00:45 +0900180class AidlAnnotation : public AidlNode {
181 public:
Steven Moreland0cea4aa2020-04-20 21:06:02 -0700182 enum class Type {
183 BACKING = 1,
184 HIDE,
185 JAVA_STABLE_PARCELABLE,
186 UNSUPPORTED_APP_USAGE,
187 VINTF_STABILITY,
188 NULLABLE,
189 UTF8_IN_CPP,
Steven Morelanda7764e52020-10-27 17:29:29 +0000190 SENSITIVE_DATA,
Jiyong Parkbf5fd5c2020-06-05 19:48:05 +0900191 JAVA_PASSTHROUGH,
Jooyung Han90345002020-10-23 15:28:53 +0900192 JAVA_DERIVE,
Jeongik Chad0a10272020-08-06 16:33:36 +0900193 JAVA_ONLY_IMMUTABLE,
Devin Moorec7e47a32020-08-07 10:55:25 -0700194 FIXED_SIZE,
Jiyong Park27fd7fd2020-08-27 16:25:09 +0900195 DESCRIPTOR,
Andrei Homescue61feb52020-08-18 15:44:24 -0700196 RUST_DERIVE,
Jooyung Hanf8dbbcc2020-12-26 03:05:55 +0900197 SUPPRESS_WARNINGS,
Steven Moreland0cea4aa2020-04-20 21:06:02 -0700198 };
199 static std::string TypeToString(Type type);
200
Andrei Onea9445fc62019-06-27 18:11:59 +0100201 static AidlAnnotation* Parse(
202 const AidlLocation& location, const string& name,
203 std::map<std::string, std::shared_ptr<AidlConstantValue>>* parameter_list);
Steven Moreland46e9da82018-07-27 15:45:29 -0700204
Steven Moreland3f658cf2018-08-20 13:40:54 -0700205 AidlAnnotation(const AidlAnnotation&) = default;
Steven Moreland3be75772018-08-20 13:27:43 -0700206 AidlAnnotation(AidlAnnotation&&) = default;
Jiyong Park68bc77a2018-07-19 19:00:45 +0900207 virtual ~AidlAnnotation() = default;
Andrei Onea9445fc62019-06-27 18:11:59 +0100208 bool CheckValid() const;
Steven Moreland3be75772018-08-20 13:27:43 -0700209
Jooyung Hand902a972020-10-23 17:32:44 +0900210 const string& GetName() const { return schema_.name; }
Steven Moreland0cea4aa2020-04-20 21:06:02 -0700211 const Type& GetType() const { return schema_.type; }
Jooyung Hand902a972020-10-23 17:32:44 +0900212 bool Repeatable() const { return schema_.repeatable; }
Jooyung Han965e31d2020-11-27 12:30:16 +0900213
214 // ToString is for dumping AIDL.
215 // Returns string representation of this annotation.
216 // e.g) "@RustDerive(Clone=true, Copy=true)"
217 string ToString() const;
218
Jooyung Hanb3c77ed2020-12-26 02:02:45 +0900219 template <typename T>
220 std::optional<T> ParamValue(const std::string& param_name) const;
221
Andrei Onea9445fc62019-06-27 18:11:59 +0100222 std::map<std::string, std::string> AnnotationParams(
223 const ConstantValueDecorator& decorator) const;
Jiyong Parka6605ab2018-11-11 14:30:21 +0900224 const string& GetComments() const { return comments_; }
225 void SetComments(const string& comments) { comments_ = comments; }
Jiyong Park68bc77a2018-07-19 19:00:45 +0900226
227 private:
Steven Moreland0cea4aa2020-04-20 21:06:02 -0700228 struct Schema {
229 AidlAnnotation::Type type;
230
231 // text name in .aidl file, e.g. "nullable"
232 std::string name;
233
234 // map from param name -> value type
Jooyung Han5c2fcae2020-12-26 00:04:39 +0900235 std::map<std::string, const AidlTypeSpecifier&> supported_parameters;
Jooyung Hand902a972020-10-23 17:32:44 +0900236
237 bool repeatable;
Jooyung Han5721a232020-12-24 04:34:55 +0900238
239 std::vector<std::string> required_parameters = {};
Steven Moreland0cea4aa2020-04-20 21:06:02 -0700240 };
241 static const std::vector<Schema>& AllSchemas();
242
243 AidlAnnotation(const AidlLocation& location, const Schema& schema,
Andrei Onea9445fc62019-06-27 18:11:59 +0100244 std::map<std::string, std::shared_ptr<AidlConstantValue>>&& parameters);
Steven Moreland0cea4aa2020-04-20 21:06:02 -0700245
246 const Schema& schema_;
Jiyong Parka6605ab2018-11-11 14:30:21 +0900247 string comments_;
Andrei Onea9445fc62019-06-27 18:11:59 +0100248 std::map<std::string, std::shared_ptr<AidlConstantValue>> parameters_;
Jiyong Park68bc77a2018-07-19 19:00:45 +0900249};
250
Steven Moreland3be75772018-08-20 13:27:43 -0700251static inline bool operator<(const AidlAnnotation& lhs, const AidlAnnotation& rhs) {
252 return lhs.GetName() < rhs.GetName();
253}
254static inline bool operator==(const AidlAnnotation& lhs, const AidlAnnotation& rhs) {
255 return lhs.GetName() == rhs.GetName();
256}
Jiyong Park3656c3c2018-08-01 20:02:01 +0900257
Casey Dahline7922932016-02-29 17:23:01 -0800258class AidlAnnotatable : public AidlNode {
Casey Dahlin0ee37582015-09-30 16:31:55 -0700259 public:
Steven Moreland46e9da82018-07-27 15:45:29 -0700260 AidlAnnotatable(const AidlLocation& location);
Steven Moreland3f658cf2018-08-20 13:40:54 -0700261
262 AidlAnnotatable(const AidlAnnotatable&) = default;
263 AidlAnnotatable(AidlAnnotatable&&) = default;
Casey Dahline7922932016-02-29 17:23:01 -0800264 virtual ~AidlAnnotatable() = default;
265
Artur Satayev91fe8712019-07-29 13:06:01 +0100266 void Annotate(vector<AidlAnnotation>&& annotations) {
267 for (auto& annotation : annotations) {
268 annotations_.emplace_back(std::move(annotation));
269 }
270 }
Jiyong Park68bc77a2018-07-19 19:00:45 +0900271 bool IsNullable() const;
Jiyong Park68bc77a2018-07-19 19:00:45 +0900272 bool IsUtf8InCpp() const;
Steven Morelanda7764e52020-10-27 17:29:29 +0000273 bool IsSensitiveData() const;
Steven Morelanda57d0a62019-07-30 09:41:14 -0700274 bool IsVintfStability() const;
Jeongik Chad0a10272020-08-06 16:33:36 +0900275 bool IsJavaOnlyImmutable() const;
Devin Moorec7e47a32020-08-07 10:55:25 -0700276 bool IsFixedSize() const;
Jeongik Cha88f95a82020-01-15 13:02:16 +0900277 bool IsStableApiParcelable(Options::Language lang) const;
Makoto Onuki78a1c1c2020-03-04 16:57:23 -0800278 bool IsHide() const;
Jooyung Han829ec7c2020-12-02 12:07:36 +0900279 bool JavaDerive(const std::string& method) const;
Jiyong Park27fd7fd2020-08-27 16:25:09 +0900280 std::string GetDescriptor() const;
Andrei Onea9445fc62019-06-27 18:11:59 +0100281
Steven Moreland7e4b9502020-02-20 18:10:42 -0800282 void DumpAnnotations(CodeWriter* writer) const;
283
Andrei Onea9445fc62019-06-27 18:11:59 +0100284 const AidlAnnotation* UnsupportedAppUsage() const;
Andrei Homescue61feb52020-08-18 15:44:24 -0700285 const AidlAnnotation* RustDerive() const;
Jooyung Han672557b2020-12-24 05:18:00 +0900286 const AidlAnnotation* BackingType() const;
Jooyung Hanf8dbbcc2020-12-26 03:05:55 +0900287 std::vector<std::string> SuppressWarnings() const;
Jooyung Han965e31d2020-11-27 12:30:16 +0900288
289 // ToString is for dumping AIDL.
290 // Returns string representation of annotations.
291 // e.g) "@JavaDerive(toString=true) @RustDerive(Clone=true, Copy=true)"
Jiyong Park68bc77a2018-07-19 19:00:45 +0900292 std::string ToString() const;
Casey Dahline7922932016-02-29 17:23:01 -0800293
Jiyong Parka6605ab2018-11-11 14:30:21 +0900294 const vector<AidlAnnotation>& GetAnnotations() const { return annotations_; }
Jooyung Han888c5bc2020-12-22 17:28:47 +0900295 bool CheckValid(const AidlTypenames&) const;
Jiyong Park3656c3c2018-08-01 20:02:01 +0900296
Steven Moreland181144c2020-04-20 19:57:56 -0700297 protected:
Steven Moreland0cea4aa2020-04-20 21:06:02 -0700298 virtual std::set<AidlAnnotation::Type> GetSupportedAnnotations() const = 0;
Steven Moreland181144c2020-04-20 19:57:56 -0700299
Casey Dahline7922932016-02-29 17:23:01 -0800300 private:
Jiyong Parka6605ab2018-11-11 14:30:21 +0900301 vector<AidlAnnotation> annotations_;
Casey Dahline7922932016-02-29 17:23:01 -0800302};
303
Jiyong Park1deecc32018-07-17 01:14:41 +0900304// AidlTypeSpecifier represents a reference to either a built-in type,
305// a defined type, or a variant (e.g., array of generic) of a type.
Jeongik Chadf76dc72019-11-28 00:08:47 +0900306class AidlTypeSpecifier final : public AidlAnnotatable,
307 public AidlParameterizable<unique_ptr<AidlTypeSpecifier>> {
Casey Dahline7922932016-02-29 17:23:01 -0800308 public:
Steven Moreland46e9da82018-07-27 15:45:29 -0700309 AidlTypeSpecifier(const AidlLocation& location, const string& unresolved_name, bool is_array,
310 vector<unique_ptr<AidlTypeSpecifier>>* type_params, const string& comments);
Jiyong Parkd59a10d2018-07-18 11:12:55 +0900311 virtual ~AidlTypeSpecifier() = default;
Casey Dahlin0ee37582015-09-30 16:31:55 -0700312
Steven Moreland3f658cf2018-08-20 13:40:54 -0700313 // Copy of this type which is not an array.
Jooyung Hand2fa0232020-10-19 02:51:41 +0900314 const AidlTypeSpecifier& ArrayBase() const;
Steven Moreland3f658cf2018-08-20 13:40:54 -0700315
Jiyong Park1deecc32018-07-17 01:14:41 +0900316 // Returns the full-qualified name of the base type.
317 // int -> int
318 // int[] -> int
319 // List<String> -> List
320 // IFoo -> foo.bar.IFoo (if IFoo is in package foo.bar)
321 const string& GetName() const {
322 if (IsResolved()) {
323 return fully_qualified_name_;
324 } else {
325 return GetUnresolvedName();
326 }
327 }
Casey Dahlin0ee37582015-09-30 16:31:55 -0700328
Jooyung Han965e31d2020-11-27 12:30:16 +0900329 // ToString is for dumping AIDL.
330 // Returns string representation of this type specifier including annotations.
331 // This is "annotations type_name type_params? array_marker?".
332 // e.g) "@utf8InCpp String[]";
333 std::string ToString() const;
Jiyong Park1deecc32018-07-17 01:14:41 +0900334
Jooyung Han965e31d2020-11-27 12:30:16 +0900335 // Signature is for comparing AIDL types.
336 // Returns string representation of this type specifier.
337 // This is "type_name type_params? array_marker?".
338 // e.g.) "String[]" (even if it is annotated with @utf8InCpp)
Jiyong Park02da7422018-07-16 16:00:26 +0900339 std::string Signature() const;
340
Jiyong Park1deecc32018-07-17 01:14:41 +0900341 const string& GetUnresolvedName() const { return unresolved_name_; }
342
Jeongik Cha997281d2020-01-16 15:23:59 +0900343 bool IsHidden() const;
344
Jiyong Park1deecc32018-07-17 01:14:41 +0900345 const string& GetComments() const { return comments_; }
346
Jeongik Cha1a7ab642019-07-29 17:31:02 +0900347 const std::vector<std::string> GetSplitName() const { return split_name_; }
348
Jiyong Parka6605ab2018-11-11 14:30:21 +0900349 void SetComments(const string& comment) { comments_ = comment; }
350
Jiyong Park1deecc32018-07-17 01:14:41 +0900351 bool IsResolved() const { return fully_qualified_name_ != ""; }
352
353 bool IsArray() const { return is_array_; }
354
Steven Moreland6c07b832020-10-29 23:39:53 +0000355 __attribute__((warn_unused_result)) bool SetArray() {
356 if (is_array_) return false;
357 is_array_ = true;
358 return true;
359 }
360
Jiyong Park1deecc32018-07-17 01:14:41 +0900361 // Resolve the base type name to a fully-qualified name. Return false if the
362 // resolution fails.
Daniel Norman716d3112019-09-10 13:11:56 -0700363 bool Resolve(const AidlTypenames& typenames);
Casey Dahlin0ee37582015-09-30 16:31:55 -0700364
Steven Moreland0cea4aa2020-04-20 21:06:02 -0700365 std::set<AidlAnnotation::Type> GetSupportedAnnotations() const override;
Jooyung Han888c5bc2020-12-22 17:28:47 +0900366 bool CheckValid(const AidlTypenames& typenames) const;
Steven Morelandd59e3172020-05-11 16:42:09 -0700367 bool LanguageSpecificCheckValid(const AidlTypenames& typenames, Options::Language lang) const;
Jeongik Chadf76dc72019-11-28 00:08:47 +0900368 const AidlNode& AsAidlNode() const override { return *this; }
Jiyong Park1d2df7d2018-07-23 15:22:50 +0900369
Jooyung Hane9bb9de2020-11-01 22:16:57 +0900370 const AidlDefinedType* GetDefinedType() const;
371
Casey Dahlin0ee37582015-09-30 16:31:55 -0700372 private:
Steven Moreland3f658cf2018-08-20 13:40:54 -0700373 AidlTypeSpecifier(const AidlTypeSpecifier&) = default;
374
Jiyong Park1deecc32018-07-17 01:14:41 +0900375 const string unresolved_name_;
376 string fully_qualified_name_;
Steven Moreland3f658cf2018-08-20 13:40:54 -0700377 bool is_array_;
Jiyong Parka6605ab2018-11-11 14:30:21 +0900378 string comments_;
Jeongik Cha1a7ab642019-07-29 17:31:02 +0900379 vector<string> split_name_;
Jooyung Han690f5842020-12-04 13:02:04 +0900380 const AidlDefinedType* defined_type_ = nullptr; // set when Resolve() for defined types
Jooyung Hand2fa0232020-10-19 02:51:41 +0900381 mutable shared_ptr<AidlTypeSpecifier> array_base_;
Casey Dahlin0ee37582015-09-30 16:31:55 -0700382};
383
Steven Moreland860b1942018-08-16 14:59:28 -0700384// Returns the universal value unaltered.
385std::string AidlConstantValueDecorator(const AidlTypeSpecifier& type, const std::string& raw_value);
386
Steven Moreland9ea10e32018-07-19 15:26:09 -0700387class AidlConstantValue;
Jooyung Han3f347ca2020-12-01 12:41:50 +0900388class AidlMethod;
389class AidlConstantDeclaration;
390class AidlVariableDeclaration;
391
Jiyong Park512ed852020-12-30 15:07:23 +0900392class AidlMember : public AidlNode, public AidlTraversable {
Jooyung Han3f347ca2020-12-01 12:41:50 +0900393 public:
394 AidlMember(const AidlLocation& location);
395 virtual ~AidlMember() = default;
396
397 // non-copyable, non-movable
398 AidlMember(const AidlMember&) = delete;
399 AidlMember(AidlMember&&) = delete;
400 AidlMember& operator=(const AidlMember&) = delete;
401 AidlMember& operator=(AidlMember&&) = delete;
402
Jooyung Han829ec7c2020-12-02 12:07:36 +0900403 virtual const AidlMethod* AsMethod() const { return nullptr; }
404 virtual const AidlConstantDeclaration* AsConstantDeclaration() const { return nullptr; }
405 virtual const AidlVariableDeclaration* AsVariableDeclaration() const { return nullptr; }
406
407 AidlMethod* AsMethod() {
408 return const_cast<AidlMethod*>(const_cast<const AidlMember*>(this)->AsMethod());
409 }
410 AidlConstantDeclaration* AsConstantDeclaration() {
411 return const_cast<AidlConstantDeclaration*>(
412 const_cast<const AidlMember*>(this)->AsConstantDeclaration());
413 }
414 AidlVariableDeclaration* AsVariableDeclaration() {
415 return const_cast<AidlVariableDeclaration*>(
416 const_cast<const AidlMember*>(this)->AsVariableDeclaration());
417 }
Jooyung Han808a2a02020-12-28 16:46:54 +0900418
Jiyong Park512ed852020-12-30 15:07:23 +0900419 void TraverseChildren(std::function<void(const AidlTraversable&)> traverse) const = 0;
420 bool DispatchVisit(AidlVisitor& v) const = 0;
Jooyung Han3f347ca2020-12-01 12:41:50 +0900421};
422
Steven Moreland541788d2020-05-21 22:05:52 +0000423// TODO: This class is used for method arguments and also parcelable fields,
424// and it should be split up since default values don't apply to method
425// arguments
Jooyung Han3f347ca2020-12-01 12:41:50 +0900426class AidlVariableDeclaration : public AidlMember {
Steven Moreland5557f1c2018-07-02 13:50:23 -0700427 public:
Steven Moreland46e9da82018-07-27 15:45:29 -0700428 AidlVariableDeclaration(const AidlLocation& location, AidlTypeSpecifier* type,
429 const std::string& name);
430 AidlVariableDeclaration(const AidlLocation& location, AidlTypeSpecifier* type,
431 const std::string& name, AidlConstantValue* default_value);
Steven Moreland5557f1c2018-07-02 13:50:23 -0700432 virtual ~AidlVariableDeclaration() = default;
433
Jiyong Parkd800fef2020-07-22 18:09:43 +0900434 // non-copyable, non-movable
435 AidlVariableDeclaration(const AidlVariableDeclaration&) = delete;
436 AidlVariableDeclaration(AidlVariableDeclaration&&) = delete;
437 AidlVariableDeclaration& operator=(const AidlVariableDeclaration&) = delete;
438 AidlVariableDeclaration& operator=(AidlVariableDeclaration&&) = delete;
439
Jooyung Han829ec7c2020-12-02 12:07:36 +0900440 const AidlVariableDeclaration* AsVariableDeclaration() const override { return this; }
Jooyung Han3f347ca2020-12-01 12:41:50 +0900441
Steven Moreland5557f1c2018-07-02 13:50:23 -0700442 std::string GetName() const { return name_; }
Jooyung Hanacae85d2020-10-28 16:39:09 +0900443 std::string GetCapitalizedName() const;
Jiyong Parkd59a10d2018-07-18 11:12:55 +0900444 const AidlTypeSpecifier& GetType() const { return *type_; }
Steven Moreland541788d2020-05-21 22:05:52 +0000445 // if this was constructed explicitly with a default value
446 bool IsDefaultUserSpecified() const { return default_user_specified_; }
447 // will return the default value this is constructed with or a default value
448 // if one is available
Steven Moreland9ea10e32018-07-19 15:26:09 -0700449 const AidlConstantValue* GetDefaultValue() const { return default_value_.get(); }
Jooyung Han53fb4242020-12-17 16:03:49 +0900450 bool HasUsefulDefaultValue() const;
Steven Moreland9ea10e32018-07-19 15:26:09 -0700451
Jiyong Parkd59a10d2018-07-18 11:12:55 +0900452 AidlTypeSpecifier* GetMutableType() { return type_.get(); }
Steven Moreland5557f1c2018-07-02 13:50:23 -0700453
Jeongik Chadb0f59e2018-11-01 18:11:21 +0900454 bool CheckValid(const AidlTypenames& typenames) const;
Jooyung Han965e31d2020-11-27 12:30:16 +0900455
456 // ToString is for dumping AIDL.
457 // Returns string representation of this variable decl including default value.
458 // This is "annotations type name default_value?".
459 // e.g) "@utf8InCpp String[] names = {"hello"}"
Steven Moreland5557f1c2018-07-02 13:50:23 -0700460 std::string ToString() const;
Jooyung Han965e31d2020-11-27 12:30:16 +0900461
462 // Signature is for comparing AIDL types.
463 // Returns string representation of this variable decl.
464 // This is "type name".
465 // e.g) "String[] name" (even if it is annotated with @utf8InCpp and has a default value.)
Jiyong Park02da7422018-07-16 16:00:26 +0900466 std::string Signature() const;
Steven Moreland5557f1c2018-07-02 13:50:23 -0700467
Steven Moreland860b1942018-08-16 14:59:28 -0700468 std::string ValueString(const ConstantValueDecorator& decorator) const;
Steven Moreland25294322018-08-07 18:13:55 -0700469
Jiyong Park512ed852020-12-30 15:07:23 +0900470 void TraverseChildren(std::function<void(const AidlTraversable&)>) const override {
471 // no children to visit
472 }
473 bool DispatchVisit(AidlVisitor& v) const override { return v.Visit(*this); }
474
Steven Moreland5557f1c2018-07-02 13:50:23 -0700475 private:
Jiyong Parkd59a10d2018-07-18 11:12:55 +0900476 std::unique_ptr<AidlTypeSpecifier> type_;
Steven Moreland5557f1c2018-07-02 13:50:23 -0700477 std::string name_;
Steven Moreland541788d2020-05-21 22:05:52 +0000478 bool default_user_specified_;
Steven Moreland9ea10e32018-07-19 15:26:09 -0700479 std::unique_ptr<AidlConstantValue> default_value_;
Steven Moreland5557f1c2018-07-02 13:50:23 -0700480};
481
482class AidlArgument : public AidlVariableDeclaration {
Casey Dahlinbc7a50a2015-09-28 19:20:50 -0700483 public:
Casey Dahlinc378c992015-09-29 16:50:40 -0700484 enum Direction { IN_DIR = 1, OUT_DIR = 2, INOUT_DIR = 3 };
485
Steven Moreland46e9da82018-07-27 15:45:29 -0700486 AidlArgument(const AidlLocation& location, AidlArgument::Direction direction,
487 AidlTypeSpecifier* type, const std::string& name);
488 AidlArgument(const AidlLocation& location, AidlTypeSpecifier* type, const std::string& name);
Casey Dahlinbc7a50a2015-09-28 19:20:50 -0700489 virtual ~AidlArgument() = default;
490
Jiyong Parkd800fef2020-07-22 18:09:43 +0900491 // non-copyable, non-movable
492 AidlArgument(const AidlArgument&) = delete;
493 AidlArgument(AidlArgument&&) = delete;
494 AidlArgument& operator=(const AidlArgument&) = delete;
495 AidlArgument& operator=(AidlArgument&&) = delete;
496
Casey Dahlinc378c992015-09-29 16:50:40 -0700497 Direction GetDirection() const { return direction_; }
Christopher Wileyad339272015-10-05 19:11:58 -0700498 bool IsOut() const { return direction_ & OUT_DIR; }
499 bool IsIn() const { return direction_ & IN_DIR; }
Casey Dahlinc378c992015-09-29 16:50:40 -0700500 bool DirectionWasSpecified() const { return direction_specified_; }
Jiyong Park3656c3c2018-08-01 20:02:01 +0900501 string GetDirectionSpecifier() const;
Christopher Wileyad339272015-10-05 19:11:58 -0700502
Jooyung Han965e31d2020-11-27 12:30:16 +0900503 // ToString is for dumping AIDL.
504 // Returns string representation of this argument including direction
505 // This is "direction annotations type name".
506 // e.g) "in @utf8InCpp String[] names"
Casey Dahlinc378c992015-09-29 16:50:40 -0700507 std::string ToString() const;
Jiyong Park512ed852020-12-30 15:07:23 +0900508
509 void TraverseChildren(std::function<void(const AidlTraversable&)>) const override {
510 // no children to visit
511 }
512 bool DispatchVisit(AidlVisitor& v) const override { return v.Visit(*this); }
Casey Dahlinc378c992015-09-29 16:50:40 -0700513
Casey Dahlinbc7a50a2015-09-28 19:20:50 -0700514 private:
Casey Dahlinc378c992015-09-29 16:50:40 -0700515 Direction direction_;
516 bool direction_specified_;
Casey Dahlina834dd42015-09-23 11:52:15 -0700517};
Adam Lesinskiffa16862014-01-23 18:17:42 -0800518
Will McVickerd7d18df2019-09-12 13:40:50 -0700519class AidlUnaryConstExpression;
520class AidlBinaryConstExpression;
Jooyung Han690f5842020-12-04 13:02:04 +0900521class AidlConstantReference;
Will McVickerd7d18df2019-09-12 13:40:50 -0700522
Steven Moreland693640b2018-07-19 13:46:27 -0700523class AidlConstantValue : public AidlNode {
Casey Dahlind40e2fe2015-11-24 14:06:52 -0800524 public:
Will McVickerd7d18df2019-09-12 13:40:50 -0700525 enum class Type {
526 // WARNING: Don't change this order! The order is used to determine type
527 // promotion during a binary expression.
528 BOOLEAN,
529 INT8,
530 INT32,
531 INT64,
532 ARRAY,
533 CHARACTER,
534 STRING,
Jooyung Han690f5842020-12-04 13:02:04 +0900535 REF,
Will McVickerd7d18df2019-09-12 13:40:50 -0700536 FLOATING,
537 UNARY,
538 BINARY,
539 ERROR,
540 };
541
Jooyung Han690f5842020-12-04 13:02:04 +0900542 struct Visitor {
543 virtual ~Visitor() {}
544 virtual void Visit(AidlConstantValue&) = 0;
545 virtual void Visit(AidlConstantReference&) = 0;
546 virtual void Visit(AidlUnaryConstExpression&) = 0;
547 virtual void Visit(AidlBinaryConstExpression&) = 0;
548 };
549
Jooyung Han535c5e82020-12-29 15:16:59 +0900550 // Returns the evaluated value. T> should match to the actual type.
Will McVickerd7d18df2019-09-12 13:40:50 -0700551 template <typename T>
Jooyung Han535c5e82020-12-29 15:16:59 +0900552 T EvaluatedValue() const {
553 is_evaluated_ || (CheckValid() && evaluate());
554 AIDL_FATAL_IF(!is_valid_, this);
555
556 if constexpr (is_vector<T>::value) {
557 AIDL_FATAL_IF(final_type_ != Type::ARRAY, this);
558 T result;
559 for (const auto& v : values_) {
560 result.push_back(v->EvaluatedValue<typename T::value_type>());
561 }
562 return result;
563 } else if constexpr (is_one_of<T, float, double>::value) {
564 AIDL_FATAL_IF(final_type_ != Type::FLOATING, this);
565 T result;
566 AIDL_FATAL_IF(!ParseFloating(value_, &result), this);
567 return result;
568 } else if constexpr (std::is_same<T, std::string>::value) {
569 AIDL_FATAL_IF(final_type_ != Type::STRING, this);
570 return final_string_value_.substr(1, final_string_value_.size() - 2); // unquote "
571 } else if constexpr (is_one_of<T, int8_t, int32_t, int64_t>::value) {
572 AIDL_FATAL_IF(final_type_ < Type::INT8 && final_type_ > Type::INT64, this);
573 return static_cast<T>(final_value_);
574 } else if constexpr (std::is_same<T, char>::value) {
575 AIDL_FATAL_IF(final_type_ != Type::CHARACTER, this);
576 return final_string_value_.at(1); // unquote '
577 } else if constexpr (std::is_same<T, bool>::value) {
578 static_assert(std::is_same<T, bool>::value, "..");
579 AIDL_FATAL_IF(final_type_ != Type::BOOLEAN, this);
580 return final_value_ != 0;
581 } else {
582 static_assert(unsupported_type<T>::value);
583 }
584 }
Casey Dahlind40e2fe2015-11-24 14:06:52 -0800585
Steven Moreland693640b2018-07-19 13:46:27 -0700586 virtual ~AidlConstantValue() = default;
Casey Dahlind40e2fe2015-11-24 14:06:52 -0800587
Jiyong Parkd800fef2020-07-22 18:09:43 +0900588 // non-copyable, non-movable
589 AidlConstantValue(const AidlConstantValue&) = delete;
590 AidlConstantValue(AidlConstantValue&&) = delete;
591 AidlConstantValue& operator=(const AidlConstantValue&) = delete;
592 AidlConstantValue& operator=(AidlConstantValue&&) = delete;
593
Steven Moreland541788d2020-05-21 22:05:52 +0000594 // creates default value, when one isn't specified
595 // nullptr if no default available
596 static AidlConstantValue* Default(const AidlTypeSpecifier& specifier);
597
Steven Moreland25294322018-08-07 18:13:55 -0700598 static AidlConstantValue* Boolean(const AidlLocation& location, bool value);
599 static AidlConstantValue* Character(const AidlLocation& location, char value);
Steven Moreland25294322018-08-07 18:13:55 -0700600 // example: 123, -5498, maybe any size
Will McVickerd7d18df2019-09-12 13:40:50 -0700601 static AidlConstantValue* Integral(const AidlLocation& location, const string& value);
602 static AidlConstantValue* Floating(const AidlLocation& location, const std::string& value);
Steven Moreland860b1942018-08-16 14:59:28 -0700603 static AidlConstantValue* Array(const AidlLocation& location,
Will McVickerd7d18df2019-09-12 13:40:50 -0700604 std::unique_ptr<vector<unique_ptr<AidlConstantValue>>> values);
Steven Moreland693640b2018-07-19 13:46:27 -0700605 // example: "\"asdf\""
Will McVickerd7d18df2019-09-12 13:40:50 -0700606 static AidlConstantValue* String(const AidlLocation& location, const string& value);
Steven Moreland693640b2018-07-19 13:46:27 -0700607
Will McVickerd7d18df2019-09-12 13:40:50 -0700608 Type GetType() const { return final_type_; }
Jooyung Han29813842020-12-08 01:28:03 +0900609 const std::string& Literal() const { return value_; }
Steven Moreland25294322018-08-07 18:13:55 -0700610
Will McVickerd7d18df2019-09-12 13:40:50 -0700611 virtual bool CheckValid() const;
Steven Moreland860b1942018-08-16 14:59:28 -0700612
613 // Raw value of type (currently valid in C++ and Java). Empty string on error.
Steven Moreland4bcb05c2019-11-27 18:57:47 -0800614 string ValueString(const AidlTypeSpecifier& type, const ConstantValueDecorator& decorator) const;
Jooyung Han29813842020-12-08 01:28:03 +0900615 virtual void Accept(Visitor& visitor) {
616 visitor.Visit(*this);
617 if (type_ == Type::ARRAY) {
618 for (const auto& v : values_) {
619 v.get()->Accept(visitor);
620 }
621 }
622 }
Casey Dahlind40e2fe2015-11-24 14:06:52 -0800623
624 private:
Will McVickerd7d18df2019-09-12 13:40:50 -0700625 AidlConstantValue(const AidlLocation& location, Type parsed_type, int64_t parsed_value,
626 const string& checked_value);
627 AidlConstantValue(const AidlLocation& location, Type type, const string& checked_value);
Steven Moreland860b1942018-08-16 14:59:28 -0700628 AidlConstantValue(const AidlLocation& location, Type type,
Jooyung Han29813842020-12-08 01:28:03 +0900629 std::unique_ptr<vector<unique_ptr<AidlConstantValue>>> values,
630 const std::string& value);
Steven Moreland25294322018-08-07 18:13:55 -0700631 static string ToString(Type type);
Will McVickerd7d18df2019-09-12 13:40:50 -0700632 static bool ParseIntegral(const string& value, int64_t* parsed_value, Type* parsed_type);
633 static bool IsHex(const string& value);
Steven Moreland4bcb05c2019-11-27 18:57:47 -0800634
Jooyung Han74675c22020-12-15 08:39:57 +0900635 virtual bool evaluate() const;
Casey Dahlind40e2fe2015-11-24 14:06:52 -0800636
Steven Moreland693640b2018-07-19 13:46:27 -0700637 const Type type_ = Type::ERROR;
Will McVickerd7d18df2019-09-12 13:40:50 -0700638 const vector<unique_ptr<AidlConstantValue>> values_; // if type_ == ARRAY
639 const string value_; // otherwise
640
641 // State for tracking evaluation of expressions
Steven Moreland4bcb05c2019-11-27 18:57:47 -0800642 mutable bool is_valid_ = false; // cache of CheckValid, but may be marked false in evaluate
643 mutable bool is_evaluated_ = false; // whether evaluate has been called
Will McVickerd7d18df2019-09-12 13:40:50 -0700644 mutable Type final_type_;
645 mutable int64_t final_value_;
646 mutable string final_string_value_ = "";
Steven Moreland693640b2018-07-19 13:46:27 -0700647
Will McVickerd7d18df2019-09-12 13:40:50 -0700648 friend AidlUnaryConstExpression;
649 friend AidlBinaryConstExpression;
Jooyung Han690f5842020-12-04 13:02:04 +0900650 friend AidlConstantReference;
651};
652
653// Represents "<type>.<field>" which resolves to a constant which is one of
654// - constant declartion
655// - enumerator
656// When a <type> is missing, <field> is of the enclosing type.
657class AidlConstantReference : public AidlConstantValue {
658 public:
659 AidlConstantReference(const AidlLocation& location, const std::string& value,
660 const std::string& comments);
661
662 const std::unique_ptr<AidlTypeSpecifier>& GetRefType() const { return ref_type_; }
663 void SetRefType(std::unique_ptr<AidlTypeSpecifier> type) { ref_type_ = std::move(type); }
664 const std::string& GetFieldName() const { return field_name_; }
665 const std::string& GetComments() const { return comments_; }
666
667 bool CheckValid() const override;
668 void Accept(Visitor& visitor) override { visitor.Visit(*this); }
Jooyung Han29813842020-12-08 01:28:03 +0900669 const AidlConstantValue* Resolve();
Jooyung Han690f5842020-12-04 13:02:04 +0900670
671 private:
Jooyung Han74675c22020-12-15 08:39:57 +0900672 bool evaluate() const override;
Jooyung Han690f5842020-12-04 13:02:04 +0900673
674 std::unique_ptr<AidlTypeSpecifier> ref_type_;
675 std::string field_name_;
676 const std::string comments_;
Jooyung Han29813842020-12-08 01:28:03 +0900677 const AidlConstantValue* resolved_ = nullptr;
Will McVickerd7d18df2019-09-12 13:40:50 -0700678};
679
680class AidlUnaryConstExpression : public AidlConstantValue {
681 public:
682 AidlUnaryConstExpression(const AidlLocation& location, const string& op,
683 std::unique_ptr<AidlConstantValue> rval);
684
685 static bool IsCompatibleType(Type type, const string& op);
686 bool CheckValid() const override;
Jooyung Han690f5842020-12-04 13:02:04 +0900687 void Accept(Visitor& visitor) override {
688 visitor.Visit(*this);
689 unary_->Accept(visitor);
690 }
691
Will McVickerd7d18df2019-09-12 13:40:50 -0700692 private:
Jooyung Han74675c22020-12-15 08:39:57 +0900693 bool evaluate() const override;
Will McVickerd7d18df2019-09-12 13:40:50 -0700694
695 std::unique_ptr<AidlConstantValue> unary_;
696 const string op_;
697};
698
699class AidlBinaryConstExpression : public AidlConstantValue {
700 public:
701 AidlBinaryConstExpression(const AidlLocation& location, std::unique_ptr<AidlConstantValue> lval,
702 const string& op, std::unique_ptr<AidlConstantValue> rval);
703
704 bool CheckValid() const override;
Will McVickerd7d18df2019-09-12 13:40:50 -0700705
706 static bool AreCompatibleTypes(Type t1, Type t2);
707 // Returns the promoted kind for both operands
708 static Type UsualArithmeticConversion(Type left, Type right);
709 // Returns the promoted integral type where INT32 is the smallest type
710 static Type IntegralPromotion(Type in);
Jooyung Han690f5842020-12-04 13:02:04 +0900711 void Accept(Visitor& visitor) override {
712 visitor.Visit(*this);
713 left_val_->Accept(visitor);
714 right_val_->Accept(visitor);
715 }
Will McVickerd7d18df2019-09-12 13:40:50 -0700716
717 private:
Jooyung Han74675c22020-12-15 08:39:57 +0900718 bool evaluate() const override;
Will McVickerd7d18df2019-09-12 13:40:50 -0700719
720 std::unique_ptr<AidlConstantValue> left_val_;
721 std::unique_ptr<AidlConstantValue> right_val_;
722 const string op_;
Christopher Wileyd6bdd8d2016-05-03 11:23:13 -0700723};
724
Andrei Onea9445fc62019-06-27 18:11:59 +0100725struct AidlAnnotationParameter {
726 std::string name;
727 std::unique_ptr<AidlConstantValue> value;
728};
729
Steven Moreland693640b2018-07-19 13:46:27 -0700730class AidlConstantDeclaration : public AidlMember {
Christopher Wileyd6bdd8d2016-05-03 11:23:13 -0700731 public:
Steven Moreland46e9da82018-07-27 15:45:29 -0700732 AidlConstantDeclaration(const AidlLocation& location, AidlTypeSpecifier* specifier,
Will McVickerd7d18df2019-09-12 13:40:50 -0700733 const string& name, AidlConstantValue* value);
Steven Moreland693640b2018-07-19 13:46:27 -0700734 virtual ~AidlConstantDeclaration() = default;
Christopher Wileyd6bdd8d2016-05-03 11:23:13 -0700735
Jiyong Parkd800fef2020-07-22 18:09:43 +0900736 // non-copyable, non-movable
737 AidlConstantDeclaration(const AidlConstantDeclaration&) = delete;
738 AidlConstantDeclaration(AidlConstantDeclaration&&) = delete;
739 AidlConstantDeclaration& operator=(const AidlConstantDeclaration&) = delete;
740 AidlConstantDeclaration& operator=(AidlConstantDeclaration&&) = delete;
741
Steven Moreland693640b2018-07-19 13:46:27 -0700742 const AidlTypeSpecifier& GetType() const { return *type_; }
Steven Moreland4d12f9a2018-10-31 14:30:55 -0700743 AidlTypeSpecifier* GetMutableType() { return type_.get(); }
Will McVickerd7d18df2019-09-12 13:40:50 -0700744 const string& GetName() const { return name_; }
Steven Moreland693640b2018-07-19 13:46:27 -0700745 const AidlConstantValue& GetValue() const { return *value_; }
Jeongik Chadb0f59e2018-11-01 18:11:21 +0900746 bool CheckValid(const AidlTypenames& typenames) const;
Christopher Wileyd6bdd8d2016-05-03 11:23:13 -0700747
Jooyung Han965e31d2020-11-27 12:30:16 +0900748 // ToString is for dumping AIDL.
749 // Returns string representation of this const decl including a const value.
750 // This is "`const` annotations type name value".
751 // e.g) "const @utf8InCpp String[] names = { "hello" }"
Will McVickerd7d18df2019-09-12 13:40:50 -0700752 string ToString() const;
Jooyung Han965e31d2020-11-27 12:30:16 +0900753
754 // Signature is for comparing types.
755 // Returns string representation of this const decl.
756 // This is "direction annotations type name".
757 // e.g) "String[] names"
Will McVickerd7d18df2019-09-12 13:40:50 -0700758 string Signature() const;
Jooyung Han965e31d2020-11-27 12:30:16 +0900759
Steven Moreland860b1942018-08-16 14:59:28 -0700760 string ValueString(const ConstantValueDecorator& decorator) const {
Will McVickerd7d18df2019-09-12 13:40:50 -0700761 return value_->ValueString(GetType(), decorator);
Steven Moreland860b1942018-08-16 14:59:28 -0700762 }
Steven Moreland25294322018-08-07 18:13:55 -0700763
Jooyung Han829ec7c2020-12-02 12:07:36 +0900764 const AidlConstantDeclaration* AsConstantDeclaration() const override { return this; }
Christopher Wileyd6bdd8d2016-05-03 11:23:13 -0700765
Jiyong Park512ed852020-12-30 15:07:23 +0900766 void TraverseChildren(std::function<void(const AidlTraversable&)>) const override {
767 // no children to traverse
768 }
769 bool DispatchVisit(AidlVisitor& v) const override { return v.Visit(*this); }
770
Christopher Wileyd6bdd8d2016-05-03 11:23:13 -0700771 private:
Steven Moreland693640b2018-07-19 13:46:27 -0700772 const unique_ptr<AidlTypeSpecifier> type_;
Will McVickerd7d18df2019-09-12 13:40:50 -0700773 const string name_;
774 unique_ptr<AidlConstantValue> value_;
Casey Dahlind40e2fe2015-11-24 14:06:52 -0800775};
776
777class AidlMethod : public AidlMember {
Casey Dahlin5c69deb2015-10-01 14:44:12 -0700778 public:
Will McVickerd7d18df2019-09-12 13:40:50 -0700779 AidlMethod(const AidlLocation& location, bool oneway, AidlTypeSpecifier* type, const string& name,
780 vector<unique_ptr<AidlArgument>>* args, const string& comments);
781 AidlMethod(const AidlLocation& location, bool oneway, AidlTypeSpecifier* type, const string& name,
782 vector<unique_ptr<AidlArgument>>* args, const string& comments, int id,
783 bool is_user_defined = true);
Casey Dahlin5c69deb2015-10-01 14:44:12 -0700784 virtual ~AidlMethod() = default;
785
Jiyong Parkd800fef2020-07-22 18:09:43 +0900786 // non-copyable, non-movable
787 AidlMethod(const AidlMethod&) = delete;
788 AidlMethod(AidlMethod&&) = delete;
789 AidlMethod& operator=(const AidlMethod&) = delete;
790 AidlMethod& operator=(AidlMethod&&) = delete;
791
Jooyung Han829ec7c2020-12-02 12:07:36 +0900792 const AidlMethod* AsMethod() const override { return this; }
Jeongik Cha997281d2020-01-16 15:23:59 +0900793 bool IsHidden() const;
Will McVickerd7d18df2019-09-12 13:40:50 -0700794 const string& GetComments() const { return comments_; }
Jiyong Parkd59a10d2018-07-18 11:12:55 +0900795 const AidlTypeSpecifier& GetType() const { return *type_; }
796 AidlTypeSpecifier* GetMutableType() { return type_.get(); }
Steven Morelandacd53472018-12-14 10:17:26 -0800797
Steven Moreland8c70ba92018-12-17 10:20:31 -0800798 // set if this method is part of an interface that is marked oneway
799 void ApplyInterfaceOneway(bool oneway) { oneway_ = oneway_ || oneway; }
Casey Dahlinf4a93112015-10-05 16:58:09 -0700800 bool IsOneway() const { return oneway_; }
Steven Morelandacd53472018-12-14 10:17:26 -0800801
Casey Dahlinf4a93112015-10-05 16:58:09 -0700802 const std::string& GetName() const { return name_; }
Casey Dahlinf4a93112015-10-05 16:58:09 -0700803 bool HasId() const { return has_id_; }
Jiyong Parked65bf42018-08-28 15:43:27 +0900804 int GetId() const { return id_; }
Casey Dahlinf4a93112015-10-05 16:58:09 -0700805 void SetId(unsigned id) { id_ = id; }
Casey Dahlinf2d23f72015-10-02 16:19:19 -0700806
Jiyong Park309668e2018-07-28 16:55:44 +0900807 bool IsUserDefined() const { return is_user_defined_; }
808
Casey Dahlinf4a93112015-10-05 16:58:09 -0700809 const std::vector<std::unique_ptr<AidlArgument>>& GetArguments() const {
Christopher Wileyad339272015-10-05 19:11:58 -0700810 return arguments_;
811 }
812 // An inout parameter will appear in both GetInArguments()
813 // and GetOutArguments(). AidlMethod retains ownership of the argument
814 // pointers returned in this way.
815 const std::vector<const AidlArgument*>& GetInArguments() const {
816 return in_arguments_;
817 }
818 const std::vector<const AidlArgument*>& GetOutArguments() const {
819 return out_arguments_;
Casey Dahlinf4a93112015-10-05 16:58:09 -0700820 }
Casey Dahlin5c69deb2015-10-01 14:44:12 -0700821
Jooyung Han965e31d2020-11-27 12:30:16 +0900822 // ToString is for dumping AIDL.
823 // Returns string representation of this method including everything.
824 // This is "ret_type name ( arg_list ) = id".
825 // e.g) "boolean foo(int, @Nullable String) = 1"
Jiyong Park309668e2018-07-28 16:55:44 +0900826 std::string ToString() const;
827
Jooyung Han965e31d2020-11-27 12:30:16 +0900828 // Signature is for comparing AIDL types.
829 // Returns string representation of this method's name & type.
830 // e.g) "foo(int, String)"
831 std::string Signature() const;
832
Jiyong Park512ed852020-12-30 15:07:23 +0900833 void TraverseChildren(std::function<void(const AidlTraversable&)> traverse) const override {
834 for (const auto& a : GetArguments()) {
835 traverse(*a);
836 }
837 }
838 bool DispatchVisit(AidlVisitor& v) const override { return v.Visit(*this); }
Jooyung Han808a2a02020-12-28 16:46:54 +0900839
Casey Dahlin5c69deb2015-10-01 14:44:12 -0700840 private:
Casey Dahlinf4a93112015-10-05 16:58:09 -0700841 bool oneway_;
Casey Dahlinf2d23f72015-10-02 16:19:19 -0700842 std::string comments_;
Jiyong Parkd59a10d2018-07-18 11:12:55 +0900843 std::unique_ptr<AidlTypeSpecifier> type_;
Casey Dahlinf4a93112015-10-05 16:58:09 -0700844 std::string name_;
Christopher Wileyad339272015-10-05 19:11:58 -0700845 const std::vector<std::unique_ptr<AidlArgument>> arguments_;
846 std::vector<const AidlArgument*> in_arguments_;
847 std::vector<const AidlArgument*> out_arguments_;
Casey Dahlinf4a93112015-10-05 16:58:09 -0700848 bool has_id_;
849 int id_;
Jiyong Park309668e2018-07-28 16:55:44 +0900850 bool is_user_defined_ = true;
Casey Dahlin0a2f8be2015-09-28 16:15:29 -0700851};
Adam Lesinskiffa16862014-01-23 18:17:42 -0800852
Steven Morelandc258abc2018-07-10 14:03:38 -0700853class AidlDefinedType;
Jiyong Parkb034bf02018-07-30 17:44:33 +0900854class AidlInterface;
855class AidlParcelable;
856class AidlStructuredParcelable;
Jooyung Han2946afc2020-10-05 20:29:16 +0900857class AidlUnionDecl;
Jooyung Han3f347ca2020-12-01 12:41:50 +0900858
Daniel Norman85aed542019-08-21 12:01:14 -0700859// AidlDefinedType represents either an interface, parcelable, or enum that is
Jiyong Park1deecc32018-07-17 01:14:41 +0900860// defined in the source file.
Jiyong Park512ed852020-12-30 15:07:23 +0900861class AidlDefinedType : public AidlAnnotatable, public AidlTraversable {
Steven Moreland787b0432018-07-03 09:00:58 -0700862 public:
Steven Moreland46e9da82018-07-27 15:45:29 -0700863 AidlDefinedType(const AidlLocation& location, const std::string& name,
Jooyung Han829ec7c2020-12-02 12:07:36 +0900864 const std::string& comments, const std::string& package,
865 std::vector<std::unique_ptr<AidlMember>>* members);
Steven Moreland787b0432018-07-03 09:00:58 -0700866 virtual ~AidlDefinedType() = default;
867
Jiyong Parkd800fef2020-07-22 18:09:43 +0900868 // non-copyable, non-movable
869 AidlDefinedType(const AidlDefinedType&) = delete;
870 AidlDefinedType(AidlDefinedType&&) = delete;
871 AidlDefinedType& operator=(const AidlDefinedType&) = delete;
872 AidlDefinedType& operator=(AidlDefinedType&&) = delete;
873
Jiyong Park1deecc32018-07-17 01:14:41 +0900874 const std::string& GetName() const { return name_; };
Jeongik Cha997281d2020-01-16 15:23:59 +0900875 bool IsHidden() const;
Jiyong Park1deecc32018-07-17 01:14:41 +0900876 const std::string& GetComments() const { return comments_; }
Jiyong Parka6605ab2018-11-11 14:30:21 +0900877 void SetComments(const std::string comments) { comments_ = comments; }
Jiyong Park1deecc32018-07-17 01:14:41 +0900878
Steven Moreland787b0432018-07-03 09:00:58 -0700879 /* dot joined package, example: "android.package.foo" */
Jiyong Park18132182020-06-08 20:24:40 +0900880 std::string GetPackage() const { return package_; }
Steven Moreland787b0432018-07-03 09:00:58 -0700881 /* dot joined package and name, example: "android.package.foo.IBar" */
882 std::string GetCanonicalName() const;
Jiyong Park18132182020-06-08 20:24:40 +0900883 const std::vector<std::string>& GetSplitPackage() const { return split_package_; }
Steven Moreland787b0432018-07-03 09:00:58 -0700884
Steven Morelanded83a282018-07-17 13:27:29 -0700885 virtual std::string GetPreprocessDeclarationName() const = 0;
Steven Morelandc258abc2018-07-10 14:03:38 -0700886
Steven Moreland5557f1c2018-07-02 13:50:23 -0700887 virtual const AidlStructuredParcelable* AsStructuredParcelable() const { return nullptr; }
Steven Morelandc258abc2018-07-10 14:03:38 -0700888 virtual const AidlParcelable* AsParcelable() const { return nullptr; }
Daniel Norman85aed542019-08-21 12:01:14 -0700889 virtual const AidlEnumDeclaration* AsEnumDeclaration() const { return nullptr; }
Jooyung Han2946afc2020-10-05 20:29:16 +0900890 virtual const AidlUnionDecl* AsUnionDeclaration() const { return nullptr; }
Steven Moreland5557f1c2018-07-02 13:50:23 -0700891 virtual const AidlInterface* AsInterface() const { return nullptr; }
Jeongik Chadf76dc72019-11-28 00:08:47 +0900892 virtual const AidlParameterizable<std::string>* AsParameterizable() const { return nullptr; }
Jooyung Han808a2a02020-12-28 16:46:54 +0900893 virtual bool CheckValid(const AidlTypenames& typenames) const;
Steven Morelandd59e3172020-05-11 16:42:09 -0700894 virtual bool LanguageSpecificCheckValid(const AidlTypenames& typenames,
895 Options::Language lang) const = 0;
Steven Morelandc258abc2018-07-10 14:03:38 -0700896 AidlStructuredParcelable* AsStructuredParcelable() {
897 return const_cast<AidlStructuredParcelable*>(
898 const_cast<const AidlDefinedType*>(this)->AsStructuredParcelable());
899 }
900 AidlParcelable* AsParcelable() {
901 return const_cast<AidlParcelable*>(const_cast<const AidlDefinedType*>(this)->AsParcelable());
902 }
Daniel Norman85aed542019-08-21 12:01:14 -0700903 AidlEnumDeclaration* AsEnumDeclaration() {
904 return const_cast<AidlEnumDeclaration*>(
905 const_cast<const AidlDefinedType*>(this)->AsEnumDeclaration());
906 }
Jooyung Han2946afc2020-10-05 20:29:16 +0900907 AidlUnionDecl* AsUnionDeclaration() {
908 return const_cast<AidlUnionDecl*>(
909 const_cast<const AidlDefinedType*>(this)->AsUnionDeclaration());
910 }
Steven Morelandc258abc2018-07-10 14:03:38 -0700911 AidlInterface* AsInterface() {
912 return const_cast<AidlInterface*>(const_cast<const AidlDefinedType*>(this)->AsInterface());
913 }
914
Jeongik Chadf76dc72019-11-28 00:08:47 +0900915 AidlParameterizable<std::string>* AsParameterizable() {
916 return const_cast<AidlParameterizable<std::string>*>(
917 const_cast<const AidlDefinedType*>(this)->AsParameterizable());
918 }
919
Steven Moreland6cee3482018-07-18 14:39:58 -0700920 const AidlParcelable* AsUnstructuredParcelable() const {
921 if (this->AsStructuredParcelable() != nullptr) return nullptr;
Jooyung Han2946afc2020-10-05 20:29:16 +0900922 if (this->AsUnionDeclaration() != nullptr) return nullptr;
Steven Moreland6cee3482018-07-18 14:39:58 -0700923 return this->AsParcelable();
924 }
925 AidlParcelable* AsUnstructuredParcelable() {
926 return const_cast<AidlParcelable*>(
927 const_cast<const AidlDefinedType*>(this)->AsUnstructuredParcelable());
928 }
929
Jeongik Cha997281d2020-01-16 15:23:59 +0900930 virtual void Dump(CodeWriter* writer) const = 0;
Steven Morelanda5d9c5c2020-02-21 16:01:09 -0800931 void DumpHeader(CodeWriter* writer) const;
Jiyong Park02da7422018-07-16 16:00:26 +0900932
Jooyung Han829ec7c2020-12-02 12:07:36 +0900933 const std::vector<std::unique_ptr<AidlVariableDeclaration>>& GetFields() const {
934 return variables_;
935 }
936 const std::vector<std::unique_ptr<AidlConstantDeclaration>>& GetConstantDeclarations() const {
937 return constants_;
938 }
939 const std::vector<std::unique_ptr<AidlMethod>>& GetMethods() const { return methods_; }
940 void AddMethod(std::unique_ptr<AidlMethod> method) { methods_.push_back(std::move(method)); }
941 const std::vector<const AidlMember*>& GetMembers() const { return members_; }
942
Jiyong Park512ed852020-12-30 15:07:23 +0900943 void TraverseChildren(std::function<void(const AidlTraversable&)>) const = 0;
944 bool DispatchVisit(AidlVisitor& v) const = 0;
945
Jooyung Han829ec7c2020-12-02 12:07:36 +0900946 protected:
947 // utility for subclasses with getter names
948 bool CheckValidForGetterNames() const;
949
Steven Moreland787b0432018-07-03 09:00:58 -0700950 private:
Jooyung Han829ec7c2020-12-02 12:07:36 +0900951 bool CheckValidWithMembers(const AidlTypenames& typenames) const;
952
Jiyong Park1deecc32018-07-17 01:14:41 +0900953 std::string name_;
Jiyong Park1deecc32018-07-17 01:14:41 +0900954 std::string comments_;
Jiyong Park18132182020-06-08 20:24:40 +0900955 const std::string package_;
956 const std::vector<std::string> split_package_;
Jooyung Han829ec7c2020-12-02 12:07:36 +0900957 std::vector<std::unique_ptr<AidlVariableDeclaration>> variables_;
958 std::vector<std::unique_ptr<AidlConstantDeclaration>> constants_;
959 std::vector<std::unique_ptr<AidlMethod>> methods_;
960 std::vector<const AidlMember*> members_; // keep members in order of appearance.
Steven Moreland787b0432018-07-03 09:00:58 -0700961};
962
Jeongik Chadf76dc72019-11-28 00:08:47 +0900963class AidlParcelable : public AidlDefinedType, public AidlParameterizable<std::string> {
Casey Dahlin1ae2bc52015-10-07 18:49:10 -0700964 public:
Jiyong Park18132182020-06-08 20:24:40 +0900965 AidlParcelable(const AidlLocation& location, const std::string& name, const std::string& package,
966 const std::string& comments, const std::string& cpp_header = "",
Jooyung Han829ec7c2020-12-02 12:07:36 +0900967 std::vector<std::string>* type_params = nullptr,
968 std::vector<std::unique_ptr<AidlMember>>* members = nullptr);
Casey Dahlin1ae2bc52015-10-07 18:49:10 -0700969 virtual ~AidlParcelable() = default;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800970
Jiyong Parkd800fef2020-07-22 18:09:43 +0900971 // non-copyable, non-movable
972 AidlParcelable(const AidlParcelable&) = delete;
973 AidlParcelable(AidlParcelable&&) = delete;
974 AidlParcelable& operator=(const AidlParcelable&) = delete;
975 AidlParcelable& operator=(AidlParcelable&&) = delete;
976
Christopher Wiley8aa4d9f2015-11-16 19:10:45 -0800977 std::string GetCppHeader() const { return cpp_header_; }
Christopher Wiley8aa4d9f2015-11-16 19:10:45 -0800978
Steven Moreland0cea4aa2020-04-20 21:06:02 -0700979 std::set<AidlAnnotation::Type> GetSupportedAnnotations() const override;
Jooyung Han808a2a02020-12-28 16:46:54 +0900980 bool CheckValid(const AidlTypenames& typenames) const override;
Steven Morelandd59e3172020-05-11 16:42:09 -0700981 bool LanguageSpecificCheckValid(const AidlTypenames& typenames,
982 Options::Language lang) const override;
Steven Morelandc258abc2018-07-10 14:03:38 -0700983 const AidlParcelable* AsParcelable() const override { return this; }
Jeongik Chadf76dc72019-11-28 00:08:47 +0900984 const AidlParameterizable<std::string>* AsParameterizable() const override { return this; }
985 const AidlNode& AsAidlNode() const override { return *this; }
Steven Morelanded83a282018-07-17 13:27:29 -0700986 std::string GetPreprocessDeclarationName() const override { return "parcelable"; }
Steven Morelandc258abc2018-07-10 14:03:38 -0700987
Jeongik Cha997281d2020-01-16 15:23:59 +0900988 void Dump(CodeWriter* writer) const override;
Jiyong Park02da7422018-07-16 16:00:26 +0900989
Jiyong Park512ed852020-12-30 15:07:23 +0900990 void TraverseChildren(std::function<void(const AidlTraversable&)> traverse) const override {
991 for (const auto c : GetMembers()) {
992 traverse(*c);
993 }
994 }
995 bool DispatchVisit(AidlVisitor& v) const override { return v.Visit(*this); }
996
Casey Dahlin1ae2bc52015-10-07 18:49:10 -0700997 private:
Christopher Wiley8aa4d9f2015-11-16 19:10:45 -0800998 std::string cpp_header_;
Casey Dahlin0a2f8be2015-09-28 16:15:29 -0700999};
Adam Lesinskiffa16862014-01-23 18:17:42 -08001000
Jooyung Han829ec7c2020-12-02 12:07:36 +09001001class AidlStructuredParcelable : public AidlParcelable {
Steven Moreland5557f1c2018-07-02 13:50:23 -07001002 public:
Jiyong Park18132182020-06-08 20:24:40 +09001003 AidlStructuredParcelable(const AidlLocation& location, const std::string& name,
1004 const std::string& package, const std::string& comments,
Jooyung Han829ec7c2020-12-02 12:07:36 +09001005 std::vector<std::string>* type_params,
1006 std::vector<std::unique_ptr<AidlMember>>* members);
Jiyong Parkd800fef2020-07-22 18:09:43 +09001007 virtual ~AidlStructuredParcelable() = default;
1008
1009 // non-copyable, non-movable
1010 AidlStructuredParcelable(const AidlStructuredParcelable&) = delete;
1011 AidlStructuredParcelable(AidlStructuredParcelable&&) = delete;
1012 AidlStructuredParcelable& operator=(const AidlStructuredParcelable&) = delete;
1013 AidlStructuredParcelable& operator=(AidlStructuredParcelable&&) = delete;
Steven Moreland5557f1c2018-07-02 13:50:23 -07001014
Steven Morelandc258abc2018-07-10 14:03:38 -07001015 const AidlStructuredParcelable* AsStructuredParcelable() const override { return this; }
Steven Morelanded83a282018-07-17 13:27:29 -07001016 std::string GetPreprocessDeclarationName() const override { return "structured_parcelable"; }
Steven Moreland5557f1c2018-07-02 13:50:23 -07001017
Jeongik Cha997281d2020-01-16 15:23:59 +09001018 void Dump(CodeWriter* writer) const override;
Jiyong Park02da7422018-07-16 16:00:26 +09001019
Steven Moreland0cea4aa2020-04-20 21:06:02 -07001020 std::set<AidlAnnotation::Type> GetSupportedAnnotations() const override;
Jooyung Han808a2a02020-12-28 16:46:54 +09001021 bool CheckValid(const AidlTypenames& typenames) const override;
Steven Morelandd59e3172020-05-11 16:42:09 -07001022 bool LanguageSpecificCheckValid(const AidlTypenames& typenames,
1023 Options::Language lang) const override;
Jiyong Park512ed852020-12-30 15:07:23 +09001024
1025 void TraverseChildren(std::function<void(const AidlTraversable&)> traverse) const override {
1026 for (const auto c : GetMembers()) {
1027 traverse(*c);
1028 }
1029 }
1030 bool DispatchVisit(AidlVisitor& v) const override { return v.Visit(*this); }
Steven Moreland5557f1c2018-07-02 13:50:23 -07001031};
1032
Jiyong Park512ed852020-12-30 15:07:23 +09001033class AidlEnumerator : public AidlNode, public AidlTraversable {
Daniel Norman85aed542019-08-21 12:01:14 -07001034 public:
Daniel Norman2e4112d2019-10-03 10:22:35 -07001035 AidlEnumerator(const AidlLocation& location, const std::string& name, AidlConstantValue* value,
1036 const std::string& comments);
Daniel Norman85aed542019-08-21 12:01:14 -07001037 virtual ~AidlEnumerator() = default;
1038
Jiyong Parkd800fef2020-07-22 18:09:43 +09001039 // non-copyable, non-movable
1040 AidlEnumerator(const AidlEnumerator&) = delete;
1041 AidlEnumerator(AidlEnumerator&&) = delete;
1042 AidlEnumerator& operator=(const AidlEnumerator&) = delete;
1043 AidlEnumerator& operator=(AidlEnumerator&&) = delete;
1044
Daniel Norman85aed542019-08-21 12:01:14 -07001045 const std::string& GetName() const { return name_; }
Will McVickerd7d18df2019-09-12 13:40:50 -07001046 AidlConstantValue* GetValue() const { return value_.get(); }
Daniel Norman2e4112d2019-10-03 10:22:35 -07001047 const std::string& GetComments() const { return comments_; }
Daniel Norman85aed542019-08-21 12:01:14 -07001048 bool CheckValid(const AidlTypeSpecifier& enum_backing_type) const;
1049
1050 string ValueString(const AidlTypeSpecifier& backing_type,
1051 const ConstantValueDecorator& decorator) const;
1052
Daniel Normanb28684e2019-10-17 15:31:39 -07001053 void SetValue(std::unique_ptr<AidlConstantValue> value) { value_ = std::move(value); }
Jooyung Han29813842020-12-08 01:28:03 +09001054 bool IsValueUserSpecified() const { return value_user_specified_; }
Daniel Normanb28684e2019-10-17 15:31:39 -07001055
Jiyong Park512ed852020-12-30 15:07:23 +09001056 void TraverseChildren(std::function<void(const AidlTraversable&)>) const override {
1057 // no children to traverse
1058 }
1059 bool DispatchVisit(AidlVisitor& v) const override { return v.Visit(*this); }
1060
Daniel Norman85aed542019-08-21 12:01:14 -07001061 private:
1062 const std::string name_;
Will McVickerd7d18df2019-09-12 13:40:50 -07001063 unique_ptr<AidlConstantValue> value_;
Daniel Norman2e4112d2019-10-03 10:22:35 -07001064 const std::string comments_;
Jooyung Han29813842020-12-08 01:28:03 +09001065 const bool value_user_specified_;
Daniel Norman85aed542019-08-21 12:01:14 -07001066};
1067
1068class AidlEnumDeclaration : public AidlDefinedType {
1069 public:
Will McVickerd7d18df2019-09-12 13:40:50 -07001070 AidlEnumDeclaration(const AidlLocation& location, const string& name,
Daniel Norman85aed542019-08-21 12:01:14 -07001071 std::vector<std::unique_ptr<AidlEnumerator>>* enumerators,
Jiyong Park18132182020-06-08 20:24:40 +09001072 const std::string& package, const std::string& comments);
Daniel Norman85aed542019-08-21 12:01:14 -07001073 virtual ~AidlEnumDeclaration() = default;
1074
Jiyong Parkd800fef2020-07-22 18:09:43 +09001075 // non-copyable, non-movable
1076 AidlEnumDeclaration(const AidlEnumDeclaration&) = delete;
1077 AidlEnumDeclaration(AidlEnumDeclaration&&) = delete;
1078 AidlEnumDeclaration& operator=(const AidlEnumDeclaration&) = delete;
1079 AidlEnumDeclaration& operator=(AidlEnumDeclaration&&) = delete;
1080
Jooyung Han672557b2020-12-24 05:18:00 +09001081 bool Autofill(const AidlTypenames&);
Daniel Norman85aed542019-08-21 12:01:14 -07001082 const AidlTypeSpecifier& GetBackingType() const { return *backing_type_; }
1083 const std::vector<std::unique_ptr<AidlEnumerator>>& GetEnumerators() const {
1084 return enumerators_;
1085 }
Steven Moreland0cea4aa2020-04-20 21:06:02 -07001086 std::set<AidlAnnotation::Type> GetSupportedAnnotations() const override;
Jooyung Han808a2a02020-12-28 16:46:54 +09001087 bool CheckValid(const AidlTypenames& typenames) const override;
Steven Morelandd59e3172020-05-11 16:42:09 -07001088 bool LanguageSpecificCheckValid(const AidlTypenames& /*typenames*/,
1089 Options::Language) const override {
1090 return true;
1091 }
Daniel Norman85aed542019-08-21 12:01:14 -07001092 std::string GetPreprocessDeclarationName() const override { return "enum"; }
Jeongik Cha997281d2020-01-16 15:23:59 +09001093 void Dump(CodeWriter* writer) const override;
Daniel Norman85aed542019-08-21 12:01:14 -07001094
1095 const AidlEnumDeclaration* AsEnumDeclaration() const override { return this; }
1096
Jiyong Park512ed852020-12-30 15:07:23 +09001097 void TraverseChildren(std::function<void(const AidlTraversable&)> traverse) const override {
1098 for (const auto& c : GetEnumerators()) {
1099 traverse(*c);
1100 }
1101 }
1102 bool DispatchVisit(AidlVisitor& v) const override { return v.Visit(*this); }
1103
Daniel Norman85aed542019-08-21 12:01:14 -07001104 private:
Jooyung Han29813842020-12-08 01:28:03 +09001105
Daniel Norman85aed542019-08-21 12:01:14 -07001106 const std::string name_;
1107 const std::vector<std::unique_ptr<AidlEnumerator>> enumerators_;
Jooyung Han672557b2020-12-24 05:18:00 +09001108 std::unique_ptr<AidlTypeSpecifier> backing_type_;
Daniel Norman85aed542019-08-21 12:01:14 -07001109};
1110
Jooyung Han829ec7c2020-12-02 12:07:36 +09001111class AidlUnionDecl : public AidlParcelable {
Jooyung Han2946afc2020-10-05 20:29:16 +09001112 public:
1113 AidlUnionDecl(const AidlLocation& location, const std::string& name, const std::string& package,
Jooyung Han829ec7c2020-12-02 12:07:36 +09001114 const std::string& comments, std::vector<std::string>* type_params,
1115 std::vector<std::unique_ptr<AidlMember>>* members);
Jooyung Han2946afc2020-10-05 20:29:16 +09001116 virtual ~AidlUnionDecl() = default;
1117
1118 // non-copyable, non-movable
1119 AidlUnionDecl(const AidlUnionDecl&) = delete;
1120 AidlUnionDecl(AidlUnionDecl&&) = delete;
1121 AidlUnionDecl& operator=(const AidlUnionDecl&) = delete;
1122 AidlUnionDecl& operator=(AidlUnionDecl&&) = delete;
1123
1124 std::set<AidlAnnotation::Type> GetSupportedAnnotations() const override;
1125
1126 const AidlNode& AsAidlNode() const override { return *this; }
Jooyung Han808a2a02020-12-28 16:46:54 +09001127 bool CheckValid(const AidlTypenames& typenames) const override;
Jooyung Hanfe89f122020-10-14 03:49:18 +09001128 bool LanguageSpecificCheckValid(const AidlTypenames& typenames,
1129 Options::Language lang) const override;
Jooyung Han2946afc2020-10-05 20:29:16 +09001130 std::string GetPreprocessDeclarationName() const override { return "union"; }
1131
1132 void Dump(CodeWriter* writer) const override;
1133 const AidlUnionDecl* AsUnionDeclaration() const override { return this; }
Jiyong Park512ed852020-12-30 15:07:23 +09001134
1135 void TraverseChildren(std::function<void(const AidlTraversable&)> traverse) const override {
1136 for (const auto c : GetMembers()) {
1137 traverse(*c);
1138 }
1139 }
1140 bool DispatchVisit(AidlVisitor& v) const override { return v.Visit(*this); }
Jooyung Han2946afc2020-10-05 20:29:16 +09001141};
1142
Jiyong Park1deecc32018-07-17 01:14:41 +09001143class AidlInterface final : public AidlDefinedType {
Casey Dahlin1ae2bc52015-10-07 18:49:10 -07001144 public:
Steven Moreland46e9da82018-07-27 15:45:29 -07001145 AidlInterface(const AidlLocation& location, const std::string& name, const std::string& comments,
Jooyung Han829ec7c2020-12-02 12:07:36 +09001146 bool oneway_, const std::string& package,
1147 std::vector<std::unique_ptr<AidlMember>>* members);
Casey Dahlin1ae2bc52015-10-07 18:49:10 -07001148 virtual ~AidlInterface() = default;
1149
Jiyong Parkd800fef2020-07-22 18:09:43 +09001150 // non-copyable, non-movable
1151 AidlInterface(const AidlInterface&) = delete;
1152 AidlInterface(AidlInterface&&) = delete;
1153 AidlInterface& operator=(const AidlInterface&) = delete;
1154 AidlInterface& operator=(AidlInterface&&) = delete;
1155
Steven Morelandc258abc2018-07-10 14:03:38 -07001156 const AidlInterface* AsInterface() const override { return this; }
Steven Morelanded83a282018-07-17 13:27:29 -07001157 std::string GetPreprocessDeclarationName() const override { return "interface"; }
Steven Moreland5557f1c2018-07-02 13:50:23 -07001158
Jeongik Cha997281d2020-01-16 15:23:59 +09001159 void Dump(CodeWriter* writer) const override;
Jiyong Park02da7422018-07-16 16:00:26 +09001160
Steven Moreland0cea4aa2020-04-20 21:06:02 -07001161 std::set<AidlAnnotation::Type> GetSupportedAnnotations() const override;
Jooyung Han808a2a02020-12-28 16:46:54 +09001162 bool CheckValid(const AidlTypenames& typenames) const override;
Steven Morelandd59e3172020-05-11 16:42:09 -07001163 bool LanguageSpecificCheckValid(const AidlTypenames& typenames,
1164 Options::Language lang) const override;
Jeongik Chadb0f59e2018-11-01 18:11:21 +09001165
Jiyong Park27fd7fd2020-08-27 16:25:09 +09001166 std::string GetDescriptor() const;
Jiyong Park512ed852020-12-30 15:07:23 +09001167
1168 void TraverseChildren(std::function<void(const AidlTraversable&)> traverse) const override {
1169 for (const auto c : GetMembers()) {
1170 traverse(*c);
1171 }
1172 }
1173 bool DispatchVisit(AidlVisitor& v) const override { return v.Visit(*this); }
Casey Dahlin0a2f8be2015-09-28 16:15:29 -07001174};
Adam Lesinskiffa16862014-01-23 18:17:42 -08001175
Casey Dahlin0edf3422015-10-07 12:34:59 -07001176class AidlImport : public AidlNode {
1177 public:
Steven Moreland46e9da82018-07-27 15:45:29 -07001178 AidlImport(const AidlLocation& location, const std::string& needed_class);
Casey Dahlin0edf3422015-10-07 12:34:59 -07001179 virtual ~AidlImport() = default;
1180
Jiyong Parkd800fef2020-07-22 18:09:43 +09001181 // non-copyable, non-movable
1182 AidlImport(const AidlImport&) = delete;
1183 AidlImport(AidlImport&&) = delete;
1184 AidlImport& operator=(const AidlImport&) = delete;
1185 AidlImport& operator=(AidlImport&&) = delete;
1186
Casey Dahlin0edf3422015-10-07 12:34:59 -07001187 const std::string& GetNeededClass() const { return needed_class_; }
Casey Dahlin0edf3422015-10-07 12:34:59 -07001188
1189 private:
Casey Dahlin0edf3422015-10-07 12:34:59 -07001190 std::string needed_class_;
Casey Dahline2507492015-09-14 17:11:20 -07001191};
1192
Jiyong Park62515512020-06-08 15:57:11 +09001193// AidlDocument models an AIDL file
Jiyong Park512ed852020-12-30 15:07:23 +09001194class AidlDocument : public AidlNode, public AidlTraversable {
Jiyong Park62515512020-06-08 15:57:11 +09001195 public:
1196 AidlDocument(const AidlLocation& location, std::vector<std::unique_ptr<AidlImport>>& imports,
Jiyong Park8e79b7f2020-07-20 20:52:38 +09001197 std::vector<std::unique_ptr<AidlDefinedType>>&& defined_types)
1198 : AidlNode(location),
1199 imports_(std::move(imports)),
1200 defined_types_(std::move(defined_types)) {}
Jiyong Parkd800fef2020-07-22 18:09:43 +09001201 ~AidlDocument() = default;
1202
1203 // non-copyable, non-movable
Jiyong Park8e79b7f2020-07-20 20:52:38 +09001204 AidlDocument(const AidlDocument&) = delete;
1205 AidlDocument(AidlDocument&&) = delete;
1206 AidlDocument& operator=(const AidlDocument&) = delete;
1207 AidlDocument& operator=(AidlDocument&&) = delete;
Jiyong Parkd800fef2020-07-22 18:09:43 +09001208
Jooyung Han29813842020-12-08 01:28:03 +09001209 std::optional<std::string> ResolveName(const std::string& unresolved_type) const;
Jiyong Parkd800fef2020-07-22 18:09:43 +09001210 const std::vector<std::unique_ptr<AidlImport>>& Imports() const { return imports_; }
1211 const std::vector<std::unique_ptr<AidlDefinedType>>& DefinedTypes() const {
1212 return defined_types_;
1213 }
Jiyong Park62515512020-06-08 15:57:11 +09001214
Jiyong Park512ed852020-12-30 15:07:23 +09001215 void TraverseChildren(std::function<void(const AidlTraversable&)> traverse) const override {
1216 for (const auto& t : DefinedTypes()) {
1217 traverse(*t);
1218 }
1219 }
1220 bool DispatchVisit(AidlVisitor& v) const override { return v.Visit(*this); }
1221
Jiyong Park62515512020-06-08 15:57:11 +09001222 private:
1223 const std::vector<std::unique_ptr<AidlImport>> imports_;
Jiyong Park8e79b7f2020-07-20 20:52:38 +09001224 const std::vector<std::unique_ptr<AidlDefinedType>> defined_types_;
Jiyong Park62515512020-06-08 15:57:11 +09001225};
Jooyung Hanb3c77ed2020-12-26 02:02:45 +09001226
1227template <typename T>
1228std::optional<T> AidlAnnotation::ParamValue(const std::string& param_name) const {
1229 auto it = parameters_.find(param_name);
1230 if (it == parameters_.end()) {
1231 return std::nullopt;
1232 }
Jooyung Han535c5e82020-12-29 15:16:59 +09001233 return it->second->EvaluatedValue<T>();
Jiyong Park512ed852020-12-30 15:07:23 +09001234}