blob: 9bcbaa907f1e1a9bdcd541f76ebd12defff6f050 [file] [log] [blame]
Will McVickerd7d18df2019-09-12 13:40:50 -07001/*
2 * Copyright (C) 2019, The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Steven Moreland9fccf582018-08-27 20:36:27 -070017#pragma once
Christopher Wileyec31a052016-01-25 07:28:51 -080018
Casey Dahlinbc7a50a2015-09-28 19:20:50 -070019#include <memory>
Jeongik Cha997281d2020-01-16 15:23:59 +090020#include <regex>
Casey Dahlindd691812015-09-09 17:59:06 -070021#include <string>
Jeongik Chadf76dc72019-11-28 00:08:47 +090022#include <unordered_set>
Casey Dahlinbc7a50a2015-09-28 19:20:50 -070023#include <vector>
Casey Dahlindd691812015-09-09 17:59:06 -070024
Elliott Hughes0a620672015-12-04 13:53:18 -080025#include <android-base/strings.h>
Casey Dahlin73d46b02015-09-11 02:47:54 +000026
Jooyung Han888c5bc2020-12-22 17:28:47 +090027#include "aidl_typenames.h"
28#include "code_writer.h"
29#include "diagnostics.h"
30#include "io_delegate.h"
31#include "options.h"
32
Jiyong Parkb034bf02018-07-30 17:44:33 +090033using android::aidl::AidlTypenames;
Jiyong Park02da7422018-07-16 16:00:26 +090034using android::aidl::CodeWriter;
Jooyung Han888c5bc2020-12-22 17:28:47 +090035using android::aidl::DiagnosticsContext;
Jeongik Cha047c5ee2019-08-07 23:16:49 +090036using android::aidl::Options;
Steven Moreland3f658cf2018-08-20 13:40:54 -070037using std::shared_ptr;
Jiyong Park1deecc32018-07-17 01:14:41 +090038using std::string;
39using std::unique_ptr;
40using std::vector;
Andrei Onea8714b022019-02-01 18:55:54 +000041class AidlNode;
42
43namespace android {
44namespace aidl {
45namespace mappings {
46std::string dump_location(const AidlNode& method);
47} // namespace mappings
Mathew Inwoodadb74672019-11-29 14:01:53 +000048namespace java {
49std::string dump_location(const AidlNode& method);
50} // namespace java
Andrei Onea8714b022019-02-01 18:55:54 +000051} // namespace aidl
52} // namespace android
53
Steven Moreland46e9da82018-07-27 15:45:29 -070054class AidlLocation {
Casey Dahlinbc7a50a2015-09-28 19:20:50 -070055 public:
Steven Moreland46e9da82018-07-27 15:45:29 -070056 struct Point {
Dan Willemsen609ba6d2019-12-30 10:44:00 -080057 int line;
58 int column;
Steven Moreland46e9da82018-07-27 15:45:29 -070059 };
60
Devin Mooredf93ebb2020-03-25 14:03:35 -070061 enum class Source {
62 // From internal aidl source code
63 INTERNAL = 0,
64 // From a parsed file
65 EXTERNAL = 1
66 };
67
68 AidlLocation(const std::string& file, Point begin, Point end, Source source);
Devin Moore5de18ed2020-04-02 13:52:29 -070069 AidlLocation(const std::string& file, Source source)
70 : AidlLocation(file, {0, 0}, {0, 0}, source) {}
Devin Mooredf93ebb2020-03-25 14:03:35 -070071
72 bool IsInternal() const { return source_ == Source::INTERNAL; }
Steven Moreland46e9da82018-07-27 15:45:29 -070073
Devin Moore5de18ed2020-04-02 13:52:29 -070074 // The first line of a file is line 1.
75 bool LocationKnown() const { return begin_.line != 0; }
76
Steven Moreland46e9da82018-07-27 15:45:29 -070077 friend std::ostream& operator<<(std::ostream& os, const AidlLocation& l);
Andrei Onea8714b022019-02-01 18:55:54 +000078 friend class AidlNode;
Casey Dahlinbc7a50a2015-09-28 19:20:50 -070079
80 private:
Steven Moreland541788d2020-05-21 22:05:52 +000081 // INTENTIONALLY HIDDEN: only operator<< should access details here.
82 // Otherwise, locations should only ever be copied around to construct new
83 // objects.
Steven Moreland46e9da82018-07-27 15:45:29 -070084 const std::string file_;
85 Point begin_;
86 Point end_;
Devin Mooredf93ebb2020-03-25 14:03:35 -070087 Source source_;
Steven Moreland46e9da82018-07-27 15:45:29 -070088};
89
Devin Mooredf93ebb2020-03-25 14:03:35 -070090#define AIDL_LOCATION_HERE \
Steven Moreland21780812020-09-11 01:29:45 +000091 (AidlLocation{__FILE__, {__LINE__, 0}, {__LINE__, 0}, AidlLocation::Source::INTERNAL})
Steven Moreland02e012e2018-08-02 14:58:10 -070092
Steven Moreland46e9da82018-07-27 15:45:29 -070093std::ostream& operator<<(std::ostream& os, const AidlLocation& l);
94
95// Anything that is locatable in a .aidl file.
96class AidlNode {
97 public:
98 AidlNode(const AidlLocation& location);
Steven Moreland3f658cf2018-08-20 13:40:54 -070099
100 AidlNode(const AidlNode&) = default;
Steven Moreland46e9da82018-07-27 15:45:29 -0700101 virtual ~AidlNode() = default;
102
Jiyong Parkd800fef2020-07-22 18:09:43 +0900103 AidlNode(AidlNode&&) = delete;
104 AidlNode& operator=(AidlNode&&) = delete;
105
Devin Mooredf93ebb2020-03-25 14:03:35 -0700106 // To be able to print AidlLocation
Steven Morelandb0d15a52020-03-31 14:03:47 -0700107 friend class AidlErrorLog;
Andrei Onea8714b022019-02-01 18:55:54 +0000108 friend std::string android::aidl::mappings::dump_location(const AidlNode&);
Mathew Inwoodadb74672019-11-29 14:01:53 +0000109 friend std::string android::aidl::java::dump_location(const AidlNode&);
Steven Moreland46e9da82018-07-27 15:45:29 -0700110
Devin Mooredf93ebb2020-03-25 14:03:35 -0700111 const AidlLocation& GetLocation() const { return location_; }
112
Steven Moreland46e9da82018-07-27 15:45:29 -0700113 private:
Mathew Inwoodadb74672019-11-29 14:01:53 +0000114 std::string PrintLine() const;
Andrei Onea8714b022019-02-01 18:55:54 +0000115 std::string PrintLocation() const;
Steven Moreland46e9da82018-07-27 15:45:29 -0700116 const AidlLocation location_;
Casey Dahlinbc7a50a2015-09-28 19:20:50 -0700117};
118
Jeongik Chadf76dc72019-11-28 00:08:47 +0900119// unique_ptr<AidlTypeSpecifier> for type arugment,
120// std::string for type parameter(T, U, and so on).
121template <typename T>
122class AidlParameterizable {
123 public:
124 AidlParameterizable(std::vector<T>* type_params) : type_params_(type_params) {}
125 virtual ~AidlParameterizable() = default;
126 bool IsGeneric() const { return type_params_ != nullptr; }
127 const std::vector<T>& GetTypeParameters() const { return *type_params_; }
128 bool CheckValid() const;
129
Steven Moreland6c07b832020-10-29 23:39:53 +0000130 __attribute__((warn_unused_result)) bool SetTypeParameters(std::vector<T>* type_params) {
131 if (type_params_) return false;
132 type_params_.reset(type_params);
133 return true;
134 }
135
Jeongik Chadf76dc72019-11-28 00:08:47 +0900136 virtual const AidlNode& AsAidlNode() const = 0;
137
138 protected:
139 AidlParameterizable(const AidlParameterizable&);
140
141 private:
Steven Moreland6c07b832020-10-29 23:39:53 +0000142 unique_ptr<std::vector<T>> type_params_;
Jeongik Chadf76dc72019-11-28 00:08:47 +0900143 static_assert(std::is_same<T, unique_ptr<AidlTypeSpecifier>>::value ||
144 std::is_same<T, std::string>::value);
145};
146template <>
147bool AidlParameterizable<std::string>::CheckValid() const;
148
Andrei Onea9445fc62019-06-27 18:11:59 +0100149class AidlConstantValue;
150class AidlConstantDeclaration;
151
152// Transforms a value string into a language specific form. Raw value as produced by
153// AidlConstantValue.
154using ConstantValueDecorator =
155 std::function<std::string(const AidlTypeSpecifier& type, const std::string& raw_value)>;
156
Jiyong Park68bc77a2018-07-19 19:00:45 +0900157class AidlAnnotation : public AidlNode {
158 public:
Steven Moreland0cea4aa2020-04-20 21:06:02 -0700159 enum class Type {
160 BACKING = 1,
161 HIDE,
162 JAVA_STABLE_PARCELABLE,
163 UNSUPPORTED_APP_USAGE,
164 VINTF_STABILITY,
165 NULLABLE,
166 UTF8_IN_CPP,
Steven Morelanda7764e52020-10-27 17:29:29 +0000167 SENSITIVE_DATA,
Jiyong Parkbf5fd5c2020-06-05 19:48:05 +0900168 JAVA_PASSTHROUGH,
Jooyung Han90345002020-10-23 15:28:53 +0900169 JAVA_DERIVE,
Jeongik Chad0a10272020-08-06 16:33:36 +0900170 JAVA_ONLY_IMMUTABLE,
Devin Moorec7e47a32020-08-07 10:55:25 -0700171 FIXED_SIZE,
Jiyong Park27fd7fd2020-08-27 16:25:09 +0900172 DESCRIPTOR,
Andrei Homescue61feb52020-08-18 15:44:24 -0700173 RUST_DERIVE,
Steven Moreland0cea4aa2020-04-20 21:06:02 -0700174 };
175 static std::string TypeToString(Type type);
176
Andrei Onea9445fc62019-06-27 18:11:59 +0100177 static AidlAnnotation* Parse(
178 const AidlLocation& location, const string& name,
179 std::map<std::string, std::shared_ptr<AidlConstantValue>>* parameter_list);
Steven Moreland46e9da82018-07-27 15:45:29 -0700180
Steven Moreland3f658cf2018-08-20 13:40:54 -0700181 AidlAnnotation(const AidlAnnotation&) = default;
Steven Moreland3be75772018-08-20 13:27:43 -0700182 AidlAnnotation(AidlAnnotation&&) = default;
Jiyong Park68bc77a2018-07-19 19:00:45 +0900183 virtual ~AidlAnnotation() = default;
Andrei Onea9445fc62019-06-27 18:11:59 +0100184 bool CheckValid() const;
Steven Moreland3be75772018-08-20 13:27:43 -0700185
Jooyung Hand902a972020-10-23 17:32:44 +0900186 const string& GetName() const { return schema_.name; }
Steven Moreland0cea4aa2020-04-20 21:06:02 -0700187 const Type& GetType() const { return schema_.type; }
Jooyung Hand902a972020-10-23 17:32:44 +0900188 bool Repeatable() const { return schema_.repeatable; }
Jooyung Han965e31d2020-11-27 12:30:16 +0900189
190 // ToString is for dumping AIDL.
191 // Returns string representation of this annotation.
192 // e.g) "@RustDerive(Clone=true, Copy=true)"
193 string ToString() const;
194
Jooyung Hanb3c77ed2020-12-26 02:02:45 +0900195 template <typename T>
196 std::optional<T> ParamValue(const std::string& param_name) const;
197
Andrei Onea9445fc62019-06-27 18:11:59 +0100198 std::map<std::string, std::string> AnnotationParams(
199 const ConstantValueDecorator& decorator) const;
Jiyong Parka6605ab2018-11-11 14:30:21 +0900200 const string& GetComments() const { return comments_; }
201 void SetComments(const string& comments) { comments_ = comments; }
Jiyong Park68bc77a2018-07-19 19:00:45 +0900202
203 private:
Steven Moreland0cea4aa2020-04-20 21:06:02 -0700204 struct Schema {
205 AidlAnnotation::Type type;
206
207 // text name in .aidl file, e.g. "nullable"
208 std::string name;
209
210 // map from param name -> value type
Jooyung Han5c2fcae2020-12-26 00:04:39 +0900211 std::map<std::string, const AidlTypeSpecifier&> supported_parameters;
Jooyung Hand902a972020-10-23 17:32:44 +0900212
213 bool repeatable;
Jooyung Han5721a232020-12-24 04:34:55 +0900214
215 std::vector<std::string> required_parameters = {};
Steven Moreland0cea4aa2020-04-20 21:06:02 -0700216 };
217 static const std::vector<Schema>& AllSchemas();
218
219 AidlAnnotation(const AidlLocation& location, const Schema& schema,
Andrei Onea9445fc62019-06-27 18:11:59 +0100220 std::map<std::string, std::shared_ptr<AidlConstantValue>>&& parameters);
Steven Moreland0cea4aa2020-04-20 21:06:02 -0700221
222 const Schema& schema_;
Jiyong Parka6605ab2018-11-11 14:30:21 +0900223 string comments_;
Andrei Onea9445fc62019-06-27 18:11:59 +0100224 std::map<std::string, std::shared_ptr<AidlConstantValue>> parameters_;
Jiyong Park68bc77a2018-07-19 19:00:45 +0900225};
226
Steven Moreland3be75772018-08-20 13:27:43 -0700227static inline bool operator<(const AidlAnnotation& lhs, const AidlAnnotation& rhs) {
228 return lhs.GetName() < rhs.GetName();
229}
230static inline bool operator==(const AidlAnnotation& lhs, const AidlAnnotation& rhs) {
231 return lhs.GetName() == rhs.GetName();
232}
Jiyong Park3656c3c2018-08-01 20:02:01 +0900233
Casey Dahline7922932016-02-29 17:23:01 -0800234class AidlAnnotatable : public AidlNode {
Casey Dahlin0ee37582015-09-30 16:31:55 -0700235 public:
Steven Moreland46e9da82018-07-27 15:45:29 -0700236 AidlAnnotatable(const AidlLocation& location);
Steven Moreland3f658cf2018-08-20 13:40:54 -0700237
238 AidlAnnotatable(const AidlAnnotatable&) = default;
239 AidlAnnotatable(AidlAnnotatable&&) = default;
Casey Dahline7922932016-02-29 17:23:01 -0800240 virtual ~AidlAnnotatable() = default;
241
Artur Satayev91fe8712019-07-29 13:06:01 +0100242 void Annotate(vector<AidlAnnotation>&& annotations) {
243 for (auto& annotation : annotations) {
244 annotations_.emplace_back(std::move(annotation));
245 }
246 }
Jiyong Park68bc77a2018-07-19 19:00:45 +0900247 bool IsNullable() const;
Jiyong Park68bc77a2018-07-19 19:00:45 +0900248 bool IsUtf8InCpp() const;
Steven Morelanda7764e52020-10-27 17:29:29 +0000249 bool IsSensitiveData() const;
Steven Morelanda57d0a62019-07-30 09:41:14 -0700250 bool IsVintfStability() const;
Jeongik Chad0a10272020-08-06 16:33:36 +0900251 bool IsJavaOnlyImmutable() const;
Devin Moorec7e47a32020-08-07 10:55:25 -0700252 bool IsFixedSize() const;
Jeongik Cha88f95a82020-01-15 13:02:16 +0900253 bool IsStableApiParcelable(Options::Language lang) const;
Makoto Onuki78a1c1c2020-03-04 16:57:23 -0800254 bool IsHide() const;
Jooyung Han829ec7c2020-12-02 12:07:36 +0900255 bool JavaDerive(const std::string& method) const;
Jiyong Park27fd7fd2020-08-27 16:25:09 +0900256 std::string GetDescriptor() const;
Andrei Onea9445fc62019-06-27 18:11:59 +0100257
Steven Moreland7e4b9502020-02-20 18:10:42 -0800258 void DumpAnnotations(CodeWriter* writer) const;
259
Andrei Onea9445fc62019-06-27 18:11:59 +0100260 const AidlAnnotation* UnsupportedAppUsage() const;
Andrei Homescue61feb52020-08-18 15:44:24 -0700261 const AidlAnnotation* RustDerive() const;
Jooyung Han672557b2020-12-24 05:18:00 +0900262 const AidlAnnotation* BackingType() const;
Jooyung Han965e31d2020-11-27 12:30:16 +0900263
264 // ToString is for dumping AIDL.
265 // Returns string representation of annotations.
266 // e.g) "@JavaDerive(toString=true) @RustDerive(Clone=true, Copy=true)"
Jiyong Park68bc77a2018-07-19 19:00:45 +0900267 std::string ToString() const;
Casey Dahline7922932016-02-29 17:23:01 -0800268
Jiyong Parka6605ab2018-11-11 14:30:21 +0900269 const vector<AidlAnnotation>& GetAnnotations() const { return annotations_; }
Jooyung Han888c5bc2020-12-22 17:28:47 +0900270 bool CheckValid(const AidlTypenames&) const;
Jiyong Park3656c3c2018-08-01 20:02:01 +0900271
Steven Moreland181144c2020-04-20 19:57:56 -0700272 protected:
Steven Moreland0cea4aa2020-04-20 21:06:02 -0700273 virtual std::set<AidlAnnotation::Type> GetSupportedAnnotations() const = 0;
Steven Moreland181144c2020-04-20 19:57:56 -0700274
Casey Dahline7922932016-02-29 17:23:01 -0800275 private:
Jiyong Parka6605ab2018-11-11 14:30:21 +0900276 vector<AidlAnnotation> annotations_;
Casey Dahline7922932016-02-29 17:23:01 -0800277};
278
Jiyong Park1deecc32018-07-17 01:14:41 +0900279// AidlTypeSpecifier represents a reference to either a built-in type,
280// a defined type, or a variant (e.g., array of generic) of a type.
Jeongik Chadf76dc72019-11-28 00:08:47 +0900281class AidlTypeSpecifier final : public AidlAnnotatable,
282 public AidlParameterizable<unique_ptr<AidlTypeSpecifier>> {
Casey Dahline7922932016-02-29 17:23:01 -0800283 public:
Steven Moreland46e9da82018-07-27 15:45:29 -0700284 AidlTypeSpecifier(const AidlLocation& location, const string& unresolved_name, bool is_array,
285 vector<unique_ptr<AidlTypeSpecifier>>* type_params, const string& comments);
Jiyong Parkd59a10d2018-07-18 11:12:55 +0900286 virtual ~AidlTypeSpecifier() = default;
Casey Dahlin0ee37582015-09-30 16:31:55 -0700287
Steven Moreland3f658cf2018-08-20 13:40:54 -0700288 // Copy of this type which is not an array.
Jooyung Hand2fa0232020-10-19 02:51:41 +0900289 const AidlTypeSpecifier& ArrayBase() const;
Steven Moreland3f658cf2018-08-20 13:40:54 -0700290
Jiyong Park1deecc32018-07-17 01:14:41 +0900291 // Returns the full-qualified name of the base type.
292 // int -> int
293 // int[] -> int
294 // List<String> -> List
295 // IFoo -> foo.bar.IFoo (if IFoo is in package foo.bar)
296 const string& GetName() const {
297 if (IsResolved()) {
298 return fully_qualified_name_;
299 } else {
300 return GetUnresolvedName();
301 }
302 }
Casey Dahlin0ee37582015-09-30 16:31:55 -0700303
Jooyung Han965e31d2020-11-27 12:30:16 +0900304 // ToString is for dumping AIDL.
305 // Returns string representation of this type specifier including annotations.
306 // This is "annotations type_name type_params? array_marker?".
307 // e.g) "@utf8InCpp String[]";
308 std::string ToString() const;
Jiyong Park1deecc32018-07-17 01:14:41 +0900309
Jooyung Han965e31d2020-11-27 12:30:16 +0900310 // Signature is for comparing AIDL types.
311 // Returns string representation of this type specifier.
312 // This is "type_name type_params? array_marker?".
313 // e.g.) "String[]" (even if it is annotated with @utf8InCpp)
Jiyong Park02da7422018-07-16 16:00:26 +0900314 std::string Signature() const;
315
Jiyong Park1deecc32018-07-17 01:14:41 +0900316 const string& GetUnresolvedName() const { return unresolved_name_; }
317
Jeongik Cha997281d2020-01-16 15:23:59 +0900318 bool IsHidden() const;
319
Jiyong Park1deecc32018-07-17 01:14:41 +0900320 const string& GetComments() const { return comments_; }
321
Jeongik Cha1a7ab642019-07-29 17:31:02 +0900322 const std::vector<std::string> GetSplitName() const { return split_name_; }
323
Jiyong Parka6605ab2018-11-11 14:30:21 +0900324 void SetComments(const string& comment) { comments_ = comment; }
325
Jiyong Park1deecc32018-07-17 01:14:41 +0900326 bool IsResolved() const { return fully_qualified_name_ != ""; }
327
328 bool IsArray() const { return is_array_; }
329
Steven Moreland6c07b832020-10-29 23:39:53 +0000330 __attribute__((warn_unused_result)) bool SetArray() {
331 if (is_array_) return false;
332 is_array_ = true;
333 return true;
334 }
335
Jiyong Park1deecc32018-07-17 01:14:41 +0900336 // Resolve the base type name to a fully-qualified name. Return false if the
337 // resolution fails.
Daniel Norman716d3112019-09-10 13:11:56 -0700338 bool Resolve(const AidlTypenames& typenames);
Casey Dahlin0ee37582015-09-30 16:31:55 -0700339
Steven Moreland0cea4aa2020-04-20 21:06:02 -0700340 std::set<AidlAnnotation::Type> GetSupportedAnnotations() const override;
Jooyung Han888c5bc2020-12-22 17:28:47 +0900341 bool CheckValid(const AidlTypenames& typenames) const;
Steven Morelandd59e3172020-05-11 16:42:09 -0700342 bool LanguageSpecificCheckValid(const AidlTypenames& typenames, Options::Language lang) const;
Jeongik Chadf76dc72019-11-28 00:08:47 +0900343 const AidlNode& AsAidlNode() const override { return *this; }
Jiyong Park1d2df7d2018-07-23 15:22:50 +0900344
Jooyung Hane9bb9de2020-11-01 22:16:57 +0900345 const AidlDefinedType* GetDefinedType() const;
346
Casey Dahlin0ee37582015-09-30 16:31:55 -0700347 private:
Steven Moreland3f658cf2018-08-20 13:40:54 -0700348 AidlTypeSpecifier(const AidlTypeSpecifier&) = default;
349
Jiyong Park1deecc32018-07-17 01:14:41 +0900350 const string unresolved_name_;
351 string fully_qualified_name_;
Steven Moreland3f658cf2018-08-20 13:40:54 -0700352 bool is_array_;
Jiyong Parka6605ab2018-11-11 14:30:21 +0900353 string comments_;
Jeongik Cha1a7ab642019-07-29 17:31:02 +0900354 vector<string> split_name_;
Jooyung Han690f5842020-12-04 13:02:04 +0900355 const AidlDefinedType* defined_type_ = nullptr; // set when Resolve() for defined types
Jooyung Hand2fa0232020-10-19 02:51:41 +0900356 mutable shared_ptr<AidlTypeSpecifier> array_base_;
Casey Dahlin0ee37582015-09-30 16:31:55 -0700357};
358
Steven Moreland860b1942018-08-16 14:59:28 -0700359// Returns the universal value unaltered.
360std::string AidlConstantValueDecorator(const AidlTypeSpecifier& type, const std::string& raw_value);
361
Steven Moreland9ea10e32018-07-19 15:26:09 -0700362class AidlConstantValue;
Jooyung Han3f347ca2020-12-01 12:41:50 +0900363class AidlMethod;
364class AidlConstantDeclaration;
365class AidlVariableDeclaration;
366
367class AidlMember : public AidlNode {
368 public:
369 AidlMember(const AidlLocation& location);
370 virtual ~AidlMember() = default;
371
372 // non-copyable, non-movable
373 AidlMember(const AidlMember&) = delete;
374 AidlMember(AidlMember&&) = delete;
375 AidlMember& operator=(const AidlMember&) = delete;
376 AidlMember& operator=(AidlMember&&) = delete;
377
Jooyung Han829ec7c2020-12-02 12:07:36 +0900378 virtual const AidlMethod* AsMethod() const { return nullptr; }
379 virtual const AidlConstantDeclaration* AsConstantDeclaration() const { return nullptr; }
380 virtual const AidlVariableDeclaration* AsVariableDeclaration() const { return nullptr; }
381
382 AidlMethod* AsMethod() {
383 return const_cast<AidlMethod*>(const_cast<const AidlMember*>(this)->AsMethod());
384 }
385 AidlConstantDeclaration* AsConstantDeclaration() {
386 return const_cast<AidlConstantDeclaration*>(
387 const_cast<const AidlMember*>(this)->AsConstantDeclaration());
388 }
389 AidlVariableDeclaration* AsVariableDeclaration() {
390 return const_cast<AidlVariableDeclaration*>(
391 const_cast<const AidlMember*>(this)->AsVariableDeclaration());
392 }
Jooyung Han3f347ca2020-12-01 12:41:50 +0900393};
394
Steven Moreland541788d2020-05-21 22:05:52 +0000395// TODO: This class is used for method arguments and also parcelable fields,
396// and it should be split up since default values don't apply to method
397// arguments
Jooyung Han3f347ca2020-12-01 12:41:50 +0900398class AidlVariableDeclaration : public AidlMember {
Steven Moreland5557f1c2018-07-02 13:50:23 -0700399 public:
Steven Moreland46e9da82018-07-27 15:45:29 -0700400 AidlVariableDeclaration(const AidlLocation& location, AidlTypeSpecifier* type,
401 const std::string& name);
402 AidlVariableDeclaration(const AidlLocation& location, AidlTypeSpecifier* type,
403 const std::string& name, AidlConstantValue* default_value);
Steven Moreland5557f1c2018-07-02 13:50:23 -0700404 virtual ~AidlVariableDeclaration() = default;
405
Jiyong Parkd800fef2020-07-22 18:09:43 +0900406 // non-copyable, non-movable
407 AidlVariableDeclaration(const AidlVariableDeclaration&) = delete;
408 AidlVariableDeclaration(AidlVariableDeclaration&&) = delete;
409 AidlVariableDeclaration& operator=(const AidlVariableDeclaration&) = delete;
410 AidlVariableDeclaration& operator=(AidlVariableDeclaration&&) = delete;
411
Jooyung Han829ec7c2020-12-02 12:07:36 +0900412 const AidlVariableDeclaration* AsVariableDeclaration() const override { return this; }
Jooyung Han3f347ca2020-12-01 12:41:50 +0900413
Steven Moreland5557f1c2018-07-02 13:50:23 -0700414 std::string GetName() const { return name_; }
Jooyung Hanacae85d2020-10-28 16:39:09 +0900415 std::string GetCapitalizedName() const;
Jiyong Parkd59a10d2018-07-18 11:12:55 +0900416 const AidlTypeSpecifier& GetType() const { return *type_; }
Steven Moreland541788d2020-05-21 22:05:52 +0000417 // if this was constructed explicitly with a default value
418 bool IsDefaultUserSpecified() const { return default_user_specified_; }
419 // will return the default value this is constructed with or a default value
420 // if one is available
Steven Moreland9ea10e32018-07-19 15:26:09 -0700421 const AidlConstantValue* GetDefaultValue() const { return default_value_.get(); }
Jooyung Han53fb4242020-12-17 16:03:49 +0900422 bool HasUsefulDefaultValue() const;
Steven Moreland9ea10e32018-07-19 15:26:09 -0700423
Jiyong Parkd59a10d2018-07-18 11:12:55 +0900424 AidlTypeSpecifier* GetMutableType() { return type_.get(); }
Steven Moreland5557f1c2018-07-02 13:50:23 -0700425
Jeongik Chadb0f59e2018-11-01 18:11:21 +0900426 bool CheckValid(const AidlTypenames& typenames) const;
Jooyung Han965e31d2020-11-27 12:30:16 +0900427
428 // ToString is for dumping AIDL.
429 // Returns string representation of this variable decl including default value.
430 // This is "annotations type name default_value?".
431 // e.g) "@utf8InCpp String[] names = {"hello"}"
Steven Moreland5557f1c2018-07-02 13:50:23 -0700432 std::string ToString() const;
Jooyung Han965e31d2020-11-27 12:30:16 +0900433
434 // Signature is for comparing AIDL types.
435 // Returns string representation of this variable decl.
436 // This is "type name".
437 // e.g) "String[] name" (even if it is annotated with @utf8InCpp and has a default value.)
Jiyong Park02da7422018-07-16 16:00:26 +0900438 std::string Signature() const;
Steven Moreland5557f1c2018-07-02 13:50:23 -0700439
Steven Moreland860b1942018-08-16 14:59:28 -0700440 std::string ValueString(const ConstantValueDecorator& decorator) const;
Steven Moreland25294322018-08-07 18:13:55 -0700441
Steven Moreland5557f1c2018-07-02 13:50:23 -0700442 private:
Jiyong Parkd59a10d2018-07-18 11:12:55 +0900443 std::unique_ptr<AidlTypeSpecifier> type_;
Steven Moreland5557f1c2018-07-02 13:50:23 -0700444 std::string name_;
Steven Moreland541788d2020-05-21 22:05:52 +0000445 bool default_user_specified_;
Steven Moreland9ea10e32018-07-19 15:26:09 -0700446 std::unique_ptr<AidlConstantValue> default_value_;
Steven Moreland5557f1c2018-07-02 13:50:23 -0700447};
448
449class AidlArgument : public AidlVariableDeclaration {
Casey Dahlinbc7a50a2015-09-28 19:20:50 -0700450 public:
Casey Dahlinc378c992015-09-29 16:50:40 -0700451 enum Direction { IN_DIR = 1, OUT_DIR = 2, INOUT_DIR = 3 };
452
Steven Moreland46e9da82018-07-27 15:45:29 -0700453 AidlArgument(const AidlLocation& location, AidlArgument::Direction direction,
454 AidlTypeSpecifier* type, const std::string& name);
455 AidlArgument(const AidlLocation& location, AidlTypeSpecifier* type, const std::string& name);
Casey Dahlinbc7a50a2015-09-28 19:20:50 -0700456 virtual ~AidlArgument() = default;
457
Jiyong Parkd800fef2020-07-22 18:09:43 +0900458 // non-copyable, non-movable
459 AidlArgument(const AidlArgument&) = delete;
460 AidlArgument(AidlArgument&&) = delete;
461 AidlArgument& operator=(const AidlArgument&) = delete;
462 AidlArgument& operator=(AidlArgument&&) = delete;
463
Casey Dahlinc378c992015-09-29 16:50:40 -0700464 Direction GetDirection() const { return direction_; }
Christopher Wileyad339272015-10-05 19:11:58 -0700465 bool IsOut() const { return direction_ & OUT_DIR; }
466 bool IsIn() const { return direction_ & IN_DIR; }
Casey Dahlinc378c992015-09-29 16:50:40 -0700467 bool DirectionWasSpecified() const { return direction_specified_; }
Jiyong Park3656c3c2018-08-01 20:02:01 +0900468 string GetDirectionSpecifier() const;
Christopher Wileyad339272015-10-05 19:11:58 -0700469
Jooyung Han965e31d2020-11-27 12:30:16 +0900470 // ToString is for dumping AIDL.
471 // Returns string representation of this argument including direction
472 // This is "direction annotations type name".
473 // e.g) "in @utf8InCpp String[] names"
Casey Dahlinc378c992015-09-29 16:50:40 -0700474 std::string ToString() const;
475
Casey Dahlinbc7a50a2015-09-28 19:20:50 -0700476 private:
Casey Dahlinc378c992015-09-29 16:50:40 -0700477 Direction direction_;
478 bool direction_specified_;
Casey Dahlina834dd42015-09-23 11:52:15 -0700479};
Adam Lesinskiffa16862014-01-23 18:17:42 -0800480
Will McVickerd7d18df2019-09-12 13:40:50 -0700481class AidlUnaryConstExpression;
482class AidlBinaryConstExpression;
Jooyung Han690f5842020-12-04 13:02:04 +0900483class AidlConstantReference;
Will McVickerd7d18df2019-09-12 13:40:50 -0700484
Steven Moreland693640b2018-07-19 13:46:27 -0700485class AidlConstantValue : public AidlNode {
Casey Dahlind40e2fe2015-11-24 14:06:52 -0800486 public:
Will McVickerd7d18df2019-09-12 13:40:50 -0700487 enum class Type {
488 // WARNING: Don't change this order! The order is used to determine type
489 // promotion during a binary expression.
490 BOOLEAN,
491 INT8,
492 INT32,
493 INT64,
494 ARRAY,
495 CHARACTER,
496 STRING,
Jooyung Han690f5842020-12-04 13:02:04 +0900497 REF,
Will McVickerd7d18df2019-09-12 13:40:50 -0700498 FLOATING,
499 UNARY,
500 BINARY,
501 ERROR,
502 };
503
Jooyung Han690f5842020-12-04 13:02:04 +0900504 struct Visitor {
505 virtual ~Visitor() {}
506 virtual void Visit(AidlConstantValue&) = 0;
507 virtual void Visit(AidlConstantReference&) = 0;
508 virtual void Visit(AidlUnaryConstExpression&) = 0;
509 virtual void Visit(AidlBinaryConstExpression&) = 0;
510 };
511
Will McVickerd7d18df2019-09-12 13:40:50 -0700512 /*
513 * Return the value casted to the given type.
514 */
515 template <typename T>
Jooyung Han71a1b582020-12-25 23:58:41 +0900516 T Cast() const;
Casey Dahlind40e2fe2015-11-24 14:06:52 -0800517
Steven Moreland693640b2018-07-19 13:46:27 -0700518 virtual ~AidlConstantValue() = default;
Casey Dahlind40e2fe2015-11-24 14:06:52 -0800519
Jiyong Parkd800fef2020-07-22 18:09:43 +0900520 // non-copyable, non-movable
521 AidlConstantValue(const AidlConstantValue&) = delete;
522 AidlConstantValue(AidlConstantValue&&) = delete;
523 AidlConstantValue& operator=(const AidlConstantValue&) = delete;
524 AidlConstantValue& operator=(AidlConstantValue&&) = delete;
525
Steven Moreland541788d2020-05-21 22:05:52 +0000526 // creates default value, when one isn't specified
527 // nullptr if no default available
528 static AidlConstantValue* Default(const AidlTypeSpecifier& specifier);
529
Steven Moreland25294322018-08-07 18:13:55 -0700530 static AidlConstantValue* Boolean(const AidlLocation& location, bool value);
531 static AidlConstantValue* Character(const AidlLocation& location, char value);
Steven Moreland25294322018-08-07 18:13:55 -0700532 // example: 123, -5498, maybe any size
Will McVickerd7d18df2019-09-12 13:40:50 -0700533 static AidlConstantValue* Integral(const AidlLocation& location, const string& value);
534 static AidlConstantValue* Floating(const AidlLocation& location, const std::string& value);
Steven Moreland860b1942018-08-16 14:59:28 -0700535 static AidlConstantValue* Array(const AidlLocation& location,
Will McVickerd7d18df2019-09-12 13:40:50 -0700536 std::unique_ptr<vector<unique_ptr<AidlConstantValue>>> values);
Steven Moreland693640b2018-07-19 13:46:27 -0700537 // example: "\"asdf\""
Will McVickerd7d18df2019-09-12 13:40:50 -0700538 static AidlConstantValue* String(const AidlLocation& location, const string& value);
Steven Moreland693640b2018-07-19 13:46:27 -0700539
Will McVickerd7d18df2019-09-12 13:40:50 -0700540 Type GetType() const { return final_type_; }
Jooyung Han29813842020-12-08 01:28:03 +0900541 const std::string& Literal() const { return value_; }
Steven Moreland25294322018-08-07 18:13:55 -0700542
Will McVickerd7d18df2019-09-12 13:40:50 -0700543 virtual bool CheckValid() const;
Steven Moreland860b1942018-08-16 14:59:28 -0700544
545 // Raw value of type (currently valid in C++ and Java). Empty string on error.
Steven Moreland4bcb05c2019-11-27 18:57:47 -0800546 string ValueString(const AidlTypeSpecifier& type, const ConstantValueDecorator& decorator) const;
Jooyung Han29813842020-12-08 01:28:03 +0900547 virtual void Accept(Visitor& visitor) {
548 visitor.Visit(*this);
549 if (type_ == Type::ARRAY) {
550 for (const auto& v : values_) {
551 v.get()->Accept(visitor);
552 }
553 }
554 }
Casey Dahlind40e2fe2015-11-24 14:06:52 -0800555
556 private:
Will McVickerd7d18df2019-09-12 13:40:50 -0700557 AidlConstantValue(const AidlLocation& location, Type parsed_type, int64_t parsed_value,
558 const string& checked_value);
559 AidlConstantValue(const AidlLocation& location, Type type, const string& checked_value);
Steven Moreland860b1942018-08-16 14:59:28 -0700560 AidlConstantValue(const AidlLocation& location, Type type,
Jooyung Han29813842020-12-08 01:28:03 +0900561 std::unique_ptr<vector<unique_ptr<AidlConstantValue>>> values,
562 const std::string& value);
Steven Moreland25294322018-08-07 18:13:55 -0700563 static string ToString(Type type);
Will McVickerd7d18df2019-09-12 13:40:50 -0700564 static bool ParseIntegral(const string& value, int64_t* parsed_value, Type* parsed_type);
565 static bool IsHex(const string& value);
Steven Moreland4bcb05c2019-11-27 18:57:47 -0800566
Jooyung Han74675c22020-12-15 08:39:57 +0900567 virtual bool evaluate() const;
Casey Dahlind40e2fe2015-11-24 14:06:52 -0800568
Steven Moreland693640b2018-07-19 13:46:27 -0700569 const Type type_ = Type::ERROR;
Will McVickerd7d18df2019-09-12 13:40:50 -0700570 const vector<unique_ptr<AidlConstantValue>> values_; // if type_ == ARRAY
571 const string value_; // otherwise
572
573 // State for tracking evaluation of expressions
Steven Moreland4bcb05c2019-11-27 18:57:47 -0800574 mutable bool is_valid_ = false; // cache of CheckValid, but may be marked false in evaluate
575 mutable bool is_evaluated_ = false; // whether evaluate has been called
Will McVickerd7d18df2019-09-12 13:40:50 -0700576 mutable Type final_type_;
577 mutable int64_t final_value_;
578 mutable string final_string_value_ = "";
Steven Moreland693640b2018-07-19 13:46:27 -0700579
Will McVickerd7d18df2019-09-12 13:40:50 -0700580 friend AidlUnaryConstExpression;
581 friend AidlBinaryConstExpression;
Jooyung Han690f5842020-12-04 13:02:04 +0900582 friend AidlConstantReference;
583};
584
585// Represents "<type>.<field>" which resolves to a constant which is one of
586// - constant declartion
587// - enumerator
588// When a <type> is missing, <field> is of the enclosing type.
589class AidlConstantReference : public AidlConstantValue {
590 public:
591 AidlConstantReference(const AidlLocation& location, const std::string& value,
592 const std::string& comments);
593
594 const std::unique_ptr<AidlTypeSpecifier>& GetRefType() const { return ref_type_; }
595 void SetRefType(std::unique_ptr<AidlTypeSpecifier> type) { ref_type_ = std::move(type); }
596 const std::string& GetFieldName() const { return field_name_; }
597 const std::string& GetComments() const { return comments_; }
598
599 bool CheckValid() const override;
600 void Accept(Visitor& visitor) override { visitor.Visit(*this); }
Jooyung Han29813842020-12-08 01:28:03 +0900601 const AidlConstantValue* Resolve();
Jooyung Han690f5842020-12-04 13:02:04 +0900602
603 private:
Jooyung Han74675c22020-12-15 08:39:57 +0900604 bool evaluate() const override;
Jooyung Han690f5842020-12-04 13:02:04 +0900605
606 std::unique_ptr<AidlTypeSpecifier> ref_type_;
607 std::string field_name_;
608 const std::string comments_;
Jooyung Han29813842020-12-08 01:28:03 +0900609 const AidlConstantValue* resolved_ = nullptr;
Will McVickerd7d18df2019-09-12 13:40:50 -0700610};
611
612class AidlUnaryConstExpression : public AidlConstantValue {
613 public:
614 AidlUnaryConstExpression(const AidlLocation& location, const string& op,
615 std::unique_ptr<AidlConstantValue> rval);
616
617 static bool IsCompatibleType(Type type, const string& op);
618 bool CheckValid() const override;
Jooyung Han690f5842020-12-04 13:02:04 +0900619 void Accept(Visitor& visitor) override {
620 visitor.Visit(*this);
621 unary_->Accept(visitor);
622 }
623
Will McVickerd7d18df2019-09-12 13:40:50 -0700624 private:
Jooyung Han74675c22020-12-15 08:39:57 +0900625 bool evaluate() const override;
Will McVickerd7d18df2019-09-12 13:40:50 -0700626
627 std::unique_ptr<AidlConstantValue> unary_;
628 const string op_;
629};
630
631class AidlBinaryConstExpression : public AidlConstantValue {
632 public:
633 AidlBinaryConstExpression(const AidlLocation& location, std::unique_ptr<AidlConstantValue> lval,
634 const string& op, std::unique_ptr<AidlConstantValue> rval);
635
636 bool CheckValid() const override;
Will McVickerd7d18df2019-09-12 13:40:50 -0700637
638 static bool AreCompatibleTypes(Type t1, Type t2);
639 // Returns the promoted kind for both operands
640 static Type UsualArithmeticConversion(Type left, Type right);
641 // Returns the promoted integral type where INT32 is the smallest type
642 static Type IntegralPromotion(Type in);
Jooyung Han690f5842020-12-04 13:02:04 +0900643 void Accept(Visitor& visitor) override {
644 visitor.Visit(*this);
645 left_val_->Accept(visitor);
646 right_val_->Accept(visitor);
647 }
Will McVickerd7d18df2019-09-12 13:40:50 -0700648
649 private:
Jooyung Han74675c22020-12-15 08:39:57 +0900650 bool evaluate() const override;
Will McVickerd7d18df2019-09-12 13:40:50 -0700651
652 std::unique_ptr<AidlConstantValue> left_val_;
653 std::unique_ptr<AidlConstantValue> right_val_;
654 const string op_;
Christopher Wileyd6bdd8d2016-05-03 11:23:13 -0700655};
656
Andrei Onea9445fc62019-06-27 18:11:59 +0100657struct AidlAnnotationParameter {
658 std::string name;
659 std::unique_ptr<AidlConstantValue> value;
660};
661
Steven Moreland693640b2018-07-19 13:46:27 -0700662class AidlConstantDeclaration : public AidlMember {
Christopher Wileyd6bdd8d2016-05-03 11:23:13 -0700663 public:
Steven Moreland46e9da82018-07-27 15:45:29 -0700664 AidlConstantDeclaration(const AidlLocation& location, AidlTypeSpecifier* specifier,
Will McVickerd7d18df2019-09-12 13:40:50 -0700665 const string& name, AidlConstantValue* value);
Steven Moreland693640b2018-07-19 13:46:27 -0700666 virtual ~AidlConstantDeclaration() = default;
Christopher Wileyd6bdd8d2016-05-03 11:23:13 -0700667
Jiyong Parkd800fef2020-07-22 18:09:43 +0900668 // non-copyable, non-movable
669 AidlConstantDeclaration(const AidlConstantDeclaration&) = delete;
670 AidlConstantDeclaration(AidlConstantDeclaration&&) = delete;
671 AidlConstantDeclaration& operator=(const AidlConstantDeclaration&) = delete;
672 AidlConstantDeclaration& operator=(AidlConstantDeclaration&&) = delete;
673
Steven Moreland693640b2018-07-19 13:46:27 -0700674 const AidlTypeSpecifier& GetType() const { return *type_; }
Steven Moreland4d12f9a2018-10-31 14:30:55 -0700675 AidlTypeSpecifier* GetMutableType() { return type_.get(); }
Will McVickerd7d18df2019-09-12 13:40:50 -0700676 const string& GetName() const { return name_; }
Steven Moreland693640b2018-07-19 13:46:27 -0700677 const AidlConstantValue& GetValue() const { return *value_; }
Jeongik Chadb0f59e2018-11-01 18:11:21 +0900678 bool CheckValid(const AidlTypenames& typenames) const;
Christopher Wileyd6bdd8d2016-05-03 11:23:13 -0700679
Jooyung Han965e31d2020-11-27 12:30:16 +0900680 // ToString is for dumping AIDL.
681 // Returns string representation of this const decl including a const value.
682 // This is "`const` annotations type name value".
683 // e.g) "const @utf8InCpp String[] names = { "hello" }"
Will McVickerd7d18df2019-09-12 13:40:50 -0700684 string ToString() const;
Jooyung Han965e31d2020-11-27 12:30:16 +0900685
686 // Signature is for comparing types.
687 // Returns string representation of this const decl.
688 // This is "direction annotations type name".
689 // e.g) "String[] names"
Will McVickerd7d18df2019-09-12 13:40:50 -0700690 string Signature() const;
Jooyung Han965e31d2020-11-27 12:30:16 +0900691
Steven Moreland860b1942018-08-16 14:59:28 -0700692 string ValueString(const ConstantValueDecorator& decorator) const {
Will McVickerd7d18df2019-09-12 13:40:50 -0700693 return value_->ValueString(GetType(), decorator);
Steven Moreland860b1942018-08-16 14:59:28 -0700694 }
Steven Moreland25294322018-08-07 18:13:55 -0700695
Jooyung Han829ec7c2020-12-02 12:07:36 +0900696 const AidlConstantDeclaration* AsConstantDeclaration() const override { return this; }
Christopher Wileyd6bdd8d2016-05-03 11:23:13 -0700697
698 private:
Steven Moreland693640b2018-07-19 13:46:27 -0700699 const unique_ptr<AidlTypeSpecifier> type_;
Will McVickerd7d18df2019-09-12 13:40:50 -0700700 const string name_;
701 unique_ptr<AidlConstantValue> value_;
Casey Dahlind40e2fe2015-11-24 14:06:52 -0800702};
703
704class AidlMethod : public AidlMember {
Casey Dahlin5c69deb2015-10-01 14:44:12 -0700705 public:
Will McVickerd7d18df2019-09-12 13:40:50 -0700706 AidlMethod(const AidlLocation& location, bool oneway, AidlTypeSpecifier* type, const string& name,
707 vector<unique_ptr<AidlArgument>>* args, const string& comments);
708 AidlMethod(const AidlLocation& location, bool oneway, AidlTypeSpecifier* type, const string& name,
709 vector<unique_ptr<AidlArgument>>* args, const string& comments, int id,
710 bool is_user_defined = true);
Casey Dahlin5c69deb2015-10-01 14:44:12 -0700711 virtual ~AidlMethod() = default;
712
Jiyong Parkd800fef2020-07-22 18:09:43 +0900713 // non-copyable, non-movable
714 AidlMethod(const AidlMethod&) = delete;
715 AidlMethod(AidlMethod&&) = delete;
716 AidlMethod& operator=(const AidlMethod&) = delete;
717 AidlMethod& operator=(AidlMethod&&) = delete;
718
Jooyung Han829ec7c2020-12-02 12:07:36 +0900719 const AidlMethod* AsMethod() const override { return this; }
Jeongik Cha997281d2020-01-16 15:23:59 +0900720 bool IsHidden() const;
Will McVickerd7d18df2019-09-12 13:40:50 -0700721 const string& GetComments() const { return comments_; }
Jiyong Parkd59a10d2018-07-18 11:12:55 +0900722 const AidlTypeSpecifier& GetType() const { return *type_; }
723 AidlTypeSpecifier* GetMutableType() { return type_.get(); }
Steven Morelandacd53472018-12-14 10:17:26 -0800724
Steven Moreland8c70ba92018-12-17 10:20:31 -0800725 // set if this method is part of an interface that is marked oneway
726 void ApplyInterfaceOneway(bool oneway) { oneway_ = oneway_ || oneway; }
Casey Dahlinf4a93112015-10-05 16:58:09 -0700727 bool IsOneway() const { return oneway_; }
Steven Morelandacd53472018-12-14 10:17:26 -0800728
Casey Dahlinf4a93112015-10-05 16:58:09 -0700729 const std::string& GetName() const { return name_; }
Casey Dahlinf4a93112015-10-05 16:58:09 -0700730 bool HasId() const { return has_id_; }
Jiyong Parked65bf42018-08-28 15:43:27 +0900731 int GetId() const { return id_; }
Casey Dahlinf4a93112015-10-05 16:58:09 -0700732 void SetId(unsigned id) { id_ = id; }
Casey Dahlinf2d23f72015-10-02 16:19:19 -0700733
Jiyong Park309668e2018-07-28 16:55:44 +0900734 bool IsUserDefined() const { return is_user_defined_; }
735
Casey Dahlinf4a93112015-10-05 16:58:09 -0700736 const std::vector<std::unique_ptr<AidlArgument>>& GetArguments() const {
Christopher Wileyad339272015-10-05 19:11:58 -0700737 return arguments_;
738 }
739 // An inout parameter will appear in both GetInArguments()
740 // and GetOutArguments(). AidlMethod retains ownership of the argument
741 // pointers returned in this way.
742 const std::vector<const AidlArgument*>& GetInArguments() const {
743 return in_arguments_;
744 }
745 const std::vector<const AidlArgument*>& GetOutArguments() const {
746 return out_arguments_;
Casey Dahlinf4a93112015-10-05 16:58:09 -0700747 }
Casey Dahlin5c69deb2015-10-01 14:44:12 -0700748
Jooyung Han965e31d2020-11-27 12:30:16 +0900749 // ToString is for dumping AIDL.
750 // Returns string representation of this method including everything.
751 // This is "ret_type name ( arg_list ) = id".
752 // e.g) "boolean foo(int, @Nullable String) = 1"
Jiyong Park309668e2018-07-28 16:55:44 +0900753 std::string ToString() const;
754
Jooyung Han965e31d2020-11-27 12:30:16 +0900755 // Signature is for comparing AIDL types.
756 // Returns string representation of this method's name & type.
757 // e.g) "foo(int, String)"
758 std::string Signature() const;
759
Casey Dahlin5c69deb2015-10-01 14:44:12 -0700760 private:
Casey Dahlinf4a93112015-10-05 16:58:09 -0700761 bool oneway_;
Casey Dahlinf2d23f72015-10-02 16:19:19 -0700762 std::string comments_;
Jiyong Parkd59a10d2018-07-18 11:12:55 +0900763 std::unique_ptr<AidlTypeSpecifier> type_;
Casey Dahlinf4a93112015-10-05 16:58:09 -0700764 std::string name_;
Christopher Wileyad339272015-10-05 19:11:58 -0700765 const std::vector<std::unique_ptr<AidlArgument>> arguments_;
766 std::vector<const AidlArgument*> in_arguments_;
767 std::vector<const AidlArgument*> out_arguments_;
Casey Dahlinf4a93112015-10-05 16:58:09 -0700768 bool has_id_;
769 int id_;
Jiyong Park309668e2018-07-28 16:55:44 +0900770 bool is_user_defined_ = true;
Casey Dahlin0a2f8be2015-09-28 16:15:29 -0700771};
Adam Lesinskiffa16862014-01-23 18:17:42 -0800772
Steven Morelandc258abc2018-07-10 14:03:38 -0700773class AidlDefinedType;
Jiyong Parkb034bf02018-07-30 17:44:33 +0900774class AidlInterface;
775class AidlParcelable;
776class AidlStructuredParcelable;
Jooyung Han2946afc2020-10-05 20:29:16 +0900777class AidlUnionDecl;
Jooyung Han3f347ca2020-12-01 12:41:50 +0900778
Daniel Norman85aed542019-08-21 12:01:14 -0700779// AidlDefinedType represents either an interface, parcelable, or enum that is
Jiyong Park1deecc32018-07-17 01:14:41 +0900780// defined in the source file.
781class AidlDefinedType : public AidlAnnotatable {
Steven Moreland787b0432018-07-03 09:00:58 -0700782 public:
Steven Moreland46e9da82018-07-27 15:45:29 -0700783 AidlDefinedType(const AidlLocation& location, const std::string& name,
Jooyung Han829ec7c2020-12-02 12:07:36 +0900784 const std::string& comments, const std::string& package,
785 std::vector<std::unique_ptr<AidlMember>>* members);
Steven Moreland787b0432018-07-03 09:00:58 -0700786 virtual ~AidlDefinedType() = default;
787
Jiyong Parkd800fef2020-07-22 18:09:43 +0900788 // non-copyable, non-movable
789 AidlDefinedType(const AidlDefinedType&) = delete;
790 AidlDefinedType(AidlDefinedType&&) = delete;
791 AidlDefinedType& operator=(const AidlDefinedType&) = delete;
792 AidlDefinedType& operator=(AidlDefinedType&&) = delete;
793
Jiyong Park1deecc32018-07-17 01:14:41 +0900794 const std::string& GetName() const { return name_; };
Jeongik Cha997281d2020-01-16 15:23:59 +0900795 bool IsHidden() const;
Jiyong Park1deecc32018-07-17 01:14:41 +0900796 const std::string& GetComments() const { return comments_; }
Jiyong Parka6605ab2018-11-11 14:30:21 +0900797 void SetComments(const std::string comments) { comments_ = comments; }
Jiyong Park1deecc32018-07-17 01:14:41 +0900798
Steven Moreland787b0432018-07-03 09:00:58 -0700799 /* dot joined package, example: "android.package.foo" */
Jiyong Park18132182020-06-08 20:24:40 +0900800 std::string GetPackage() const { return package_; }
Steven Moreland787b0432018-07-03 09:00:58 -0700801 /* dot joined package and name, example: "android.package.foo.IBar" */
802 std::string GetCanonicalName() const;
Jiyong Park18132182020-06-08 20:24:40 +0900803 const std::vector<std::string>& GetSplitPackage() const { return split_package_; }
Steven Moreland787b0432018-07-03 09:00:58 -0700804
Steven Morelanded83a282018-07-17 13:27:29 -0700805 virtual std::string GetPreprocessDeclarationName() const = 0;
Steven Morelandc258abc2018-07-10 14:03:38 -0700806
Steven Moreland5557f1c2018-07-02 13:50:23 -0700807 virtual const AidlStructuredParcelable* AsStructuredParcelable() const { return nullptr; }
Steven Morelandc258abc2018-07-10 14:03:38 -0700808 virtual const AidlParcelable* AsParcelable() const { return nullptr; }
Daniel Norman85aed542019-08-21 12:01:14 -0700809 virtual const AidlEnumDeclaration* AsEnumDeclaration() const { return nullptr; }
Jooyung Han2946afc2020-10-05 20:29:16 +0900810 virtual const AidlUnionDecl* AsUnionDeclaration() const { return nullptr; }
Steven Moreland5557f1c2018-07-02 13:50:23 -0700811 virtual const AidlInterface* AsInterface() const { return nullptr; }
Jeongik Chadf76dc72019-11-28 00:08:47 +0900812 virtual const AidlParameterizable<std::string>* AsParameterizable() const { return nullptr; }
Jooyung Han888c5bc2020-12-22 17:28:47 +0900813 virtual bool CheckValid(const AidlTypenames& typenames, DiagnosticsContext& context) const;
Steven Morelandd59e3172020-05-11 16:42:09 -0700814 virtual bool LanguageSpecificCheckValid(const AidlTypenames& typenames,
815 Options::Language lang) const = 0;
Steven Morelandc258abc2018-07-10 14:03:38 -0700816 AidlStructuredParcelable* AsStructuredParcelable() {
817 return const_cast<AidlStructuredParcelable*>(
818 const_cast<const AidlDefinedType*>(this)->AsStructuredParcelable());
819 }
820 AidlParcelable* AsParcelable() {
821 return const_cast<AidlParcelable*>(const_cast<const AidlDefinedType*>(this)->AsParcelable());
822 }
Daniel Norman85aed542019-08-21 12:01:14 -0700823 AidlEnumDeclaration* AsEnumDeclaration() {
824 return const_cast<AidlEnumDeclaration*>(
825 const_cast<const AidlDefinedType*>(this)->AsEnumDeclaration());
826 }
Jooyung Han2946afc2020-10-05 20:29:16 +0900827 AidlUnionDecl* AsUnionDeclaration() {
828 return const_cast<AidlUnionDecl*>(
829 const_cast<const AidlDefinedType*>(this)->AsUnionDeclaration());
830 }
Steven Morelandc258abc2018-07-10 14:03:38 -0700831 AidlInterface* AsInterface() {
832 return const_cast<AidlInterface*>(const_cast<const AidlDefinedType*>(this)->AsInterface());
833 }
834
Jeongik Chadf76dc72019-11-28 00:08:47 +0900835 AidlParameterizable<std::string>* AsParameterizable() {
836 return const_cast<AidlParameterizable<std::string>*>(
837 const_cast<const AidlDefinedType*>(this)->AsParameterizable());
838 }
839
Steven Moreland6cee3482018-07-18 14:39:58 -0700840 const AidlParcelable* AsUnstructuredParcelable() const {
841 if (this->AsStructuredParcelable() != nullptr) return nullptr;
Jooyung Han2946afc2020-10-05 20:29:16 +0900842 if (this->AsUnionDeclaration() != nullptr) return nullptr;
Steven Moreland6cee3482018-07-18 14:39:58 -0700843 return this->AsParcelable();
844 }
845 AidlParcelable* AsUnstructuredParcelable() {
846 return const_cast<AidlParcelable*>(
847 const_cast<const AidlDefinedType*>(this)->AsUnstructuredParcelable());
848 }
849
Jeongik Cha997281d2020-01-16 15:23:59 +0900850 virtual void Dump(CodeWriter* writer) const = 0;
Steven Morelanda5d9c5c2020-02-21 16:01:09 -0800851 void DumpHeader(CodeWriter* writer) const;
Jiyong Park02da7422018-07-16 16:00:26 +0900852
Jooyung Han829ec7c2020-12-02 12:07:36 +0900853 const std::vector<std::unique_ptr<AidlVariableDeclaration>>& GetFields() const {
854 return variables_;
855 }
856 const std::vector<std::unique_ptr<AidlConstantDeclaration>>& GetConstantDeclarations() const {
857 return constants_;
858 }
859 const std::vector<std::unique_ptr<AidlMethod>>& GetMethods() const { return methods_; }
860 void AddMethod(std::unique_ptr<AidlMethod> method) { methods_.push_back(std::move(method)); }
861 const std::vector<const AidlMember*>& GetMembers() const { return members_; }
862
863 protected:
864 // utility for subclasses with getter names
865 bool CheckValidForGetterNames() const;
866
Steven Moreland787b0432018-07-03 09:00:58 -0700867 private:
Jooyung Han829ec7c2020-12-02 12:07:36 +0900868 bool CheckValidWithMembers(const AidlTypenames& typenames) const;
869
Jiyong Park1deecc32018-07-17 01:14:41 +0900870 std::string name_;
Jiyong Park1deecc32018-07-17 01:14:41 +0900871 std::string comments_;
Jiyong Park18132182020-06-08 20:24:40 +0900872 const std::string package_;
873 const std::vector<std::string> split_package_;
Jooyung Han829ec7c2020-12-02 12:07:36 +0900874 std::vector<std::unique_ptr<AidlVariableDeclaration>> variables_;
875 std::vector<std::unique_ptr<AidlConstantDeclaration>> constants_;
876 std::vector<std::unique_ptr<AidlMethod>> methods_;
877 std::vector<const AidlMember*> members_; // keep members in order of appearance.
Steven Moreland787b0432018-07-03 09:00:58 -0700878};
879
Jeongik Chadf76dc72019-11-28 00:08:47 +0900880class AidlParcelable : public AidlDefinedType, public AidlParameterizable<std::string> {
Casey Dahlin1ae2bc52015-10-07 18:49:10 -0700881 public:
Jiyong Park18132182020-06-08 20:24:40 +0900882 AidlParcelable(const AidlLocation& location, const std::string& name, const std::string& package,
883 const std::string& comments, const std::string& cpp_header = "",
Jooyung Han829ec7c2020-12-02 12:07:36 +0900884 std::vector<std::string>* type_params = nullptr,
885 std::vector<std::unique_ptr<AidlMember>>* members = nullptr);
Casey Dahlin1ae2bc52015-10-07 18:49:10 -0700886 virtual ~AidlParcelable() = default;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800887
Jiyong Parkd800fef2020-07-22 18:09:43 +0900888 // non-copyable, non-movable
889 AidlParcelable(const AidlParcelable&) = delete;
890 AidlParcelable(AidlParcelable&&) = delete;
891 AidlParcelable& operator=(const AidlParcelable&) = delete;
892 AidlParcelable& operator=(AidlParcelable&&) = delete;
893
Christopher Wiley8aa4d9f2015-11-16 19:10:45 -0800894 std::string GetCppHeader() const { return cpp_header_; }
Christopher Wiley8aa4d9f2015-11-16 19:10:45 -0800895
Steven Moreland0cea4aa2020-04-20 21:06:02 -0700896 std::set<AidlAnnotation::Type> GetSupportedAnnotations() const override;
Jooyung Han888c5bc2020-12-22 17:28:47 +0900897 bool CheckValid(const AidlTypenames& typenames, DiagnosticsContext& context) const override;
Steven Morelandd59e3172020-05-11 16:42:09 -0700898 bool LanguageSpecificCheckValid(const AidlTypenames& typenames,
899 Options::Language lang) const override;
Steven Morelandc258abc2018-07-10 14:03:38 -0700900 const AidlParcelable* AsParcelable() const override { return this; }
Jeongik Chadf76dc72019-11-28 00:08:47 +0900901 const AidlParameterizable<std::string>* AsParameterizable() const override { return this; }
902 const AidlNode& AsAidlNode() const override { return *this; }
Steven Morelanded83a282018-07-17 13:27:29 -0700903 std::string GetPreprocessDeclarationName() const override { return "parcelable"; }
Steven Morelandc258abc2018-07-10 14:03:38 -0700904
Jeongik Cha997281d2020-01-16 15:23:59 +0900905 void Dump(CodeWriter* writer) const override;
Jiyong Park02da7422018-07-16 16:00:26 +0900906
Casey Dahlin1ae2bc52015-10-07 18:49:10 -0700907 private:
Christopher Wiley8aa4d9f2015-11-16 19:10:45 -0800908 std::string cpp_header_;
Casey Dahlin0a2f8be2015-09-28 16:15:29 -0700909};
Adam Lesinskiffa16862014-01-23 18:17:42 -0800910
Jooyung Han829ec7c2020-12-02 12:07:36 +0900911class AidlStructuredParcelable : public AidlParcelable {
Steven Moreland5557f1c2018-07-02 13:50:23 -0700912 public:
Jiyong Park18132182020-06-08 20:24:40 +0900913 AidlStructuredParcelable(const AidlLocation& location, const std::string& name,
914 const std::string& package, const std::string& comments,
Jooyung Han829ec7c2020-12-02 12:07:36 +0900915 std::vector<std::string>* type_params,
916 std::vector<std::unique_ptr<AidlMember>>* members);
Jiyong Parkd800fef2020-07-22 18:09:43 +0900917 virtual ~AidlStructuredParcelable() = default;
918
919 // non-copyable, non-movable
920 AidlStructuredParcelable(const AidlStructuredParcelable&) = delete;
921 AidlStructuredParcelable(AidlStructuredParcelable&&) = delete;
922 AidlStructuredParcelable& operator=(const AidlStructuredParcelable&) = delete;
923 AidlStructuredParcelable& operator=(AidlStructuredParcelable&&) = delete;
Steven Moreland5557f1c2018-07-02 13:50:23 -0700924
Steven Morelandc258abc2018-07-10 14:03:38 -0700925 const AidlStructuredParcelable* AsStructuredParcelable() const override { return this; }
Steven Morelanded83a282018-07-17 13:27:29 -0700926 std::string GetPreprocessDeclarationName() const override { return "structured_parcelable"; }
Steven Moreland5557f1c2018-07-02 13:50:23 -0700927
Jeongik Cha997281d2020-01-16 15:23:59 +0900928 void Dump(CodeWriter* writer) const override;
Jiyong Park02da7422018-07-16 16:00:26 +0900929
Steven Moreland0cea4aa2020-04-20 21:06:02 -0700930 std::set<AidlAnnotation::Type> GetSupportedAnnotations() const override;
Jooyung Han888c5bc2020-12-22 17:28:47 +0900931 bool CheckValid(const AidlTypenames& typenames, DiagnosticsContext& context) const override;
Steven Morelandd59e3172020-05-11 16:42:09 -0700932 bool LanguageSpecificCheckValid(const AidlTypenames& typenames,
933 Options::Language lang) const override;
Steven Moreland5557f1c2018-07-02 13:50:23 -0700934};
935
Daniel Norman85aed542019-08-21 12:01:14 -0700936class AidlEnumerator : public AidlNode {
937 public:
Daniel Norman2e4112d2019-10-03 10:22:35 -0700938 AidlEnumerator(const AidlLocation& location, const std::string& name, AidlConstantValue* value,
939 const std::string& comments);
Daniel Norman85aed542019-08-21 12:01:14 -0700940 virtual ~AidlEnumerator() = default;
941
Jiyong Parkd800fef2020-07-22 18:09:43 +0900942 // non-copyable, non-movable
943 AidlEnumerator(const AidlEnumerator&) = delete;
944 AidlEnumerator(AidlEnumerator&&) = delete;
945 AidlEnumerator& operator=(const AidlEnumerator&) = delete;
946 AidlEnumerator& operator=(AidlEnumerator&&) = delete;
947
Daniel Norman85aed542019-08-21 12:01:14 -0700948 const std::string& GetName() const { return name_; }
Will McVickerd7d18df2019-09-12 13:40:50 -0700949 AidlConstantValue* GetValue() const { return value_.get(); }
Daniel Norman2e4112d2019-10-03 10:22:35 -0700950 const std::string& GetComments() const { return comments_; }
Daniel Norman85aed542019-08-21 12:01:14 -0700951 bool CheckValid(const AidlTypeSpecifier& enum_backing_type) const;
952
953 string ValueString(const AidlTypeSpecifier& backing_type,
954 const ConstantValueDecorator& decorator) const;
955
Daniel Normanb28684e2019-10-17 15:31:39 -0700956 void SetValue(std::unique_ptr<AidlConstantValue> value) { value_ = std::move(value); }
Jooyung Han29813842020-12-08 01:28:03 +0900957 bool IsValueUserSpecified() const { return value_user_specified_; }
Daniel Normanb28684e2019-10-17 15:31:39 -0700958
Daniel Norman85aed542019-08-21 12:01:14 -0700959 private:
960 const std::string name_;
Will McVickerd7d18df2019-09-12 13:40:50 -0700961 unique_ptr<AidlConstantValue> value_;
Daniel Norman2e4112d2019-10-03 10:22:35 -0700962 const std::string comments_;
Jooyung Han29813842020-12-08 01:28:03 +0900963 const bool value_user_specified_;
Daniel Norman85aed542019-08-21 12:01:14 -0700964};
965
966class AidlEnumDeclaration : public AidlDefinedType {
967 public:
Will McVickerd7d18df2019-09-12 13:40:50 -0700968 AidlEnumDeclaration(const AidlLocation& location, const string& name,
Daniel Norman85aed542019-08-21 12:01:14 -0700969 std::vector<std::unique_ptr<AidlEnumerator>>* enumerators,
Jiyong Park18132182020-06-08 20:24:40 +0900970 const std::string& package, const std::string& comments);
Daniel Norman85aed542019-08-21 12:01:14 -0700971 virtual ~AidlEnumDeclaration() = default;
972
Jiyong Parkd800fef2020-07-22 18:09:43 +0900973 // non-copyable, non-movable
974 AidlEnumDeclaration(const AidlEnumDeclaration&) = delete;
975 AidlEnumDeclaration(AidlEnumDeclaration&&) = delete;
976 AidlEnumDeclaration& operator=(const AidlEnumDeclaration&) = delete;
977 AidlEnumDeclaration& operator=(AidlEnumDeclaration&&) = delete;
978
Jooyung Han672557b2020-12-24 05:18:00 +0900979 bool Autofill(const AidlTypenames&);
Daniel Norman85aed542019-08-21 12:01:14 -0700980 const AidlTypeSpecifier& GetBackingType() const { return *backing_type_; }
981 const std::vector<std::unique_ptr<AidlEnumerator>>& GetEnumerators() const {
982 return enumerators_;
983 }
Steven Moreland0cea4aa2020-04-20 21:06:02 -0700984 std::set<AidlAnnotation::Type> GetSupportedAnnotations() const override;
Jooyung Han888c5bc2020-12-22 17:28:47 +0900985 bool CheckValid(const AidlTypenames& typenames, DiagnosticsContext& context) const override;
Steven Morelandd59e3172020-05-11 16:42:09 -0700986 bool LanguageSpecificCheckValid(const AidlTypenames& /*typenames*/,
987 Options::Language) const override {
988 return true;
989 }
Daniel Norman85aed542019-08-21 12:01:14 -0700990 std::string GetPreprocessDeclarationName() const override { return "enum"; }
Jeongik Cha997281d2020-01-16 15:23:59 +0900991 void Dump(CodeWriter* writer) const override;
Daniel Norman85aed542019-08-21 12:01:14 -0700992
993 const AidlEnumDeclaration* AsEnumDeclaration() const override { return this; }
994
995 private:
Jooyung Han29813842020-12-08 01:28:03 +0900996
Daniel Norman85aed542019-08-21 12:01:14 -0700997 const std::string name_;
998 const std::vector<std::unique_ptr<AidlEnumerator>> enumerators_;
Jooyung Han672557b2020-12-24 05:18:00 +0900999 std::unique_ptr<AidlTypeSpecifier> backing_type_;
Daniel Norman85aed542019-08-21 12:01:14 -07001000};
1001
Jooyung Han829ec7c2020-12-02 12:07:36 +09001002class AidlUnionDecl : public AidlParcelable {
Jooyung Han2946afc2020-10-05 20:29:16 +09001003 public:
1004 AidlUnionDecl(const AidlLocation& location, const std::string& name, const std::string& package,
Jooyung Han829ec7c2020-12-02 12:07:36 +09001005 const std::string& comments, std::vector<std::string>* type_params,
1006 std::vector<std::unique_ptr<AidlMember>>* members);
Jooyung Han2946afc2020-10-05 20:29:16 +09001007 virtual ~AidlUnionDecl() = default;
1008
1009 // non-copyable, non-movable
1010 AidlUnionDecl(const AidlUnionDecl&) = delete;
1011 AidlUnionDecl(AidlUnionDecl&&) = delete;
1012 AidlUnionDecl& operator=(const AidlUnionDecl&) = delete;
1013 AidlUnionDecl& operator=(AidlUnionDecl&&) = delete;
1014
1015 std::set<AidlAnnotation::Type> GetSupportedAnnotations() const override;
1016
1017 const AidlNode& AsAidlNode() const override { return *this; }
1018
Jooyung Han888c5bc2020-12-22 17:28:47 +09001019 bool CheckValid(const AidlTypenames& typenames, DiagnosticsContext& context) const override;
Jooyung Hanfe89f122020-10-14 03:49:18 +09001020 bool LanguageSpecificCheckValid(const AidlTypenames& typenames,
1021 Options::Language lang) const override;
Jooyung Han2946afc2020-10-05 20:29:16 +09001022 std::string GetPreprocessDeclarationName() const override { return "union"; }
1023
1024 void Dump(CodeWriter* writer) const override;
1025 const AidlUnionDecl* AsUnionDeclaration() const override { return this; }
Jooyung Han2946afc2020-10-05 20:29:16 +09001026};
1027
Jiyong Park1deecc32018-07-17 01:14:41 +09001028class AidlInterface final : public AidlDefinedType {
Casey Dahlin1ae2bc52015-10-07 18:49:10 -07001029 public:
Steven Moreland46e9da82018-07-27 15:45:29 -07001030 AidlInterface(const AidlLocation& location, const std::string& name, const std::string& comments,
Jooyung Han829ec7c2020-12-02 12:07:36 +09001031 bool oneway_, const std::string& package,
1032 std::vector<std::unique_ptr<AidlMember>>* members);
Casey Dahlin1ae2bc52015-10-07 18:49:10 -07001033 virtual ~AidlInterface() = default;
1034
Jiyong Parkd800fef2020-07-22 18:09:43 +09001035 // non-copyable, non-movable
1036 AidlInterface(const AidlInterface&) = delete;
1037 AidlInterface(AidlInterface&&) = delete;
1038 AidlInterface& operator=(const AidlInterface&) = delete;
1039 AidlInterface& operator=(AidlInterface&&) = delete;
1040
Steven Morelandc258abc2018-07-10 14:03:38 -07001041 const AidlInterface* AsInterface() const override { return this; }
Steven Morelanded83a282018-07-17 13:27:29 -07001042 std::string GetPreprocessDeclarationName() const override { return "interface"; }
Steven Moreland5557f1c2018-07-02 13:50:23 -07001043
Jeongik Cha997281d2020-01-16 15:23:59 +09001044 void Dump(CodeWriter* writer) const override;
Jiyong Park02da7422018-07-16 16:00:26 +09001045
Steven Moreland0cea4aa2020-04-20 21:06:02 -07001046 std::set<AidlAnnotation::Type> GetSupportedAnnotations() const override;
Jooyung Han888c5bc2020-12-22 17:28:47 +09001047 bool CheckValid(const AidlTypenames& typenames, DiagnosticsContext& context) const override;
Steven Morelandd59e3172020-05-11 16:42:09 -07001048 bool LanguageSpecificCheckValid(const AidlTypenames& typenames,
1049 Options::Language lang) const override;
Jeongik Chadb0f59e2018-11-01 18:11:21 +09001050
Jiyong Park27fd7fd2020-08-27 16:25:09 +09001051 std::string GetDescriptor() const;
Casey Dahlin0a2f8be2015-09-28 16:15:29 -07001052};
Adam Lesinskiffa16862014-01-23 18:17:42 -08001053
Casey Dahlin0edf3422015-10-07 12:34:59 -07001054class AidlImport : public AidlNode {
1055 public:
Steven Moreland46e9da82018-07-27 15:45:29 -07001056 AidlImport(const AidlLocation& location, const std::string& needed_class);
Casey Dahlin0edf3422015-10-07 12:34:59 -07001057 virtual ~AidlImport() = default;
1058
Jiyong Parkd800fef2020-07-22 18:09:43 +09001059 // non-copyable, non-movable
1060 AidlImport(const AidlImport&) = delete;
1061 AidlImport(AidlImport&&) = delete;
1062 AidlImport& operator=(const AidlImport&) = delete;
1063 AidlImport& operator=(AidlImport&&) = delete;
1064
Casey Dahlin0edf3422015-10-07 12:34:59 -07001065 const std::string& GetNeededClass() const { return needed_class_; }
Casey Dahlin0edf3422015-10-07 12:34:59 -07001066
1067 private:
Casey Dahlin0edf3422015-10-07 12:34:59 -07001068 std::string needed_class_;
Casey Dahline2507492015-09-14 17:11:20 -07001069};
1070
Jiyong Park62515512020-06-08 15:57:11 +09001071// AidlDocument models an AIDL file
1072class AidlDocument : public AidlNode {
1073 public:
1074 AidlDocument(const AidlLocation& location, std::vector<std::unique_ptr<AidlImport>>& imports,
Jiyong Park8e79b7f2020-07-20 20:52:38 +09001075 std::vector<std::unique_ptr<AidlDefinedType>>&& defined_types)
1076 : AidlNode(location),
1077 imports_(std::move(imports)),
1078 defined_types_(std::move(defined_types)) {}
Jiyong Parkd800fef2020-07-22 18:09:43 +09001079 ~AidlDocument() = default;
1080
1081 // non-copyable, non-movable
Jiyong Park8e79b7f2020-07-20 20:52:38 +09001082 AidlDocument(const AidlDocument&) = delete;
1083 AidlDocument(AidlDocument&&) = delete;
1084 AidlDocument& operator=(const AidlDocument&) = delete;
1085 AidlDocument& operator=(AidlDocument&&) = delete;
Jiyong Parkd800fef2020-07-22 18:09:43 +09001086
Jooyung Han3557e482020-12-24 05:11:15 +09001087 bool CheckValid(const AidlTypenames& typenames, DiagnosticsContext& diag) const;
Jooyung Han29813842020-12-08 01:28:03 +09001088 std::optional<std::string> ResolveName(const std::string& unresolved_type) const;
Jiyong Parkd800fef2020-07-22 18:09:43 +09001089 const std::vector<std::unique_ptr<AidlImport>>& Imports() const { return imports_; }
1090 const std::vector<std::unique_ptr<AidlDefinedType>>& DefinedTypes() const {
1091 return defined_types_;
1092 }
Jiyong Park62515512020-06-08 15:57:11 +09001093
1094 private:
1095 const std::vector<std::unique_ptr<AidlImport>> imports_;
Jiyong Park8e79b7f2020-07-20 20:52:38 +09001096 const std::vector<std::unique_ptr<AidlDefinedType>> defined_types_;
Jiyong Park62515512020-06-08 15:57:11 +09001097};
Jooyung Hanb3c77ed2020-12-26 02:02:45 +09001098
1099template <typename T>
1100std::optional<T> AidlAnnotation::ParamValue(const std::string& param_name) const {
1101 auto it = parameters_.find(param_name);
1102 if (it == parameters_.end()) {
1103 return std::nullopt;
1104 }
1105 return it->second->Cast<T>();
1106}