blob: 112c6dc1cd948548a5f13de13c6546310060ecb6 [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,
Jooyung Hanf8dbbcc2020-12-26 03:05:55 +0900174 SUPPRESS_WARNINGS,
Steven Moreland0cea4aa2020-04-20 21:06:02 -0700175 };
176 static std::string TypeToString(Type type);
177
Andrei Onea9445fc62019-06-27 18:11:59 +0100178 static AidlAnnotation* Parse(
179 const AidlLocation& location, const string& name,
180 std::map<std::string, std::shared_ptr<AidlConstantValue>>* parameter_list);
Steven Moreland46e9da82018-07-27 15:45:29 -0700181
Steven Moreland3f658cf2018-08-20 13:40:54 -0700182 AidlAnnotation(const AidlAnnotation&) = default;
Steven Moreland3be75772018-08-20 13:27:43 -0700183 AidlAnnotation(AidlAnnotation&&) = default;
Jiyong Park68bc77a2018-07-19 19:00:45 +0900184 virtual ~AidlAnnotation() = default;
Andrei Onea9445fc62019-06-27 18:11:59 +0100185 bool CheckValid() const;
Steven Moreland3be75772018-08-20 13:27:43 -0700186
Jooyung Hand902a972020-10-23 17:32:44 +0900187 const string& GetName() const { return schema_.name; }
Steven Moreland0cea4aa2020-04-20 21:06:02 -0700188 const Type& GetType() const { return schema_.type; }
Jooyung Hand902a972020-10-23 17:32:44 +0900189 bool Repeatable() const { return schema_.repeatable; }
Jooyung Han965e31d2020-11-27 12:30:16 +0900190
191 // ToString is for dumping AIDL.
192 // Returns string representation of this annotation.
193 // e.g) "@RustDerive(Clone=true, Copy=true)"
194 string ToString() const;
195
Jooyung Hanb3c77ed2020-12-26 02:02:45 +0900196 template <typename T>
197 std::optional<T> ParamValue(const std::string& param_name) const;
198
Andrei Onea9445fc62019-06-27 18:11:59 +0100199 std::map<std::string, std::string> AnnotationParams(
200 const ConstantValueDecorator& decorator) const;
Jiyong Parka6605ab2018-11-11 14:30:21 +0900201 const string& GetComments() const { return comments_; }
202 void SetComments(const string& comments) { comments_ = comments; }
Jiyong Park68bc77a2018-07-19 19:00:45 +0900203
204 private:
Steven Moreland0cea4aa2020-04-20 21:06:02 -0700205 struct Schema {
206 AidlAnnotation::Type type;
207
208 // text name in .aidl file, e.g. "nullable"
209 std::string name;
210
211 // map from param name -> value type
Jooyung Han5c2fcae2020-12-26 00:04:39 +0900212 std::map<std::string, const AidlTypeSpecifier&> supported_parameters;
Jooyung Hand902a972020-10-23 17:32:44 +0900213
214 bool repeatable;
Jooyung Han5721a232020-12-24 04:34:55 +0900215
216 std::vector<std::string> required_parameters = {};
Steven Moreland0cea4aa2020-04-20 21:06:02 -0700217 };
218 static const std::vector<Schema>& AllSchemas();
219
220 AidlAnnotation(const AidlLocation& location, const Schema& schema,
Andrei Onea9445fc62019-06-27 18:11:59 +0100221 std::map<std::string, std::shared_ptr<AidlConstantValue>>&& parameters);
Steven Moreland0cea4aa2020-04-20 21:06:02 -0700222
223 const Schema& schema_;
Jiyong Parka6605ab2018-11-11 14:30:21 +0900224 string comments_;
Andrei Onea9445fc62019-06-27 18:11:59 +0100225 std::map<std::string, std::shared_ptr<AidlConstantValue>> parameters_;
Jiyong Park68bc77a2018-07-19 19:00:45 +0900226};
227
Steven Moreland3be75772018-08-20 13:27:43 -0700228static inline bool operator<(const AidlAnnotation& lhs, const AidlAnnotation& rhs) {
229 return lhs.GetName() < rhs.GetName();
230}
231static inline bool operator==(const AidlAnnotation& lhs, const AidlAnnotation& rhs) {
232 return lhs.GetName() == rhs.GetName();
233}
Jiyong Park3656c3c2018-08-01 20:02:01 +0900234
Casey Dahline7922932016-02-29 17:23:01 -0800235class AidlAnnotatable : public AidlNode {
Casey Dahlin0ee37582015-09-30 16:31:55 -0700236 public:
Steven Moreland46e9da82018-07-27 15:45:29 -0700237 AidlAnnotatable(const AidlLocation& location);
Steven Moreland3f658cf2018-08-20 13:40:54 -0700238
239 AidlAnnotatable(const AidlAnnotatable&) = default;
240 AidlAnnotatable(AidlAnnotatable&&) = default;
Casey Dahline7922932016-02-29 17:23:01 -0800241 virtual ~AidlAnnotatable() = default;
242
Artur Satayev91fe8712019-07-29 13:06:01 +0100243 void Annotate(vector<AidlAnnotation>&& annotations) {
244 for (auto& annotation : annotations) {
245 annotations_.emplace_back(std::move(annotation));
246 }
247 }
Jiyong Park68bc77a2018-07-19 19:00:45 +0900248 bool IsNullable() const;
Jiyong Park68bc77a2018-07-19 19:00:45 +0900249 bool IsUtf8InCpp() const;
Steven Morelanda7764e52020-10-27 17:29:29 +0000250 bool IsSensitiveData() const;
Steven Morelanda57d0a62019-07-30 09:41:14 -0700251 bool IsVintfStability() const;
Jeongik Chad0a10272020-08-06 16:33:36 +0900252 bool IsJavaOnlyImmutable() const;
Devin Moorec7e47a32020-08-07 10:55:25 -0700253 bool IsFixedSize() const;
Jeongik Cha88f95a82020-01-15 13:02:16 +0900254 bool IsStableApiParcelable(Options::Language lang) const;
Makoto Onuki78a1c1c2020-03-04 16:57:23 -0800255 bool IsHide() const;
Jooyung Han829ec7c2020-12-02 12:07:36 +0900256 bool JavaDerive(const std::string& method) const;
Jiyong Park27fd7fd2020-08-27 16:25:09 +0900257 std::string GetDescriptor() const;
Andrei Onea9445fc62019-06-27 18:11:59 +0100258
Steven Moreland7e4b9502020-02-20 18:10:42 -0800259 void DumpAnnotations(CodeWriter* writer) const;
260
Andrei Onea9445fc62019-06-27 18:11:59 +0100261 const AidlAnnotation* UnsupportedAppUsage() const;
Andrei Homescue61feb52020-08-18 15:44:24 -0700262 const AidlAnnotation* RustDerive() const;
Jooyung Han672557b2020-12-24 05:18:00 +0900263 const AidlAnnotation* BackingType() const;
Jooyung Hanf8dbbcc2020-12-26 03:05:55 +0900264 std::vector<std::string> SuppressWarnings() const;
Jooyung Han965e31d2020-11-27 12:30:16 +0900265
266 // ToString is for dumping AIDL.
267 // Returns string representation of annotations.
268 // e.g) "@JavaDerive(toString=true) @RustDerive(Clone=true, Copy=true)"
Jiyong Park68bc77a2018-07-19 19:00:45 +0900269 std::string ToString() const;
Casey Dahline7922932016-02-29 17:23:01 -0800270
Jiyong Parka6605ab2018-11-11 14:30:21 +0900271 const vector<AidlAnnotation>& GetAnnotations() const { return annotations_; }
Jooyung Han888c5bc2020-12-22 17:28:47 +0900272 bool CheckValid(const AidlTypenames&) const;
Jiyong Park3656c3c2018-08-01 20:02:01 +0900273
Steven Moreland181144c2020-04-20 19:57:56 -0700274 protected:
Steven Moreland0cea4aa2020-04-20 21:06:02 -0700275 virtual std::set<AidlAnnotation::Type> GetSupportedAnnotations() const = 0;
Steven Moreland181144c2020-04-20 19:57:56 -0700276
Casey Dahline7922932016-02-29 17:23:01 -0800277 private:
Jiyong Parka6605ab2018-11-11 14:30:21 +0900278 vector<AidlAnnotation> annotations_;
Casey Dahline7922932016-02-29 17:23:01 -0800279};
280
Jiyong Park1deecc32018-07-17 01:14:41 +0900281// AidlTypeSpecifier represents a reference to either a built-in type,
282// a defined type, or a variant (e.g., array of generic) of a type.
Jeongik Chadf76dc72019-11-28 00:08:47 +0900283class AidlTypeSpecifier final : public AidlAnnotatable,
284 public AidlParameterizable<unique_ptr<AidlTypeSpecifier>> {
Casey Dahline7922932016-02-29 17:23:01 -0800285 public:
Steven Moreland46e9da82018-07-27 15:45:29 -0700286 AidlTypeSpecifier(const AidlLocation& location, const string& unresolved_name, bool is_array,
287 vector<unique_ptr<AidlTypeSpecifier>>* type_params, const string& comments);
Jiyong Parkd59a10d2018-07-18 11:12:55 +0900288 virtual ~AidlTypeSpecifier() = default;
Casey Dahlin0ee37582015-09-30 16:31:55 -0700289
Steven Moreland3f658cf2018-08-20 13:40:54 -0700290 // Copy of this type which is not an array.
Jooyung Hand2fa0232020-10-19 02:51:41 +0900291 const AidlTypeSpecifier& ArrayBase() const;
Steven Moreland3f658cf2018-08-20 13:40:54 -0700292
Jiyong Park1deecc32018-07-17 01:14:41 +0900293 // Returns the full-qualified name of the base type.
294 // int -> int
295 // int[] -> int
296 // List<String> -> List
297 // IFoo -> foo.bar.IFoo (if IFoo is in package foo.bar)
298 const string& GetName() const {
299 if (IsResolved()) {
300 return fully_qualified_name_;
301 } else {
302 return GetUnresolvedName();
303 }
304 }
Casey Dahlin0ee37582015-09-30 16:31:55 -0700305
Jooyung Han965e31d2020-11-27 12:30:16 +0900306 // ToString is for dumping AIDL.
307 // Returns string representation of this type specifier including annotations.
308 // This is "annotations type_name type_params? array_marker?".
309 // e.g) "@utf8InCpp String[]";
310 std::string ToString() const;
Jiyong Park1deecc32018-07-17 01:14:41 +0900311
Jooyung Han965e31d2020-11-27 12:30:16 +0900312 // Signature is for comparing AIDL types.
313 // Returns string representation of this type specifier.
314 // This is "type_name type_params? array_marker?".
315 // e.g.) "String[]" (even if it is annotated with @utf8InCpp)
Jiyong Park02da7422018-07-16 16:00:26 +0900316 std::string Signature() const;
317
Jiyong Park1deecc32018-07-17 01:14:41 +0900318 const string& GetUnresolvedName() const { return unresolved_name_; }
319
Jeongik Cha997281d2020-01-16 15:23:59 +0900320 bool IsHidden() const;
321
Jiyong Park1deecc32018-07-17 01:14:41 +0900322 const string& GetComments() const { return comments_; }
323
Jeongik Cha1a7ab642019-07-29 17:31:02 +0900324 const std::vector<std::string> GetSplitName() const { return split_name_; }
325
Jiyong Parka6605ab2018-11-11 14:30:21 +0900326 void SetComments(const string& comment) { comments_ = comment; }
327
Jiyong Park1deecc32018-07-17 01:14:41 +0900328 bool IsResolved() const { return fully_qualified_name_ != ""; }
329
330 bool IsArray() const { return is_array_; }
331
Steven Moreland6c07b832020-10-29 23:39:53 +0000332 __attribute__((warn_unused_result)) bool SetArray() {
333 if (is_array_) return false;
334 is_array_ = true;
335 return true;
336 }
337
Jiyong Park1deecc32018-07-17 01:14:41 +0900338 // Resolve the base type name to a fully-qualified name. Return false if the
339 // resolution fails.
Daniel Norman716d3112019-09-10 13:11:56 -0700340 bool Resolve(const AidlTypenames& typenames);
Casey Dahlin0ee37582015-09-30 16:31:55 -0700341
Steven Moreland0cea4aa2020-04-20 21:06:02 -0700342 std::set<AidlAnnotation::Type> GetSupportedAnnotations() const override;
Jooyung Han888c5bc2020-12-22 17:28:47 +0900343 bool CheckValid(const AidlTypenames& typenames) const;
Steven Morelandd59e3172020-05-11 16:42:09 -0700344 bool LanguageSpecificCheckValid(const AidlTypenames& typenames, Options::Language lang) const;
Jeongik Chadf76dc72019-11-28 00:08:47 +0900345 const AidlNode& AsAidlNode() const override { return *this; }
Jiyong Park1d2df7d2018-07-23 15:22:50 +0900346
Jooyung Hane9bb9de2020-11-01 22:16:57 +0900347 const AidlDefinedType* GetDefinedType() const;
348
Casey Dahlin0ee37582015-09-30 16:31:55 -0700349 private:
Steven Moreland3f658cf2018-08-20 13:40:54 -0700350 AidlTypeSpecifier(const AidlTypeSpecifier&) = default;
351
Jiyong Park1deecc32018-07-17 01:14:41 +0900352 const string unresolved_name_;
353 string fully_qualified_name_;
Steven Moreland3f658cf2018-08-20 13:40:54 -0700354 bool is_array_;
Jiyong Parka6605ab2018-11-11 14:30:21 +0900355 string comments_;
Jeongik Cha1a7ab642019-07-29 17:31:02 +0900356 vector<string> split_name_;
Jooyung Han690f5842020-12-04 13:02:04 +0900357 const AidlDefinedType* defined_type_ = nullptr; // set when Resolve() for defined types
Jooyung Hand2fa0232020-10-19 02:51:41 +0900358 mutable shared_ptr<AidlTypeSpecifier> array_base_;
Casey Dahlin0ee37582015-09-30 16:31:55 -0700359};
360
Steven Moreland860b1942018-08-16 14:59:28 -0700361// Returns the universal value unaltered.
362std::string AidlConstantValueDecorator(const AidlTypeSpecifier& type, const std::string& raw_value);
363
Steven Moreland9ea10e32018-07-19 15:26:09 -0700364class AidlConstantValue;
Jooyung Han3f347ca2020-12-01 12:41:50 +0900365class AidlMethod;
366class AidlConstantDeclaration;
367class AidlVariableDeclaration;
368
369class AidlMember : public AidlNode {
370 public:
371 AidlMember(const AidlLocation& location);
372 virtual ~AidlMember() = default;
373
374 // non-copyable, non-movable
375 AidlMember(const AidlMember&) = delete;
376 AidlMember(AidlMember&&) = delete;
377 AidlMember& operator=(const AidlMember&) = delete;
378 AidlMember& operator=(AidlMember&&) = delete;
379
Jooyung Han829ec7c2020-12-02 12:07:36 +0900380 virtual const AidlMethod* AsMethod() const { return nullptr; }
381 virtual const AidlConstantDeclaration* AsConstantDeclaration() const { return nullptr; }
382 virtual const AidlVariableDeclaration* AsVariableDeclaration() const { return nullptr; }
383
384 AidlMethod* AsMethod() {
385 return const_cast<AidlMethod*>(const_cast<const AidlMember*>(this)->AsMethod());
386 }
387 AidlConstantDeclaration* AsConstantDeclaration() {
388 return const_cast<AidlConstantDeclaration*>(
389 const_cast<const AidlMember*>(this)->AsConstantDeclaration());
390 }
391 AidlVariableDeclaration* AsVariableDeclaration() {
392 return const_cast<AidlVariableDeclaration*>(
393 const_cast<const AidlMember*>(this)->AsVariableDeclaration());
394 }
Jooyung Han3f347ca2020-12-01 12:41:50 +0900395};
396
Steven Moreland541788d2020-05-21 22:05:52 +0000397// TODO: This class is used for method arguments and also parcelable fields,
398// and it should be split up since default values don't apply to method
399// arguments
Jooyung Han3f347ca2020-12-01 12:41:50 +0900400class AidlVariableDeclaration : public AidlMember {
Steven Moreland5557f1c2018-07-02 13:50:23 -0700401 public:
Steven Moreland46e9da82018-07-27 15:45:29 -0700402 AidlVariableDeclaration(const AidlLocation& location, AidlTypeSpecifier* type,
403 const std::string& name);
404 AidlVariableDeclaration(const AidlLocation& location, AidlTypeSpecifier* type,
405 const std::string& name, AidlConstantValue* default_value);
Steven Moreland5557f1c2018-07-02 13:50:23 -0700406 virtual ~AidlVariableDeclaration() = default;
407
Jiyong Parkd800fef2020-07-22 18:09:43 +0900408 // non-copyable, non-movable
409 AidlVariableDeclaration(const AidlVariableDeclaration&) = delete;
410 AidlVariableDeclaration(AidlVariableDeclaration&&) = delete;
411 AidlVariableDeclaration& operator=(const AidlVariableDeclaration&) = delete;
412 AidlVariableDeclaration& operator=(AidlVariableDeclaration&&) = delete;
413
Jooyung Han829ec7c2020-12-02 12:07:36 +0900414 const AidlVariableDeclaration* AsVariableDeclaration() const override { return this; }
Jooyung Han3f347ca2020-12-01 12:41:50 +0900415
Steven Moreland5557f1c2018-07-02 13:50:23 -0700416 std::string GetName() const { return name_; }
Jooyung Hanacae85d2020-10-28 16:39:09 +0900417 std::string GetCapitalizedName() const;
Jiyong Parkd59a10d2018-07-18 11:12:55 +0900418 const AidlTypeSpecifier& GetType() const { return *type_; }
Steven Moreland541788d2020-05-21 22:05:52 +0000419 // if this was constructed explicitly with a default value
420 bool IsDefaultUserSpecified() const { return default_user_specified_; }
421 // will return the default value this is constructed with or a default value
422 // if one is available
Steven Moreland9ea10e32018-07-19 15:26:09 -0700423 const AidlConstantValue* GetDefaultValue() const { return default_value_.get(); }
Jooyung Han53fb4242020-12-17 16:03:49 +0900424 bool HasUsefulDefaultValue() const;
Steven Moreland9ea10e32018-07-19 15:26:09 -0700425
Jiyong Parkd59a10d2018-07-18 11:12:55 +0900426 AidlTypeSpecifier* GetMutableType() { return type_.get(); }
Steven Moreland5557f1c2018-07-02 13:50:23 -0700427
Jeongik Chadb0f59e2018-11-01 18:11:21 +0900428 bool CheckValid(const AidlTypenames& typenames) const;
Jooyung Han965e31d2020-11-27 12:30:16 +0900429
430 // ToString is for dumping AIDL.
431 // Returns string representation of this variable decl including default value.
432 // This is "annotations type name default_value?".
433 // e.g) "@utf8InCpp String[] names = {"hello"}"
Steven Moreland5557f1c2018-07-02 13:50:23 -0700434 std::string ToString() const;
Jooyung Han965e31d2020-11-27 12:30:16 +0900435
436 // Signature is for comparing AIDL types.
437 // Returns string representation of this variable decl.
438 // This is "type name".
439 // e.g) "String[] name" (even if it is annotated with @utf8InCpp and has a default value.)
Jiyong Park02da7422018-07-16 16:00:26 +0900440 std::string Signature() const;
Steven Moreland5557f1c2018-07-02 13:50:23 -0700441
Steven Moreland860b1942018-08-16 14:59:28 -0700442 std::string ValueString(const ConstantValueDecorator& decorator) const;
Steven Moreland25294322018-08-07 18:13:55 -0700443
Steven Moreland5557f1c2018-07-02 13:50:23 -0700444 private:
Jiyong Parkd59a10d2018-07-18 11:12:55 +0900445 std::unique_ptr<AidlTypeSpecifier> type_;
Steven Moreland5557f1c2018-07-02 13:50:23 -0700446 std::string name_;
Steven Moreland541788d2020-05-21 22:05:52 +0000447 bool default_user_specified_;
Steven Moreland9ea10e32018-07-19 15:26:09 -0700448 std::unique_ptr<AidlConstantValue> default_value_;
Steven Moreland5557f1c2018-07-02 13:50:23 -0700449};
450
451class AidlArgument : public AidlVariableDeclaration {
Casey Dahlinbc7a50a2015-09-28 19:20:50 -0700452 public:
Casey Dahlinc378c992015-09-29 16:50:40 -0700453 enum Direction { IN_DIR = 1, OUT_DIR = 2, INOUT_DIR = 3 };
454
Steven Moreland46e9da82018-07-27 15:45:29 -0700455 AidlArgument(const AidlLocation& location, AidlArgument::Direction direction,
456 AidlTypeSpecifier* type, const std::string& name);
457 AidlArgument(const AidlLocation& location, AidlTypeSpecifier* type, const std::string& name);
Casey Dahlinbc7a50a2015-09-28 19:20:50 -0700458 virtual ~AidlArgument() = default;
459
Jiyong Parkd800fef2020-07-22 18:09:43 +0900460 // non-copyable, non-movable
461 AidlArgument(const AidlArgument&) = delete;
462 AidlArgument(AidlArgument&&) = delete;
463 AidlArgument& operator=(const AidlArgument&) = delete;
464 AidlArgument& operator=(AidlArgument&&) = delete;
465
Casey Dahlinc378c992015-09-29 16:50:40 -0700466 Direction GetDirection() const { return direction_; }
Christopher Wileyad339272015-10-05 19:11:58 -0700467 bool IsOut() const { return direction_ & OUT_DIR; }
468 bool IsIn() const { return direction_ & IN_DIR; }
Casey Dahlinc378c992015-09-29 16:50:40 -0700469 bool DirectionWasSpecified() const { return direction_specified_; }
Jiyong Park3656c3c2018-08-01 20:02:01 +0900470 string GetDirectionSpecifier() const;
Christopher Wileyad339272015-10-05 19:11:58 -0700471
Jooyung Han965e31d2020-11-27 12:30:16 +0900472 // ToString is for dumping AIDL.
473 // Returns string representation of this argument including direction
474 // This is "direction annotations type name".
475 // e.g) "in @utf8InCpp String[] names"
Casey Dahlinc378c992015-09-29 16:50:40 -0700476 std::string ToString() const;
477
Casey Dahlinbc7a50a2015-09-28 19:20:50 -0700478 private:
Casey Dahlinc378c992015-09-29 16:50:40 -0700479 Direction direction_;
480 bool direction_specified_;
Casey Dahlina834dd42015-09-23 11:52:15 -0700481};
Adam Lesinskiffa16862014-01-23 18:17:42 -0800482
Will McVickerd7d18df2019-09-12 13:40:50 -0700483class AidlUnaryConstExpression;
484class AidlBinaryConstExpression;
Jooyung Han690f5842020-12-04 13:02:04 +0900485class AidlConstantReference;
Will McVickerd7d18df2019-09-12 13:40:50 -0700486
Steven Moreland693640b2018-07-19 13:46:27 -0700487class AidlConstantValue : public AidlNode {
Casey Dahlind40e2fe2015-11-24 14:06:52 -0800488 public:
Will McVickerd7d18df2019-09-12 13:40:50 -0700489 enum class Type {
490 // WARNING: Don't change this order! The order is used to determine type
491 // promotion during a binary expression.
492 BOOLEAN,
493 INT8,
494 INT32,
495 INT64,
496 ARRAY,
497 CHARACTER,
498 STRING,
Jooyung Han690f5842020-12-04 13:02:04 +0900499 REF,
Will McVickerd7d18df2019-09-12 13:40:50 -0700500 FLOATING,
501 UNARY,
502 BINARY,
503 ERROR,
504 };
505
Jooyung Han690f5842020-12-04 13:02:04 +0900506 struct Visitor {
507 virtual ~Visitor() {}
508 virtual void Visit(AidlConstantValue&) = 0;
509 virtual void Visit(AidlConstantReference&) = 0;
510 virtual void Visit(AidlUnaryConstExpression&) = 0;
511 virtual void Visit(AidlBinaryConstExpression&) = 0;
512 };
513
Will McVickerd7d18df2019-09-12 13:40:50 -0700514 /*
515 * Return the value casted to the given type.
516 */
517 template <typename T>
Jooyung Han71a1b582020-12-25 23:58:41 +0900518 T Cast() const;
Casey Dahlind40e2fe2015-11-24 14:06:52 -0800519
Steven Moreland693640b2018-07-19 13:46:27 -0700520 virtual ~AidlConstantValue() = default;
Casey Dahlind40e2fe2015-11-24 14:06:52 -0800521
Jiyong Parkd800fef2020-07-22 18:09:43 +0900522 // non-copyable, non-movable
523 AidlConstantValue(const AidlConstantValue&) = delete;
524 AidlConstantValue(AidlConstantValue&&) = delete;
525 AidlConstantValue& operator=(const AidlConstantValue&) = delete;
526 AidlConstantValue& operator=(AidlConstantValue&&) = delete;
527
Steven Moreland541788d2020-05-21 22:05:52 +0000528 // creates default value, when one isn't specified
529 // nullptr if no default available
530 static AidlConstantValue* Default(const AidlTypeSpecifier& specifier);
531
Steven Moreland25294322018-08-07 18:13:55 -0700532 static AidlConstantValue* Boolean(const AidlLocation& location, bool value);
533 static AidlConstantValue* Character(const AidlLocation& location, char value);
Steven Moreland25294322018-08-07 18:13:55 -0700534 // example: 123, -5498, maybe any size
Will McVickerd7d18df2019-09-12 13:40:50 -0700535 static AidlConstantValue* Integral(const AidlLocation& location, const string& value);
536 static AidlConstantValue* Floating(const AidlLocation& location, const std::string& value);
Steven Moreland860b1942018-08-16 14:59:28 -0700537 static AidlConstantValue* Array(const AidlLocation& location,
Will McVickerd7d18df2019-09-12 13:40:50 -0700538 std::unique_ptr<vector<unique_ptr<AidlConstantValue>>> values);
Steven Moreland693640b2018-07-19 13:46:27 -0700539 // example: "\"asdf\""
Will McVickerd7d18df2019-09-12 13:40:50 -0700540 static AidlConstantValue* String(const AidlLocation& location, const string& value);
Steven Moreland693640b2018-07-19 13:46:27 -0700541
Will McVickerd7d18df2019-09-12 13:40:50 -0700542 Type GetType() const { return final_type_; }
Jooyung Han29813842020-12-08 01:28:03 +0900543 const std::string& Literal() const { return value_; }
Steven Moreland25294322018-08-07 18:13:55 -0700544
Will McVickerd7d18df2019-09-12 13:40:50 -0700545 virtual bool CheckValid() const;
Steven Moreland860b1942018-08-16 14:59:28 -0700546
547 // Raw value of type (currently valid in C++ and Java). Empty string on error.
Steven Moreland4bcb05c2019-11-27 18:57:47 -0800548 string ValueString(const AidlTypeSpecifier& type, const ConstantValueDecorator& decorator) const;
Jooyung Han29813842020-12-08 01:28:03 +0900549 virtual void Accept(Visitor& visitor) {
550 visitor.Visit(*this);
551 if (type_ == Type::ARRAY) {
552 for (const auto& v : values_) {
553 v.get()->Accept(visitor);
554 }
555 }
556 }
Casey Dahlind40e2fe2015-11-24 14:06:52 -0800557
558 private:
Will McVickerd7d18df2019-09-12 13:40:50 -0700559 AidlConstantValue(const AidlLocation& location, Type parsed_type, int64_t parsed_value,
560 const string& checked_value);
561 AidlConstantValue(const AidlLocation& location, Type type, const string& checked_value);
Steven Moreland860b1942018-08-16 14:59:28 -0700562 AidlConstantValue(const AidlLocation& location, Type type,
Jooyung Han29813842020-12-08 01:28:03 +0900563 std::unique_ptr<vector<unique_ptr<AidlConstantValue>>> values,
564 const std::string& value);
Steven Moreland25294322018-08-07 18:13:55 -0700565 static string ToString(Type type);
Will McVickerd7d18df2019-09-12 13:40:50 -0700566 static bool ParseIntegral(const string& value, int64_t* parsed_value, Type* parsed_type);
567 static bool IsHex(const string& value);
Steven Moreland4bcb05c2019-11-27 18:57:47 -0800568
Jooyung Han74675c22020-12-15 08:39:57 +0900569 virtual bool evaluate() const;
Casey Dahlind40e2fe2015-11-24 14:06:52 -0800570
Steven Moreland693640b2018-07-19 13:46:27 -0700571 const Type type_ = Type::ERROR;
Will McVickerd7d18df2019-09-12 13:40:50 -0700572 const vector<unique_ptr<AidlConstantValue>> values_; // if type_ == ARRAY
573 const string value_; // otherwise
574
575 // State for tracking evaluation of expressions
Steven Moreland4bcb05c2019-11-27 18:57:47 -0800576 mutable bool is_valid_ = false; // cache of CheckValid, but may be marked false in evaluate
577 mutable bool is_evaluated_ = false; // whether evaluate has been called
Will McVickerd7d18df2019-09-12 13:40:50 -0700578 mutable Type final_type_;
579 mutable int64_t final_value_;
580 mutable string final_string_value_ = "";
Steven Moreland693640b2018-07-19 13:46:27 -0700581
Will McVickerd7d18df2019-09-12 13:40:50 -0700582 friend AidlUnaryConstExpression;
583 friend AidlBinaryConstExpression;
Jooyung Han690f5842020-12-04 13:02:04 +0900584 friend AidlConstantReference;
585};
586
587// Represents "<type>.<field>" which resolves to a constant which is one of
588// - constant declartion
589// - enumerator
590// When a <type> is missing, <field> is of the enclosing type.
591class AidlConstantReference : public AidlConstantValue {
592 public:
593 AidlConstantReference(const AidlLocation& location, const std::string& value,
594 const std::string& comments);
595
596 const std::unique_ptr<AidlTypeSpecifier>& GetRefType() const { return ref_type_; }
597 void SetRefType(std::unique_ptr<AidlTypeSpecifier> type) { ref_type_ = std::move(type); }
598 const std::string& GetFieldName() const { return field_name_; }
599 const std::string& GetComments() const { return comments_; }
600
601 bool CheckValid() const override;
602 void Accept(Visitor& visitor) override { visitor.Visit(*this); }
Jooyung Han29813842020-12-08 01:28:03 +0900603 const AidlConstantValue* Resolve();
Jooyung Han690f5842020-12-04 13:02:04 +0900604
605 private:
Jooyung Han74675c22020-12-15 08:39:57 +0900606 bool evaluate() const override;
Jooyung Han690f5842020-12-04 13:02:04 +0900607
608 std::unique_ptr<AidlTypeSpecifier> ref_type_;
609 std::string field_name_;
610 const std::string comments_;
Jooyung Han29813842020-12-08 01:28:03 +0900611 const AidlConstantValue* resolved_ = nullptr;
Will McVickerd7d18df2019-09-12 13:40:50 -0700612};
613
614class AidlUnaryConstExpression : public AidlConstantValue {
615 public:
616 AidlUnaryConstExpression(const AidlLocation& location, const string& op,
617 std::unique_ptr<AidlConstantValue> rval);
618
619 static bool IsCompatibleType(Type type, const string& op);
620 bool CheckValid() const override;
Jooyung Han690f5842020-12-04 13:02:04 +0900621 void Accept(Visitor& visitor) override {
622 visitor.Visit(*this);
623 unary_->Accept(visitor);
624 }
625
Will McVickerd7d18df2019-09-12 13:40:50 -0700626 private:
Jooyung Han74675c22020-12-15 08:39:57 +0900627 bool evaluate() const override;
Will McVickerd7d18df2019-09-12 13:40:50 -0700628
629 std::unique_ptr<AidlConstantValue> unary_;
630 const string op_;
631};
632
633class AidlBinaryConstExpression : public AidlConstantValue {
634 public:
635 AidlBinaryConstExpression(const AidlLocation& location, std::unique_ptr<AidlConstantValue> lval,
636 const string& op, std::unique_ptr<AidlConstantValue> rval);
637
638 bool CheckValid() const override;
Will McVickerd7d18df2019-09-12 13:40:50 -0700639
640 static bool AreCompatibleTypes(Type t1, Type t2);
641 // Returns the promoted kind for both operands
642 static Type UsualArithmeticConversion(Type left, Type right);
643 // Returns the promoted integral type where INT32 is the smallest type
644 static Type IntegralPromotion(Type in);
Jooyung Han690f5842020-12-04 13:02:04 +0900645 void Accept(Visitor& visitor) override {
646 visitor.Visit(*this);
647 left_val_->Accept(visitor);
648 right_val_->Accept(visitor);
649 }
Will McVickerd7d18df2019-09-12 13:40:50 -0700650
651 private:
Jooyung Han74675c22020-12-15 08:39:57 +0900652 bool evaluate() const override;
Will McVickerd7d18df2019-09-12 13:40:50 -0700653
654 std::unique_ptr<AidlConstantValue> left_val_;
655 std::unique_ptr<AidlConstantValue> right_val_;
656 const string op_;
Christopher Wileyd6bdd8d2016-05-03 11:23:13 -0700657};
658
Andrei Onea9445fc62019-06-27 18:11:59 +0100659struct AidlAnnotationParameter {
660 std::string name;
661 std::unique_ptr<AidlConstantValue> value;
662};
663
Steven Moreland693640b2018-07-19 13:46:27 -0700664class AidlConstantDeclaration : public AidlMember {
Christopher Wileyd6bdd8d2016-05-03 11:23:13 -0700665 public:
Steven Moreland46e9da82018-07-27 15:45:29 -0700666 AidlConstantDeclaration(const AidlLocation& location, AidlTypeSpecifier* specifier,
Will McVickerd7d18df2019-09-12 13:40:50 -0700667 const string& name, AidlConstantValue* value);
Steven Moreland693640b2018-07-19 13:46:27 -0700668 virtual ~AidlConstantDeclaration() = default;
Christopher Wileyd6bdd8d2016-05-03 11:23:13 -0700669
Jiyong Parkd800fef2020-07-22 18:09:43 +0900670 // non-copyable, non-movable
671 AidlConstantDeclaration(const AidlConstantDeclaration&) = delete;
672 AidlConstantDeclaration(AidlConstantDeclaration&&) = delete;
673 AidlConstantDeclaration& operator=(const AidlConstantDeclaration&) = delete;
674 AidlConstantDeclaration& operator=(AidlConstantDeclaration&&) = delete;
675
Steven Moreland693640b2018-07-19 13:46:27 -0700676 const AidlTypeSpecifier& GetType() const { return *type_; }
Steven Moreland4d12f9a2018-10-31 14:30:55 -0700677 AidlTypeSpecifier* GetMutableType() { return type_.get(); }
Will McVickerd7d18df2019-09-12 13:40:50 -0700678 const string& GetName() const { return name_; }
Steven Moreland693640b2018-07-19 13:46:27 -0700679 const AidlConstantValue& GetValue() const { return *value_; }
Jeongik Chadb0f59e2018-11-01 18:11:21 +0900680 bool CheckValid(const AidlTypenames& typenames) const;
Christopher Wileyd6bdd8d2016-05-03 11:23:13 -0700681
Jooyung Han965e31d2020-11-27 12:30:16 +0900682 // ToString is for dumping AIDL.
683 // Returns string representation of this const decl including a const value.
684 // This is "`const` annotations type name value".
685 // e.g) "const @utf8InCpp String[] names = { "hello" }"
Will McVickerd7d18df2019-09-12 13:40:50 -0700686 string ToString() const;
Jooyung Han965e31d2020-11-27 12:30:16 +0900687
688 // Signature is for comparing types.
689 // Returns string representation of this const decl.
690 // This is "direction annotations type name".
691 // e.g) "String[] names"
Will McVickerd7d18df2019-09-12 13:40:50 -0700692 string Signature() const;
Jooyung Han965e31d2020-11-27 12:30:16 +0900693
Steven Moreland860b1942018-08-16 14:59:28 -0700694 string ValueString(const ConstantValueDecorator& decorator) const {
Will McVickerd7d18df2019-09-12 13:40:50 -0700695 return value_->ValueString(GetType(), decorator);
Steven Moreland860b1942018-08-16 14:59:28 -0700696 }
Steven Moreland25294322018-08-07 18:13:55 -0700697
Jooyung Han829ec7c2020-12-02 12:07:36 +0900698 const AidlConstantDeclaration* AsConstantDeclaration() const override { return this; }
Christopher Wileyd6bdd8d2016-05-03 11:23:13 -0700699
700 private:
Steven Moreland693640b2018-07-19 13:46:27 -0700701 const unique_ptr<AidlTypeSpecifier> type_;
Will McVickerd7d18df2019-09-12 13:40:50 -0700702 const string name_;
703 unique_ptr<AidlConstantValue> value_;
Casey Dahlind40e2fe2015-11-24 14:06:52 -0800704};
705
706class AidlMethod : public AidlMember {
Casey Dahlin5c69deb2015-10-01 14:44:12 -0700707 public:
Will McVickerd7d18df2019-09-12 13:40:50 -0700708 AidlMethod(const AidlLocation& location, bool oneway, AidlTypeSpecifier* type, const string& name,
709 vector<unique_ptr<AidlArgument>>* args, const string& comments);
710 AidlMethod(const AidlLocation& location, bool oneway, AidlTypeSpecifier* type, const string& name,
711 vector<unique_ptr<AidlArgument>>* args, const string& comments, int id,
712 bool is_user_defined = true);
Casey Dahlin5c69deb2015-10-01 14:44:12 -0700713 virtual ~AidlMethod() = default;
714
Jiyong Parkd800fef2020-07-22 18:09:43 +0900715 // non-copyable, non-movable
716 AidlMethod(const AidlMethod&) = delete;
717 AidlMethod(AidlMethod&&) = delete;
718 AidlMethod& operator=(const AidlMethod&) = delete;
719 AidlMethod& operator=(AidlMethod&&) = delete;
720
Jooyung Han829ec7c2020-12-02 12:07:36 +0900721 const AidlMethod* AsMethod() const override { return this; }
Jeongik Cha997281d2020-01-16 15:23:59 +0900722 bool IsHidden() const;
Will McVickerd7d18df2019-09-12 13:40:50 -0700723 const string& GetComments() const { return comments_; }
Jiyong Parkd59a10d2018-07-18 11:12:55 +0900724 const AidlTypeSpecifier& GetType() const { return *type_; }
725 AidlTypeSpecifier* GetMutableType() { return type_.get(); }
Steven Morelandacd53472018-12-14 10:17:26 -0800726
Steven Moreland8c70ba92018-12-17 10:20:31 -0800727 // set if this method is part of an interface that is marked oneway
728 void ApplyInterfaceOneway(bool oneway) { oneway_ = oneway_ || oneway; }
Casey Dahlinf4a93112015-10-05 16:58:09 -0700729 bool IsOneway() const { return oneway_; }
Steven Morelandacd53472018-12-14 10:17:26 -0800730
Casey Dahlinf4a93112015-10-05 16:58:09 -0700731 const std::string& GetName() const { return name_; }
Casey Dahlinf4a93112015-10-05 16:58:09 -0700732 bool HasId() const { return has_id_; }
Jiyong Parked65bf42018-08-28 15:43:27 +0900733 int GetId() const { return id_; }
Casey Dahlinf4a93112015-10-05 16:58:09 -0700734 void SetId(unsigned id) { id_ = id; }
Casey Dahlinf2d23f72015-10-02 16:19:19 -0700735
Jiyong Park309668e2018-07-28 16:55:44 +0900736 bool IsUserDefined() const { return is_user_defined_; }
737
Casey Dahlinf4a93112015-10-05 16:58:09 -0700738 const std::vector<std::unique_ptr<AidlArgument>>& GetArguments() const {
Christopher Wileyad339272015-10-05 19:11:58 -0700739 return arguments_;
740 }
741 // An inout parameter will appear in both GetInArguments()
742 // and GetOutArguments(). AidlMethod retains ownership of the argument
743 // pointers returned in this way.
744 const std::vector<const AidlArgument*>& GetInArguments() const {
745 return in_arguments_;
746 }
747 const std::vector<const AidlArgument*>& GetOutArguments() const {
748 return out_arguments_;
Casey Dahlinf4a93112015-10-05 16:58:09 -0700749 }
Casey Dahlin5c69deb2015-10-01 14:44:12 -0700750
Jooyung Han965e31d2020-11-27 12:30:16 +0900751 // ToString is for dumping AIDL.
752 // Returns string representation of this method including everything.
753 // This is "ret_type name ( arg_list ) = id".
754 // e.g) "boolean foo(int, @Nullable String) = 1"
Jiyong Park309668e2018-07-28 16:55:44 +0900755 std::string ToString() const;
756
Jooyung Han965e31d2020-11-27 12:30:16 +0900757 // Signature is for comparing AIDL types.
758 // Returns string representation of this method's name & type.
759 // e.g) "foo(int, String)"
760 std::string Signature() const;
761
Casey Dahlin5c69deb2015-10-01 14:44:12 -0700762 private:
Casey Dahlinf4a93112015-10-05 16:58:09 -0700763 bool oneway_;
Casey Dahlinf2d23f72015-10-02 16:19:19 -0700764 std::string comments_;
Jiyong Parkd59a10d2018-07-18 11:12:55 +0900765 std::unique_ptr<AidlTypeSpecifier> type_;
Casey Dahlinf4a93112015-10-05 16:58:09 -0700766 std::string name_;
Christopher Wileyad339272015-10-05 19:11:58 -0700767 const std::vector<std::unique_ptr<AidlArgument>> arguments_;
768 std::vector<const AidlArgument*> in_arguments_;
769 std::vector<const AidlArgument*> out_arguments_;
Casey Dahlinf4a93112015-10-05 16:58:09 -0700770 bool has_id_;
771 int id_;
Jiyong Park309668e2018-07-28 16:55:44 +0900772 bool is_user_defined_ = true;
Casey Dahlin0a2f8be2015-09-28 16:15:29 -0700773};
Adam Lesinskiffa16862014-01-23 18:17:42 -0800774
Steven Morelandc258abc2018-07-10 14:03:38 -0700775class AidlDefinedType;
Jiyong Parkb034bf02018-07-30 17:44:33 +0900776class AidlInterface;
777class AidlParcelable;
778class AidlStructuredParcelable;
Jooyung Han2946afc2020-10-05 20:29:16 +0900779class AidlUnionDecl;
Jooyung Han3f347ca2020-12-01 12:41:50 +0900780
Daniel Norman85aed542019-08-21 12:01:14 -0700781// AidlDefinedType represents either an interface, parcelable, or enum that is
Jiyong Park1deecc32018-07-17 01:14:41 +0900782// defined in the source file.
783class AidlDefinedType : public AidlAnnotatable {
Steven Moreland787b0432018-07-03 09:00:58 -0700784 public:
Steven Moreland46e9da82018-07-27 15:45:29 -0700785 AidlDefinedType(const AidlLocation& location, const std::string& name,
Jooyung Han829ec7c2020-12-02 12:07:36 +0900786 const std::string& comments, const std::string& package,
787 std::vector<std::unique_ptr<AidlMember>>* members);
Steven Moreland787b0432018-07-03 09:00:58 -0700788 virtual ~AidlDefinedType() = default;
789
Jiyong Parkd800fef2020-07-22 18:09:43 +0900790 // non-copyable, non-movable
791 AidlDefinedType(const AidlDefinedType&) = delete;
792 AidlDefinedType(AidlDefinedType&&) = delete;
793 AidlDefinedType& operator=(const AidlDefinedType&) = delete;
794 AidlDefinedType& operator=(AidlDefinedType&&) = delete;
795
Jiyong Park1deecc32018-07-17 01:14:41 +0900796 const std::string& GetName() const { return name_; };
Jeongik Cha997281d2020-01-16 15:23:59 +0900797 bool IsHidden() const;
Jiyong Park1deecc32018-07-17 01:14:41 +0900798 const std::string& GetComments() const { return comments_; }
Jiyong Parka6605ab2018-11-11 14:30:21 +0900799 void SetComments(const std::string comments) { comments_ = comments; }
Jiyong Park1deecc32018-07-17 01:14:41 +0900800
Steven Moreland787b0432018-07-03 09:00:58 -0700801 /* dot joined package, example: "android.package.foo" */
Jiyong Park18132182020-06-08 20:24:40 +0900802 std::string GetPackage() const { return package_; }
Steven Moreland787b0432018-07-03 09:00:58 -0700803 /* dot joined package and name, example: "android.package.foo.IBar" */
804 std::string GetCanonicalName() const;
Jiyong Park18132182020-06-08 20:24:40 +0900805 const std::vector<std::string>& GetSplitPackage() const { return split_package_; }
Steven Moreland787b0432018-07-03 09:00:58 -0700806
Steven Morelanded83a282018-07-17 13:27:29 -0700807 virtual std::string GetPreprocessDeclarationName() const = 0;
Steven Morelandc258abc2018-07-10 14:03:38 -0700808
Steven Moreland5557f1c2018-07-02 13:50:23 -0700809 virtual const AidlStructuredParcelable* AsStructuredParcelable() const { return nullptr; }
Steven Morelandc258abc2018-07-10 14:03:38 -0700810 virtual const AidlParcelable* AsParcelable() const { return nullptr; }
Daniel Norman85aed542019-08-21 12:01:14 -0700811 virtual const AidlEnumDeclaration* AsEnumDeclaration() const { return nullptr; }
Jooyung Han2946afc2020-10-05 20:29:16 +0900812 virtual const AidlUnionDecl* AsUnionDeclaration() const { return nullptr; }
Steven Moreland5557f1c2018-07-02 13:50:23 -0700813 virtual const AidlInterface* AsInterface() const { return nullptr; }
Jeongik Chadf76dc72019-11-28 00:08:47 +0900814 virtual const AidlParameterizable<std::string>* AsParameterizable() const { return nullptr; }
Jooyung Han888c5bc2020-12-22 17:28:47 +0900815 virtual bool CheckValid(const AidlTypenames& typenames, DiagnosticsContext& context) const;
Steven Morelandd59e3172020-05-11 16:42:09 -0700816 virtual bool LanguageSpecificCheckValid(const AidlTypenames& typenames,
817 Options::Language lang) const = 0;
Steven Morelandc258abc2018-07-10 14:03:38 -0700818 AidlStructuredParcelable* AsStructuredParcelable() {
819 return const_cast<AidlStructuredParcelable*>(
820 const_cast<const AidlDefinedType*>(this)->AsStructuredParcelable());
821 }
822 AidlParcelable* AsParcelable() {
823 return const_cast<AidlParcelable*>(const_cast<const AidlDefinedType*>(this)->AsParcelable());
824 }
Daniel Norman85aed542019-08-21 12:01:14 -0700825 AidlEnumDeclaration* AsEnumDeclaration() {
826 return const_cast<AidlEnumDeclaration*>(
827 const_cast<const AidlDefinedType*>(this)->AsEnumDeclaration());
828 }
Jooyung Han2946afc2020-10-05 20:29:16 +0900829 AidlUnionDecl* AsUnionDeclaration() {
830 return const_cast<AidlUnionDecl*>(
831 const_cast<const AidlDefinedType*>(this)->AsUnionDeclaration());
832 }
Steven Morelandc258abc2018-07-10 14:03:38 -0700833 AidlInterface* AsInterface() {
834 return const_cast<AidlInterface*>(const_cast<const AidlDefinedType*>(this)->AsInterface());
835 }
836
Jeongik Chadf76dc72019-11-28 00:08:47 +0900837 AidlParameterizable<std::string>* AsParameterizable() {
838 return const_cast<AidlParameterizable<std::string>*>(
839 const_cast<const AidlDefinedType*>(this)->AsParameterizable());
840 }
841
Steven Moreland6cee3482018-07-18 14:39:58 -0700842 const AidlParcelable* AsUnstructuredParcelable() const {
843 if (this->AsStructuredParcelable() != nullptr) return nullptr;
Jooyung Han2946afc2020-10-05 20:29:16 +0900844 if (this->AsUnionDeclaration() != nullptr) return nullptr;
Steven Moreland6cee3482018-07-18 14:39:58 -0700845 return this->AsParcelable();
846 }
847 AidlParcelable* AsUnstructuredParcelable() {
848 return const_cast<AidlParcelable*>(
849 const_cast<const AidlDefinedType*>(this)->AsUnstructuredParcelable());
850 }
851
Jeongik Cha997281d2020-01-16 15:23:59 +0900852 virtual void Dump(CodeWriter* writer) const = 0;
Steven Morelanda5d9c5c2020-02-21 16:01:09 -0800853 void DumpHeader(CodeWriter* writer) const;
Jiyong Park02da7422018-07-16 16:00:26 +0900854
Jooyung Han829ec7c2020-12-02 12:07:36 +0900855 const std::vector<std::unique_ptr<AidlVariableDeclaration>>& GetFields() const {
856 return variables_;
857 }
858 const std::vector<std::unique_ptr<AidlConstantDeclaration>>& GetConstantDeclarations() const {
859 return constants_;
860 }
861 const std::vector<std::unique_ptr<AidlMethod>>& GetMethods() const { return methods_; }
862 void AddMethod(std::unique_ptr<AidlMethod> method) { methods_.push_back(std::move(method)); }
863 const std::vector<const AidlMember*>& GetMembers() const { return members_; }
864
865 protected:
866 // utility for subclasses with getter names
867 bool CheckValidForGetterNames() const;
868
Steven Moreland787b0432018-07-03 09:00:58 -0700869 private:
Jooyung Han829ec7c2020-12-02 12:07:36 +0900870 bool CheckValidWithMembers(const AidlTypenames& typenames) const;
871
Jiyong Park1deecc32018-07-17 01:14:41 +0900872 std::string name_;
Jiyong Park1deecc32018-07-17 01:14:41 +0900873 std::string comments_;
Jiyong Park18132182020-06-08 20:24:40 +0900874 const std::string package_;
875 const std::vector<std::string> split_package_;
Jooyung Han829ec7c2020-12-02 12:07:36 +0900876 std::vector<std::unique_ptr<AidlVariableDeclaration>> variables_;
877 std::vector<std::unique_ptr<AidlConstantDeclaration>> constants_;
878 std::vector<std::unique_ptr<AidlMethod>> methods_;
879 std::vector<const AidlMember*> members_; // keep members in order of appearance.
Steven Moreland787b0432018-07-03 09:00:58 -0700880};
881
Jeongik Chadf76dc72019-11-28 00:08:47 +0900882class AidlParcelable : public AidlDefinedType, public AidlParameterizable<std::string> {
Casey Dahlin1ae2bc52015-10-07 18:49:10 -0700883 public:
Jiyong Park18132182020-06-08 20:24:40 +0900884 AidlParcelable(const AidlLocation& location, const std::string& name, const std::string& package,
885 const std::string& comments, const std::string& cpp_header = "",
Jooyung Han829ec7c2020-12-02 12:07:36 +0900886 std::vector<std::string>* type_params = nullptr,
887 std::vector<std::unique_ptr<AidlMember>>* members = nullptr);
Casey Dahlin1ae2bc52015-10-07 18:49:10 -0700888 virtual ~AidlParcelable() = default;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800889
Jiyong Parkd800fef2020-07-22 18:09:43 +0900890 // non-copyable, non-movable
891 AidlParcelable(const AidlParcelable&) = delete;
892 AidlParcelable(AidlParcelable&&) = delete;
893 AidlParcelable& operator=(const AidlParcelable&) = delete;
894 AidlParcelable& operator=(AidlParcelable&&) = delete;
895
Christopher Wiley8aa4d9f2015-11-16 19:10:45 -0800896 std::string GetCppHeader() const { return cpp_header_; }
Christopher Wiley8aa4d9f2015-11-16 19:10:45 -0800897
Steven Moreland0cea4aa2020-04-20 21:06:02 -0700898 std::set<AidlAnnotation::Type> GetSupportedAnnotations() const override;
Jooyung Han888c5bc2020-12-22 17:28:47 +0900899 bool CheckValid(const AidlTypenames& typenames, DiagnosticsContext& context) const override;
Steven Morelandd59e3172020-05-11 16:42:09 -0700900 bool LanguageSpecificCheckValid(const AidlTypenames& typenames,
901 Options::Language lang) const override;
Steven Morelandc258abc2018-07-10 14:03:38 -0700902 const AidlParcelable* AsParcelable() const override { return this; }
Jeongik Chadf76dc72019-11-28 00:08:47 +0900903 const AidlParameterizable<std::string>* AsParameterizable() const override { return this; }
904 const AidlNode& AsAidlNode() const override { return *this; }
Steven Morelanded83a282018-07-17 13:27:29 -0700905 std::string GetPreprocessDeclarationName() const override { return "parcelable"; }
Steven Morelandc258abc2018-07-10 14:03:38 -0700906
Jeongik Cha997281d2020-01-16 15:23:59 +0900907 void Dump(CodeWriter* writer) const override;
Jiyong Park02da7422018-07-16 16:00:26 +0900908
Casey Dahlin1ae2bc52015-10-07 18:49:10 -0700909 private:
Christopher Wiley8aa4d9f2015-11-16 19:10:45 -0800910 std::string cpp_header_;
Casey Dahlin0a2f8be2015-09-28 16:15:29 -0700911};
Adam Lesinskiffa16862014-01-23 18:17:42 -0800912
Jooyung Han829ec7c2020-12-02 12:07:36 +0900913class AidlStructuredParcelable : public AidlParcelable {
Steven Moreland5557f1c2018-07-02 13:50:23 -0700914 public:
Jiyong Park18132182020-06-08 20:24:40 +0900915 AidlStructuredParcelable(const AidlLocation& location, const std::string& name,
916 const std::string& package, const std::string& comments,
Jooyung Han829ec7c2020-12-02 12:07:36 +0900917 std::vector<std::string>* type_params,
918 std::vector<std::unique_ptr<AidlMember>>* members);
Jiyong Parkd800fef2020-07-22 18:09:43 +0900919 virtual ~AidlStructuredParcelable() = default;
920
921 // non-copyable, non-movable
922 AidlStructuredParcelable(const AidlStructuredParcelable&) = delete;
923 AidlStructuredParcelable(AidlStructuredParcelable&&) = delete;
924 AidlStructuredParcelable& operator=(const AidlStructuredParcelable&) = delete;
925 AidlStructuredParcelable& operator=(AidlStructuredParcelable&&) = delete;
Steven Moreland5557f1c2018-07-02 13:50:23 -0700926
Steven Morelandc258abc2018-07-10 14:03:38 -0700927 const AidlStructuredParcelable* AsStructuredParcelable() const override { return this; }
Steven Morelanded83a282018-07-17 13:27:29 -0700928 std::string GetPreprocessDeclarationName() const override { return "structured_parcelable"; }
Steven Moreland5557f1c2018-07-02 13:50:23 -0700929
Jeongik Cha997281d2020-01-16 15:23:59 +0900930 void Dump(CodeWriter* writer) const override;
Jiyong Park02da7422018-07-16 16:00:26 +0900931
Steven Moreland0cea4aa2020-04-20 21:06:02 -0700932 std::set<AidlAnnotation::Type> GetSupportedAnnotations() const override;
Jooyung Han888c5bc2020-12-22 17:28:47 +0900933 bool CheckValid(const AidlTypenames& typenames, DiagnosticsContext& context) const override;
Steven Morelandd59e3172020-05-11 16:42:09 -0700934 bool LanguageSpecificCheckValid(const AidlTypenames& typenames,
935 Options::Language lang) const override;
Steven Moreland5557f1c2018-07-02 13:50:23 -0700936};
937
Daniel Norman85aed542019-08-21 12:01:14 -0700938class AidlEnumerator : public AidlNode {
939 public:
Daniel Norman2e4112d2019-10-03 10:22:35 -0700940 AidlEnumerator(const AidlLocation& location, const std::string& name, AidlConstantValue* value,
941 const std::string& comments);
Daniel Norman85aed542019-08-21 12:01:14 -0700942 virtual ~AidlEnumerator() = default;
943
Jiyong Parkd800fef2020-07-22 18:09:43 +0900944 // non-copyable, non-movable
945 AidlEnumerator(const AidlEnumerator&) = delete;
946 AidlEnumerator(AidlEnumerator&&) = delete;
947 AidlEnumerator& operator=(const AidlEnumerator&) = delete;
948 AidlEnumerator& operator=(AidlEnumerator&&) = delete;
949
Daniel Norman85aed542019-08-21 12:01:14 -0700950 const std::string& GetName() const { return name_; }
Will McVickerd7d18df2019-09-12 13:40:50 -0700951 AidlConstantValue* GetValue() const { return value_.get(); }
Daniel Norman2e4112d2019-10-03 10:22:35 -0700952 const std::string& GetComments() const { return comments_; }
Daniel Norman85aed542019-08-21 12:01:14 -0700953 bool CheckValid(const AidlTypeSpecifier& enum_backing_type) const;
954
955 string ValueString(const AidlTypeSpecifier& backing_type,
956 const ConstantValueDecorator& decorator) const;
957
Daniel Normanb28684e2019-10-17 15:31:39 -0700958 void SetValue(std::unique_ptr<AidlConstantValue> value) { value_ = std::move(value); }
Jooyung Han29813842020-12-08 01:28:03 +0900959 bool IsValueUserSpecified() const { return value_user_specified_; }
Daniel Normanb28684e2019-10-17 15:31:39 -0700960
Daniel Norman85aed542019-08-21 12:01:14 -0700961 private:
962 const std::string name_;
Will McVickerd7d18df2019-09-12 13:40:50 -0700963 unique_ptr<AidlConstantValue> value_;
Daniel Norman2e4112d2019-10-03 10:22:35 -0700964 const std::string comments_;
Jooyung Han29813842020-12-08 01:28:03 +0900965 const bool value_user_specified_;
Daniel Norman85aed542019-08-21 12:01:14 -0700966};
967
968class AidlEnumDeclaration : public AidlDefinedType {
969 public:
Will McVickerd7d18df2019-09-12 13:40:50 -0700970 AidlEnumDeclaration(const AidlLocation& location, const string& name,
Daniel Norman85aed542019-08-21 12:01:14 -0700971 std::vector<std::unique_ptr<AidlEnumerator>>* enumerators,
Jiyong Park18132182020-06-08 20:24:40 +0900972 const std::string& package, const std::string& comments);
Daniel Norman85aed542019-08-21 12:01:14 -0700973 virtual ~AidlEnumDeclaration() = default;
974
Jiyong Parkd800fef2020-07-22 18:09:43 +0900975 // non-copyable, non-movable
976 AidlEnumDeclaration(const AidlEnumDeclaration&) = delete;
977 AidlEnumDeclaration(AidlEnumDeclaration&&) = delete;
978 AidlEnumDeclaration& operator=(const AidlEnumDeclaration&) = delete;
979 AidlEnumDeclaration& operator=(AidlEnumDeclaration&&) = delete;
980
Jooyung Han672557b2020-12-24 05:18:00 +0900981 bool Autofill(const AidlTypenames&);
Daniel Norman85aed542019-08-21 12:01:14 -0700982 const AidlTypeSpecifier& GetBackingType() const { return *backing_type_; }
983 const std::vector<std::unique_ptr<AidlEnumerator>>& GetEnumerators() const {
984 return enumerators_;
985 }
Steven Moreland0cea4aa2020-04-20 21:06:02 -0700986 std::set<AidlAnnotation::Type> GetSupportedAnnotations() const override;
Jooyung Han888c5bc2020-12-22 17:28:47 +0900987 bool CheckValid(const AidlTypenames& typenames, DiagnosticsContext& context) const override;
Steven Morelandd59e3172020-05-11 16:42:09 -0700988 bool LanguageSpecificCheckValid(const AidlTypenames& /*typenames*/,
989 Options::Language) const override {
990 return true;
991 }
Daniel Norman85aed542019-08-21 12:01:14 -0700992 std::string GetPreprocessDeclarationName() const override { return "enum"; }
Jeongik Cha997281d2020-01-16 15:23:59 +0900993 void Dump(CodeWriter* writer) const override;
Daniel Norman85aed542019-08-21 12:01:14 -0700994
995 const AidlEnumDeclaration* AsEnumDeclaration() const override { return this; }
996
997 private:
Jooyung Han29813842020-12-08 01:28:03 +0900998
Daniel Norman85aed542019-08-21 12:01:14 -0700999 const std::string name_;
1000 const std::vector<std::unique_ptr<AidlEnumerator>> enumerators_;
Jooyung Han672557b2020-12-24 05:18:00 +09001001 std::unique_ptr<AidlTypeSpecifier> backing_type_;
Daniel Norman85aed542019-08-21 12:01:14 -07001002};
1003
Jooyung Han829ec7c2020-12-02 12:07:36 +09001004class AidlUnionDecl : public AidlParcelable {
Jooyung Han2946afc2020-10-05 20:29:16 +09001005 public:
1006 AidlUnionDecl(const AidlLocation& location, const std::string& name, const std::string& package,
Jooyung Han829ec7c2020-12-02 12:07:36 +09001007 const std::string& comments, std::vector<std::string>* type_params,
1008 std::vector<std::unique_ptr<AidlMember>>* members);
Jooyung Han2946afc2020-10-05 20:29:16 +09001009 virtual ~AidlUnionDecl() = default;
1010
1011 // non-copyable, non-movable
1012 AidlUnionDecl(const AidlUnionDecl&) = delete;
1013 AidlUnionDecl(AidlUnionDecl&&) = delete;
1014 AidlUnionDecl& operator=(const AidlUnionDecl&) = delete;
1015 AidlUnionDecl& operator=(AidlUnionDecl&&) = delete;
1016
1017 std::set<AidlAnnotation::Type> GetSupportedAnnotations() const override;
1018
1019 const AidlNode& AsAidlNode() const override { return *this; }
1020
Jooyung Han888c5bc2020-12-22 17:28:47 +09001021 bool CheckValid(const AidlTypenames& typenames, DiagnosticsContext& context) const override;
Jooyung Hanfe89f122020-10-14 03:49:18 +09001022 bool LanguageSpecificCheckValid(const AidlTypenames& typenames,
1023 Options::Language lang) const override;
Jooyung Han2946afc2020-10-05 20:29:16 +09001024 std::string GetPreprocessDeclarationName() const override { return "union"; }
1025
1026 void Dump(CodeWriter* writer) const override;
1027 const AidlUnionDecl* AsUnionDeclaration() const override { return this; }
Jooyung Han2946afc2020-10-05 20:29:16 +09001028};
1029
Jiyong Park1deecc32018-07-17 01:14:41 +09001030class AidlInterface final : public AidlDefinedType {
Casey Dahlin1ae2bc52015-10-07 18:49:10 -07001031 public:
Steven Moreland46e9da82018-07-27 15:45:29 -07001032 AidlInterface(const AidlLocation& location, const std::string& name, const std::string& comments,
Jooyung Han829ec7c2020-12-02 12:07:36 +09001033 bool oneway_, const std::string& package,
1034 std::vector<std::unique_ptr<AidlMember>>* members);
Casey Dahlin1ae2bc52015-10-07 18:49:10 -07001035 virtual ~AidlInterface() = default;
1036
Jiyong Parkd800fef2020-07-22 18:09:43 +09001037 // non-copyable, non-movable
1038 AidlInterface(const AidlInterface&) = delete;
1039 AidlInterface(AidlInterface&&) = delete;
1040 AidlInterface& operator=(const AidlInterface&) = delete;
1041 AidlInterface& operator=(AidlInterface&&) = delete;
1042
Steven Morelandc258abc2018-07-10 14:03:38 -07001043 const AidlInterface* AsInterface() const override { return this; }
Steven Morelanded83a282018-07-17 13:27:29 -07001044 std::string GetPreprocessDeclarationName() const override { return "interface"; }
Steven Moreland5557f1c2018-07-02 13:50:23 -07001045
Jeongik Cha997281d2020-01-16 15:23:59 +09001046 void Dump(CodeWriter* writer) const override;
Jiyong Park02da7422018-07-16 16:00:26 +09001047
Steven Moreland0cea4aa2020-04-20 21:06:02 -07001048 std::set<AidlAnnotation::Type> GetSupportedAnnotations() const override;
Jooyung Han888c5bc2020-12-22 17:28:47 +09001049 bool CheckValid(const AidlTypenames& typenames, DiagnosticsContext& context) const override;
Steven Morelandd59e3172020-05-11 16:42:09 -07001050 bool LanguageSpecificCheckValid(const AidlTypenames& typenames,
1051 Options::Language lang) const override;
Jeongik Chadb0f59e2018-11-01 18:11:21 +09001052
Jiyong Park27fd7fd2020-08-27 16:25:09 +09001053 std::string GetDescriptor() const;
Casey Dahlin0a2f8be2015-09-28 16:15:29 -07001054};
Adam Lesinskiffa16862014-01-23 18:17:42 -08001055
Casey Dahlin0edf3422015-10-07 12:34:59 -07001056class AidlImport : public AidlNode {
1057 public:
Steven Moreland46e9da82018-07-27 15:45:29 -07001058 AidlImport(const AidlLocation& location, const std::string& needed_class);
Casey Dahlin0edf3422015-10-07 12:34:59 -07001059 virtual ~AidlImport() = default;
1060
Jiyong Parkd800fef2020-07-22 18:09:43 +09001061 // non-copyable, non-movable
1062 AidlImport(const AidlImport&) = delete;
1063 AidlImport(AidlImport&&) = delete;
1064 AidlImport& operator=(const AidlImport&) = delete;
1065 AidlImport& operator=(AidlImport&&) = delete;
1066
Casey Dahlin0edf3422015-10-07 12:34:59 -07001067 const std::string& GetNeededClass() const { return needed_class_; }
Casey Dahlin0edf3422015-10-07 12:34:59 -07001068
1069 private:
Casey Dahlin0edf3422015-10-07 12:34:59 -07001070 std::string needed_class_;
Casey Dahline2507492015-09-14 17:11:20 -07001071};
1072
Jiyong Park62515512020-06-08 15:57:11 +09001073// AidlDocument models an AIDL file
1074class AidlDocument : public AidlNode {
1075 public:
1076 AidlDocument(const AidlLocation& location, std::vector<std::unique_ptr<AidlImport>>& imports,
Jiyong Park8e79b7f2020-07-20 20:52:38 +09001077 std::vector<std::unique_ptr<AidlDefinedType>>&& defined_types)
1078 : AidlNode(location),
1079 imports_(std::move(imports)),
1080 defined_types_(std::move(defined_types)) {}
Jiyong Parkd800fef2020-07-22 18:09:43 +09001081 ~AidlDocument() = default;
1082
1083 // non-copyable, non-movable
Jiyong Park8e79b7f2020-07-20 20:52:38 +09001084 AidlDocument(const AidlDocument&) = delete;
1085 AidlDocument(AidlDocument&&) = delete;
1086 AidlDocument& operator=(const AidlDocument&) = delete;
1087 AidlDocument& operator=(AidlDocument&&) = delete;
Jiyong Parkd800fef2020-07-22 18:09:43 +09001088
Jooyung Han3557e482020-12-24 05:11:15 +09001089 bool CheckValid(const AidlTypenames& typenames, DiagnosticsContext& diag) const;
Jooyung Han29813842020-12-08 01:28:03 +09001090 std::optional<std::string> ResolveName(const std::string& unresolved_type) const;
Jiyong Parkd800fef2020-07-22 18:09:43 +09001091 const std::vector<std::unique_ptr<AidlImport>>& Imports() const { return imports_; }
1092 const std::vector<std::unique_ptr<AidlDefinedType>>& DefinedTypes() const {
1093 return defined_types_;
1094 }
Jiyong Park62515512020-06-08 15:57:11 +09001095
1096 private:
1097 const std::vector<std::unique_ptr<AidlImport>> imports_;
Jiyong Park8e79b7f2020-07-20 20:52:38 +09001098 const std::vector<std::unique_ptr<AidlDefinedType>> defined_types_;
Jiyong Park62515512020-06-08 15:57:11 +09001099};
Jooyung Hanb3c77ed2020-12-26 02:02:45 +09001100
1101template <typename T>
1102std::optional<T> AidlAnnotation::ParamValue(const std::string& param_name) const {
1103 auto it = parameters_.find(param_name);
1104 if (it == parameters_.end()) {
1105 return std::nullopt;
1106 }
1107 return it->second->Cast<T>();
1108}