blob: 25709e2a789e6cea5d3164ba0675145e03e54b7c [file] [log] [blame]
Will McVickerd7d18df2019-09-12 13:40:50 -07001/*
2 * Copyright (C) 2019, The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Steven Moreland9fccf582018-08-27 20:36:27 -070017#pragma once
Christopher Wileyec31a052016-01-25 07:28:51 -080018
Jiyong Park1deecc32018-07-17 01:14:41 +090019#include "aidl_typenames.h"
Jiyong Park02da7422018-07-16 16:00:26 +090020#include "code_writer.h"
Jiyong Park1deecc32018-07-17 01:14:41 +090021#include "io_delegate.h"
Jeongik Cha047c5ee2019-08-07 23:16:49 +090022#include "options.h"
Jiyong Park1deecc32018-07-17 01:14:41 +090023
Casey Dahlinbc7a50a2015-09-28 19:20:50 -070024#include <memory>
Jeongik Cha997281d2020-01-16 15:23:59 +090025#include <regex>
Casey Dahlindd691812015-09-09 17:59:06 -070026#include <string>
Jeongik Chadf76dc72019-11-28 00:08:47 +090027#include <unordered_set>
Casey Dahlinbc7a50a2015-09-28 19:20:50 -070028#include <vector>
Casey Dahlindd691812015-09-09 17:59:06 -070029
Elliott Hughes0a620672015-12-04 13:53:18 -080030#include <android-base/strings.h>
Casey Dahlin73d46b02015-09-11 02:47:54 +000031
Jiyong Parkb034bf02018-07-30 17:44:33 +090032using android::aidl::AidlTypenames;
Jiyong Park02da7422018-07-16 16:00:26 +090033using android::aidl::CodeWriter;
Jeongik Cha047c5ee2019-08-07 23:16:49 +090034using android::aidl::Options;
Steven Moreland3f658cf2018-08-20 13:40:54 -070035using std::shared_ptr;
Jiyong Park1deecc32018-07-17 01:14:41 +090036using std::string;
37using std::unique_ptr;
38using std::vector;
Andrei Onea8714b022019-02-01 18:55:54 +000039class AidlNode;
40
41namespace android {
42namespace aidl {
43namespace mappings {
44std::string dump_location(const AidlNode& method);
45} // namespace mappings
Mathew Inwoodadb74672019-11-29 14:01:53 +000046namespace java {
47std::string dump_location(const AidlNode& method);
48} // namespace java
Andrei Onea8714b022019-02-01 18:55:54 +000049} // namespace aidl
50} // namespace android
51
Steven Moreland46e9da82018-07-27 15:45:29 -070052class AidlLocation {
Casey Dahlinbc7a50a2015-09-28 19:20:50 -070053 public:
Steven Moreland46e9da82018-07-27 15:45:29 -070054 struct Point {
Dan Willemsen609ba6d2019-12-30 10:44:00 -080055 int line;
56 int column;
Steven Moreland46e9da82018-07-27 15:45:29 -070057 };
58
Devin Mooredf93ebb2020-03-25 14:03:35 -070059 enum class Source {
60 // From internal aidl source code
61 INTERNAL = 0,
62 // From a parsed file
63 EXTERNAL = 1
64 };
65
66 AidlLocation(const std::string& file, Point begin, Point end, Source source);
Devin Moore5de18ed2020-04-02 13:52:29 -070067 AidlLocation(const std::string& file, Source source)
68 : AidlLocation(file, {0, 0}, {0, 0}, source) {}
Devin Mooredf93ebb2020-03-25 14:03:35 -070069
70 bool IsInternal() const { return source_ == Source::INTERNAL; }
Steven Moreland46e9da82018-07-27 15:45:29 -070071
Devin Moore5de18ed2020-04-02 13:52:29 -070072 // The first line of a file is line 1.
73 bool LocationKnown() const { return begin_.line != 0; }
74
Steven Moreland46e9da82018-07-27 15:45:29 -070075 friend std::ostream& operator<<(std::ostream& os, const AidlLocation& l);
Andrei Onea8714b022019-02-01 18:55:54 +000076 friend class AidlNode;
Casey Dahlinbc7a50a2015-09-28 19:20:50 -070077
78 private:
Steven Moreland541788d2020-05-21 22:05:52 +000079 // INTENTIONALLY HIDDEN: only operator<< should access details here.
80 // Otherwise, locations should only ever be copied around to construct new
81 // objects.
Steven Moreland46e9da82018-07-27 15:45:29 -070082 const std::string file_;
83 Point begin_;
84 Point end_;
Devin Mooredf93ebb2020-03-25 14:03:35 -070085 Source source_;
Steven Moreland46e9da82018-07-27 15:45:29 -070086};
87
Devin Mooredf93ebb2020-03-25 14:03:35 -070088#define AIDL_LOCATION_HERE \
Steven Moreland21780812020-09-11 01:29:45 +000089 (AidlLocation{__FILE__, {__LINE__, 0}, {__LINE__, 0}, AidlLocation::Source::INTERNAL})
Steven Moreland02e012e2018-08-02 14:58:10 -070090
Steven Moreland46e9da82018-07-27 15:45:29 -070091std::ostream& operator<<(std::ostream& os, const AidlLocation& l);
92
93// Anything that is locatable in a .aidl file.
94class AidlNode {
95 public:
96 AidlNode(const AidlLocation& location);
Steven Moreland3f658cf2018-08-20 13:40:54 -070097
98 AidlNode(const AidlNode&) = default;
Steven Moreland46e9da82018-07-27 15:45:29 -070099 virtual ~AidlNode() = default;
100
Jiyong Parkd800fef2020-07-22 18:09:43 +0900101 AidlNode(AidlNode&&) = delete;
102 AidlNode& operator=(AidlNode&&) = delete;
103
Devin Mooredf93ebb2020-03-25 14:03:35 -0700104 // To be able to print AidlLocation
Steven Morelandb0d15a52020-03-31 14:03:47 -0700105 friend class AidlErrorLog;
Andrei Onea8714b022019-02-01 18:55:54 +0000106 friend std::string android::aidl::mappings::dump_location(const AidlNode&);
Mathew Inwoodadb74672019-11-29 14:01:53 +0000107 friend std::string android::aidl::java::dump_location(const AidlNode&);
Steven Moreland46e9da82018-07-27 15:45:29 -0700108
Devin Mooredf93ebb2020-03-25 14:03:35 -0700109 const AidlLocation& GetLocation() const { return location_; }
110
Steven Moreland46e9da82018-07-27 15:45:29 -0700111 private:
Mathew Inwoodadb74672019-11-29 14:01:53 +0000112 std::string PrintLine() const;
Andrei Onea8714b022019-02-01 18:55:54 +0000113 std::string PrintLocation() const;
Steven Moreland46e9da82018-07-27 15:45:29 -0700114 const AidlLocation location_;
Casey Dahlinbc7a50a2015-09-28 19:20:50 -0700115};
116
Casey Dahlina2f77c42015-12-01 18:26:02 -0800117namespace android {
118namespace aidl {
119
Jiyong Park1deecc32018-07-17 01:14:41 +0900120class AidlTypenames;
Casey Dahlina2f77c42015-12-01 18:26:02 -0800121
122} // namespace aidl
123} // namespace android
124
Jeongik Chadf76dc72019-11-28 00:08:47 +0900125// unique_ptr<AidlTypeSpecifier> for type arugment,
126// std::string for type parameter(T, U, and so on).
127template <typename T>
128class AidlParameterizable {
129 public:
130 AidlParameterizable(std::vector<T>* type_params) : type_params_(type_params) {}
131 virtual ~AidlParameterizable() = default;
132 bool IsGeneric() const { return type_params_ != nullptr; }
133 const std::vector<T>& GetTypeParameters() const { return *type_params_; }
134 bool CheckValid() const;
135
Steven Moreland6c07b832020-10-29 23:39:53 +0000136 __attribute__((warn_unused_result)) bool SetTypeParameters(std::vector<T>* type_params) {
137 if (type_params_) return false;
138 type_params_.reset(type_params);
139 return true;
140 }
141
Jeongik Chadf76dc72019-11-28 00:08:47 +0900142 virtual const AidlNode& AsAidlNode() const = 0;
143
144 protected:
145 AidlParameterizable(const AidlParameterizable&);
146
147 private:
Steven Moreland6c07b832020-10-29 23:39:53 +0000148 unique_ptr<std::vector<T>> type_params_;
Jeongik Chadf76dc72019-11-28 00:08:47 +0900149 static_assert(std::is_same<T, unique_ptr<AidlTypeSpecifier>>::value ||
150 std::is_same<T, std::string>::value);
151};
152template <>
153bool AidlParameterizable<std::string>::CheckValid() const;
154
Andrei Onea9445fc62019-06-27 18:11:59 +0100155class AidlConstantValue;
156class AidlConstantDeclaration;
157
158// Transforms a value string into a language specific form. Raw value as produced by
159// AidlConstantValue.
160using ConstantValueDecorator =
161 std::function<std::string(const AidlTypeSpecifier& type, const std::string& raw_value)>;
162
Jiyong Park68bc77a2018-07-19 19:00:45 +0900163class AidlAnnotation : public AidlNode {
164 public:
Steven Moreland0cea4aa2020-04-20 21:06:02 -0700165 enum class Type {
166 BACKING = 1,
167 HIDE,
168 JAVA_STABLE_PARCELABLE,
169 UNSUPPORTED_APP_USAGE,
170 VINTF_STABILITY,
171 NULLABLE,
172 UTF8_IN_CPP,
Steven Morelanda7764e52020-10-27 17:29:29 +0000173 SENSITIVE_DATA,
Jiyong Parkbf5fd5c2020-06-05 19:48:05 +0900174 JAVA_PASSTHROUGH,
Jooyung Han90345002020-10-23 15:28:53 +0900175 JAVA_DERIVE,
Jeongik Chad0a10272020-08-06 16:33:36 +0900176 JAVA_ONLY_IMMUTABLE,
Devin Moorec7e47a32020-08-07 10:55:25 -0700177 FIXED_SIZE,
Jiyong Park27fd7fd2020-08-27 16:25:09 +0900178 DESCRIPTOR,
Andrei Homescue61feb52020-08-18 15:44:24 -0700179 RUST_DERIVE,
Steven Moreland0cea4aa2020-04-20 21:06:02 -0700180 };
181 static std::string TypeToString(Type type);
182
Andrei Onea9445fc62019-06-27 18:11:59 +0100183 static AidlAnnotation* Parse(
184 const AidlLocation& location, const string& name,
185 std::map<std::string, std::shared_ptr<AidlConstantValue>>* parameter_list);
Steven Moreland46e9da82018-07-27 15:45:29 -0700186
Steven Moreland3f658cf2018-08-20 13:40:54 -0700187 AidlAnnotation(const AidlAnnotation&) = default;
Steven Moreland3be75772018-08-20 13:27:43 -0700188 AidlAnnotation(AidlAnnotation&&) = default;
Jiyong Park68bc77a2018-07-19 19:00:45 +0900189 virtual ~AidlAnnotation() = default;
Andrei Onea9445fc62019-06-27 18:11:59 +0100190 bool CheckValid() const;
Steven Moreland3be75772018-08-20 13:27:43 -0700191
Jooyung Hand902a972020-10-23 17:32:44 +0900192 const string& GetName() const { return schema_.name; }
Steven Moreland0cea4aa2020-04-20 21:06:02 -0700193 const Type& GetType() const { return schema_.type; }
Jooyung Hand902a972020-10-23 17:32:44 +0900194 bool Repeatable() const { return schema_.repeatable; }
Daniel Norman37d43dd2019-09-09 17:22:34 -0700195 string ToString(const ConstantValueDecorator& decorator) const;
Andrei Onea9445fc62019-06-27 18:11:59 +0100196 std::map<std::string, std::string> AnnotationParams(
197 const ConstantValueDecorator& decorator) const;
Jiyong Parka6605ab2018-11-11 14:30:21 +0900198 const string& GetComments() const { return comments_; }
199 void SetComments(const string& comments) { comments_ = comments; }
Jiyong Park68bc77a2018-07-19 19:00:45 +0900200
201 private:
Steven Moreland0cea4aa2020-04-20 21:06:02 -0700202 struct Schema {
203 AidlAnnotation::Type type;
204
205 // text name in .aidl file, e.g. "nullable"
206 std::string name;
207
208 // map from param name -> value type
209 std::map<std::string, std::string> supported_parameters;
Jooyung Hand902a972020-10-23 17:32:44 +0900210
211 bool repeatable;
Steven Moreland0cea4aa2020-04-20 21:06:02 -0700212 };
213 static const std::vector<Schema>& AllSchemas();
214
215 AidlAnnotation(const AidlLocation& location, const Schema& schema,
Andrei Onea9445fc62019-06-27 18:11:59 +0100216 std::map<std::string, std::shared_ptr<AidlConstantValue>>&& parameters);
Steven Moreland0cea4aa2020-04-20 21:06:02 -0700217
218 const Schema& schema_;
Jiyong Parka6605ab2018-11-11 14:30:21 +0900219 string comments_;
Andrei Onea9445fc62019-06-27 18:11:59 +0100220 std::map<std::string, std::shared_ptr<AidlConstantValue>> parameters_;
Jiyong Park68bc77a2018-07-19 19:00:45 +0900221};
222
Steven Moreland3be75772018-08-20 13:27:43 -0700223static inline bool operator<(const AidlAnnotation& lhs, const AidlAnnotation& rhs) {
224 return lhs.GetName() < rhs.GetName();
225}
226static inline bool operator==(const AidlAnnotation& lhs, const AidlAnnotation& rhs) {
227 return lhs.GetName() == rhs.GetName();
228}
Jiyong Park3656c3c2018-08-01 20:02:01 +0900229
Casey Dahline7922932016-02-29 17:23:01 -0800230class AidlAnnotatable : public AidlNode {
Casey Dahlin0ee37582015-09-30 16:31:55 -0700231 public:
Steven Moreland46e9da82018-07-27 15:45:29 -0700232 AidlAnnotatable(const AidlLocation& location);
Steven Moreland3f658cf2018-08-20 13:40:54 -0700233
234 AidlAnnotatable(const AidlAnnotatable&) = default;
235 AidlAnnotatable(AidlAnnotatable&&) = default;
Casey Dahline7922932016-02-29 17:23:01 -0800236 virtual ~AidlAnnotatable() = default;
237
Artur Satayev91fe8712019-07-29 13:06:01 +0100238 void Annotate(vector<AidlAnnotation>&& annotations) {
239 for (auto& annotation : annotations) {
240 annotations_.emplace_back(std::move(annotation));
241 }
242 }
Jiyong Park68bc77a2018-07-19 19:00:45 +0900243 bool IsNullable() const;
Jiyong Park68bc77a2018-07-19 19:00:45 +0900244 bool IsUtf8InCpp() const;
Steven Morelanda7764e52020-10-27 17:29:29 +0000245 bool IsSensitiveData() const;
Steven Morelanda57d0a62019-07-30 09:41:14 -0700246 bool IsVintfStability() const;
Jeongik Chad0a10272020-08-06 16:33:36 +0900247 bool IsJavaOnlyImmutable() const;
Devin Moorec7e47a32020-08-07 10:55:25 -0700248 bool IsFixedSize() const;
Jeongik Cha88f95a82020-01-15 13:02:16 +0900249 bool IsStableApiParcelable(Options::Language lang) const;
Makoto Onuki78a1c1c2020-03-04 16:57:23 -0800250 bool IsHide() const;
Jooyung Han90345002020-10-23 15:28:53 +0900251 const AidlAnnotation* JavaDerive() const;
Jiyong Park27fd7fd2020-08-27 16:25:09 +0900252 std::string GetDescriptor() const;
Andrei Onea9445fc62019-06-27 18:11:59 +0100253
Steven Moreland7e4b9502020-02-20 18:10:42 -0800254 void DumpAnnotations(CodeWriter* writer) const;
255
Andrei Onea9445fc62019-06-27 18:11:59 +0100256 const AidlAnnotation* UnsupportedAppUsage() const;
Andrei Homescue61feb52020-08-18 15:44:24 -0700257 const AidlAnnotation* RustDerive() const;
Daniel Norman716d3112019-09-10 13:11:56 -0700258 const AidlTypeSpecifier* BackingType(const AidlTypenames& typenames) const;
Jiyong Park68bc77a2018-07-19 19:00:45 +0900259 std::string ToString() const;
Casey Dahline7922932016-02-29 17:23:01 -0800260
Jiyong Parka6605ab2018-11-11 14:30:21 +0900261 const vector<AidlAnnotation>& GetAnnotations() const { return annotations_; }
Devin Moore24f68572020-02-26 13:20:59 -0800262 virtual bool CheckValid(const AidlTypenames&) const;
Jiyong Park3656c3c2018-08-01 20:02:01 +0900263
Steven Moreland181144c2020-04-20 19:57:56 -0700264 protected:
Steven Moreland0cea4aa2020-04-20 21:06:02 -0700265 virtual std::set<AidlAnnotation::Type> GetSupportedAnnotations() const = 0;
Steven Moreland181144c2020-04-20 19:57:56 -0700266
Casey Dahline7922932016-02-29 17:23:01 -0800267 private:
Jiyong Parka6605ab2018-11-11 14:30:21 +0900268 vector<AidlAnnotation> annotations_;
Casey Dahline7922932016-02-29 17:23:01 -0800269};
270
Jiyong Park1deecc32018-07-17 01:14:41 +0900271// AidlTypeSpecifier represents a reference to either a built-in type,
272// a defined type, or a variant (e.g., array of generic) of a type.
Jeongik Chadf76dc72019-11-28 00:08:47 +0900273class AidlTypeSpecifier final : public AidlAnnotatable,
274 public AidlParameterizable<unique_ptr<AidlTypeSpecifier>> {
Casey Dahline7922932016-02-29 17:23:01 -0800275 public:
Steven Moreland46e9da82018-07-27 15:45:29 -0700276 AidlTypeSpecifier(const AidlLocation& location, const string& unresolved_name, bool is_array,
277 vector<unique_ptr<AidlTypeSpecifier>>* type_params, const string& comments);
Jiyong Parkd59a10d2018-07-18 11:12:55 +0900278 virtual ~AidlTypeSpecifier() = default;
Casey Dahlin0ee37582015-09-30 16:31:55 -0700279
Steven Moreland3f658cf2018-08-20 13:40:54 -0700280 // Copy of this type which is not an array.
Jooyung Hand2fa0232020-10-19 02:51:41 +0900281 const AidlTypeSpecifier& ArrayBase() const;
Steven Moreland3f658cf2018-08-20 13:40:54 -0700282
Jiyong Park1deecc32018-07-17 01:14:41 +0900283 // Returns the full-qualified name of the base type.
284 // int -> int
285 // int[] -> int
286 // List<String> -> List
287 // IFoo -> foo.bar.IFoo (if IFoo is in package foo.bar)
288 const string& GetName() const {
289 if (IsResolved()) {
290 return fully_qualified_name_;
291 } else {
292 return GetUnresolvedName();
293 }
294 }
Casey Dahlin0ee37582015-09-30 16:31:55 -0700295
Jiyong Park1deecc32018-07-17 01:14:41 +0900296 // Returns string representation of this type specifier.
Artur Satayev91fe8712019-07-29 13:06:01 +0100297 // This is GetBaseTypeName() + array modifier or generic type parameters
Jiyong Park1deecc32018-07-17 01:14:41 +0900298 string ToString() const;
299
Jiyong Park02da7422018-07-16 16:00:26 +0900300 std::string Signature() const;
301
Jiyong Park1deecc32018-07-17 01:14:41 +0900302 const string& GetUnresolvedName() const { return unresolved_name_; }
303
Jeongik Cha997281d2020-01-16 15:23:59 +0900304 bool IsHidden() const;
305
Jiyong Park1deecc32018-07-17 01:14:41 +0900306 const string& GetComments() const { return comments_; }
307
Jeongik Cha1a7ab642019-07-29 17:31:02 +0900308 const std::vector<std::string> GetSplitName() const { return split_name_; }
309
Jiyong Parka6605ab2018-11-11 14:30:21 +0900310 void SetComments(const string& comment) { comments_ = comment; }
311
Jiyong Park1deecc32018-07-17 01:14:41 +0900312 bool IsResolved() const { return fully_qualified_name_ != ""; }
313
314 bool IsArray() const { return is_array_; }
315
Steven Moreland6c07b832020-10-29 23:39:53 +0000316 __attribute__((warn_unused_result)) bool SetArray() {
317 if (is_array_) return false;
318 is_array_ = true;
319 return true;
320 }
321
Jiyong Park1deecc32018-07-17 01:14:41 +0900322 // Resolve the base type name to a fully-qualified name. Return false if the
323 // resolution fails.
Daniel Norman716d3112019-09-10 13:11:56 -0700324 bool Resolve(const AidlTypenames& typenames);
Casey Dahlin0ee37582015-09-30 16:31:55 -0700325
Steven Moreland0cea4aa2020-04-20 21:06:02 -0700326 std::set<AidlAnnotation::Type> GetSupportedAnnotations() const override;
Devin Moore24f68572020-02-26 13:20:59 -0800327 bool CheckValid(const AidlTypenames& typenames) const override;
Steven Morelandd59e3172020-05-11 16:42:09 -0700328 bool LanguageSpecificCheckValid(const AidlTypenames& typenames, Options::Language lang) const;
Jeongik Chadf76dc72019-11-28 00:08:47 +0900329 const AidlNode& AsAidlNode() const override { return *this; }
Jiyong Park1d2df7d2018-07-23 15:22:50 +0900330
Jooyung Hane9bb9de2020-11-01 22:16:57 +0900331 const AidlDefinedType* GetDefinedType() const;
332
Casey Dahlin0ee37582015-09-30 16:31:55 -0700333 private:
Steven Moreland3f658cf2018-08-20 13:40:54 -0700334 AidlTypeSpecifier(const AidlTypeSpecifier&) = default;
335
Jiyong Park1deecc32018-07-17 01:14:41 +0900336 const string unresolved_name_;
337 string fully_qualified_name_;
Steven Moreland3f658cf2018-08-20 13:40:54 -0700338 bool is_array_;
Jiyong Parka6605ab2018-11-11 14:30:21 +0900339 string comments_;
Jeongik Cha1a7ab642019-07-29 17:31:02 +0900340 vector<string> split_name_;
Jooyung Hane9bb9de2020-11-01 22:16:57 +0900341 const AidlDefinedType* defined_type_; // set when Resolve() for defined types
Jooyung Hand2fa0232020-10-19 02:51:41 +0900342 mutable shared_ptr<AidlTypeSpecifier> array_base_;
Casey Dahlin0ee37582015-09-30 16:31:55 -0700343};
344
Steven Moreland860b1942018-08-16 14:59:28 -0700345// Returns the universal value unaltered.
346std::string AidlConstantValueDecorator(const AidlTypeSpecifier& type, const std::string& raw_value);
347
Steven Moreland9ea10e32018-07-19 15:26:09 -0700348class AidlConstantValue;
Steven Moreland541788d2020-05-21 22:05:52 +0000349// TODO: This class is used for method arguments and also parcelable fields,
350// and it should be split up since default values don't apply to method
351// arguments
Steven Moreland5557f1c2018-07-02 13:50:23 -0700352class AidlVariableDeclaration : public AidlNode {
353 public:
Steven Moreland46e9da82018-07-27 15:45:29 -0700354 AidlVariableDeclaration(const AidlLocation& location, AidlTypeSpecifier* type,
355 const std::string& name);
356 AidlVariableDeclaration(const AidlLocation& location, AidlTypeSpecifier* type,
357 const std::string& name, AidlConstantValue* default_value);
Steven Moreland5557f1c2018-07-02 13:50:23 -0700358 virtual ~AidlVariableDeclaration() = default;
359
Jiyong Parkd800fef2020-07-22 18:09:43 +0900360 // non-copyable, non-movable
361 AidlVariableDeclaration(const AidlVariableDeclaration&) = delete;
362 AidlVariableDeclaration(AidlVariableDeclaration&&) = delete;
363 AidlVariableDeclaration& operator=(const AidlVariableDeclaration&) = delete;
364 AidlVariableDeclaration& operator=(AidlVariableDeclaration&&) = delete;
365
Steven Moreland5557f1c2018-07-02 13:50:23 -0700366 std::string GetName() const { return name_; }
Jooyung Hanacae85d2020-10-28 16:39:09 +0900367 std::string GetCapitalizedName() const;
Jiyong Parkd59a10d2018-07-18 11:12:55 +0900368 const AidlTypeSpecifier& GetType() const { return *type_; }
Steven Moreland541788d2020-05-21 22:05:52 +0000369 // if this was constructed explicitly with a default value
370 bool IsDefaultUserSpecified() const { return default_user_specified_; }
371 // will return the default value this is constructed with or a default value
372 // if one is available
Steven Moreland9ea10e32018-07-19 15:26:09 -0700373 const AidlConstantValue* GetDefaultValue() const { return default_value_.get(); }
374
Jiyong Parkd59a10d2018-07-18 11:12:55 +0900375 AidlTypeSpecifier* GetMutableType() { return type_.get(); }
Steven Moreland5557f1c2018-07-02 13:50:23 -0700376
Jeongik Chadb0f59e2018-11-01 18:11:21 +0900377 bool CheckValid(const AidlTypenames& typenames) const;
Steven Moreland5557f1c2018-07-02 13:50:23 -0700378 std::string ToString() const;
Jiyong Park02da7422018-07-16 16:00:26 +0900379 std::string Signature() const;
Steven Moreland5557f1c2018-07-02 13:50:23 -0700380
Steven Moreland860b1942018-08-16 14:59:28 -0700381 std::string ValueString(const ConstantValueDecorator& decorator) const;
Steven Moreland25294322018-08-07 18:13:55 -0700382
Steven Moreland5557f1c2018-07-02 13:50:23 -0700383 private:
Jiyong Parkd59a10d2018-07-18 11:12:55 +0900384 std::unique_ptr<AidlTypeSpecifier> type_;
Steven Moreland5557f1c2018-07-02 13:50:23 -0700385 std::string name_;
Steven Moreland541788d2020-05-21 22:05:52 +0000386 bool default_user_specified_;
Steven Moreland9ea10e32018-07-19 15:26:09 -0700387 std::unique_ptr<AidlConstantValue> default_value_;
Steven Moreland5557f1c2018-07-02 13:50:23 -0700388};
389
390class AidlArgument : public AidlVariableDeclaration {
Casey Dahlinbc7a50a2015-09-28 19:20:50 -0700391 public:
Casey Dahlinc378c992015-09-29 16:50:40 -0700392 enum Direction { IN_DIR = 1, OUT_DIR = 2, INOUT_DIR = 3 };
393
Steven Moreland46e9da82018-07-27 15:45:29 -0700394 AidlArgument(const AidlLocation& location, AidlArgument::Direction direction,
395 AidlTypeSpecifier* type, const std::string& name);
396 AidlArgument(const AidlLocation& location, AidlTypeSpecifier* type, const std::string& name);
Casey Dahlinbc7a50a2015-09-28 19:20:50 -0700397 virtual ~AidlArgument() = default;
398
Jiyong Parkd800fef2020-07-22 18:09:43 +0900399 // non-copyable, non-movable
400 AidlArgument(const AidlArgument&) = delete;
401 AidlArgument(AidlArgument&&) = delete;
402 AidlArgument& operator=(const AidlArgument&) = delete;
403 AidlArgument& operator=(AidlArgument&&) = delete;
404
Casey Dahlinc378c992015-09-29 16:50:40 -0700405 Direction GetDirection() const { return direction_; }
Christopher Wileyad339272015-10-05 19:11:58 -0700406 bool IsOut() const { return direction_ & OUT_DIR; }
407 bool IsIn() const { return direction_ & IN_DIR; }
Casey Dahlinc378c992015-09-29 16:50:40 -0700408 bool DirectionWasSpecified() const { return direction_specified_; }
Jiyong Park3656c3c2018-08-01 20:02:01 +0900409 string GetDirectionSpecifier() const;
Christopher Wileyad339272015-10-05 19:11:58 -0700410
Casey Dahlinc378c992015-09-29 16:50:40 -0700411 std::string ToString() const;
Jiyong Park02da7422018-07-16 16:00:26 +0900412 std::string Signature() const;
Casey Dahlinc378c992015-09-29 16:50:40 -0700413
Casey Dahlinbc7a50a2015-09-28 19:20:50 -0700414 private:
Casey Dahlinc378c992015-09-29 16:50:40 -0700415 Direction direction_;
416 bool direction_specified_;
Casey Dahlina834dd42015-09-23 11:52:15 -0700417};
Adam Lesinskiffa16862014-01-23 18:17:42 -0800418
Casey Dahlind40e2fe2015-11-24 14:06:52 -0800419class AidlMethod;
Steven Moreland693640b2018-07-19 13:46:27 -0700420class AidlConstantDeclaration;
Daniel Norman85aed542019-08-21 12:01:14 -0700421class AidlEnumDeclaration;
Casey Dahlind40e2fe2015-11-24 14:06:52 -0800422class AidlMember : public AidlNode {
423 public:
Steven Moreland46e9da82018-07-27 15:45:29 -0700424 AidlMember(const AidlLocation& location);
Casey Dahlind40e2fe2015-11-24 14:06:52 -0800425 virtual ~AidlMember() = default;
426
Jiyong Parkd800fef2020-07-22 18:09:43 +0900427 // non-copyable, non-movable
428 AidlMember(const AidlMember&) = delete;
429 AidlMember(AidlMember&&) = delete;
430 AidlMember& operator=(const AidlMember&) = delete;
431 AidlMember& operator=(AidlMember&&) = delete;
432
Casey Dahlind40e2fe2015-11-24 14:06:52 -0800433 virtual AidlMethod* AsMethod() { return nullptr; }
Steven Moreland693640b2018-07-19 13:46:27 -0700434 virtual AidlConstantDeclaration* AsConstantDeclaration() { return nullptr; }
Casey Dahlind40e2fe2015-11-24 14:06:52 -0800435};
436
Will McVickerd7d18df2019-09-12 13:40:50 -0700437class AidlUnaryConstExpression;
438class AidlBinaryConstExpression;
439
Steven Moreland693640b2018-07-19 13:46:27 -0700440class AidlConstantValue : public AidlNode {
Casey Dahlind40e2fe2015-11-24 14:06:52 -0800441 public:
Will McVickerd7d18df2019-09-12 13:40:50 -0700442 enum class Type {
443 // WARNING: Don't change this order! The order is used to determine type
444 // promotion during a binary expression.
445 BOOLEAN,
446 INT8,
447 INT32,
448 INT64,
449 ARRAY,
450 CHARACTER,
451 STRING,
452 FLOATING,
453 UNARY,
454 BINARY,
455 ERROR,
456 };
457
458 /*
459 * Return the value casted to the given type.
460 */
461 template <typename T>
462 T cast() const;
Casey Dahlind40e2fe2015-11-24 14:06:52 -0800463
Steven Moreland693640b2018-07-19 13:46:27 -0700464 virtual ~AidlConstantValue() = default;
Casey Dahlind40e2fe2015-11-24 14:06:52 -0800465
Jiyong Parkd800fef2020-07-22 18:09:43 +0900466 // non-copyable, non-movable
467 AidlConstantValue(const AidlConstantValue&) = delete;
468 AidlConstantValue(AidlConstantValue&&) = delete;
469 AidlConstantValue& operator=(const AidlConstantValue&) = delete;
470 AidlConstantValue& operator=(AidlConstantValue&&) = delete;
471
Steven Moreland541788d2020-05-21 22:05:52 +0000472 // creates default value, when one isn't specified
473 // nullptr if no default available
474 static AidlConstantValue* Default(const AidlTypeSpecifier& specifier);
475
Steven Moreland25294322018-08-07 18:13:55 -0700476 static AidlConstantValue* Boolean(const AidlLocation& location, bool value);
477 static AidlConstantValue* Character(const AidlLocation& location, char value);
Steven Moreland25294322018-08-07 18:13:55 -0700478 // example: 123, -5498, maybe any size
Will McVickerd7d18df2019-09-12 13:40:50 -0700479 static AidlConstantValue* Integral(const AidlLocation& location, const string& value);
480 static AidlConstantValue* Floating(const AidlLocation& location, const std::string& value);
Steven Moreland860b1942018-08-16 14:59:28 -0700481 static AidlConstantValue* Array(const AidlLocation& location,
Will McVickerd7d18df2019-09-12 13:40:50 -0700482 std::unique_ptr<vector<unique_ptr<AidlConstantValue>>> values);
Steven Moreland693640b2018-07-19 13:46:27 -0700483 // example: "\"asdf\""
Will McVickerd7d18df2019-09-12 13:40:50 -0700484 static AidlConstantValue* String(const AidlLocation& location, const string& value);
Steven Moreland693640b2018-07-19 13:46:27 -0700485
Daniel Normanf0ca44f2019-10-25 09:59:44 -0700486 // Construct an AidlConstantValue by evaluating the other integral constant's
487 // value string. This does not preserve the structure of the copied constant.
Steven Moreland59e53e42019-11-26 20:38:08 -0800488 // Returns nullptr and logs if value cannot be copied.
Daniel Normanf0ca44f2019-10-25 09:59:44 -0700489 static AidlConstantValue* ShallowIntegralCopy(const AidlConstantValue& other);
Daniel Normanb28684e2019-10-17 15:31:39 -0700490
Will McVickerd7d18df2019-09-12 13:40:50 -0700491 Type GetType() const { return final_type_; }
Steven Moreland25294322018-08-07 18:13:55 -0700492
Will McVickerd7d18df2019-09-12 13:40:50 -0700493 virtual bool CheckValid() const;
Steven Moreland860b1942018-08-16 14:59:28 -0700494
495 // Raw value of type (currently valid in C++ and Java). Empty string on error.
Steven Moreland4bcb05c2019-11-27 18:57:47 -0800496 string ValueString(const AidlTypeSpecifier& type, const ConstantValueDecorator& decorator) const;
Casey Dahlind40e2fe2015-11-24 14:06:52 -0800497
498 private:
Will McVickerd7d18df2019-09-12 13:40:50 -0700499 AidlConstantValue(const AidlLocation& location, Type parsed_type, int64_t parsed_value,
500 const string& checked_value);
501 AidlConstantValue(const AidlLocation& location, Type type, const string& checked_value);
Steven Moreland860b1942018-08-16 14:59:28 -0700502 AidlConstantValue(const AidlLocation& location, Type type,
Will McVickerd7d18df2019-09-12 13:40:50 -0700503 std::unique_ptr<vector<unique_ptr<AidlConstantValue>>> values);
Steven Moreland25294322018-08-07 18:13:55 -0700504 static string ToString(Type type);
Will McVickerd7d18df2019-09-12 13:40:50 -0700505 static bool ParseIntegral(const string& value, int64_t* parsed_value, Type* parsed_type);
506 static bool IsHex(const string& value);
Steven Moreland4bcb05c2019-11-27 18:57:47 -0800507
Will McVickerd7d18df2019-09-12 13:40:50 -0700508 virtual bool evaluate(const AidlTypeSpecifier& type) const;
Casey Dahlind40e2fe2015-11-24 14:06:52 -0800509
Steven Moreland693640b2018-07-19 13:46:27 -0700510 const Type type_ = Type::ERROR;
Will McVickerd7d18df2019-09-12 13:40:50 -0700511 const vector<unique_ptr<AidlConstantValue>> values_; // if type_ == ARRAY
512 const string value_; // otherwise
513
514 // State for tracking evaluation of expressions
Steven Moreland4bcb05c2019-11-27 18:57:47 -0800515 mutable bool is_valid_ = false; // cache of CheckValid, but may be marked false in evaluate
516 mutable bool is_evaluated_ = false; // whether evaluate has been called
Will McVickerd7d18df2019-09-12 13:40:50 -0700517 mutable Type final_type_;
518 mutable int64_t final_value_;
519 mutable string final_string_value_ = "";
Steven Moreland693640b2018-07-19 13:46:27 -0700520
Will McVickerd7d18df2019-09-12 13:40:50 -0700521 friend AidlUnaryConstExpression;
522 friend AidlBinaryConstExpression;
523};
524
525class AidlUnaryConstExpression : public AidlConstantValue {
526 public:
527 AidlUnaryConstExpression(const AidlLocation& location, const string& op,
528 std::unique_ptr<AidlConstantValue> rval);
529
530 static bool IsCompatibleType(Type type, const string& op);
531 bool CheckValid() const override;
Will McVickerd7d18df2019-09-12 13:40:50 -0700532 private:
533 bool evaluate(const AidlTypeSpecifier& type) const override;
534
535 std::unique_ptr<AidlConstantValue> unary_;
536 const string op_;
537};
538
539class AidlBinaryConstExpression : public AidlConstantValue {
540 public:
541 AidlBinaryConstExpression(const AidlLocation& location, std::unique_ptr<AidlConstantValue> lval,
542 const string& op, std::unique_ptr<AidlConstantValue> rval);
543
544 bool CheckValid() const override;
Will McVickerd7d18df2019-09-12 13:40:50 -0700545
546 static bool AreCompatibleTypes(Type t1, Type t2);
547 // Returns the promoted kind for both operands
548 static Type UsualArithmeticConversion(Type left, Type right);
549 // Returns the promoted integral type where INT32 is the smallest type
550 static Type IntegralPromotion(Type in);
551
552 private:
553 bool evaluate(const AidlTypeSpecifier& type) const override;
554
555 std::unique_ptr<AidlConstantValue> left_val_;
556 std::unique_ptr<AidlConstantValue> right_val_;
557 const string op_;
Christopher Wileyd6bdd8d2016-05-03 11:23:13 -0700558};
559
Andrei Onea9445fc62019-06-27 18:11:59 +0100560struct AidlAnnotationParameter {
561 std::string name;
562 std::unique_ptr<AidlConstantValue> value;
563};
564
Steven Moreland693640b2018-07-19 13:46:27 -0700565class AidlConstantDeclaration : public AidlMember {
Christopher Wileyd6bdd8d2016-05-03 11:23:13 -0700566 public:
Steven Moreland46e9da82018-07-27 15:45:29 -0700567 AidlConstantDeclaration(const AidlLocation& location, AidlTypeSpecifier* specifier,
Will McVickerd7d18df2019-09-12 13:40:50 -0700568 const string& name, AidlConstantValue* value);
Steven Moreland693640b2018-07-19 13:46:27 -0700569 virtual ~AidlConstantDeclaration() = default;
Christopher Wileyd6bdd8d2016-05-03 11:23:13 -0700570
Jiyong Parkd800fef2020-07-22 18:09:43 +0900571 // non-copyable, non-movable
572 AidlConstantDeclaration(const AidlConstantDeclaration&) = delete;
573 AidlConstantDeclaration(AidlConstantDeclaration&&) = delete;
574 AidlConstantDeclaration& operator=(const AidlConstantDeclaration&) = delete;
575 AidlConstantDeclaration& operator=(AidlConstantDeclaration&&) = delete;
576
Steven Moreland693640b2018-07-19 13:46:27 -0700577 const AidlTypeSpecifier& GetType() const { return *type_; }
Steven Moreland4d12f9a2018-10-31 14:30:55 -0700578 AidlTypeSpecifier* GetMutableType() { return type_.get(); }
Will McVickerd7d18df2019-09-12 13:40:50 -0700579 const string& GetName() const { return name_; }
Steven Moreland693640b2018-07-19 13:46:27 -0700580 const AidlConstantValue& GetValue() const { return *value_; }
Jeongik Chadb0f59e2018-11-01 18:11:21 +0900581 bool CheckValid(const AidlTypenames& typenames) const;
Christopher Wileyd6bdd8d2016-05-03 11:23:13 -0700582
Will McVickerd7d18df2019-09-12 13:40:50 -0700583 string ToString() const;
584 string Signature() const;
Steven Moreland860b1942018-08-16 14:59:28 -0700585 string ValueString(const ConstantValueDecorator& decorator) const {
Will McVickerd7d18df2019-09-12 13:40:50 -0700586 return value_->ValueString(GetType(), decorator);
Steven Moreland860b1942018-08-16 14:59:28 -0700587 }
Steven Moreland25294322018-08-07 18:13:55 -0700588
Steven Moreland693640b2018-07-19 13:46:27 -0700589 AidlConstantDeclaration* AsConstantDeclaration() override { return this; }
Christopher Wileyd6bdd8d2016-05-03 11:23:13 -0700590
591 private:
Steven Moreland693640b2018-07-19 13:46:27 -0700592 const unique_ptr<AidlTypeSpecifier> type_;
Will McVickerd7d18df2019-09-12 13:40:50 -0700593 const string name_;
594 unique_ptr<AidlConstantValue> value_;
Casey Dahlind40e2fe2015-11-24 14:06:52 -0800595};
596
597class AidlMethod : public AidlMember {
Casey Dahlin5c69deb2015-10-01 14:44:12 -0700598 public:
Will McVickerd7d18df2019-09-12 13:40:50 -0700599 AidlMethod(const AidlLocation& location, bool oneway, AidlTypeSpecifier* type, const string& name,
600 vector<unique_ptr<AidlArgument>>* args, const string& comments);
601 AidlMethod(const AidlLocation& location, bool oneway, AidlTypeSpecifier* type, const string& name,
602 vector<unique_ptr<AidlArgument>>* args, const string& comments, int id,
603 bool is_user_defined = true);
Casey Dahlin5c69deb2015-10-01 14:44:12 -0700604 virtual ~AidlMethod() = default;
605
Jiyong Parkd800fef2020-07-22 18:09:43 +0900606 // non-copyable, non-movable
607 AidlMethod(const AidlMethod&) = delete;
608 AidlMethod(AidlMethod&&) = delete;
609 AidlMethod& operator=(const AidlMethod&) = delete;
610 AidlMethod& operator=(AidlMethod&&) = delete;
611
Casey Dahlind40e2fe2015-11-24 14:06:52 -0800612 AidlMethod* AsMethod() override { return this; }
Jeongik Cha997281d2020-01-16 15:23:59 +0900613 bool IsHidden() const;
Will McVickerd7d18df2019-09-12 13:40:50 -0700614 const string& GetComments() const { return comments_; }
Jiyong Parkd59a10d2018-07-18 11:12:55 +0900615 const AidlTypeSpecifier& GetType() const { return *type_; }
616 AidlTypeSpecifier* GetMutableType() { return type_.get(); }
Steven Morelandacd53472018-12-14 10:17:26 -0800617
Steven Moreland8c70ba92018-12-17 10:20:31 -0800618 // set if this method is part of an interface that is marked oneway
619 void ApplyInterfaceOneway(bool oneway) { oneway_ = oneway_ || oneway; }
Casey Dahlinf4a93112015-10-05 16:58:09 -0700620 bool IsOneway() const { return oneway_; }
Steven Morelandacd53472018-12-14 10:17:26 -0800621
Casey Dahlinf4a93112015-10-05 16:58:09 -0700622 const std::string& GetName() const { return name_; }
Casey Dahlinf4a93112015-10-05 16:58:09 -0700623 bool HasId() const { return has_id_; }
Jiyong Parked65bf42018-08-28 15:43:27 +0900624 int GetId() const { return id_; }
Casey Dahlinf4a93112015-10-05 16:58:09 -0700625 void SetId(unsigned id) { id_ = id; }
Casey Dahlinf2d23f72015-10-02 16:19:19 -0700626
Jiyong Park309668e2018-07-28 16:55:44 +0900627 bool IsUserDefined() const { return is_user_defined_; }
628
Casey Dahlinf4a93112015-10-05 16:58:09 -0700629 const std::vector<std::unique_ptr<AidlArgument>>& GetArguments() const {
Christopher Wileyad339272015-10-05 19:11:58 -0700630 return arguments_;
631 }
632 // An inout parameter will appear in both GetInArguments()
633 // and GetOutArguments(). AidlMethod retains ownership of the argument
634 // pointers returned in this way.
635 const std::vector<const AidlArgument*>& GetInArguments() const {
636 return in_arguments_;
637 }
638 const std::vector<const AidlArgument*>& GetOutArguments() const {
639 return out_arguments_;
Casey Dahlinf4a93112015-10-05 16:58:09 -0700640 }
Casey Dahlin5c69deb2015-10-01 14:44:12 -0700641
Jiyong Park309668e2018-07-28 16:55:44 +0900642 // name + type parameter types
643 // i.e, foo(int, String)
Jiyong Park02da7422018-07-16 16:00:26 +0900644 std::string Signature() const;
645
Jiyong Park309668e2018-07-28 16:55:44 +0900646 // return type + name + type parameter types + annotations
647 // i.e, boolean foo(int, @Nullable String)
648 std::string ToString() const;
649
Casey Dahlin5c69deb2015-10-01 14:44:12 -0700650 private:
Casey Dahlinf4a93112015-10-05 16:58:09 -0700651 bool oneway_;
Casey Dahlinf2d23f72015-10-02 16:19:19 -0700652 std::string comments_;
Jiyong Parkd59a10d2018-07-18 11:12:55 +0900653 std::unique_ptr<AidlTypeSpecifier> type_;
Casey Dahlinf4a93112015-10-05 16:58:09 -0700654 std::string name_;
Christopher Wileyad339272015-10-05 19:11:58 -0700655 const std::vector<std::unique_ptr<AidlArgument>> arguments_;
656 std::vector<const AidlArgument*> in_arguments_;
657 std::vector<const AidlArgument*> out_arguments_;
Casey Dahlinf4a93112015-10-05 16:58:09 -0700658 bool has_id_;
659 int id_;
Jiyong Park309668e2018-07-28 16:55:44 +0900660 bool is_user_defined_ = true;
Casey Dahlin0a2f8be2015-09-28 16:15:29 -0700661};
Adam Lesinskiffa16862014-01-23 18:17:42 -0800662
Steven Morelandc258abc2018-07-10 14:03:38 -0700663class AidlDefinedType;
Jiyong Parkb034bf02018-07-30 17:44:33 +0900664class AidlInterface;
665class AidlParcelable;
666class AidlStructuredParcelable;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800667
Steven Moreland46e9da82018-07-27 15:45:29 -0700668class AidlInterface;
669class AidlParcelable;
670class AidlStructuredParcelable;
Jooyung Han2946afc2020-10-05 20:29:16 +0900671
672class AidlUnionDecl;
Daniel Norman85aed542019-08-21 12:01:14 -0700673// AidlDefinedType represents either an interface, parcelable, or enum that is
Jiyong Park1deecc32018-07-17 01:14:41 +0900674// defined in the source file.
675class AidlDefinedType : public AidlAnnotatable {
Steven Moreland787b0432018-07-03 09:00:58 -0700676 public:
Steven Moreland46e9da82018-07-27 15:45:29 -0700677 AidlDefinedType(const AidlLocation& location, const std::string& name,
Jiyong Park18132182020-06-08 20:24:40 +0900678 const std::string& comments, const std::string& package);
Steven Moreland787b0432018-07-03 09:00:58 -0700679 virtual ~AidlDefinedType() = default;
680
Jiyong Parkd800fef2020-07-22 18:09:43 +0900681 // non-copyable, non-movable
682 AidlDefinedType(const AidlDefinedType&) = delete;
683 AidlDefinedType(AidlDefinedType&&) = delete;
684 AidlDefinedType& operator=(const AidlDefinedType&) = delete;
685 AidlDefinedType& operator=(AidlDefinedType&&) = delete;
686
Jiyong Park1deecc32018-07-17 01:14:41 +0900687 const std::string& GetName() const { return name_; };
Jeongik Cha997281d2020-01-16 15:23:59 +0900688 bool IsHidden() const;
Jiyong Park1deecc32018-07-17 01:14:41 +0900689 const std::string& GetComments() const { return comments_; }
Jiyong Parka6605ab2018-11-11 14:30:21 +0900690 void SetComments(const std::string comments) { comments_ = comments; }
Jiyong Park1deecc32018-07-17 01:14:41 +0900691
Steven Moreland787b0432018-07-03 09:00:58 -0700692 /* dot joined package, example: "android.package.foo" */
Jiyong Park18132182020-06-08 20:24:40 +0900693 std::string GetPackage() const { return package_; }
Steven Moreland787b0432018-07-03 09:00:58 -0700694 /* dot joined package and name, example: "android.package.foo.IBar" */
695 std::string GetCanonicalName() const;
Jiyong Park18132182020-06-08 20:24:40 +0900696 const std::vector<std::string>& GetSplitPackage() const { return split_package_; }
Steven Moreland787b0432018-07-03 09:00:58 -0700697
Steven Morelanded83a282018-07-17 13:27:29 -0700698 virtual std::string GetPreprocessDeclarationName() const = 0;
Steven Morelandc258abc2018-07-10 14:03:38 -0700699
Steven Moreland5557f1c2018-07-02 13:50:23 -0700700 virtual const AidlStructuredParcelable* AsStructuredParcelable() const { return nullptr; }
Steven Morelandc258abc2018-07-10 14:03:38 -0700701 virtual const AidlParcelable* AsParcelable() const { return nullptr; }
Daniel Norman85aed542019-08-21 12:01:14 -0700702 virtual const AidlEnumDeclaration* AsEnumDeclaration() const { return nullptr; }
Jooyung Han2946afc2020-10-05 20:29:16 +0900703 virtual const AidlUnionDecl* AsUnionDeclaration() const { return nullptr; }
Steven Moreland5557f1c2018-07-02 13:50:23 -0700704 virtual const AidlInterface* AsInterface() const { return nullptr; }
Jeongik Chadf76dc72019-11-28 00:08:47 +0900705 virtual const AidlParameterizable<std::string>* AsParameterizable() const { return nullptr; }
Devin Moore24f68572020-02-26 13:20:59 -0800706 bool CheckValid(const AidlTypenames& typenames) const override;
Steven Morelandd59e3172020-05-11 16:42:09 -0700707 virtual bool LanguageSpecificCheckValid(const AidlTypenames& typenames,
708 Options::Language lang) const = 0;
Steven Morelandc258abc2018-07-10 14:03:38 -0700709 AidlStructuredParcelable* AsStructuredParcelable() {
710 return const_cast<AidlStructuredParcelable*>(
711 const_cast<const AidlDefinedType*>(this)->AsStructuredParcelable());
712 }
713 AidlParcelable* AsParcelable() {
714 return const_cast<AidlParcelable*>(const_cast<const AidlDefinedType*>(this)->AsParcelable());
715 }
Daniel Norman85aed542019-08-21 12:01:14 -0700716 AidlEnumDeclaration* AsEnumDeclaration() {
717 return const_cast<AidlEnumDeclaration*>(
718 const_cast<const AidlDefinedType*>(this)->AsEnumDeclaration());
719 }
Jooyung Han2946afc2020-10-05 20:29:16 +0900720 AidlUnionDecl* AsUnionDeclaration() {
721 return const_cast<AidlUnionDecl*>(
722 const_cast<const AidlDefinedType*>(this)->AsUnionDeclaration());
723 }
Steven Morelandc258abc2018-07-10 14:03:38 -0700724 AidlInterface* AsInterface() {
725 return const_cast<AidlInterface*>(const_cast<const AidlDefinedType*>(this)->AsInterface());
726 }
727
Jeongik Chadf76dc72019-11-28 00:08:47 +0900728 AidlParameterizable<std::string>* AsParameterizable() {
729 return const_cast<AidlParameterizable<std::string>*>(
730 const_cast<const AidlDefinedType*>(this)->AsParameterizable());
731 }
732
Steven Moreland6cee3482018-07-18 14:39:58 -0700733 const AidlParcelable* AsUnstructuredParcelable() const {
734 if (this->AsStructuredParcelable() != nullptr) return nullptr;
Jooyung Han2946afc2020-10-05 20:29:16 +0900735 if (this->AsUnionDeclaration() != nullptr) return nullptr;
Steven Moreland6cee3482018-07-18 14:39:58 -0700736 return this->AsParcelable();
737 }
738 AidlParcelable* AsUnstructuredParcelable() {
739 return const_cast<AidlParcelable*>(
740 const_cast<const AidlDefinedType*>(this)->AsUnstructuredParcelable());
741 }
742
Jeongik Cha997281d2020-01-16 15:23:59 +0900743 virtual void Dump(CodeWriter* writer) const = 0;
Steven Morelanda5d9c5c2020-02-21 16:01:09 -0800744 void DumpHeader(CodeWriter* writer) const;
Jiyong Park02da7422018-07-16 16:00:26 +0900745
Steven Moreland787b0432018-07-03 09:00:58 -0700746 private:
Jiyong Park1deecc32018-07-17 01:14:41 +0900747 std::string name_;
Jiyong Park1deecc32018-07-17 01:14:41 +0900748 std::string comments_;
Jiyong Park18132182020-06-08 20:24:40 +0900749 const std::string package_;
750 const std::vector<std::string> split_package_;
Steven Moreland787b0432018-07-03 09:00:58 -0700751};
752
Jeongik Chadf76dc72019-11-28 00:08:47 +0900753class AidlParcelable : public AidlDefinedType, public AidlParameterizable<std::string> {
Casey Dahlin1ae2bc52015-10-07 18:49:10 -0700754 public:
Jiyong Park18132182020-06-08 20:24:40 +0900755 AidlParcelable(const AidlLocation& location, const std::string& name, const std::string& package,
756 const std::string& comments, const std::string& cpp_header = "",
Jeongik Chadf76dc72019-11-28 00:08:47 +0900757 std::vector<std::string>* type_params = nullptr);
Casey Dahlin1ae2bc52015-10-07 18:49:10 -0700758 virtual ~AidlParcelable() = default;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800759
Jiyong Parkd800fef2020-07-22 18:09:43 +0900760 // non-copyable, non-movable
761 AidlParcelable(const AidlParcelable&) = delete;
762 AidlParcelable(AidlParcelable&&) = delete;
763 AidlParcelable& operator=(const AidlParcelable&) = delete;
764 AidlParcelable& operator=(AidlParcelable&&) = delete;
765
Christopher Wiley8aa4d9f2015-11-16 19:10:45 -0800766 std::string GetCppHeader() const { return cpp_header_; }
Christopher Wiley8aa4d9f2015-11-16 19:10:45 -0800767
Steven Moreland0cea4aa2020-04-20 21:06:02 -0700768 std::set<AidlAnnotation::Type> GetSupportedAnnotations() const override;
Jeongik Cha82317dd2019-02-27 20:26:11 +0900769 bool CheckValid(const AidlTypenames& typenames) const override;
Steven Morelandd59e3172020-05-11 16:42:09 -0700770 bool LanguageSpecificCheckValid(const AidlTypenames& typenames,
771 Options::Language lang) const override;
Steven Morelandc258abc2018-07-10 14:03:38 -0700772 const AidlParcelable* AsParcelable() const override { return this; }
Jeongik Chadf76dc72019-11-28 00:08:47 +0900773 const AidlParameterizable<std::string>* AsParameterizable() const override { return this; }
774 const AidlNode& AsAidlNode() const override { return *this; }
Steven Morelanded83a282018-07-17 13:27:29 -0700775 std::string GetPreprocessDeclarationName() const override { return "parcelable"; }
Steven Morelandc258abc2018-07-10 14:03:38 -0700776
Jeongik Cha997281d2020-01-16 15:23:59 +0900777 void Dump(CodeWriter* writer) const override;
Jiyong Park02da7422018-07-16 16:00:26 +0900778
Casey Dahlin1ae2bc52015-10-07 18:49:10 -0700779 private:
Christopher Wiley8aa4d9f2015-11-16 19:10:45 -0800780 std::string cpp_header_;
Casey Dahlin0a2f8be2015-09-28 16:15:29 -0700781};
Adam Lesinskiffa16862014-01-23 18:17:42 -0800782
Jooyung Han59af9cc2020-10-25 21:44:14 +0900783class AidlWithFields {
784 public:
785 AidlWithFields(std::vector<std::unique_ptr<AidlVariableDeclaration>>* variables)
786 : variables_(std::move(*variables)) {}
787
788 const std::vector<std::unique_ptr<AidlVariableDeclaration>>& GetFields() const {
789 return variables_;
790 }
791
792 protected:
793 bool CheckValid(const AidlParcelable& parcel, const AidlTypenames& typenames) const;
794 bool CheckValidForGetterNames(const AidlParcelable& parcel) const;
795
796 private:
797 const std::vector<std::unique_ptr<AidlVariableDeclaration>> variables_;
798};
799
800class AidlStructuredParcelable : public AidlParcelable, public AidlWithFields {
Steven Moreland5557f1c2018-07-02 13:50:23 -0700801 public:
Jiyong Park18132182020-06-08 20:24:40 +0900802 AidlStructuredParcelable(const AidlLocation& location, const std::string& name,
803 const std::string& package, const std::string& comments,
Devin Moore53fc99c2020-08-12 08:07:52 -0700804 std::vector<std::unique_ptr<AidlVariableDeclaration>>* variables,
805 std::vector<std::string>* type_params);
Jiyong Parkd800fef2020-07-22 18:09:43 +0900806 virtual ~AidlStructuredParcelable() = default;
807
808 // non-copyable, non-movable
809 AidlStructuredParcelable(const AidlStructuredParcelable&) = delete;
810 AidlStructuredParcelable(AidlStructuredParcelable&&) = delete;
811 AidlStructuredParcelable& operator=(const AidlStructuredParcelable&) = delete;
812 AidlStructuredParcelable& operator=(AidlStructuredParcelable&&) = delete;
Steven Moreland5557f1c2018-07-02 13:50:23 -0700813
Steven Morelandc258abc2018-07-10 14:03:38 -0700814 const AidlStructuredParcelable* AsStructuredParcelable() const override { return this; }
Steven Morelanded83a282018-07-17 13:27:29 -0700815 std::string GetPreprocessDeclarationName() const override { return "structured_parcelable"; }
Steven Moreland5557f1c2018-07-02 13:50:23 -0700816
Jeongik Cha997281d2020-01-16 15:23:59 +0900817 void Dump(CodeWriter* writer) const override;
Jiyong Park02da7422018-07-16 16:00:26 +0900818
Steven Moreland0cea4aa2020-04-20 21:06:02 -0700819 std::set<AidlAnnotation::Type> GetSupportedAnnotations() const override;
Jeongik Chadb0f59e2018-11-01 18:11:21 +0900820 bool CheckValid(const AidlTypenames& typenames) const override;
Steven Morelandd59e3172020-05-11 16:42:09 -0700821 bool LanguageSpecificCheckValid(const AidlTypenames& typenames,
822 Options::Language lang) const override;
Steven Moreland5557f1c2018-07-02 13:50:23 -0700823};
824
Daniel Norman85aed542019-08-21 12:01:14 -0700825class AidlEnumerator : public AidlNode {
826 public:
Daniel Norman2e4112d2019-10-03 10:22:35 -0700827 AidlEnumerator(const AidlLocation& location, const std::string& name, AidlConstantValue* value,
828 const std::string& comments);
Daniel Norman85aed542019-08-21 12:01:14 -0700829 virtual ~AidlEnumerator() = default;
830
Jiyong Parkd800fef2020-07-22 18:09:43 +0900831 // non-copyable, non-movable
832 AidlEnumerator(const AidlEnumerator&) = delete;
833 AidlEnumerator(AidlEnumerator&&) = delete;
834 AidlEnumerator& operator=(const AidlEnumerator&) = delete;
835 AidlEnumerator& operator=(AidlEnumerator&&) = delete;
836
Daniel Norman85aed542019-08-21 12:01:14 -0700837 const std::string& GetName() const { return name_; }
Will McVickerd7d18df2019-09-12 13:40:50 -0700838 AidlConstantValue* GetValue() const { return value_.get(); }
Daniel Norman2e4112d2019-10-03 10:22:35 -0700839 const std::string& GetComments() const { return comments_; }
Daniel Norman85aed542019-08-21 12:01:14 -0700840 bool CheckValid(const AidlTypeSpecifier& enum_backing_type) const;
841
842 string ValueString(const AidlTypeSpecifier& backing_type,
843 const ConstantValueDecorator& decorator) const;
844
Daniel Normanb28684e2019-10-17 15:31:39 -0700845 void SetValue(std::unique_ptr<AidlConstantValue> value) { value_ = std::move(value); }
846
Daniel Norman85aed542019-08-21 12:01:14 -0700847 private:
848 const std::string name_;
Will McVickerd7d18df2019-09-12 13:40:50 -0700849 unique_ptr<AidlConstantValue> value_;
Daniel Norman2e4112d2019-10-03 10:22:35 -0700850 const std::string comments_;
Daniel Norman85aed542019-08-21 12:01:14 -0700851};
852
853class AidlEnumDeclaration : public AidlDefinedType {
854 public:
Will McVickerd7d18df2019-09-12 13:40:50 -0700855 AidlEnumDeclaration(const AidlLocation& location, const string& name,
Daniel Norman85aed542019-08-21 12:01:14 -0700856 std::vector<std::unique_ptr<AidlEnumerator>>* enumerators,
Jiyong Park18132182020-06-08 20:24:40 +0900857 const std::string& package, const std::string& comments);
Daniel Norman85aed542019-08-21 12:01:14 -0700858 virtual ~AidlEnumDeclaration() = default;
859
Jiyong Parkd800fef2020-07-22 18:09:43 +0900860 // non-copyable, non-movable
861 AidlEnumDeclaration(const AidlEnumDeclaration&) = delete;
862 AidlEnumDeclaration(AidlEnumDeclaration&&) = delete;
863 AidlEnumDeclaration& operator=(const AidlEnumDeclaration&) = delete;
864 AidlEnumDeclaration& operator=(AidlEnumDeclaration&&) = delete;
865
Daniel Norman85aed542019-08-21 12:01:14 -0700866 void SetBackingType(std::unique_ptr<const AidlTypeSpecifier> type);
867 const AidlTypeSpecifier& GetBackingType() const { return *backing_type_; }
868 const std::vector<std::unique_ptr<AidlEnumerator>>& GetEnumerators() const {
869 return enumerators_;
870 }
Steven Moreland59e53e42019-11-26 20:38:08 -0800871 bool Autofill();
Steven Moreland0cea4aa2020-04-20 21:06:02 -0700872 std::set<AidlAnnotation::Type> GetSupportedAnnotations() const override;
Daniel Norman85aed542019-08-21 12:01:14 -0700873 bool CheckValid(const AidlTypenames& typenames) const override;
Steven Morelandd59e3172020-05-11 16:42:09 -0700874 bool LanguageSpecificCheckValid(const AidlTypenames& /*typenames*/,
875 Options::Language) const override {
876 return true;
877 }
Daniel Norman85aed542019-08-21 12:01:14 -0700878 std::string GetPreprocessDeclarationName() const override { return "enum"; }
Jeongik Cha997281d2020-01-16 15:23:59 +0900879 void Dump(CodeWriter* writer) const override;
Daniel Norman85aed542019-08-21 12:01:14 -0700880
881 const AidlEnumDeclaration* AsEnumDeclaration() const override { return this; }
882
883 private:
884 const std::string name_;
885 const std::vector<std::unique_ptr<AidlEnumerator>> enumerators_;
886 std::unique_ptr<const AidlTypeSpecifier> backing_type_;
Daniel Norman85aed542019-08-21 12:01:14 -0700887};
888
Jooyung Han59af9cc2020-10-25 21:44:14 +0900889class AidlUnionDecl : public AidlParcelable, public AidlWithFields {
Jooyung Han2946afc2020-10-05 20:29:16 +0900890 public:
891 AidlUnionDecl(const AidlLocation& location, const std::string& name, const std::string& package,
892 const std::string& comments,
893 std::vector<std::unique_ptr<AidlVariableDeclaration>>* variables,
894 std::vector<std::string>* type_params);
895 virtual ~AidlUnionDecl() = default;
896
897 // non-copyable, non-movable
898 AidlUnionDecl(const AidlUnionDecl&) = delete;
899 AidlUnionDecl(AidlUnionDecl&&) = delete;
900 AidlUnionDecl& operator=(const AidlUnionDecl&) = delete;
901 AidlUnionDecl& operator=(AidlUnionDecl&&) = delete;
902
903 std::set<AidlAnnotation::Type> GetSupportedAnnotations() const override;
904
905 const AidlNode& AsAidlNode() const override { return *this; }
906
Jooyung Hanfe89f122020-10-14 03:49:18 +0900907 bool CheckValid(const AidlTypenames& typenames) const override;
908 bool LanguageSpecificCheckValid(const AidlTypenames& typenames,
909 Options::Language lang) const override;
Jooyung Han2946afc2020-10-05 20:29:16 +0900910 std::string GetPreprocessDeclarationName() const override { return "union"; }
911
912 void Dump(CodeWriter* writer) const override;
913 const AidlUnionDecl* AsUnionDeclaration() const override { return this; }
Jooyung Han2946afc2020-10-05 20:29:16 +0900914};
915
Jiyong Park1deecc32018-07-17 01:14:41 +0900916class AidlInterface final : public AidlDefinedType {
Casey Dahlin1ae2bc52015-10-07 18:49:10 -0700917 public:
Steven Moreland46e9da82018-07-27 15:45:29 -0700918 AidlInterface(const AidlLocation& location, const std::string& name, const std::string& comments,
919 bool oneway_, std::vector<std::unique_ptr<AidlMember>>* members,
Jiyong Park18132182020-06-08 20:24:40 +0900920 const std::string& package);
Casey Dahlin1ae2bc52015-10-07 18:49:10 -0700921 virtual ~AidlInterface() = default;
922
Jiyong Parkd800fef2020-07-22 18:09:43 +0900923 // non-copyable, non-movable
924 AidlInterface(const AidlInterface&) = delete;
925 AidlInterface(AidlInterface&&) = delete;
926 AidlInterface& operator=(const AidlInterface&) = delete;
927 AidlInterface& operator=(AidlInterface&&) = delete;
928
Casey Dahlinfb7da2e2015-10-08 17:26:09 -0700929 const std::vector<std::unique_ptr<AidlMethod>>& GetMethods() const
930 { return methods_; }
Jiyong Park309668e2018-07-28 16:55:44 +0900931 std::vector<std::unique_ptr<AidlMethod>>& GetMutableMethods() { return methods_; }
Steven Moreland693640b2018-07-19 13:46:27 -0700932 const std::vector<std::unique_ptr<AidlConstantDeclaration>>& GetConstantDeclarations() const {
933 return constants_;
934 }
Casey Dahlina2f77c42015-12-01 18:26:02 -0800935
Steven Morelandc258abc2018-07-10 14:03:38 -0700936 const AidlInterface* AsInterface() const override { return this; }
Steven Morelanded83a282018-07-17 13:27:29 -0700937 std::string GetPreprocessDeclarationName() const override { return "interface"; }
Steven Moreland5557f1c2018-07-02 13:50:23 -0700938
Jeongik Cha997281d2020-01-16 15:23:59 +0900939 void Dump(CodeWriter* writer) const override;
Jiyong Park02da7422018-07-16 16:00:26 +0900940
Steven Moreland0cea4aa2020-04-20 21:06:02 -0700941 std::set<AidlAnnotation::Type> GetSupportedAnnotations() const override;
Jeongik Chadb0f59e2018-11-01 18:11:21 +0900942 bool CheckValid(const AidlTypenames& typenames) const override;
Steven Morelandd59e3172020-05-11 16:42:09 -0700943 bool LanguageSpecificCheckValid(const AidlTypenames& typenames,
944 Options::Language lang) const override;
Jeongik Chadb0f59e2018-11-01 18:11:21 +0900945
Jiyong Park27fd7fd2020-08-27 16:25:09 +0900946 std::string GetDescriptor() const;
947
Casey Dahlin1ae2bc52015-10-07 18:49:10 -0700948 private:
Casey Dahlinfb7da2e2015-10-08 17:26:09 -0700949 std::vector<std::unique_ptr<AidlMethod>> methods_;
Steven Moreland693640b2018-07-19 13:46:27 -0700950 std::vector<std::unique_ptr<AidlConstantDeclaration>> constants_;
Casey Dahlin0a2f8be2015-09-28 16:15:29 -0700951};
Adam Lesinskiffa16862014-01-23 18:17:42 -0800952
Casey Dahlin0edf3422015-10-07 12:34:59 -0700953class AidlImport : public AidlNode {
954 public:
Steven Moreland46e9da82018-07-27 15:45:29 -0700955 AidlImport(const AidlLocation& location, const std::string& needed_class);
Casey Dahlin0edf3422015-10-07 12:34:59 -0700956 virtual ~AidlImport() = default;
957
Jiyong Parkd800fef2020-07-22 18:09:43 +0900958 // non-copyable, non-movable
959 AidlImport(const AidlImport&) = delete;
960 AidlImport(AidlImport&&) = delete;
961 AidlImport& operator=(const AidlImport&) = delete;
962 AidlImport& operator=(AidlImport&&) = delete;
963
Casey Dahlin0edf3422015-10-07 12:34:59 -0700964 const std::string& GetNeededClass() const { return needed_class_; }
Casey Dahlin0edf3422015-10-07 12:34:59 -0700965
966 private:
Casey Dahlin0edf3422015-10-07 12:34:59 -0700967 std::string needed_class_;
Casey Dahline2507492015-09-14 17:11:20 -0700968};
969
Jiyong Park62515512020-06-08 15:57:11 +0900970// AidlDocument models an AIDL file
971class AidlDocument : public AidlNode {
972 public:
973 AidlDocument(const AidlLocation& location, std::vector<std::unique_ptr<AidlImport>>& imports,
Jiyong Park8e79b7f2020-07-20 20:52:38 +0900974 std::vector<std::unique_ptr<AidlDefinedType>>&& defined_types)
975 : AidlNode(location),
976 imports_(std::move(imports)),
977 defined_types_(std::move(defined_types)) {}
Jiyong Parkd800fef2020-07-22 18:09:43 +0900978 ~AidlDocument() = default;
979
980 // non-copyable, non-movable
Jiyong Park8e79b7f2020-07-20 20:52:38 +0900981 AidlDocument(const AidlDocument&) = delete;
982 AidlDocument(AidlDocument&&) = delete;
983 AidlDocument& operator=(const AidlDocument&) = delete;
984 AidlDocument& operator=(AidlDocument&&) = delete;
Jiyong Parkd800fef2020-07-22 18:09:43 +0900985
986 const std::vector<std::unique_ptr<AidlImport>>& Imports() const { return imports_; }
987 const std::vector<std::unique_ptr<AidlDefinedType>>& DefinedTypes() const {
988 return defined_types_;
989 }
Jiyong Park62515512020-06-08 15:57:11 +0900990
991 private:
992 const std::vector<std::unique_ptr<AidlImport>> imports_;
Jiyong Park8e79b7f2020-07-20 20:52:38 +0900993 const std::vector<std::unique_ptr<AidlDefinedType>> defined_types_;
Jiyong Park62515512020-06-08 15:57:11 +0900994};