blob: 30e31988b76bfa84a5eb874cd8adf3f40f7db122 [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/macros.h>
31#include <android-base/strings.h>
Casey Dahlin73d46b02015-09-11 02:47:54 +000032
Jiyong Parkb034bf02018-07-30 17:44:33 +090033using android::aidl::AidlTypenames;
Jiyong Park02da7422018-07-16 16:00:26 +090034using android::aidl::CodeWriter;
Jeongik Cha047c5ee2019-08-07 23:16:49 +090035using android::aidl::Options;
Steven Moreland3f658cf2018-08-20 13:40:54 -070036using std::shared_ptr;
Jiyong Park1deecc32018-07-17 01:14:41 +090037using std::string;
38using std::unique_ptr;
39using std::vector;
Andrei Onea8714b022019-02-01 18:55:54 +000040class AidlNode;
41
42namespace android {
43namespace aidl {
44namespace mappings {
45std::string dump_location(const AidlNode& method);
46} // namespace mappings
Mathew Inwoodadb74672019-11-29 14:01:53 +000047namespace java {
48std::string dump_location(const AidlNode& method);
49} // namespace java
Andrei Onea8714b022019-02-01 18:55:54 +000050} // namespace aidl
51} // namespace android
52
Casey Dahlincdbbc8c2015-10-14 15:31:04 -070053class AidlToken {
54 public:
55 AidlToken(const std::string& text, const std::string& comments);
Adam Lesinskiffa16862014-01-23 18:17:42 -080056
Casey Dahlincdbbc8c2015-10-14 15:31:04 -070057 const std::string& GetText() const { return text_; }
58 const std::string& GetComments() const { return comments_; }
Adam Lesinskiffa16862014-01-23 18:17:42 -080059
Casey Dahlincdbbc8c2015-10-14 15:31:04 -070060 private:
61 std::string text_;
62 std::string comments_;
Casey Dahlin082f1d12015-09-21 14:06:25 -070063
Casey Dahlincdbbc8c2015-10-14 15:31:04 -070064 DISALLOW_COPY_AND_ASSIGN(AidlToken);
Casey Dahlin0a2f8be2015-09-28 16:15:29 -070065};
Adam Lesinskiffa16862014-01-23 18:17:42 -080066
Steven Moreland46e9da82018-07-27 15:45:29 -070067class AidlLocation {
Casey Dahlinbc7a50a2015-09-28 19:20:50 -070068 public:
Steven Moreland46e9da82018-07-27 15:45:29 -070069 struct Point {
Dan Willemsen609ba6d2019-12-30 10:44:00 -080070 int line;
71 int column;
Steven Moreland46e9da82018-07-27 15:45:29 -070072 };
73
Devin Mooredf93ebb2020-03-25 14:03:35 -070074 enum class Source {
75 // From internal aidl source code
76 INTERNAL = 0,
77 // From a parsed file
78 EXTERNAL = 1
79 };
80
81 AidlLocation(const std::string& file, Point begin, Point end, Source source);
Devin Moore5de18ed2020-04-02 13:52:29 -070082 AidlLocation(const std::string& file, Source source)
83 : AidlLocation(file, {0, 0}, {0, 0}, source) {}
Devin Mooredf93ebb2020-03-25 14:03:35 -070084
85 bool IsInternal() const { return source_ == Source::INTERNAL; }
Steven Moreland46e9da82018-07-27 15:45:29 -070086
Devin Moore5de18ed2020-04-02 13:52:29 -070087 // The first line of a file is line 1.
88 bool LocationKnown() const { return begin_.line != 0; }
89
Steven Moreland46e9da82018-07-27 15:45:29 -070090 friend std::ostream& operator<<(std::ostream& os, const AidlLocation& l);
Andrei Onea8714b022019-02-01 18:55:54 +000091 friend class AidlNode;
Casey Dahlinbc7a50a2015-09-28 19:20:50 -070092
93 private:
Steven Moreland541788d2020-05-21 22:05:52 +000094 // INTENTIONALLY HIDDEN: only operator<< should access details here.
95 // Otherwise, locations should only ever be copied around to construct new
96 // objects.
Steven Moreland46e9da82018-07-27 15:45:29 -070097 const std::string file_;
98 Point begin_;
99 Point end_;
Devin Mooredf93ebb2020-03-25 14:03:35 -0700100 Source source_;
Steven Moreland46e9da82018-07-27 15:45:29 -0700101};
102
Devin Mooredf93ebb2020-03-25 14:03:35 -0700103#define AIDL_LOCATION_HERE \
104 AidlLocation { __FILE__, {__LINE__, 0}, {__LINE__, 0}, AidlLocation::Source::INTERNAL }
Steven Moreland02e012e2018-08-02 14:58:10 -0700105
Steven Moreland46e9da82018-07-27 15:45:29 -0700106std::ostream& operator<<(std::ostream& os, const AidlLocation& l);
107
108// Anything that is locatable in a .aidl file.
109class AidlNode {
110 public:
111 AidlNode(const AidlLocation& location);
Steven Moreland3f658cf2018-08-20 13:40:54 -0700112
113 AidlNode(const AidlNode&) = default;
Steven Moreland3be75772018-08-20 13:27:43 -0700114 AidlNode(AidlNode&&) = default;
Steven Moreland46e9da82018-07-27 15:45:29 -0700115 virtual ~AidlNode() = default;
116
Devin Mooredf93ebb2020-03-25 14:03:35 -0700117 // To be able to print AidlLocation
Steven Morelandb0d15a52020-03-31 14:03:47 -0700118 friend class AidlErrorLog;
Andrei Onea8714b022019-02-01 18:55:54 +0000119 friend std::string android::aidl::mappings::dump_location(const AidlNode&);
Mathew Inwoodadb74672019-11-29 14:01:53 +0000120 friend std::string android::aidl::java::dump_location(const AidlNode&);
Steven Moreland46e9da82018-07-27 15:45:29 -0700121
Devin Mooredf93ebb2020-03-25 14:03:35 -0700122 const AidlLocation& GetLocation() const { return location_; }
123
Steven Moreland46e9da82018-07-27 15:45:29 -0700124 private:
Mathew Inwoodadb74672019-11-29 14:01:53 +0000125 std::string PrintLine() const;
Andrei Onea8714b022019-02-01 18:55:54 +0000126 std::string PrintLocation() const;
Steven Moreland46e9da82018-07-27 15:45:29 -0700127 const AidlLocation location_;
Casey Dahlinbc7a50a2015-09-28 19:20:50 -0700128};
129
Steven Moreland46e9da82018-07-27 15:45:29 -0700130// Generic point for printing any error in the AIDL compiler.
Steven Morelandb0d15a52020-03-31 14:03:47 -0700131class AidlErrorLog {
Steven Moreland46e9da82018-07-27 15:45:29 -0700132 public:
Devin Moore5de18ed2020-04-02 13:52:29 -0700133 AidlErrorLog(bool fatal, const std::string& filename)
134 : AidlErrorLog(fatal, AidlLocation(filename, AidlLocation::Source::EXTERNAL)) {}
Steven Morelandb0d15a52020-03-31 14:03:47 -0700135 AidlErrorLog(bool fatal, const AidlLocation& location);
136 AidlErrorLog(bool fatal, const AidlNode& node) : AidlErrorLog(fatal, node.location_) {}
137 AidlErrorLog(bool fatal, const AidlNode* node) : AidlErrorLog(fatal, *node) {}
Steven Moreland92c55f12018-07-31 14:08:37 -0700138
139 template <typename T>
Steven Morelandb0d15a52020-03-31 14:03:47 -0700140 AidlErrorLog(bool fatal, const std::unique_ptr<T>& node) : AidlErrorLog(fatal, *node) {}
141 ~AidlErrorLog() {
Steven Moreland46e9da82018-07-27 15:45:29 -0700142 os_ << std::endl;
143 if (fatal_) abort();
Devin Moore5de18ed2020-04-02 13:52:29 -0700144 if (location_.IsInternal()) {
145 os_ << "Logging an internal location should not happen. Offending location: " << location_
146 << std::endl;
147 abort();
148 }
Steven Moreland46e9da82018-07-27 15:45:29 -0700149 }
150
151 std::ostream& os_;
152
Steven Moreland33efcf62020-04-10 16:40:43 -0700153 static void clearError() { sHadError = false; }
Steven Morelandfdb57cd2020-01-08 20:03:30 -0800154 static bool hadError() { return sHadError; }
155
Steven Moreland46e9da82018-07-27 15:45:29 -0700156 private:
Steven Moreland46e9da82018-07-27 15:45:29 -0700157
158 bool fatal_;
159
Devin Moore5de18ed2020-04-02 13:52:29 -0700160 const AidlLocation location_;
161
Steven Morelandfdb57cd2020-01-08 20:03:30 -0800162 static bool sHadError;
163
Steven Morelandb0d15a52020-03-31 14:03:47 -0700164 DISALLOW_COPY_AND_ASSIGN(AidlErrorLog);
Steven Moreland46e9da82018-07-27 15:45:29 -0700165};
166
George Burgess IVae1b2652020-07-22 14:26:10 -0700167// A class used to make it obvious to clang that code is going to abort. This
168// informs static analyses of the aborting behavior of `AIDL_FATAL`, and
169// helps generate slightly faster/smaller code.
170class AidlAbortOnDestruction {
171 public:
172 __attribute__((noreturn)) ~AidlAbortOnDestruction() { abort(); }
173};
174
Steven Morelandb0d15a52020-03-31 14:03:47 -0700175#define AIDL_ERROR(CONTEXT) ::AidlErrorLog(false /*fatal*/, (CONTEXT)).os_
George Burgess IVae1b2652020-07-22 14:26:10 -0700176#define AIDL_FATAL(CONTEXT) \
177 (::AidlAbortOnDestruction(), ::AidlErrorLog(true /*fatal*/, (CONTEXT)).os_)
Steven Moreland3f658cf2018-08-20 13:40:54 -0700178#define AIDL_FATAL_IF(CONDITION, CONTEXT) \
179 if (CONDITION) AIDL_FATAL(CONTEXT) << "Bad internal state: " << #CONDITION << ": "
Steven Moreland46e9da82018-07-27 15:45:29 -0700180
Casey Dahlina2f77c42015-12-01 18:26:02 -0800181namespace android {
182namespace aidl {
183
Jiyong Park1deecc32018-07-17 01:14:41 +0900184class AidlTypenames;
Casey Dahlina2f77c42015-12-01 18:26:02 -0800185
186} // namespace aidl
187} // namespace android
188
Jeongik Chadf76dc72019-11-28 00:08:47 +0900189// unique_ptr<AidlTypeSpecifier> for type arugment,
190// std::string for type parameter(T, U, and so on).
191template <typename T>
192class AidlParameterizable {
193 public:
194 AidlParameterizable(std::vector<T>* type_params) : type_params_(type_params) {}
195 virtual ~AidlParameterizable() = default;
196 bool IsGeneric() const { return type_params_ != nullptr; }
197 const std::vector<T>& GetTypeParameters() const { return *type_params_; }
198 bool CheckValid() const;
199
200 virtual const AidlNode& AsAidlNode() const = 0;
201
202 protected:
203 AidlParameterizable(const AidlParameterizable&);
204
205 private:
206 const unique_ptr<std::vector<T>> type_params_;
207 static_assert(std::is_same<T, unique_ptr<AidlTypeSpecifier>>::value ||
208 std::is_same<T, std::string>::value);
209};
210template <>
211bool AidlParameterizable<std::string>::CheckValid() const;
212
Andrei Onea9445fc62019-06-27 18:11:59 +0100213class AidlConstantValue;
214class AidlConstantDeclaration;
215
216// Transforms a value string into a language specific form. Raw value as produced by
217// AidlConstantValue.
218using ConstantValueDecorator =
219 std::function<std::string(const AidlTypeSpecifier& type, const std::string& raw_value)>;
220
Jiyong Park68bc77a2018-07-19 19:00:45 +0900221class AidlAnnotation : public AidlNode {
222 public:
Steven Moreland0cea4aa2020-04-20 21:06:02 -0700223 enum class Type {
224 BACKING = 1,
225 HIDE,
226 JAVA_STABLE_PARCELABLE,
227 UNSUPPORTED_APP_USAGE,
228 VINTF_STABILITY,
229 NULLABLE,
230 UTF8_IN_CPP,
Jiyong Parkbf5fd5c2020-06-05 19:48:05 +0900231 JAVA_PASSTHROUGH,
Jiyong Park43113fb2020-07-20 16:26:19 +0900232 JAVA_DEBUG,
Steven Moreland0cea4aa2020-04-20 21:06:02 -0700233 };
234 static std::string TypeToString(Type type);
235
Andrei Onea9445fc62019-06-27 18:11:59 +0100236 static AidlAnnotation* Parse(
237 const AidlLocation& location, const string& name,
238 std::map<std::string, std::shared_ptr<AidlConstantValue>>* parameter_list);
Steven Moreland46e9da82018-07-27 15:45:29 -0700239
Steven Moreland3f658cf2018-08-20 13:40:54 -0700240 AidlAnnotation(const AidlAnnotation&) = default;
Steven Moreland3be75772018-08-20 13:27:43 -0700241 AidlAnnotation(AidlAnnotation&&) = default;
Jiyong Park68bc77a2018-07-19 19:00:45 +0900242 virtual ~AidlAnnotation() = default;
Andrei Onea9445fc62019-06-27 18:11:59 +0100243 bool CheckValid() const;
Steven Moreland3be75772018-08-20 13:27:43 -0700244
Steven Moreland0cea4aa2020-04-20 21:06:02 -0700245 const string& GetName() const { return schema_.name; };
246 const Type& GetType() const { return schema_.type; }
Daniel Norman37d43dd2019-09-09 17:22:34 -0700247 string ToString(const ConstantValueDecorator& decorator) const;
Andrei Onea9445fc62019-06-27 18:11:59 +0100248 std::map<std::string, std::string> AnnotationParams(
249 const ConstantValueDecorator& decorator) const;
Jiyong Parka6605ab2018-11-11 14:30:21 +0900250 const string& GetComments() const { return comments_; }
251 void SetComments(const string& comments) { comments_ = comments; }
Jiyong Park68bc77a2018-07-19 19:00:45 +0900252
253 private:
Steven Moreland0cea4aa2020-04-20 21:06:02 -0700254 struct Schema {
255 AidlAnnotation::Type type;
256
257 // text name in .aidl file, e.g. "nullable"
258 std::string name;
259
260 // map from param name -> value type
261 std::map<std::string, std::string> supported_parameters;
262 };
263 static const std::vector<Schema>& AllSchemas();
264
265 AidlAnnotation(const AidlLocation& location, const Schema& schema,
Andrei Onea9445fc62019-06-27 18:11:59 +0100266 std::map<std::string, std::shared_ptr<AidlConstantValue>>&& parameters);
Steven Moreland0cea4aa2020-04-20 21:06:02 -0700267
268 const Schema& schema_;
Jiyong Parka6605ab2018-11-11 14:30:21 +0900269 string comments_;
Andrei Onea9445fc62019-06-27 18:11:59 +0100270 std::map<std::string, std::shared_ptr<AidlConstantValue>> parameters_;
Jiyong Park68bc77a2018-07-19 19:00:45 +0900271};
272
Steven Moreland3be75772018-08-20 13:27:43 -0700273static inline bool operator<(const AidlAnnotation& lhs, const AidlAnnotation& rhs) {
274 return lhs.GetName() < rhs.GetName();
275}
276static inline bool operator==(const AidlAnnotation& lhs, const AidlAnnotation& rhs) {
277 return lhs.GetName() == rhs.GetName();
278}
Jiyong Park3656c3c2018-08-01 20:02:01 +0900279
Casey Dahline7922932016-02-29 17:23:01 -0800280class AidlAnnotatable : public AidlNode {
Casey Dahlin0ee37582015-09-30 16:31:55 -0700281 public:
Steven Moreland46e9da82018-07-27 15:45:29 -0700282 AidlAnnotatable(const AidlLocation& location);
Steven Moreland3f658cf2018-08-20 13:40:54 -0700283
284 AidlAnnotatable(const AidlAnnotatable&) = default;
285 AidlAnnotatable(AidlAnnotatable&&) = default;
Casey Dahline7922932016-02-29 17:23:01 -0800286 virtual ~AidlAnnotatable() = default;
287
Artur Satayev91fe8712019-07-29 13:06:01 +0100288 void Annotate(vector<AidlAnnotation>&& annotations) {
289 for (auto& annotation : annotations) {
290 annotations_.emplace_back(std::move(annotation));
291 }
292 }
Jiyong Park68bc77a2018-07-19 19:00:45 +0900293 bool IsNullable() const;
Jiyong Park68bc77a2018-07-19 19:00:45 +0900294 bool IsUtf8InCpp() const;
Steven Morelanda57d0a62019-07-30 09:41:14 -0700295 bool IsVintfStability() const;
Jeongik Cha88f95a82020-01-15 13:02:16 +0900296 bool IsStableApiParcelable(Options::Language lang) const;
Makoto Onuki78a1c1c2020-03-04 16:57:23 -0800297 bool IsHide() const;
Jiyong Park43113fb2020-07-20 16:26:19 +0900298 bool IsJavaDebug() const;
Andrei Onea9445fc62019-06-27 18:11:59 +0100299
Steven Moreland7e4b9502020-02-20 18:10:42 -0800300 void DumpAnnotations(CodeWriter* writer) const;
301
Andrei Onea9445fc62019-06-27 18:11:59 +0100302 const AidlAnnotation* UnsupportedAppUsage() const;
Jiyong Parkbf5fd5c2020-06-05 19:48:05 +0900303 const AidlAnnotation* JavaPassthrough() const;
Daniel Norman716d3112019-09-10 13:11:56 -0700304 const AidlTypeSpecifier* BackingType(const AidlTypenames& typenames) const;
Jiyong Park68bc77a2018-07-19 19:00:45 +0900305 std::string ToString() const;
Casey Dahline7922932016-02-29 17:23:01 -0800306
Jiyong Parka6605ab2018-11-11 14:30:21 +0900307 const vector<AidlAnnotation>& GetAnnotations() const { return annotations_; }
Devin Moore24f68572020-02-26 13:20:59 -0800308 virtual bool CheckValid(const AidlTypenames&) const;
Jiyong Park3656c3c2018-08-01 20:02:01 +0900309
Steven Moreland181144c2020-04-20 19:57:56 -0700310 protected:
Steven Moreland0cea4aa2020-04-20 21:06:02 -0700311 virtual std::set<AidlAnnotation::Type> GetSupportedAnnotations() const = 0;
Steven Moreland181144c2020-04-20 19:57:56 -0700312
Casey Dahline7922932016-02-29 17:23:01 -0800313 private:
Jiyong Parka6605ab2018-11-11 14:30:21 +0900314 vector<AidlAnnotation> annotations_;
Casey Dahline7922932016-02-29 17:23:01 -0800315};
316
Jiyong Park1deecc32018-07-17 01:14:41 +0900317// AidlTypeSpecifier represents a reference to either a built-in type,
318// a defined type, or a variant (e.g., array of generic) of a type.
Jeongik Chadf76dc72019-11-28 00:08:47 +0900319class AidlTypeSpecifier final : public AidlAnnotatable,
320 public AidlParameterizable<unique_ptr<AidlTypeSpecifier>> {
Casey Dahline7922932016-02-29 17:23:01 -0800321 public:
Steven Moreland46e9da82018-07-27 15:45:29 -0700322 AidlTypeSpecifier(const AidlLocation& location, const string& unresolved_name, bool is_array,
323 vector<unique_ptr<AidlTypeSpecifier>>* type_params, const string& comments);
Jiyong Parkd59a10d2018-07-18 11:12:55 +0900324 virtual ~AidlTypeSpecifier() = default;
Casey Dahlin0ee37582015-09-30 16:31:55 -0700325
Steven Moreland3f658cf2018-08-20 13:40:54 -0700326 // Copy of this type which is not an array.
327 AidlTypeSpecifier ArrayBase() const;
328
Jiyong Park1deecc32018-07-17 01:14:41 +0900329 // Returns the full-qualified name of the base type.
330 // int -> int
331 // int[] -> int
332 // List<String> -> List
333 // IFoo -> foo.bar.IFoo (if IFoo is in package foo.bar)
334 const string& GetName() const {
335 if (IsResolved()) {
336 return fully_qualified_name_;
337 } else {
338 return GetUnresolvedName();
339 }
340 }
Casey Dahlin0ee37582015-09-30 16:31:55 -0700341
Jiyong Park1deecc32018-07-17 01:14:41 +0900342 // Returns string representation of this type specifier.
Artur Satayev91fe8712019-07-29 13:06:01 +0100343 // This is GetBaseTypeName() + array modifier or generic type parameters
Jiyong Park1deecc32018-07-17 01:14:41 +0900344 string ToString() const;
345
Jiyong Park02da7422018-07-16 16:00:26 +0900346 std::string Signature() const;
347
Jiyong Park1deecc32018-07-17 01:14:41 +0900348 const string& GetUnresolvedName() const { return unresolved_name_; }
349
Jeongik Cha997281d2020-01-16 15:23:59 +0900350 bool IsHidden() const;
351
Jiyong Park1deecc32018-07-17 01:14:41 +0900352 const string& GetComments() const { return comments_; }
353
Jeongik Cha1a7ab642019-07-29 17:31:02 +0900354 const std::vector<std::string> GetSplitName() const { return split_name_; }
355
Jiyong Parka6605ab2018-11-11 14:30:21 +0900356 void SetComments(const string& comment) { comments_ = comment; }
357
Jiyong Park1deecc32018-07-17 01:14:41 +0900358 bool IsResolved() const { return fully_qualified_name_ != ""; }
359
360 bool IsArray() const { return is_array_; }
361
Jiyong Park1deecc32018-07-17 01:14:41 +0900362 // Resolve the base type name to a fully-qualified name. Return false if the
363 // resolution fails.
Daniel Norman716d3112019-09-10 13:11:56 -0700364 bool Resolve(const AidlTypenames& typenames);
Casey Dahlin0ee37582015-09-30 16:31:55 -0700365
Steven Moreland0cea4aa2020-04-20 21:06:02 -0700366 std::set<AidlAnnotation::Type> GetSupportedAnnotations() const override;
Devin Moore24f68572020-02-26 13:20:59 -0800367 bool CheckValid(const AidlTypenames& typenames) const override;
Steven Morelandd59e3172020-05-11 16:42:09 -0700368 bool LanguageSpecificCheckValid(const AidlTypenames& typenames, Options::Language lang) const;
Jeongik Chadf76dc72019-11-28 00:08:47 +0900369 const AidlNode& AsAidlNode() const override { return *this; }
Jiyong Park1d2df7d2018-07-23 15:22:50 +0900370
Casey Dahlin0ee37582015-09-30 16:31:55 -0700371 private:
Steven Moreland3f658cf2018-08-20 13:40:54 -0700372 AidlTypeSpecifier(const AidlTypeSpecifier&) = default;
373
Jiyong Park1deecc32018-07-17 01:14:41 +0900374 const string unresolved_name_;
375 string fully_qualified_name_;
Steven Moreland3f658cf2018-08-20 13:40:54 -0700376 bool is_array_;
Jiyong Parka6605ab2018-11-11 14:30:21 +0900377 string comments_;
Jeongik Cha1a7ab642019-07-29 17:31:02 +0900378 vector<string> split_name_;
Casey Dahlin0ee37582015-09-30 16:31:55 -0700379};
380
Steven Moreland860b1942018-08-16 14:59:28 -0700381// Returns the universal value unaltered.
382std::string AidlConstantValueDecorator(const AidlTypeSpecifier& type, const std::string& raw_value);
383
Steven Moreland9ea10e32018-07-19 15:26:09 -0700384class AidlConstantValue;
Steven Moreland541788d2020-05-21 22:05:52 +0000385// TODO: This class is used for method arguments and also parcelable fields,
386// and it should be split up since default values don't apply to method
387// arguments
Steven Moreland5557f1c2018-07-02 13:50:23 -0700388class AidlVariableDeclaration : public AidlNode {
389 public:
Steven Moreland46e9da82018-07-27 15:45:29 -0700390 AidlVariableDeclaration(const AidlLocation& location, AidlTypeSpecifier* type,
391 const std::string& name);
392 AidlVariableDeclaration(const AidlLocation& location, AidlTypeSpecifier* type,
393 const std::string& name, AidlConstantValue* default_value);
Steven Moreland5557f1c2018-07-02 13:50:23 -0700394 virtual ~AidlVariableDeclaration() = default;
395
396 std::string GetName() const { return name_; }
Jiyong Parkd59a10d2018-07-18 11:12:55 +0900397 const AidlTypeSpecifier& GetType() const { return *type_; }
Steven Moreland541788d2020-05-21 22:05:52 +0000398 // if this was constructed explicitly with a default value
399 bool IsDefaultUserSpecified() const { return default_user_specified_; }
400 // will return the default value this is constructed with or a default value
401 // if one is available
Steven Moreland9ea10e32018-07-19 15:26:09 -0700402 const AidlConstantValue* GetDefaultValue() const { return default_value_.get(); }
403
Jiyong Parkd59a10d2018-07-18 11:12:55 +0900404 AidlTypeSpecifier* GetMutableType() { return type_.get(); }
Steven Moreland5557f1c2018-07-02 13:50:23 -0700405
Jeongik Chadb0f59e2018-11-01 18:11:21 +0900406 bool CheckValid(const AidlTypenames& typenames) const;
Steven Moreland5557f1c2018-07-02 13:50:23 -0700407 std::string ToString() const;
Jiyong Park02da7422018-07-16 16:00:26 +0900408 std::string Signature() const;
Steven Moreland5557f1c2018-07-02 13:50:23 -0700409
Steven Moreland860b1942018-08-16 14:59:28 -0700410 std::string ValueString(const ConstantValueDecorator& decorator) const;
Steven Moreland25294322018-08-07 18:13:55 -0700411
Steven Moreland5557f1c2018-07-02 13:50:23 -0700412 private:
Jiyong Parkd59a10d2018-07-18 11:12:55 +0900413 std::unique_ptr<AidlTypeSpecifier> type_;
Steven Moreland5557f1c2018-07-02 13:50:23 -0700414 std::string name_;
Steven Moreland541788d2020-05-21 22:05:52 +0000415 bool default_user_specified_;
Steven Moreland9ea10e32018-07-19 15:26:09 -0700416 std::unique_ptr<AidlConstantValue> default_value_;
Steven Moreland5557f1c2018-07-02 13:50:23 -0700417
418 DISALLOW_COPY_AND_ASSIGN(AidlVariableDeclaration);
419};
420
421class AidlArgument : public AidlVariableDeclaration {
Casey Dahlinbc7a50a2015-09-28 19:20:50 -0700422 public:
Casey Dahlinc378c992015-09-29 16:50:40 -0700423 enum Direction { IN_DIR = 1, OUT_DIR = 2, INOUT_DIR = 3 };
424
Steven Moreland46e9da82018-07-27 15:45:29 -0700425 AidlArgument(const AidlLocation& location, AidlArgument::Direction direction,
426 AidlTypeSpecifier* type, const std::string& name);
427 AidlArgument(const AidlLocation& location, AidlTypeSpecifier* type, const std::string& name);
Casey Dahlinbc7a50a2015-09-28 19:20:50 -0700428 virtual ~AidlArgument() = default;
429
Casey Dahlinc378c992015-09-29 16:50:40 -0700430 Direction GetDirection() const { return direction_; }
Christopher Wileyad339272015-10-05 19:11:58 -0700431 bool IsOut() const { return direction_ & OUT_DIR; }
432 bool IsIn() const { return direction_ & IN_DIR; }
Casey Dahlinc378c992015-09-29 16:50:40 -0700433 bool DirectionWasSpecified() const { return direction_specified_; }
Jiyong Park3656c3c2018-08-01 20:02:01 +0900434 string GetDirectionSpecifier() const;
Christopher Wileyad339272015-10-05 19:11:58 -0700435
Casey Dahlinc378c992015-09-29 16:50:40 -0700436 std::string ToString() const;
Jiyong Park02da7422018-07-16 16:00:26 +0900437 std::string Signature() const;
Casey Dahlinc378c992015-09-29 16:50:40 -0700438
Casey Dahlinbc7a50a2015-09-28 19:20:50 -0700439 private:
Casey Dahlinc378c992015-09-29 16:50:40 -0700440 Direction direction_;
441 bool direction_specified_;
442
Casey Dahlinbc7a50a2015-09-28 19:20:50 -0700443 DISALLOW_COPY_AND_ASSIGN(AidlArgument);
Casey Dahlina834dd42015-09-23 11:52:15 -0700444};
Adam Lesinskiffa16862014-01-23 18:17:42 -0800445
Casey Dahlind40e2fe2015-11-24 14:06:52 -0800446class AidlMethod;
Steven Moreland693640b2018-07-19 13:46:27 -0700447class AidlConstantDeclaration;
Daniel Norman85aed542019-08-21 12:01:14 -0700448class AidlEnumDeclaration;
Casey Dahlind40e2fe2015-11-24 14:06:52 -0800449class AidlMember : public AidlNode {
450 public:
Steven Moreland46e9da82018-07-27 15:45:29 -0700451 AidlMember(const AidlLocation& location);
Casey Dahlind40e2fe2015-11-24 14:06:52 -0800452 virtual ~AidlMember() = default;
453
454 virtual AidlMethod* AsMethod() { return nullptr; }
Steven Moreland693640b2018-07-19 13:46:27 -0700455 virtual AidlConstantDeclaration* AsConstantDeclaration() { return nullptr; }
Casey Dahlind40e2fe2015-11-24 14:06:52 -0800456
457 private:
458 DISALLOW_COPY_AND_ASSIGN(AidlMember);
459};
460
Will McVickerd7d18df2019-09-12 13:40:50 -0700461class AidlUnaryConstExpression;
462class AidlBinaryConstExpression;
463
Steven Moreland693640b2018-07-19 13:46:27 -0700464class AidlConstantValue : public AidlNode {
Casey Dahlind40e2fe2015-11-24 14:06:52 -0800465 public:
Will McVickerd7d18df2019-09-12 13:40:50 -0700466 enum class Type {
467 // WARNING: Don't change this order! The order is used to determine type
468 // promotion during a binary expression.
469 BOOLEAN,
470 INT8,
471 INT32,
472 INT64,
473 ARRAY,
474 CHARACTER,
475 STRING,
476 FLOATING,
477 UNARY,
478 BINARY,
479 ERROR,
480 };
481
482 /*
483 * Return the value casted to the given type.
484 */
485 template <typename T>
486 T cast() const;
Casey Dahlind40e2fe2015-11-24 14:06:52 -0800487
Steven Moreland693640b2018-07-19 13:46:27 -0700488 virtual ~AidlConstantValue() = default;
Casey Dahlind40e2fe2015-11-24 14:06:52 -0800489
Steven Moreland541788d2020-05-21 22:05:52 +0000490 // creates default value, when one isn't specified
491 // nullptr if no default available
492 static AidlConstantValue* Default(const AidlTypeSpecifier& specifier);
493
Steven Moreland25294322018-08-07 18:13:55 -0700494 static AidlConstantValue* Boolean(const AidlLocation& location, bool value);
495 static AidlConstantValue* Character(const AidlLocation& location, char value);
Steven Moreland25294322018-08-07 18:13:55 -0700496 // example: 123, -5498, maybe any size
Will McVickerd7d18df2019-09-12 13:40:50 -0700497 static AidlConstantValue* Integral(const AidlLocation& location, const string& value);
498 static AidlConstantValue* Floating(const AidlLocation& location, const std::string& value);
Steven Moreland860b1942018-08-16 14:59:28 -0700499 static AidlConstantValue* Array(const AidlLocation& location,
Will McVickerd7d18df2019-09-12 13:40:50 -0700500 std::unique_ptr<vector<unique_ptr<AidlConstantValue>>> values);
Steven Moreland693640b2018-07-19 13:46:27 -0700501 // example: "\"asdf\""
Will McVickerd7d18df2019-09-12 13:40:50 -0700502 static AidlConstantValue* String(const AidlLocation& location, const string& value);
Steven Moreland693640b2018-07-19 13:46:27 -0700503
Daniel Normanf0ca44f2019-10-25 09:59:44 -0700504 // Construct an AidlConstantValue by evaluating the other integral constant's
505 // value string. This does not preserve the structure of the copied constant.
Steven Moreland59e53e42019-11-26 20:38:08 -0800506 // Returns nullptr and logs if value cannot be copied.
Daniel Normanf0ca44f2019-10-25 09:59:44 -0700507 static AidlConstantValue* ShallowIntegralCopy(const AidlConstantValue& other);
Daniel Normanb28684e2019-10-17 15:31:39 -0700508
Will McVickerd7d18df2019-09-12 13:40:50 -0700509 Type GetType() const { return final_type_; }
Steven Moreland25294322018-08-07 18:13:55 -0700510
Will McVickerd7d18df2019-09-12 13:40:50 -0700511 virtual bool CheckValid() const;
Steven Moreland860b1942018-08-16 14:59:28 -0700512
513 // Raw value of type (currently valid in C++ and Java). Empty string on error.
Steven Moreland4bcb05c2019-11-27 18:57:47 -0800514 string ValueString(const AidlTypeSpecifier& type, const ConstantValueDecorator& decorator) const;
Casey Dahlind40e2fe2015-11-24 14:06:52 -0800515
516 private:
Will McVickerd7d18df2019-09-12 13:40:50 -0700517 AidlConstantValue(const AidlLocation& location, Type parsed_type, int64_t parsed_value,
518 const string& checked_value);
519 AidlConstantValue(const AidlLocation& location, Type type, const string& checked_value);
Steven Moreland860b1942018-08-16 14:59:28 -0700520 AidlConstantValue(const AidlLocation& location, Type type,
Will McVickerd7d18df2019-09-12 13:40:50 -0700521 std::unique_ptr<vector<unique_ptr<AidlConstantValue>>> values);
Steven Moreland25294322018-08-07 18:13:55 -0700522 static string ToString(Type type);
Will McVickerd7d18df2019-09-12 13:40:50 -0700523 static bool ParseIntegral(const string& value, int64_t* parsed_value, Type* parsed_type);
524 static bool IsHex(const string& value);
Steven Moreland4bcb05c2019-11-27 18:57:47 -0800525
Will McVickerd7d18df2019-09-12 13:40:50 -0700526 virtual bool evaluate(const AidlTypeSpecifier& type) const;
Casey Dahlind40e2fe2015-11-24 14:06:52 -0800527
Steven Moreland693640b2018-07-19 13:46:27 -0700528 const Type type_ = Type::ERROR;
Will McVickerd7d18df2019-09-12 13:40:50 -0700529 const vector<unique_ptr<AidlConstantValue>> values_; // if type_ == ARRAY
530 const string value_; // otherwise
531
532 // State for tracking evaluation of expressions
Steven Moreland4bcb05c2019-11-27 18:57:47 -0800533 mutable bool is_valid_ = false; // cache of CheckValid, but may be marked false in evaluate
534 mutable bool is_evaluated_ = false; // whether evaluate has been called
Will McVickerd7d18df2019-09-12 13:40:50 -0700535 mutable Type final_type_;
536 mutable int64_t final_value_;
537 mutable string final_string_value_ = "";
Steven Moreland693640b2018-07-19 13:46:27 -0700538
539 DISALLOW_COPY_AND_ASSIGN(AidlConstantValue);
Will McVickerd7d18df2019-09-12 13:40:50 -0700540
541 friend AidlUnaryConstExpression;
542 friend AidlBinaryConstExpression;
543};
544
545class AidlUnaryConstExpression : public AidlConstantValue {
546 public:
547 AidlUnaryConstExpression(const AidlLocation& location, const string& op,
548 std::unique_ptr<AidlConstantValue> rval);
549
550 static bool IsCompatibleType(Type type, const string& op);
551 bool CheckValid() const override;
Will McVickerd7d18df2019-09-12 13:40:50 -0700552 private:
553 bool evaluate(const AidlTypeSpecifier& type) const override;
554
555 std::unique_ptr<AidlConstantValue> unary_;
556 const string op_;
557};
558
559class AidlBinaryConstExpression : public AidlConstantValue {
560 public:
561 AidlBinaryConstExpression(const AidlLocation& location, std::unique_ptr<AidlConstantValue> lval,
562 const string& op, std::unique_ptr<AidlConstantValue> rval);
563
564 bool CheckValid() const override;
Will McVickerd7d18df2019-09-12 13:40:50 -0700565
566 static bool AreCompatibleTypes(Type t1, Type t2);
567 // Returns the promoted kind for both operands
568 static Type UsualArithmeticConversion(Type left, Type right);
569 // Returns the promoted integral type where INT32 is the smallest type
570 static Type IntegralPromotion(Type in);
571
572 private:
573 bool evaluate(const AidlTypeSpecifier& type) const override;
574
575 std::unique_ptr<AidlConstantValue> left_val_;
576 std::unique_ptr<AidlConstantValue> right_val_;
577 const string op_;
Christopher Wileyd6bdd8d2016-05-03 11:23:13 -0700578};
579
Andrei Onea9445fc62019-06-27 18:11:59 +0100580struct AidlAnnotationParameter {
581 std::string name;
582 std::unique_ptr<AidlConstantValue> value;
583};
584
Steven Moreland693640b2018-07-19 13:46:27 -0700585class AidlConstantDeclaration : public AidlMember {
Christopher Wileyd6bdd8d2016-05-03 11:23:13 -0700586 public:
Steven Moreland46e9da82018-07-27 15:45:29 -0700587 AidlConstantDeclaration(const AidlLocation& location, AidlTypeSpecifier* specifier,
Will McVickerd7d18df2019-09-12 13:40:50 -0700588 const string& name, AidlConstantValue* value);
Steven Moreland693640b2018-07-19 13:46:27 -0700589 virtual ~AidlConstantDeclaration() = default;
Christopher Wileyd6bdd8d2016-05-03 11:23:13 -0700590
Steven Moreland693640b2018-07-19 13:46:27 -0700591 const AidlTypeSpecifier& GetType() const { return *type_; }
Steven Moreland4d12f9a2018-10-31 14:30:55 -0700592 AidlTypeSpecifier* GetMutableType() { return type_.get(); }
Will McVickerd7d18df2019-09-12 13:40:50 -0700593 const string& GetName() const { return name_; }
Steven Moreland693640b2018-07-19 13:46:27 -0700594 const AidlConstantValue& GetValue() const { return *value_; }
Jeongik Chadb0f59e2018-11-01 18:11:21 +0900595 bool CheckValid(const AidlTypenames& typenames) const;
Christopher Wileyd6bdd8d2016-05-03 11:23:13 -0700596
Will McVickerd7d18df2019-09-12 13:40:50 -0700597 string ToString() const;
598 string Signature() const;
Steven Moreland860b1942018-08-16 14:59:28 -0700599 string ValueString(const ConstantValueDecorator& decorator) const {
Will McVickerd7d18df2019-09-12 13:40:50 -0700600 return value_->ValueString(GetType(), decorator);
Steven Moreland860b1942018-08-16 14:59:28 -0700601 }
Steven Moreland25294322018-08-07 18:13:55 -0700602
Steven Moreland693640b2018-07-19 13:46:27 -0700603 AidlConstantDeclaration* AsConstantDeclaration() override { return this; }
Christopher Wileyd6bdd8d2016-05-03 11:23:13 -0700604
605 private:
Steven Moreland693640b2018-07-19 13:46:27 -0700606 const unique_ptr<AidlTypeSpecifier> type_;
Will McVickerd7d18df2019-09-12 13:40:50 -0700607 const string name_;
608 unique_ptr<AidlConstantValue> value_;
Christopher Wileyd6bdd8d2016-05-03 11:23:13 -0700609
Steven Moreland693640b2018-07-19 13:46:27 -0700610 DISALLOW_COPY_AND_ASSIGN(AidlConstantDeclaration);
Casey Dahlind40e2fe2015-11-24 14:06:52 -0800611};
612
613class AidlMethod : public AidlMember {
Casey Dahlin5c69deb2015-10-01 14:44:12 -0700614 public:
Will McVickerd7d18df2019-09-12 13:40:50 -0700615 AidlMethod(const AidlLocation& location, bool oneway, AidlTypeSpecifier* type, const string& name,
616 vector<unique_ptr<AidlArgument>>* args, const string& comments);
617 AidlMethod(const AidlLocation& location, bool oneway, AidlTypeSpecifier* type, const string& name,
618 vector<unique_ptr<AidlArgument>>* args, const string& comments, int id,
619 bool is_user_defined = true);
Casey Dahlin5c69deb2015-10-01 14:44:12 -0700620 virtual ~AidlMethod() = default;
621
Casey Dahlind40e2fe2015-11-24 14:06:52 -0800622 AidlMethod* AsMethod() override { return this; }
Jeongik Cha997281d2020-01-16 15:23:59 +0900623 bool IsHidden() const;
Will McVickerd7d18df2019-09-12 13:40:50 -0700624 const string& GetComments() const { return comments_; }
Jiyong Parkd59a10d2018-07-18 11:12:55 +0900625 const AidlTypeSpecifier& GetType() const { return *type_; }
626 AidlTypeSpecifier* GetMutableType() { return type_.get(); }
Steven Morelandacd53472018-12-14 10:17:26 -0800627
Steven Moreland8c70ba92018-12-17 10:20:31 -0800628 // set if this method is part of an interface that is marked oneway
629 void ApplyInterfaceOneway(bool oneway) { oneway_ = oneway_ || oneway; }
Casey Dahlinf4a93112015-10-05 16:58:09 -0700630 bool IsOneway() const { return oneway_; }
Steven Morelandacd53472018-12-14 10:17:26 -0800631
Casey Dahlinf4a93112015-10-05 16:58:09 -0700632 const std::string& GetName() const { return name_; }
Casey Dahlinf4a93112015-10-05 16:58:09 -0700633 bool HasId() const { return has_id_; }
Jiyong Parked65bf42018-08-28 15:43:27 +0900634 int GetId() const { return id_; }
Casey Dahlinf4a93112015-10-05 16:58:09 -0700635 void SetId(unsigned id) { id_ = id; }
Casey Dahlinf2d23f72015-10-02 16:19:19 -0700636
Jiyong Park309668e2018-07-28 16:55:44 +0900637 bool IsUserDefined() const { return is_user_defined_; }
638
Casey Dahlinf4a93112015-10-05 16:58:09 -0700639 const std::vector<std::unique_ptr<AidlArgument>>& GetArguments() const {
Christopher Wileyad339272015-10-05 19:11:58 -0700640 return arguments_;
641 }
642 // An inout parameter will appear in both GetInArguments()
643 // and GetOutArguments(). AidlMethod retains ownership of the argument
644 // pointers returned in this way.
645 const std::vector<const AidlArgument*>& GetInArguments() const {
646 return in_arguments_;
647 }
648 const std::vector<const AidlArgument*>& GetOutArguments() const {
649 return out_arguments_;
Casey Dahlinf4a93112015-10-05 16:58:09 -0700650 }
Casey Dahlin5c69deb2015-10-01 14:44:12 -0700651
Jiyong Park309668e2018-07-28 16:55:44 +0900652 // name + type parameter types
653 // i.e, foo(int, String)
Jiyong Park02da7422018-07-16 16:00:26 +0900654 std::string Signature() const;
655
Jiyong Park309668e2018-07-28 16:55:44 +0900656 // return type + name + type parameter types + annotations
657 // i.e, boolean foo(int, @Nullable String)
658 std::string ToString() const;
659
Casey Dahlin5c69deb2015-10-01 14:44:12 -0700660 private:
Casey Dahlinf4a93112015-10-05 16:58:09 -0700661 bool oneway_;
Casey Dahlinf2d23f72015-10-02 16:19:19 -0700662 std::string comments_;
Jiyong Parkd59a10d2018-07-18 11:12:55 +0900663 std::unique_ptr<AidlTypeSpecifier> type_;
Casey Dahlinf4a93112015-10-05 16:58:09 -0700664 std::string name_;
Christopher Wileyad339272015-10-05 19:11:58 -0700665 const std::vector<std::unique_ptr<AidlArgument>> arguments_;
666 std::vector<const AidlArgument*> in_arguments_;
667 std::vector<const AidlArgument*> out_arguments_;
Casey Dahlinf4a93112015-10-05 16:58:09 -0700668 bool has_id_;
669 int id_;
Jiyong Park309668e2018-07-28 16:55:44 +0900670 bool is_user_defined_ = true;
Casey Dahlinf2d23f72015-10-02 16:19:19 -0700671
Casey Dahlin5c69deb2015-10-01 14:44:12 -0700672 DISALLOW_COPY_AND_ASSIGN(AidlMethod);
Casey Dahlin0a2f8be2015-09-28 16:15:29 -0700673};
Adam Lesinskiffa16862014-01-23 18:17:42 -0800674
Steven Morelandc258abc2018-07-10 14:03:38 -0700675class AidlDefinedType;
Jiyong Parkb034bf02018-07-30 17:44:33 +0900676class AidlInterface;
677class AidlParcelable;
678class AidlStructuredParcelable;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800679
Steven Moreland46e9da82018-07-27 15:45:29 -0700680class AidlInterface;
681class AidlParcelable;
682class AidlStructuredParcelable;
Daniel Norman85aed542019-08-21 12:01:14 -0700683// AidlDefinedType represents either an interface, parcelable, or enum that is
Jiyong Park1deecc32018-07-17 01:14:41 +0900684// defined in the source file.
685class AidlDefinedType : public AidlAnnotatable {
Steven Moreland787b0432018-07-03 09:00:58 -0700686 public:
Steven Moreland46e9da82018-07-27 15:45:29 -0700687 AidlDefinedType(const AidlLocation& location, const std::string& name,
Jiyong Park18132182020-06-08 20:24:40 +0900688 const std::string& comments, const std::string& package);
Steven Moreland787b0432018-07-03 09:00:58 -0700689 virtual ~AidlDefinedType() = default;
690
Jiyong Park1deecc32018-07-17 01:14:41 +0900691 const std::string& GetName() const { return name_; };
Jeongik Cha997281d2020-01-16 15:23:59 +0900692 bool IsHidden() const;
Jiyong Park1deecc32018-07-17 01:14:41 +0900693 const std::string& GetComments() const { return comments_; }
Jiyong Parka6605ab2018-11-11 14:30:21 +0900694 void SetComments(const std::string comments) { comments_ = comments; }
Jiyong Park1deecc32018-07-17 01:14:41 +0900695
Steven Moreland787b0432018-07-03 09:00:58 -0700696 /* dot joined package, example: "android.package.foo" */
Jiyong Park18132182020-06-08 20:24:40 +0900697 std::string GetPackage() const { return package_; }
Steven Moreland787b0432018-07-03 09:00:58 -0700698 /* dot joined package and name, example: "android.package.foo.IBar" */
699 std::string GetCanonicalName() const;
Jiyong Park18132182020-06-08 20:24:40 +0900700 const std::vector<std::string>& GetSplitPackage() const { return split_package_; }
Steven Moreland787b0432018-07-03 09:00:58 -0700701
Steven Morelanded83a282018-07-17 13:27:29 -0700702 virtual std::string GetPreprocessDeclarationName() const = 0;
Steven Morelandc258abc2018-07-10 14:03:38 -0700703
Steven Moreland5557f1c2018-07-02 13:50:23 -0700704 virtual const AidlStructuredParcelable* AsStructuredParcelable() const { return nullptr; }
Steven Morelandc258abc2018-07-10 14:03:38 -0700705 virtual const AidlParcelable* AsParcelable() const { return nullptr; }
Daniel Norman85aed542019-08-21 12:01:14 -0700706 virtual const AidlEnumDeclaration* AsEnumDeclaration() const { return nullptr; }
Steven Moreland5557f1c2018-07-02 13:50:23 -0700707 virtual const AidlInterface* AsInterface() const { return nullptr; }
Jeongik Chadf76dc72019-11-28 00:08:47 +0900708 virtual const AidlParameterizable<std::string>* AsParameterizable() const { return nullptr; }
Devin Moore24f68572020-02-26 13:20:59 -0800709 bool CheckValid(const AidlTypenames& typenames) const override;
Steven Morelandd59e3172020-05-11 16:42:09 -0700710 virtual bool LanguageSpecificCheckValid(const AidlTypenames& typenames,
711 Options::Language lang) const = 0;
Steven Morelandc258abc2018-07-10 14:03:38 -0700712 AidlStructuredParcelable* AsStructuredParcelable() {
713 return const_cast<AidlStructuredParcelable*>(
714 const_cast<const AidlDefinedType*>(this)->AsStructuredParcelable());
715 }
716 AidlParcelable* AsParcelable() {
717 return const_cast<AidlParcelable*>(const_cast<const AidlDefinedType*>(this)->AsParcelable());
718 }
Daniel Norman85aed542019-08-21 12:01:14 -0700719 AidlEnumDeclaration* AsEnumDeclaration() {
720 return const_cast<AidlEnumDeclaration*>(
721 const_cast<const AidlDefinedType*>(this)->AsEnumDeclaration());
722 }
Steven Morelandc258abc2018-07-10 14:03:38 -0700723 AidlInterface* AsInterface() {
724 return const_cast<AidlInterface*>(const_cast<const AidlDefinedType*>(this)->AsInterface());
725 }
726
Jeongik Chadf76dc72019-11-28 00:08:47 +0900727 AidlParameterizable<std::string>* AsParameterizable() {
728 return const_cast<AidlParameterizable<std::string>*>(
729 const_cast<const AidlDefinedType*>(this)->AsParameterizable());
730 }
731
Steven Moreland6cee3482018-07-18 14:39:58 -0700732 const AidlParcelable* AsUnstructuredParcelable() const {
733 if (this->AsStructuredParcelable() != nullptr) return nullptr;
734 return this->AsParcelable();
735 }
736 AidlParcelable* AsUnstructuredParcelable() {
737 return const_cast<AidlParcelable*>(
738 const_cast<const AidlDefinedType*>(this)->AsUnstructuredParcelable());
739 }
740
Jeongik Cha997281d2020-01-16 15:23:59 +0900741 virtual void Dump(CodeWriter* writer) const = 0;
Steven Morelanda5d9c5c2020-02-21 16:01:09 -0800742 void DumpHeader(CodeWriter* writer) const;
Jiyong Park02da7422018-07-16 16:00:26 +0900743
Steven Moreland787b0432018-07-03 09:00:58 -0700744 private:
Jiyong Park1deecc32018-07-17 01:14:41 +0900745 std::string name_;
Jiyong Park1deecc32018-07-17 01:14:41 +0900746 std::string comments_;
Jiyong Park18132182020-06-08 20:24:40 +0900747 const std::string package_;
748 const std::vector<std::string> split_package_;
Steven Moreland5557f1c2018-07-02 13:50:23 -0700749
750 DISALLOW_COPY_AND_ASSIGN(AidlDefinedType);
Jiyong Park8e79b7f2020-07-20 20:52:38 +0900751 AidlDefinedType(AidlDefinedType&&) = delete;
752 AidlDefinedType& operator=(AidlDefinedType&&) = delete;
Steven Moreland787b0432018-07-03 09:00:58 -0700753};
754
Jeongik Chadf76dc72019-11-28 00:08:47 +0900755class AidlParcelable : public AidlDefinedType, public AidlParameterizable<std::string> {
Casey Dahlin1ae2bc52015-10-07 18:49:10 -0700756 public:
Jiyong Park18132182020-06-08 20:24:40 +0900757 AidlParcelable(const AidlLocation& location, const std::string& name, const std::string& package,
758 const std::string& comments, const std::string& cpp_header = "",
Jeongik Chadf76dc72019-11-28 00:08:47 +0900759 std::vector<std::string>* type_params = nullptr);
Casey Dahlin1ae2bc52015-10-07 18:49:10 -0700760 virtual ~AidlParcelable() = default;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800761
Christopher Wiley8aa4d9f2015-11-16 19:10:45 -0800762 std::string GetCppHeader() const { return cpp_header_; }
Christopher Wiley8aa4d9f2015-11-16 19:10:45 -0800763
Steven Moreland0cea4aa2020-04-20 21:06:02 -0700764 std::set<AidlAnnotation::Type> GetSupportedAnnotations() const override;
Jeongik Cha82317dd2019-02-27 20:26:11 +0900765 bool CheckValid(const AidlTypenames& typenames) const override;
Steven Morelandd59e3172020-05-11 16:42:09 -0700766 bool LanguageSpecificCheckValid(const AidlTypenames& typenames,
767 Options::Language lang) const override;
Steven Morelandc258abc2018-07-10 14:03:38 -0700768 const AidlParcelable* AsParcelable() const override { return this; }
Jeongik Chadf76dc72019-11-28 00:08:47 +0900769 const AidlParameterizable<std::string>* AsParameterizable() const override { return this; }
770 const AidlNode& AsAidlNode() const override { return *this; }
Steven Morelanded83a282018-07-17 13:27:29 -0700771 std::string GetPreprocessDeclarationName() const override { return "parcelable"; }
Steven Morelandc258abc2018-07-10 14:03:38 -0700772
Jeongik Cha997281d2020-01-16 15:23:59 +0900773 void Dump(CodeWriter* writer) const override;
Jiyong Park02da7422018-07-16 16:00:26 +0900774
Casey Dahlin1ae2bc52015-10-07 18:49:10 -0700775 private:
Christopher Wiley8aa4d9f2015-11-16 19:10:45 -0800776 std::string cpp_header_;
Casey Dahlin59401da2015-10-09 18:16:45 -0700777
Casey Dahlin1ae2bc52015-10-07 18:49:10 -0700778 DISALLOW_COPY_AND_ASSIGN(AidlParcelable);
Casey Dahlin0a2f8be2015-09-28 16:15:29 -0700779};
Adam Lesinskiffa16862014-01-23 18:17:42 -0800780
Steven Moreland5557f1c2018-07-02 13:50:23 -0700781class AidlStructuredParcelable : public AidlParcelable {
782 public:
Jiyong Park18132182020-06-08 20:24:40 +0900783 AidlStructuredParcelable(const AidlLocation& location, const std::string& name,
784 const std::string& package, const std::string& comments,
Steven Moreland5557f1c2018-07-02 13:50:23 -0700785 std::vector<std::unique_ptr<AidlVariableDeclaration>>* variables);
786
787 const std::vector<std::unique_ptr<AidlVariableDeclaration>>& GetFields() const {
788 return variables_;
789 }
790
Steven Morelandc258abc2018-07-10 14:03:38 -0700791 const AidlStructuredParcelable* AsStructuredParcelable() const override { return this; }
Steven Morelanded83a282018-07-17 13:27:29 -0700792 std::string GetPreprocessDeclarationName() const override { return "structured_parcelable"; }
Steven Moreland5557f1c2018-07-02 13:50:23 -0700793
Jeongik Cha997281d2020-01-16 15:23:59 +0900794 void Dump(CodeWriter* writer) const override;
Jiyong Park02da7422018-07-16 16:00:26 +0900795
Steven Moreland0cea4aa2020-04-20 21:06:02 -0700796 std::set<AidlAnnotation::Type> GetSupportedAnnotations() const override;
Jeongik Chadb0f59e2018-11-01 18:11:21 +0900797 bool CheckValid(const AidlTypenames& typenames) const override;
Steven Morelandd59e3172020-05-11 16:42:09 -0700798 bool LanguageSpecificCheckValid(const AidlTypenames& typenames,
799 Options::Language lang) const override;
Jeongik Chadb0f59e2018-11-01 18:11:21 +0900800
Steven Moreland5557f1c2018-07-02 13:50:23 -0700801 private:
802 const std::vector<std::unique_ptr<AidlVariableDeclaration>> variables_;
803
804 DISALLOW_COPY_AND_ASSIGN(AidlStructuredParcelable);
805};
806
Daniel Norman85aed542019-08-21 12:01:14 -0700807class AidlEnumerator : public AidlNode {
808 public:
Daniel Norman2e4112d2019-10-03 10:22:35 -0700809 AidlEnumerator(const AidlLocation& location, const std::string& name, AidlConstantValue* value,
810 const std::string& comments);
Daniel Norman85aed542019-08-21 12:01:14 -0700811 virtual ~AidlEnumerator() = default;
812
813 const std::string& GetName() const { return name_; }
Will McVickerd7d18df2019-09-12 13:40:50 -0700814 AidlConstantValue* GetValue() const { return value_.get(); }
Daniel Norman2e4112d2019-10-03 10:22:35 -0700815 const std::string& GetComments() const { return comments_; }
Daniel Norman85aed542019-08-21 12:01:14 -0700816 bool CheckValid(const AidlTypeSpecifier& enum_backing_type) const;
817
818 string ValueString(const AidlTypeSpecifier& backing_type,
819 const ConstantValueDecorator& decorator) const;
820
Daniel Normanb28684e2019-10-17 15:31:39 -0700821 void SetValue(std::unique_ptr<AidlConstantValue> value) { value_ = std::move(value); }
822
Daniel Norman85aed542019-08-21 12:01:14 -0700823 private:
824 const std::string name_;
Will McVickerd7d18df2019-09-12 13:40:50 -0700825 unique_ptr<AidlConstantValue> value_;
Daniel Norman2e4112d2019-10-03 10:22:35 -0700826 const std::string comments_;
Daniel Norman85aed542019-08-21 12:01:14 -0700827
828 DISALLOW_COPY_AND_ASSIGN(AidlEnumerator);
829};
830
831class AidlEnumDeclaration : public AidlDefinedType {
832 public:
Will McVickerd7d18df2019-09-12 13:40:50 -0700833 AidlEnumDeclaration(const AidlLocation& location, const string& name,
Daniel Norman85aed542019-08-21 12:01:14 -0700834 std::vector<std::unique_ptr<AidlEnumerator>>* enumerators,
Jiyong Park18132182020-06-08 20:24:40 +0900835 const std::string& package, const std::string& comments);
Daniel Norman85aed542019-08-21 12:01:14 -0700836 virtual ~AidlEnumDeclaration() = default;
837
838 void SetBackingType(std::unique_ptr<const AidlTypeSpecifier> type);
839 const AidlTypeSpecifier& GetBackingType() const { return *backing_type_; }
840 const std::vector<std::unique_ptr<AidlEnumerator>>& GetEnumerators() const {
841 return enumerators_;
842 }
Steven Moreland59e53e42019-11-26 20:38:08 -0800843 bool Autofill();
Steven Moreland0cea4aa2020-04-20 21:06:02 -0700844 std::set<AidlAnnotation::Type> GetSupportedAnnotations() const override;
Daniel Norman85aed542019-08-21 12:01:14 -0700845 bool CheckValid(const AidlTypenames& typenames) const override;
Steven Morelandd59e3172020-05-11 16:42:09 -0700846 bool LanguageSpecificCheckValid(const AidlTypenames& /*typenames*/,
847 Options::Language) const override {
848 return true;
849 }
Daniel Norman85aed542019-08-21 12:01:14 -0700850 std::string GetPreprocessDeclarationName() const override { return "enum"; }
Jeongik Cha997281d2020-01-16 15:23:59 +0900851 void Dump(CodeWriter* writer) const override;
Daniel Norman85aed542019-08-21 12:01:14 -0700852
853 const AidlEnumDeclaration* AsEnumDeclaration() const override { return this; }
854
855 private:
856 const std::string name_;
857 const std::vector<std::unique_ptr<AidlEnumerator>> enumerators_;
858 std::unique_ptr<const AidlTypeSpecifier> backing_type_;
859
860 DISALLOW_COPY_AND_ASSIGN(AidlEnumDeclaration);
861};
862
Jiyong Park1deecc32018-07-17 01:14:41 +0900863class AidlInterface final : public AidlDefinedType {
Casey Dahlin1ae2bc52015-10-07 18:49:10 -0700864 public:
Steven Moreland46e9da82018-07-27 15:45:29 -0700865 AidlInterface(const AidlLocation& location, const std::string& name, const std::string& comments,
866 bool oneway_, std::vector<std::unique_ptr<AidlMember>>* members,
Jiyong Park18132182020-06-08 20:24:40 +0900867 const std::string& package);
Casey Dahlin1ae2bc52015-10-07 18:49:10 -0700868 virtual ~AidlInterface() = default;
869
Casey Dahlinfb7da2e2015-10-08 17:26:09 -0700870 const std::vector<std::unique_ptr<AidlMethod>>& GetMethods() const
871 { return methods_; }
Jiyong Park309668e2018-07-28 16:55:44 +0900872 std::vector<std::unique_ptr<AidlMethod>>& GetMutableMethods() { return methods_; }
Steven Moreland693640b2018-07-19 13:46:27 -0700873 const std::vector<std::unique_ptr<AidlConstantDeclaration>>& GetConstantDeclarations() const {
874 return constants_;
875 }
Casey Dahlina2f77c42015-12-01 18:26:02 -0800876
Steven Morelandc258abc2018-07-10 14:03:38 -0700877 const AidlInterface* AsInterface() const override { return this; }
Steven Morelanded83a282018-07-17 13:27:29 -0700878 std::string GetPreprocessDeclarationName() const override { return "interface"; }
Steven Moreland5557f1c2018-07-02 13:50:23 -0700879
Jeongik Cha997281d2020-01-16 15:23:59 +0900880 void Dump(CodeWriter* writer) const override;
Jiyong Park02da7422018-07-16 16:00:26 +0900881
Steven Moreland0cea4aa2020-04-20 21:06:02 -0700882 std::set<AidlAnnotation::Type> GetSupportedAnnotations() const override;
Jeongik Chadb0f59e2018-11-01 18:11:21 +0900883 bool CheckValid(const AidlTypenames& typenames) const override;
Steven Morelandd59e3172020-05-11 16:42:09 -0700884 bool LanguageSpecificCheckValid(const AidlTypenames& typenames,
885 Options::Language lang) const override;
Jeongik Chadb0f59e2018-11-01 18:11:21 +0900886
Casey Dahlin1ae2bc52015-10-07 18:49:10 -0700887 private:
Casey Dahlinfb7da2e2015-10-08 17:26:09 -0700888 std::vector<std::unique_ptr<AidlMethod>> methods_;
Steven Moreland693640b2018-07-19 13:46:27 -0700889 std::vector<std::unique_ptr<AidlConstantDeclaration>> constants_;
Casey Dahlinfb7da2e2015-10-08 17:26:09 -0700890
Casey Dahlin1ae2bc52015-10-07 18:49:10 -0700891 DISALLOW_COPY_AND_ASSIGN(AidlInterface);
Casey Dahlin0a2f8be2015-09-28 16:15:29 -0700892};
Adam Lesinskiffa16862014-01-23 18:17:42 -0800893
Casey Dahlin0edf3422015-10-07 12:34:59 -0700894class AidlImport : public AidlNode {
895 public:
Steven Moreland46e9da82018-07-27 15:45:29 -0700896 AidlImport(const AidlLocation& location, const std::string& needed_class);
Casey Dahlin0edf3422015-10-07 12:34:59 -0700897 virtual ~AidlImport() = default;
898
Casey Dahlin0edf3422015-10-07 12:34:59 -0700899 const std::string& GetFilename() const { return filename_; }
900 const std::string& GetNeededClass() const { return needed_class_; }
Casey Dahlin0edf3422015-10-07 12:34:59 -0700901
902 private:
Casey Dahlin0edf3422015-10-07 12:34:59 -0700903 std::string filename_;
904 std::string needed_class_;
Casey Dahlin0edf3422015-10-07 12:34:59 -0700905
906 DISALLOW_COPY_AND_ASSIGN(AidlImport);
Casey Dahline2507492015-09-14 17:11:20 -0700907};
908
Jiyong Park62515512020-06-08 15:57:11 +0900909// AidlDocument models an AIDL file
910class AidlDocument : public AidlNode {
911 public:
912 AidlDocument(const AidlLocation& location, std::vector<std::unique_ptr<AidlImport>>& imports,
Jiyong Park8e79b7f2020-07-20 20:52:38 +0900913 std::vector<std::unique_ptr<AidlDefinedType>>&& defined_types)
914 : AidlNode(location),
915 imports_(std::move(imports)),
916 defined_types_(std::move(defined_types)) {}
Jiyong Park62515512020-06-08 15:57:11 +0900917 const std::vector<std::unique_ptr<AidlImport>>& Imports() const { return imports_; }
Jiyong Park8e79b7f2020-07-20 20:52:38 +0900918 const std::vector<std::unique_ptr<AidlDefinedType>>& DefinedTypes() const {
919 return defined_types_;
920 }
921 AidlDocument(const AidlDocument&) = delete;
922 AidlDocument(AidlDocument&&) = delete;
923 AidlDocument& operator=(const AidlDocument&) = delete;
924 AidlDocument& operator=(AidlDocument&&) = delete;
925 ~AidlDocument() = default;
Jiyong Park62515512020-06-08 15:57:11 +0900926
927 private:
928 const std::vector<std::unique_ptr<AidlImport>> imports_;
Jiyong Park8e79b7f2020-07-20 20:52:38 +0900929 const std::vector<std::unique_ptr<AidlDefinedType>> defined_types_;
Jiyong Park62515512020-06-08 15:57:11 +0900930};