blob: 699880f7e2ada191048f0a4f52c7cd22bcdfac77 [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
Steven Morelandb0d15a52020-03-31 14:03:47 -0700167#define AIDL_ERROR(CONTEXT) ::AidlErrorLog(false /*fatal*/, (CONTEXT)).os_
168#define AIDL_FATAL(CONTEXT) ::AidlErrorLog(true /*fatal*/, (CONTEXT)).os_
Steven Moreland3f658cf2018-08-20 13:40:54 -0700169#define AIDL_FATAL_IF(CONDITION, CONTEXT) \
170 if (CONDITION) AIDL_FATAL(CONTEXT) << "Bad internal state: " << #CONDITION << ": "
Steven Moreland46e9da82018-07-27 15:45:29 -0700171
Casey Dahlina2f77c42015-12-01 18:26:02 -0800172namespace android {
173namespace aidl {
174
Jiyong Park1deecc32018-07-17 01:14:41 +0900175class AidlTypenames;
Casey Dahlina2f77c42015-12-01 18:26:02 -0800176
177} // namespace aidl
178} // namespace android
179
Jeongik Chadf76dc72019-11-28 00:08:47 +0900180// unique_ptr<AidlTypeSpecifier> for type arugment,
181// std::string for type parameter(T, U, and so on).
182template <typename T>
183class AidlParameterizable {
184 public:
185 AidlParameterizable(std::vector<T>* type_params) : type_params_(type_params) {}
186 virtual ~AidlParameterizable() = default;
187 bool IsGeneric() const { return type_params_ != nullptr; }
188 const std::vector<T>& GetTypeParameters() const { return *type_params_; }
189 bool CheckValid() const;
190
191 virtual const AidlNode& AsAidlNode() const = 0;
192
193 protected:
194 AidlParameterizable(const AidlParameterizable&);
195
196 private:
197 const unique_ptr<std::vector<T>> type_params_;
198 static_assert(std::is_same<T, unique_ptr<AidlTypeSpecifier>>::value ||
199 std::is_same<T, std::string>::value);
200};
201template <>
202bool AidlParameterizable<std::string>::CheckValid() const;
203
Andrei Onea9445fc62019-06-27 18:11:59 +0100204class AidlConstantValue;
205class AidlConstantDeclaration;
206
207// Transforms a value string into a language specific form. Raw value as produced by
208// AidlConstantValue.
209using ConstantValueDecorator =
210 std::function<std::string(const AidlTypeSpecifier& type, const std::string& raw_value)>;
211
Jiyong Park68bc77a2018-07-19 19:00:45 +0900212class AidlAnnotation : public AidlNode {
213 public:
Steven Moreland0cea4aa2020-04-20 21:06:02 -0700214 enum class Type {
215 BACKING = 1,
216 HIDE,
217 JAVA_STABLE_PARCELABLE,
218 UNSUPPORTED_APP_USAGE,
219 VINTF_STABILITY,
220 NULLABLE,
221 UTF8_IN_CPP,
Jiyong Parkbf5fd5c2020-06-05 19:48:05 +0900222 JAVA_PASSTHROUGH,
Steven Moreland0cea4aa2020-04-20 21:06:02 -0700223 };
224 static std::string TypeToString(Type type);
225
Andrei Onea9445fc62019-06-27 18:11:59 +0100226 static AidlAnnotation* Parse(
227 const AidlLocation& location, const string& name,
228 std::map<std::string, std::shared_ptr<AidlConstantValue>>* parameter_list);
Steven Moreland46e9da82018-07-27 15:45:29 -0700229
Steven Moreland3f658cf2018-08-20 13:40:54 -0700230 AidlAnnotation(const AidlAnnotation&) = default;
Steven Moreland3be75772018-08-20 13:27:43 -0700231 AidlAnnotation(AidlAnnotation&&) = default;
Jiyong Park68bc77a2018-07-19 19:00:45 +0900232 virtual ~AidlAnnotation() = default;
Andrei Onea9445fc62019-06-27 18:11:59 +0100233 bool CheckValid() const;
Steven Moreland3be75772018-08-20 13:27:43 -0700234
Steven Moreland0cea4aa2020-04-20 21:06:02 -0700235 const string& GetName() const { return schema_.name; };
236 const Type& GetType() const { return schema_.type; }
Daniel Norman37d43dd2019-09-09 17:22:34 -0700237 string ToString(const ConstantValueDecorator& decorator) const;
Andrei Onea9445fc62019-06-27 18:11:59 +0100238 std::map<std::string, std::string> AnnotationParams(
239 const ConstantValueDecorator& decorator) const;
Jiyong Parka6605ab2018-11-11 14:30:21 +0900240 const string& GetComments() const { return comments_; }
241 void SetComments(const string& comments) { comments_ = comments; }
Jiyong Park68bc77a2018-07-19 19:00:45 +0900242
243 private:
Steven Moreland0cea4aa2020-04-20 21:06:02 -0700244 struct Schema {
245 AidlAnnotation::Type type;
246
247 // text name in .aidl file, e.g. "nullable"
248 std::string name;
249
250 // map from param name -> value type
251 std::map<std::string, std::string> supported_parameters;
252 };
253 static const std::vector<Schema>& AllSchemas();
254
255 AidlAnnotation(const AidlLocation& location, const Schema& schema,
Andrei Onea9445fc62019-06-27 18:11:59 +0100256 std::map<std::string, std::shared_ptr<AidlConstantValue>>&& parameters);
Steven Moreland0cea4aa2020-04-20 21:06:02 -0700257
258 const Schema& schema_;
Jiyong Parka6605ab2018-11-11 14:30:21 +0900259 string comments_;
Andrei Onea9445fc62019-06-27 18:11:59 +0100260 std::map<std::string, std::shared_ptr<AidlConstantValue>> parameters_;
Jiyong Park68bc77a2018-07-19 19:00:45 +0900261};
262
Steven Moreland3be75772018-08-20 13:27:43 -0700263static inline bool operator<(const AidlAnnotation& lhs, const AidlAnnotation& rhs) {
264 return lhs.GetName() < rhs.GetName();
265}
266static inline bool operator==(const AidlAnnotation& lhs, const AidlAnnotation& rhs) {
267 return lhs.GetName() == rhs.GetName();
268}
Jiyong Park3656c3c2018-08-01 20:02:01 +0900269
Casey Dahline7922932016-02-29 17:23:01 -0800270class AidlAnnotatable : public AidlNode {
Casey Dahlin0ee37582015-09-30 16:31:55 -0700271 public:
Steven Moreland46e9da82018-07-27 15:45:29 -0700272 AidlAnnotatable(const AidlLocation& location);
Steven Moreland3f658cf2018-08-20 13:40:54 -0700273
274 AidlAnnotatable(const AidlAnnotatable&) = default;
275 AidlAnnotatable(AidlAnnotatable&&) = default;
Casey Dahline7922932016-02-29 17:23:01 -0800276 virtual ~AidlAnnotatable() = default;
277
Artur Satayev91fe8712019-07-29 13:06:01 +0100278 void Annotate(vector<AidlAnnotation>&& annotations) {
279 for (auto& annotation : annotations) {
280 annotations_.emplace_back(std::move(annotation));
281 }
282 }
Jiyong Park68bc77a2018-07-19 19:00:45 +0900283 bool IsNullable() const;
Jiyong Park68bc77a2018-07-19 19:00:45 +0900284 bool IsUtf8InCpp() const;
Steven Morelanda57d0a62019-07-30 09:41:14 -0700285 bool IsVintfStability() const;
Jeongik Cha88f95a82020-01-15 13:02:16 +0900286 bool IsStableApiParcelable(Options::Language lang) const;
Makoto Onuki78a1c1c2020-03-04 16:57:23 -0800287 bool IsHide() const;
Andrei Onea9445fc62019-06-27 18:11:59 +0100288
Steven Moreland7e4b9502020-02-20 18:10:42 -0800289 void DumpAnnotations(CodeWriter* writer) const;
290
Andrei Onea9445fc62019-06-27 18:11:59 +0100291 const AidlAnnotation* UnsupportedAppUsage() const;
Jiyong Parkbf5fd5c2020-06-05 19:48:05 +0900292 const AidlAnnotation* JavaPassthrough() const;
Daniel Norman716d3112019-09-10 13:11:56 -0700293 const AidlTypeSpecifier* BackingType(const AidlTypenames& typenames) const;
Jiyong Park68bc77a2018-07-19 19:00:45 +0900294 std::string ToString() const;
Casey Dahline7922932016-02-29 17:23:01 -0800295
Jiyong Parka6605ab2018-11-11 14:30:21 +0900296 const vector<AidlAnnotation>& GetAnnotations() const { return annotations_; }
Devin Moore24f68572020-02-26 13:20:59 -0800297 virtual bool CheckValid(const AidlTypenames&) const;
Jiyong Park3656c3c2018-08-01 20:02:01 +0900298
Steven Moreland181144c2020-04-20 19:57:56 -0700299 protected:
Steven Moreland0cea4aa2020-04-20 21:06:02 -0700300 virtual std::set<AidlAnnotation::Type> GetSupportedAnnotations() const = 0;
Steven Moreland181144c2020-04-20 19:57:56 -0700301
Casey Dahline7922932016-02-29 17:23:01 -0800302 private:
Jiyong Parka6605ab2018-11-11 14:30:21 +0900303 vector<AidlAnnotation> annotations_;
Casey Dahline7922932016-02-29 17:23:01 -0800304};
305
Jiyong Park1deecc32018-07-17 01:14:41 +0900306// AidlTypeSpecifier represents a reference to either a built-in type,
307// a defined type, or a variant (e.g., array of generic) of a type.
Jeongik Chadf76dc72019-11-28 00:08:47 +0900308class AidlTypeSpecifier final : public AidlAnnotatable,
309 public AidlParameterizable<unique_ptr<AidlTypeSpecifier>> {
Casey Dahline7922932016-02-29 17:23:01 -0800310 public:
Steven Moreland46e9da82018-07-27 15:45:29 -0700311 AidlTypeSpecifier(const AidlLocation& location, const string& unresolved_name, bool is_array,
312 vector<unique_ptr<AidlTypeSpecifier>>* type_params, const string& comments);
Jiyong Parkd59a10d2018-07-18 11:12:55 +0900313 virtual ~AidlTypeSpecifier() = default;
Casey Dahlin0ee37582015-09-30 16:31:55 -0700314
Steven Moreland3f658cf2018-08-20 13:40:54 -0700315 // Copy of this type which is not an array.
316 AidlTypeSpecifier ArrayBase() const;
317
Jiyong Park1deecc32018-07-17 01:14:41 +0900318 // Returns the full-qualified name of the base type.
319 // int -> int
320 // int[] -> int
321 // List<String> -> List
322 // IFoo -> foo.bar.IFoo (if IFoo is in package foo.bar)
323 const string& GetName() const {
324 if (IsResolved()) {
325 return fully_qualified_name_;
326 } else {
327 return GetUnresolvedName();
328 }
329 }
Casey Dahlin0ee37582015-09-30 16:31:55 -0700330
Jiyong Park1deecc32018-07-17 01:14:41 +0900331 // Returns string representation of this type specifier.
Artur Satayev91fe8712019-07-29 13:06:01 +0100332 // This is GetBaseTypeName() + array modifier or generic type parameters
Jiyong Park1deecc32018-07-17 01:14:41 +0900333 string ToString() const;
334
Jiyong Park02da7422018-07-16 16:00:26 +0900335 std::string Signature() const;
336
Jiyong Park1deecc32018-07-17 01:14:41 +0900337 const string& GetUnresolvedName() const { return unresolved_name_; }
338
Jeongik Cha997281d2020-01-16 15:23:59 +0900339 bool IsHidden() const;
340
Jiyong Park1deecc32018-07-17 01:14:41 +0900341 const string& GetComments() const { return comments_; }
342
Jeongik Cha1a7ab642019-07-29 17:31:02 +0900343 const std::vector<std::string> GetSplitName() const { return split_name_; }
344
Jiyong Parka6605ab2018-11-11 14:30:21 +0900345 void SetComments(const string& comment) { comments_ = comment; }
346
Jiyong Park1deecc32018-07-17 01:14:41 +0900347 bool IsResolved() const { return fully_qualified_name_ != ""; }
348
349 bool IsArray() const { return is_array_; }
350
Jiyong Park1deecc32018-07-17 01:14:41 +0900351 // Resolve the base type name to a fully-qualified name. Return false if the
352 // resolution fails.
Daniel Norman716d3112019-09-10 13:11:56 -0700353 bool Resolve(const AidlTypenames& typenames);
Casey Dahlin0ee37582015-09-30 16:31:55 -0700354
Steven Moreland0cea4aa2020-04-20 21:06:02 -0700355 std::set<AidlAnnotation::Type> GetSupportedAnnotations() const override;
Devin Moore24f68572020-02-26 13:20:59 -0800356 bool CheckValid(const AidlTypenames& typenames) const override;
Steven Morelandd59e3172020-05-11 16:42:09 -0700357 bool LanguageSpecificCheckValid(const AidlTypenames& typenames, Options::Language lang) const;
Jeongik Chadf76dc72019-11-28 00:08:47 +0900358 const AidlNode& AsAidlNode() const override { return *this; }
Jiyong Park1d2df7d2018-07-23 15:22:50 +0900359
Casey Dahlin0ee37582015-09-30 16:31:55 -0700360 private:
Steven Moreland3f658cf2018-08-20 13:40:54 -0700361 AidlTypeSpecifier(const AidlTypeSpecifier&) = default;
362
Jiyong Park1deecc32018-07-17 01:14:41 +0900363 const string unresolved_name_;
364 string fully_qualified_name_;
Steven Moreland3f658cf2018-08-20 13:40:54 -0700365 bool is_array_;
Jiyong Parka6605ab2018-11-11 14:30:21 +0900366 string comments_;
Jeongik Cha1a7ab642019-07-29 17:31:02 +0900367 vector<string> split_name_;
Casey Dahlin0ee37582015-09-30 16:31:55 -0700368};
369
Steven Moreland860b1942018-08-16 14:59:28 -0700370// Returns the universal value unaltered.
371std::string AidlConstantValueDecorator(const AidlTypeSpecifier& type, const std::string& raw_value);
372
Steven Moreland9ea10e32018-07-19 15:26:09 -0700373class AidlConstantValue;
Steven Moreland541788d2020-05-21 22:05:52 +0000374// TODO: This class is used for method arguments and also parcelable fields,
375// and it should be split up since default values don't apply to method
376// arguments
Steven Moreland5557f1c2018-07-02 13:50:23 -0700377class AidlVariableDeclaration : public AidlNode {
378 public:
Steven Moreland46e9da82018-07-27 15:45:29 -0700379 AidlVariableDeclaration(const AidlLocation& location, AidlTypeSpecifier* type,
380 const std::string& name);
381 AidlVariableDeclaration(const AidlLocation& location, AidlTypeSpecifier* type,
382 const std::string& name, AidlConstantValue* default_value);
Steven Moreland5557f1c2018-07-02 13:50:23 -0700383 virtual ~AidlVariableDeclaration() = default;
384
385 std::string GetName() const { return name_; }
Jiyong Parkd59a10d2018-07-18 11:12:55 +0900386 const AidlTypeSpecifier& GetType() const { return *type_; }
Steven Moreland541788d2020-05-21 22:05:52 +0000387 // if this was constructed explicitly with a default value
388 bool IsDefaultUserSpecified() const { return default_user_specified_; }
389 // will return the default value this is constructed with or a default value
390 // if one is available
Steven Moreland9ea10e32018-07-19 15:26:09 -0700391 const AidlConstantValue* GetDefaultValue() const { return default_value_.get(); }
392
Jiyong Parkd59a10d2018-07-18 11:12:55 +0900393 AidlTypeSpecifier* GetMutableType() { return type_.get(); }
Steven Moreland5557f1c2018-07-02 13:50:23 -0700394
Jeongik Chadb0f59e2018-11-01 18:11:21 +0900395 bool CheckValid(const AidlTypenames& typenames) const;
Steven Moreland5557f1c2018-07-02 13:50:23 -0700396 std::string ToString() const;
Jiyong Park02da7422018-07-16 16:00:26 +0900397 std::string Signature() const;
Steven Moreland5557f1c2018-07-02 13:50:23 -0700398
Steven Moreland860b1942018-08-16 14:59:28 -0700399 std::string ValueString(const ConstantValueDecorator& decorator) const;
Steven Moreland25294322018-08-07 18:13:55 -0700400
Steven Moreland5557f1c2018-07-02 13:50:23 -0700401 private:
Jiyong Parkd59a10d2018-07-18 11:12:55 +0900402 std::unique_ptr<AidlTypeSpecifier> type_;
Steven Moreland5557f1c2018-07-02 13:50:23 -0700403 std::string name_;
Steven Moreland541788d2020-05-21 22:05:52 +0000404 bool default_user_specified_;
Steven Moreland9ea10e32018-07-19 15:26:09 -0700405 std::unique_ptr<AidlConstantValue> default_value_;
Steven Moreland5557f1c2018-07-02 13:50:23 -0700406
407 DISALLOW_COPY_AND_ASSIGN(AidlVariableDeclaration);
408};
409
410class AidlArgument : public AidlVariableDeclaration {
Casey Dahlinbc7a50a2015-09-28 19:20:50 -0700411 public:
Casey Dahlinc378c992015-09-29 16:50:40 -0700412 enum Direction { IN_DIR = 1, OUT_DIR = 2, INOUT_DIR = 3 };
413
Steven Moreland46e9da82018-07-27 15:45:29 -0700414 AidlArgument(const AidlLocation& location, AidlArgument::Direction direction,
415 AidlTypeSpecifier* type, const std::string& name);
416 AidlArgument(const AidlLocation& location, AidlTypeSpecifier* type, const std::string& name);
Casey Dahlinbc7a50a2015-09-28 19:20:50 -0700417 virtual ~AidlArgument() = default;
418
Casey Dahlinc378c992015-09-29 16:50:40 -0700419 Direction GetDirection() const { return direction_; }
Christopher Wileyad339272015-10-05 19:11:58 -0700420 bool IsOut() const { return direction_ & OUT_DIR; }
421 bool IsIn() const { return direction_ & IN_DIR; }
Casey Dahlinc378c992015-09-29 16:50:40 -0700422 bool DirectionWasSpecified() const { return direction_specified_; }
Jiyong Park3656c3c2018-08-01 20:02:01 +0900423 string GetDirectionSpecifier() const;
Christopher Wileyad339272015-10-05 19:11:58 -0700424
Casey Dahlinc378c992015-09-29 16:50:40 -0700425 std::string ToString() const;
Jiyong Park02da7422018-07-16 16:00:26 +0900426 std::string Signature() const;
Casey Dahlinc378c992015-09-29 16:50:40 -0700427
Casey Dahlinbc7a50a2015-09-28 19:20:50 -0700428 private:
Casey Dahlinc378c992015-09-29 16:50:40 -0700429 Direction direction_;
430 bool direction_specified_;
431
Casey Dahlinbc7a50a2015-09-28 19:20:50 -0700432 DISALLOW_COPY_AND_ASSIGN(AidlArgument);
Casey Dahlina834dd42015-09-23 11:52:15 -0700433};
Adam Lesinskiffa16862014-01-23 18:17:42 -0800434
Casey Dahlind40e2fe2015-11-24 14:06:52 -0800435class AidlMethod;
Steven Moreland693640b2018-07-19 13:46:27 -0700436class AidlConstantDeclaration;
Daniel Norman85aed542019-08-21 12:01:14 -0700437class AidlEnumDeclaration;
Casey Dahlind40e2fe2015-11-24 14:06:52 -0800438class AidlMember : public AidlNode {
439 public:
Steven Moreland46e9da82018-07-27 15:45:29 -0700440 AidlMember(const AidlLocation& location);
Casey Dahlind40e2fe2015-11-24 14:06:52 -0800441 virtual ~AidlMember() = default;
442
443 virtual AidlMethod* AsMethod() { return nullptr; }
Steven Moreland693640b2018-07-19 13:46:27 -0700444 virtual AidlConstantDeclaration* AsConstantDeclaration() { return nullptr; }
Casey Dahlind40e2fe2015-11-24 14:06:52 -0800445
446 private:
447 DISALLOW_COPY_AND_ASSIGN(AidlMember);
448};
449
Will McVickerd7d18df2019-09-12 13:40:50 -0700450class AidlUnaryConstExpression;
451class AidlBinaryConstExpression;
452
Steven Moreland693640b2018-07-19 13:46:27 -0700453class AidlConstantValue : public AidlNode {
Casey Dahlind40e2fe2015-11-24 14:06:52 -0800454 public:
Will McVickerd7d18df2019-09-12 13:40:50 -0700455 enum class Type {
456 // WARNING: Don't change this order! The order is used to determine type
457 // promotion during a binary expression.
458 BOOLEAN,
459 INT8,
460 INT32,
461 INT64,
462 ARRAY,
463 CHARACTER,
464 STRING,
465 FLOATING,
466 UNARY,
467 BINARY,
468 ERROR,
469 };
470
471 /*
472 * Return the value casted to the given type.
473 */
474 template <typename T>
475 T cast() const;
Casey Dahlind40e2fe2015-11-24 14:06:52 -0800476
Steven Moreland693640b2018-07-19 13:46:27 -0700477 virtual ~AidlConstantValue() = default;
Casey Dahlind40e2fe2015-11-24 14:06:52 -0800478
Steven Moreland541788d2020-05-21 22:05:52 +0000479 // creates default value, when one isn't specified
480 // nullptr if no default available
481 static AidlConstantValue* Default(const AidlTypeSpecifier& specifier);
482
Steven Moreland25294322018-08-07 18:13:55 -0700483 static AidlConstantValue* Boolean(const AidlLocation& location, bool value);
484 static AidlConstantValue* Character(const AidlLocation& location, char value);
Steven Moreland25294322018-08-07 18:13:55 -0700485 // example: 123, -5498, maybe any size
Will McVickerd7d18df2019-09-12 13:40:50 -0700486 static AidlConstantValue* Integral(const AidlLocation& location, const string& value);
487 static AidlConstantValue* Floating(const AidlLocation& location, const std::string& value);
Steven Moreland860b1942018-08-16 14:59:28 -0700488 static AidlConstantValue* Array(const AidlLocation& location,
Will McVickerd7d18df2019-09-12 13:40:50 -0700489 std::unique_ptr<vector<unique_ptr<AidlConstantValue>>> values);
Steven Moreland693640b2018-07-19 13:46:27 -0700490 // example: "\"asdf\""
Will McVickerd7d18df2019-09-12 13:40:50 -0700491 static AidlConstantValue* String(const AidlLocation& location, const string& value);
Steven Moreland693640b2018-07-19 13:46:27 -0700492
Daniel Normanf0ca44f2019-10-25 09:59:44 -0700493 // Construct an AidlConstantValue by evaluating the other integral constant's
494 // value string. This does not preserve the structure of the copied constant.
Steven Moreland59e53e42019-11-26 20:38:08 -0800495 // Returns nullptr and logs if value cannot be copied.
Daniel Normanf0ca44f2019-10-25 09:59:44 -0700496 static AidlConstantValue* ShallowIntegralCopy(const AidlConstantValue& other);
Daniel Normanb28684e2019-10-17 15:31:39 -0700497
Will McVickerd7d18df2019-09-12 13:40:50 -0700498 Type GetType() const { return final_type_; }
Steven Moreland25294322018-08-07 18:13:55 -0700499
Will McVickerd7d18df2019-09-12 13:40:50 -0700500 virtual bool CheckValid() const;
Steven Moreland860b1942018-08-16 14:59:28 -0700501
502 // Raw value of type (currently valid in C++ and Java). Empty string on error.
Steven Moreland4bcb05c2019-11-27 18:57:47 -0800503 string ValueString(const AidlTypeSpecifier& type, const ConstantValueDecorator& decorator) const;
Casey Dahlind40e2fe2015-11-24 14:06:52 -0800504
505 private:
Will McVickerd7d18df2019-09-12 13:40:50 -0700506 AidlConstantValue(const AidlLocation& location, Type parsed_type, int64_t parsed_value,
507 const string& checked_value);
508 AidlConstantValue(const AidlLocation& location, Type type, const string& checked_value);
Steven Moreland860b1942018-08-16 14:59:28 -0700509 AidlConstantValue(const AidlLocation& location, Type type,
Will McVickerd7d18df2019-09-12 13:40:50 -0700510 std::unique_ptr<vector<unique_ptr<AidlConstantValue>>> values);
Steven Moreland25294322018-08-07 18:13:55 -0700511 static string ToString(Type type);
Will McVickerd7d18df2019-09-12 13:40:50 -0700512 static bool ParseIntegral(const string& value, int64_t* parsed_value, Type* parsed_type);
513 static bool IsHex(const string& value);
Steven Moreland4bcb05c2019-11-27 18:57:47 -0800514
Will McVickerd7d18df2019-09-12 13:40:50 -0700515 virtual bool evaluate(const AidlTypeSpecifier& type) const;
Casey Dahlind40e2fe2015-11-24 14:06:52 -0800516
Steven Moreland693640b2018-07-19 13:46:27 -0700517 const Type type_ = Type::ERROR;
Will McVickerd7d18df2019-09-12 13:40:50 -0700518 const vector<unique_ptr<AidlConstantValue>> values_; // if type_ == ARRAY
519 const string value_; // otherwise
520
521 // State for tracking evaluation of expressions
Steven Moreland4bcb05c2019-11-27 18:57:47 -0800522 mutable bool is_valid_ = false; // cache of CheckValid, but may be marked false in evaluate
523 mutable bool is_evaluated_ = false; // whether evaluate has been called
Will McVickerd7d18df2019-09-12 13:40:50 -0700524 mutable Type final_type_;
525 mutable int64_t final_value_;
526 mutable string final_string_value_ = "";
Steven Moreland693640b2018-07-19 13:46:27 -0700527
528 DISALLOW_COPY_AND_ASSIGN(AidlConstantValue);
Will McVickerd7d18df2019-09-12 13:40:50 -0700529
530 friend AidlUnaryConstExpression;
531 friend AidlBinaryConstExpression;
532};
533
534class AidlUnaryConstExpression : public AidlConstantValue {
535 public:
536 AidlUnaryConstExpression(const AidlLocation& location, const string& op,
537 std::unique_ptr<AidlConstantValue> rval);
538
539 static bool IsCompatibleType(Type type, const string& op);
540 bool CheckValid() const override;
Will McVickerd7d18df2019-09-12 13:40:50 -0700541 private:
542 bool evaluate(const AidlTypeSpecifier& type) const override;
543
544 std::unique_ptr<AidlConstantValue> unary_;
545 const string op_;
546};
547
548class AidlBinaryConstExpression : public AidlConstantValue {
549 public:
550 AidlBinaryConstExpression(const AidlLocation& location, std::unique_ptr<AidlConstantValue> lval,
551 const string& op, std::unique_ptr<AidlConstantValue> rval);
552
553 bool CheckValid() const override;
Will McVickerd7d18df2019-09-12 13:40:50 -0700554
555 static bool AreCompatibleTypes(Type t1, Type t2);
556 // Returns the promoted kind for both operands
557 static Type UsualArithmeticConversion(Type left, Type right);
558 // Returns the promoted integral type where INT32 is the smallest type
559 static Type IntegralPromotion(Type in);
560
561 private:
562 bool evaluate(const AidlTypeSpecifier& type) const override;
563
564 std::unique_ptr<AidlConstantValue> left_val_;
565 std::unique_ptr<AidlConstantValue> right_val_;
566 const string op_;
Christopher Wileyd6bdd8d2016-05-03 11:23:13 -0700567};
568
Andrei Onea9445fc62019-06-27 18:11:59 +0100569struct AidlAnnotationParameter {
570 std::string name;
571 std::unique_ptr<AidlConstantValue> value;
572};
573
Steven Moreland693640b2018-07-19 13:46:27 -0700574class AidlConstantDeclaration : public AidlMember {
Christopher Wileyd6bdd8d2016-05-03 11:23:13 -0700575 public:
Steven Moreland46e9da82018-07-27 15:45:29 -0700576 AidlConstantDeclaration(const AidlLocation& location, AidlTypeSpecifier* specifier,
Will McVickerd7d18df2019-09-12 13:40:50 -0700577 const string& name, AidlConstantValue* value);
Steven Moreland693640b2018-07-19 13:46:27 -0700578 virtual ~AidlConstantDeclaration() = default;
Christopher Wileyd6bdd8d2016-05-03 11:23:13 -0700579
Steven Moreland693640b2018-07-19 13:46:27 -0700580 const AidlTypeSpecifier& GetType() const { return *type_; }
Steven Moreland4d12f9a2018-10-31 14:30:55 -0700581 AidlTypeSpecifier* GetMutableType() { return type_.get(); }
Will McVickerd7d18df2019-09-12 13:40:50 -0700582 const string& GetName() const { return name_; }
Steven Moreland693640b2018-07-19 13:46:27 -0700583 const AidlConstantValue& GetValue() const { return *value_; }
Jeongik Chadb0f59e2018-11-01 18:11:21 +0900584 bool CheckValid(const AidlTypenames& typenames) const;
Christopher Wileyd6bdd8d2016-05-03 11:23:13 -0700585
Will McVickerd7d18df2019-09-12 13:40:50 -0700586 string ToString() const;
587 string Signature() const;
Steven Moreland860b1942018-08-16 14:59:28 -0700588 string ValueString(const ConstantValueDecorator& decorator) const {
Will McVickerd7d18df2019-09-12 13:40:50 -0700589 return value_->ValueString(GetType(), decorator);
Steven Moreland860b1942018-08-16 14:59:28 -0700590 }
Steven Moreland25294322018-08-07 18:13:55 -0700591
Steven Moreland693640b2018-07-19 13:46:27 -0700592 AidlConstantDeclaration* AsConstantDeclaration() override { return this; }
Christopher Wileyd6bdd8d2016-05-03 11:23:13 -0700593
594 private:
Steven Moreland693640b2018-07-19 13:46:27 -0700595 const unique_ptr<AidlTypeSpecifier> type_;
Will McVickerd7d18df2019-09-12 13:40:50 -0700596 const string name_;
597 unique_ptr<AidlConstantValue> value_;
Christopher Wileyd6bdd8d2016-05-03 11:23:13 -0700598
Steven Moreland693640b2018-07-19 13:46:27 -0700599 DISALLOW_COPY_AND_ASSIGN(AidlConstantDeclaration);
Casey Dahlind40e2fe2015-11-24 14:06:52 -0800600};
601
602class AidlMethod : public AidlMember {
Casey Dahlin5c69deb2015-10-01 14:44:12 -0700603 public:
Will McVickerd7d18df2019-09-12 13:40:50 -0700604 AidlMethod(const AidlLocation& location, bool oneway, AidlTypeSpecifier* type, const string& name,
605 vector<unique_ptr<AidlArgument>>* args, const string& comments);
606 AidlMethod(const AidlLocation& location, bool oneway, AidlTypeSpecifier* type, const string& name,
607 vector<unique_ptr<AidlArgument>>* args, const string& comments, int id,
608 bool is_user_defined = true);
Casey Dahlin5c69deb2015-10-01 14:44:12 -0700609 virtual ~AidlMethod() = default;
610
Casey Dahlind40e2fe2015-11-24 14:06:52 -0800611 AidlMethod* AsMethod() override { return this; }
Jeongik Cha997281d2020-01-16 15:23:59 +0900612 bool IsHidden() const;
Will McVickerd7d18df2019-09-12 13:40:50 -0700613 const string& GetComments() const { return comments_; }
Jiyong Parkd59a10d2018-07-18 11:12:55 +0900614 const AidlTypeSpecifier& GetType() const { return *type_; }
615 AidlTypeSpecifier* GetMutableType() { return type_.get(); }
Steven Morelandacd53472018-12-14 10:17:26 -0800616
Steven Moreland8c70ba92018-12-17 10:20:31 -0800617 // set if this method is part of an interface that is marked oneway
618 void ApplyInterfaceOneway(bool oneway) { oneway_ = oneway_ || oneway; }
Casey Dahlinf4a93112015-10-05 16:58:09 -0700619 bool IsOneway() const { return oneway_; }
Steven Morelandacd53472018-12-14 10:17:26 -0800620
Casey Dahlinf4a93112015-10-05 16:58:09 -0700621 const std::string& GetName() const { return name_; }
Casey Dahlinf4a93112015-10-05 16:58:09 -0700622 bool HasId() const { return has_id_; }
Jiyong Parked65bf42018-08-28 15:43:27 +0900623 int GetId() const { return id_; }
Casey Dahlinf4a93112015-10-05 16:58:09 -0700624 void SetId(unsigned id) { id_ = id; }
Casey Dahlinf2d23f72015-10-02 16:19:19 -0700625
Jiyong Park309668e2018-07-28 16:55:44 +0900626 bool IsUserDefined() const { return is_user_defined_; }
627
Casey Dahlinf4a93112015-10-05 16:58:09 -0700628 const std::vector<std::unique_ptr<AidlArgument>>& GetArguments() const {
Christopher Wileyad339272015-10-05 19:11:58 -0700629 return arguments_;
630 }
631 // An inout parameter will appear in both GetInArguments()
632 // and GetOutArguments(). AidlMethod retains ownership of the argument
633 // pointers returned in this way.
634 const std::vector<const AidlArgument*>& GetInArguments() const {
635 return in_arguments_;
636 }
637 const std::vector<const AidlArgument*>& GetOutArguments() const {
638 return out_arguments_;
Casey Dahlinf4a93112015-10-05 16:58:09 -0700639 }
Casey Dahlin5c69deb2015-10-01 14:44:12 -0700640
Jiyong Park309668e2018-07-28 16:55:44 +0900641 // name + type parameter types
642 // i.e, foo(int, String)
Jiyong Park02da7422018-07-16 16:00:26 +0900643 std::string Signature() const;
644
Jiyong Park309668e2018-07-28 16:55:44 +0900645 // return type + name + type parameter types + annotations
646 // i.e, boolean foo(int, @Nullable String)
647 std::string ToString() const;
648
Casey Dahlin5c69deb2015-10-01 14:44:12 -0700649 private:
Casey Dahlinf4a93112015-10-05 16:58:09 -0700650 bool oneway_;
Casey Dahlinf2d23f72015-10-02 16:19:19 -0700651 std::string comments_;
Jiyong Parkd59a10d2018-07-18 11:12:55 +0900652 std::unique_ptr<AidlTypeSpecifier> type_;
Casey Dahlinf4a93112015-10-05 16:58:09 -0700653 std::string name_;
Christopher Wileyad339272015-10-05 19:11:58 -0700654 const std::vector<std::unique_ptr<AidlArgument>> arguments_;
655 std::vector<const AidlArgument*> in_arguments_;
656 std::vector<const AidlArgument*> out_arguments_;
Casey Dahlinf4a93112015-10-05 16:58:09 -0700657 bool has_id_;
658 int id_;
Jiyong Park309668e2018-07-28 16:55:44 +0900659 bool is_user_defined_ = true;
Casey Dahlinf2d23f72015-10-02 16:19:19 -0700660
Casey Dahlin5c69deb2015-10-01 14:44:12 -0700661 DISALLOW_COPY_AND_ASSIGN(AidlMethod);
Casey Dahlin0a2f8be2015-09-28 16:15:29 -0700662};
Adam Lesinskiffa16862014-01-23 18:17:42 -0800663
Steven Morelandc258abc2018-07-10 14:03:38 -0700664class AidlDefinedType;
Jiyong Parkb034bf02018-07-30 17:44:33 +0900665class AidlInterface;
666class AidlParcelable;
667class AidlStructuredParcelable;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800668
Steven Moreland46e9da82018-07-27 15:45:29 -0700669class AidlInterface;
670class AidlParcelable;
671class AidlStructuredParcelable;
Daniel Norman85aed542019-08-21 12:01:14 -0700672// AidlDefinedType represents either an interface, parcelable, or enum that is
Jiyong Park1deecc32018-07-17 01:14:41 +0900673// defined in the source file.
674class AidlDefinedType : public AidlAnnotatable {
Steven Moreland787b0432018-07-03 09:00:58 -0700675 public:
Steven Moreland46e9da82018-07-27 15:45:29 -0700676 AidlDefinedType(const AidlLocation& location, const std::string& name,
Jiyong Park18132182020-06-08 20:24:40 +0900677 const std::string& comments, const std::string& package);
Steven Moreland787b0432018-07-03 09:00:58 -0700678 virtual ~AidlDefinedType() = default;
679
Jiyong Park1deecc32018-07-17 01:14:41 +0900680 const std::string& GetName() const { return name_; };
Jeongik Cha997281d2020-01-16 15:23:59 +0900681 bool IsHidden() const;
Jiyong Park1deecc32018-07-17 01:14:41 +0900682 const std::string& GetComments() const { return comments_; }
Jiyong Parka6605ab2018-11-11 14:30:21 +0900683 void SetComments(const std::string comments) { comments_ = comments; }
Jiyong Park1deecc32018-07-17 01:14:41 +0900684
Steven Moreland787b0432018-07-03 09:00:58 -0700685 /* dot joined package, example: "android.package.foo" */
Jiyong Park18132182020-06-08 20:24:40 +0900686 std::string GetPackage() const { return package_; }
Steven Moreland787b0432018-07-03 09:00:58 -0700687 /* dot joined package and name, example: "android.package.foo.IBar" */
688 std::string GetCanonicalName() const;
Jiyong Park18132182020-06-08 20:24:40 +0900689 const std::vector<std::string>& GetSplitPackage() const { return split_package_; }
Steven Moreland787b0432018-07-03 09:00:58 -0700690
Steven Morelanded83a282018-07-17 13:27:29 -0700691 virtual std::string GetPreprocessDeclarationName() const = 0;
Steven Morelandc258abc2018-07-10 14:03:38 -0700692
Steven Moreland5557f1c2018-07-02 13:50:23 -0700693 virtual const AidlStructuredParcelable* AsStructuredParcelable() const { return nullptr; }
Steven Morelandc258abc2018-07-10 14:03:38 -0700694 virtual const AidlParcelable* AsParcelable() const { return nullptr; }
Daniel Norman85aed542019-08-21 12:01:14 -0700695 virtual const AidlEnumDeclaration* AsEnumDeclaration() const { return nullptr; }
Steven Moreland5557f1c2018-07-02 13:50:23 -0700696 virtual const AidlInterface* AsInterface() const { return nullptr; }
Jeongik Chadf76dc72019-11-28 00:08:47 +0900697 virtual const AidlParameterizable<std::string>* AsParameterizable() const { return nullptr; }
Devin Moore24f68572020-02-26 13:20:59 -0800698 bool CheckValid(const AidlTypenames& typenames) const override;
Steven Morelandd59e3172020-05-11 16:42:09 -0700699 virtual bool LanguageSpecificCheckValid(const AidlTypenames& typenames,
700 Options::Language lang) const = 0;
Steven Morelandc258abc2018-07-10 14:03:38 -0700701 AidlStructuredParcelable* AsStructuredParcelable() {
702 return const_cast<AidlStructuredParcelable*>(
703 const_cast<const AidlDefinedType*>(this)->AsStructuredParcelable());
704 }
705 AidlParcelable* AsParcelable() {
706 return const_cast<AidlParcelable*>(const_cast<const AidlDefinedType*>(this)->AsParcelable());
707 }
Daniel Norman85aed542019-08-21 12:01:14 -0700708 AidlEnumDeclaration* AsEnumDeclaration() {
709 return const_cast<AidlEnumDeclaration*>(
710 const_cast<const AidlDefinedType*>(this)->AsEnumDeclaration());
711 }
Steven Morelandc258abc2018-07-10 14:03:38 -0700712 AidlInterface* AsInterface() {
713 return const_cast<AidlInterface*>(const_cast<const AidlDefinedType*>(this)->AsInterface());
714 }
715
Jeongik Chadf76dc72019-11-28 00:08:47 +0900716 AidlParameterizable<std::string>* AsParameterizable() {
717 return const_cast<AidlParameterizable<std::string>*>(
718 const_cast<const AidlDefinedType*>(this)->AsParameterizable());
719 }
720
Steven Moreland6cee3482018-07-18 14:39:58 -0700721 const AidlParcelable* AsUnstructuredParcelable() const {
722 if (this->AsStructuredParcelable() != nullptr) return nullptr;
723 return this->AsParcelable();
724 }
725 AidlParcelable* AsUnstructuredParcelable() {
726 return const_cast<AidlParcelable*>(
727 const_cast<const AidlDefinedType*>(this)->AsUnstructuredParcelable());
728 }
729
Jeongik Cha997281d2020-01-16 15:23:59 +0900730 virtual void Dump(CodeWriter* writer) const = 0;
Steven Morelanda5d9c5c2020-02-21 16:01:09 -0800731 void DumpHeader(CodeWriter* writer) const;
Jiyong Park02da7422018-07-16 16:00:26 +0900732
Steven Moreland787b0432018-07-03 09:00:58 -0700733 private:
Jiyong Park1deecc32018-07-17 01:14:41 +0900734 std::string name_;
Jiyong Park1deecc32018-07-17 01:14:41 +0900735 std::string comments_;
Jiyong Park18132182020-06-08 20:24:40 +0900736 const std::string package_;
737 const std::vector<std::string> split_package_;
Steven Moreland5557f1c2018-07-02 13:50:23 -0700738
739 DISALLOW_COPY_AND_ASSIGN(AidlDefinedType);
Steven Moreland787b0432018-07-03 09:00:58 -0700740};
741
Jeongik Chadf76dc72019-11-28 00:08:47 +0900742class AidlParcelable : public AidlDefinedType, public AidlParameterizable<std::string> {
Casey Dahlin1ae2bc52015-10-07 18:49:10 -0700743 public:
Jiyong Park18132182020-06-08 20:24:40 +0900744 AidlParcelable(const AidlLocation& location, const std::string& name, const std::string& package,
745 const std::string& comments, const std::string& cpp_header = "",
Jeongik Chadf76dc72019-11-28 00:08:47 +0900746 std::vector<std::string>* type_params = nullptr);
Casey Dahlin1ae2bc52015-10-07 18:49:10 -0700747 virtual ~AidlParcelable() = default;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800748
Christopher Wiley8aa4d9f2015-11-16 19:10:45 -0800749 std::string GetCppHeader() const { return cpp_header_; }
Christopher Wiley8aa4d9f2015-11-16 19:10:45 -0800750
Steven Moreland0cea4aa2020-04-20 21:06:02 -0700751 std::set<AidlAnnotation::Type> GetSupportedAnnotations() const override;
Jeongik Cha82317dd2019-02-27 20:26:11 +0900752 bool CheckValid(const AidlTypenames& typenames) const override;
Steven Morelandd59e3172020-05-11 16:42:09 -0700753 bool LanguageSpecificCheckValid(const AidlTypenames& typenames,
754 Options::Language lang) const override;
Steven Morelandc258abc2018-07-10 14:03:38 -0700755 const AidlParcelable* AsParcelable() const override { return this; }
Jeongik Chadf76dc72019-11-28 00:08:47 +0900756 const AidlParameterizable<std::string>* AsParameterizable() const override { return this; }
757 const AidlNode& AsAidlNode() const override { return *this; }
Steven Morelanded83a282018-07-17 13:27:29 -0700758 std::string GetPreprocessDeclarationName() const override { return "parcelable"; }
Steven Morelandc258abc2018-07-10 14:03:38 -0700759
Jeongik Cha997281d2020-01-16 15:23:59 +0900760 void Dump(CodeWriter* writer) const override;
Jiyong Park02da7422018-07-16 16:00:26 +0900761
Casey Dahlin1ae2bc52015-10-07 18:49:10 -0700762 private:
Christopher Wiley8aa4d9f2015-11-16 19:10:45 -0800763 std::string cpp_header_;
Casey Dahlin59401da2015-10-09 18:16:45 -0700764
Casey Dahlin1ae2bc52015-10-07 18:49:10 -0700765 DISALLOW_COPY_AND_ASSIGN(AidlParcelable);
Casey Dahlin0a2f8be2015-09-28 16:15:29 -0700766};
Adam Lesinskiffa16862014-01-23 18:17:42 -0800767
Steven Moreland5557f1c2018-07-02 13:50:23 -0700768class AidlStructuredParcelable : public AidlParcelable {
769 public:
Jiyong Park18132182020-06-08 20:24:40 +0900770 AidlStructuredParcelable(const AidlLocation& location, const std::string& name,
771 const std::string& package, const std::string& comments,
Steven Moreland5557f1c2018-07-02 13:50:23 -0700772 std::vector<std::unique_ptr<AidlVariableDeclaration>>* variables);
773
774 const std::vector<std::unique_ptr<AidlVariableDeclaration>>& GetFields() const {
775 return variables_;
776 }
777
Steven Morelandc258abc2018-07-10 14:03:38 -0700778 const AidlStructuredParcelable* AsStructuredParcelable() const override { return this; }
Steven Morelanded83a282018-07-17 13:27:29 -0700779 std::string GetPreprocessDeclarationName() const override { return "structured_parcelable"; }
Steven Moreland5557f1c2018-07-02 13:50:23 -0700780
Jeongik Cha997281d2020-01-16 15:23:59 +0900781 void Dump(CodeWriter* writer) const override;
Jiyong Park02da7422018-07-16 16:00:26 +0900782
Steven Moreland0cea4aa2020-04-20 21:06:02 -0700783 std::set<AidlAnnotation::Type> GetSupportedAnnotations() const override;
Jeongik Chadb0f59e2018-11-01 18:11:21 +0900784 bool CheckValid(const AidlTypenames& typenames) const override;
Steven Morelandd59e3172020-05-11 16:42:09 -0700785 bool LanguageSpecificCheckValid(const AidlTypenames& typenames,
786 Options::Language lang) const override;
Jeongik Chadb0f59e2018-11-01 18:11:21 +0900787
Steven Moreland5557f1c2018-07-02 13:50:23 -0700788 private:
789 const std::vector<std::unique_ptr<AidlVariableDeclaration>> variables_;
790
791 DISALLOW_COPY_AND_ASSIGN(AidlStructuredParcelable);
792};
793
Daniel Norman85aed542019-08-21 12:01:14 -0700794class AidlEnumerator : public AidlNode {
795 public:
Daniel Norman2e4112d2019-10-03 10:22:35 -0700796 AidlEnumerator(const AidlLocation& location, const std::string& name, AidlConstantValue* value,
797 const std::string& comments);
Daniel Norman85aed542019-08-21 12:01:14 -0700798 virtual ~AidlEnumerator() = default;
799
800 const std::string& GetName() const { return name_; }
Will McVickerd7d18df2019-09-12 13:40:50 -0700801 AidlConstantValue* GetValue() const { return value_.get(); }
Daniel Norman2e4112d2019-10-03 10:22:35 -0700802 const std::string& GetComments() const { return comments_; }
Daniel Norman85aed542019-08-21 12:01:14 -0700803 bool CheckValid(const AidlTypeSpecifier& enum_backing_type) const;
804
805 string ValueString(const AidlTypeSpecifier& backing_type,
806 const ConstantValueDecorator& decorator) const;
807
Daniel Normanb28684e2019-10-17 15:31:39 -0700808 void SetValue(std::unique_ptr<AidlConstantValue> value) { value_ = std::move(value); }
809
Daniel Norman85aed542019-08-21 12:01:14 -0700810 private:
811 const std::string name_;
Will McVickerd7d18df2019-09-12 13:40:50 -0700812 unique_ptr<AidlConstantValue> value_;
Daniel Norman2e4112d2019-10-03 10:22:35 -0700813 const std::string comments_;
Daniel Norman85aed542019-08-21 12:01:14 -0700814
815 DISALLOW_COPY_AND_ASSIGN(AidlEnumerator);
816};
817
818class AidlEnumDeclaration : public AidlDefinedType {
819 public:
Will McVickerd7d18df2019-09-12 13:40:50 -0700820 AidlEnumDeclaration(const AidlLocation& location, const string& name,
Daniel Norman85aed542019-08-21 12:01:14 -0700821 std::vector<std::unique_ptr<AidlEnumerator>>* enumerators,
Jiyong Park18132182020-06-08 20:24:40 +0900822 const std::string& package, const std::string& comments);
Daniel Norman85aed542019-08-21 12:01:14 -0700823 virtual ~AidlEnumDeclaration() = default;
824
825 void SetBackingType(std::unique_ptr<const AidlTypeSpecifier> type);
826 const AidlTypeSpecifier& GetBackingType() const { return *backing_type_; }
827 const std::vector<std::unique_ptr<AidlEnumerator>>& GetEnumerators() const {
828 return enumerators_;
829 }
Steven Moreland59e53e42019-11-26 20:38:08 -0800830 bool Autofill();
Steven Moreland0cea4aa2020-04-20 21:06:02 -0700831 std::set<AidlAnnotation::Type> GetSupportedAnnotations() const override;
Daniel Norman85aed542019-08-21 12:01:14 -0700832 bool CheckValid(const AidlTypenames& typenames) const override;
Steven Morelandd59e3172020-05-11 16:42:09 -0700833 bool LanguageSpecificCheckValid(const AidlTypenames& /*typenames*/,
834 Options::Language) const override {
835 return true;
836 }
Daniel Norman85aed542019-08-21 12:01:14 -0700837 std::string GetPreprocessDeclarationName() const override { return "enum"; }
Jeongik Cha997281d2020-01-16 15:23:59 +0900838 void Dump(CodeWriter* writer) const override;
Daniel Norman85aed542019-08-21 12:01:14 -0700839
840 const AidlEnumDeclaration* AsEnumDeclaration() const override { return this; }
841
842 private:
843 const std::string name_;
844 const std::vector<std::unique_ptr<AidlEnumerator>> enumerators_;
845 std::unique_ptr<const AidlTypeSpecifier> backing_type_;
846
847 DISALLOW_COPY_AND_ASSIGN(AidlEnumDeclaration);
848};
849
Jiyong Park1deecc32018-07-17 01:14:41 +0900850class AidlInterface final : public AidlDefinedType {
Casey Dahlin1ae2bc52015-10-07 18:49:10 -0700851 public:
Steven Moreland46e9da82018-07-27 15:45:29 -0700852 AidlInterface(const AidlLocation& location, const std::string& name, const std::string& comments,
853 bool oneway_, std::vector<std::unique_ptr<AidlMember>>* members,
Jiyong Park18132182020-06-08 20:24:40 +0900854 const std::string& package);
Casey Dahlin1ae2bc52015-10-07 18:49:10 -0700855 virtual ~AidlInterface() = default;
856
Casey Dahlinfb7da2e2015-10-08 17:26:09 -0700857 const std::vector<std::unique_ptr<AidlMethod>>& GetMethods() const
858 { return methods_; }
Jiyong Park309668e2018-07-28 16:55:44 +0900859 std::vector<std::unique_ptr<AidlMethod>>& GetMutableMethods() { return methods_; }
Steven Moreland693640b2018-07-19 13:46:27 -0700860 const std::vector<std::unique_ptr<AidlConstantDeclaration>>& GetConstantDeclarations() const {
861 return constants_;
862 }
Casey Dahlina2f77c42015-12-01 18:26:02 -0800863
Steven Morelandc258abc2018-07-10 14:03:38 -0700864 const AidlInterface* AsInterface() const override { return this; }
Steven Morelanded83a282018-07-17 13:27:29 -0700865 std::string GetPreprocessDeclarationName() const override { return "interface"; }
Steven Moreland5557f1c2018-07-02 13:50:23 -0700866
Jeongik Cha997281d2020-01-16 15:23:59 +0900867 void Dump(CodeWriter* writer) const override;
Jiyong Park02da7422018-07-16 16:00:26 +0900868
Steven Moreland0cea4aa2020-04-20 21:06:02 -0700869 std::set<AidlAnnotation::Type> GetSupportedAnnotations() const override;
Jeongik Chadb0f59e2018-11-01 18:11:21 +0900870 bool CheckValid(const AidlTypenames& typenames) const override;
Steven Morelandd59e3172020-05-11 16:42:09 -0700871 bool LanguageSpecificCheckValid(const AidlTypenames& typenames,
872 Options::Language lang) const override;
Jeongik Chadb0f59e2018-11-01 18:11:21 +0900873
Casey Dahlin1ae2bc52015-10-07 18:49:10 -0700874 private:
Casey Dahlinfb7da2e2015-10-08 17:26:09 -0700875 std::vector<std::unique_ptr<AidlMethod>> methods_;
Steven Moreland693640b2018-07-19 13:46:27 -0700876 std::vector<std::unique_ptr<AidlConstantDeclaration>> constants_;
Casey Dahlinfb7da2e2015-10-08 17:26:09 -0700877
Casey Dahlin1ae2bc52015-10-07 18:49:10 -0700878 DISALLOW_COPY_AND_ASSIGN(AidlInterface);
Casey Dahlin0a2f8be2015-09-28 16:15:29 -0700879};
Adam Lesinskiffa16862014-01-23 18:17:42 -0800880
Casey Dahlin0edf3422015-10-07 12:34:59 -0700881class AidlImport : public AidlNode {
882 public:
Steven Moreland46e9da82018-07-27 15:45:29 -0700883 AidlImport(const AidlLocation& location, const std::string& needed_class);
Casey Dahlin0edf3422015-10-07 12:34:59 -0700884 virtual ~AidlImport() = default;
885
Casey Dahlin0edf3422015-10-07 12:34:59 -0700886 const std::string& GetFilename() const { return filename_; }
887 const std::string& GetNeededClass() const { return needed_class_; }
Casey Dahlin0edf3422015-10-07 12:34:59 -0700888
889 private:
Casey Dahlin0edf3422015-10-07 12:34:59 -0700890 std::string filename_;
891 std::string needed_class_;
Casey Dahlin0edf3422015-10-07 12:34:59 -0700892
893 DISALLOW_COPY_AND_ASSIGN(AidlImport);
Casey Dahline2507492015-09-14 17:11:20 -0700894};
895
Jiyong Park62515512020-06-08 15:57:11 +0900896// AidlDocument models an AIDL file
897class AidlDocument : public AidlNode {
898 public:
899 AidlDocument(const AidlLocation& location, std::vector<std::unique_ptr<AidlImport>>& imports,
900 std::vector<AidlDefinedType*>& defined_types)
901 : AidlNode(location), imports_(std::move(imports)), defined_types_(defined_types) {}
902 const std::vector<std::unique_ptr<AidlImport>>& Imports() const { return imports_; }
903 const std::vector<AidlDefinedType*>& DefinedTypes() const { return defined_types_; }
904
905 private:
906 const std::vector<std::unique_ptr<AidlImport>> imports_;
907 const std::vector<AidlDefinedType*> defined_types_;
908};