blob: ea9fd38b3f036a32a94a078d4fc0abf0a4a95f8d [file] [log] [blame]
Will McVickerd7d18df2019-09-12 13:40:50 -07001/*
2 * Copyright (C) 2019, The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Steven Moreland9fccf582018-08-27 20:36:27 -070017#pragma once
Christopher Wileyec31a052016-01-25 07:28:51 -080018
Jiyong Park1deecc32018-07-17 01:14:41 +090019#include "aidl_typenames.h"
Jiyong Park02da7422018-07-16 16:00:26 +090020#include "code_writer.h"
Jiyong Park1deecc32018-07-17 01:14:41 +090021#include "io_delegate.h"
Jeongik Cha047c5ee2019-08-07 23:16:49 +090022#include "options.h"
Jiyong Park1deecc32018-07-17 01:14:41 +090023
Casey Dahlinbc7a50a2015-09-28 19:20:50 -070024#include <memory>
Jeongik Cha997281d2020-01-16 15:23:59 +090025#include <regex>
Casey Dahlindd691812015-09-09 17:59:06 -070026#include <string>
Jeongik Chadf76dc72019-11-28 00:08:47 +090027#include <unordered_set>
Casey Dahlinbc7a50a2015-09-28 19:20:50 -070028#include <vector>
Casey Dahlindd691812015-09-09 17:59:06 -070029
Elliott Hughes0a620672015-12-04 13:53:18 -080030#include <android-base/strings.h>
Casey Dahlin73d46b02015-09-11 02:47:54 +000031
Jiyong Parkb034bf02018-07-30 17:44:33 +090032using android::aidl::AidlTypenames;
Jiyong Park02da7422018-07-16 16:00:26 +090033using android::aidl::CodeWriter;
Jeongik Cha047c5ee2019-08-07 23:16:49 +090034using android::aidl::Options;
Steven Moreland3f658cf2018-08-20 13:40:54 -070035using std::shared_ptr;
Jiyong Park1deecc32018-07-17 01:14:41 +090036using std::string;
37using std::unique_ptr;
38using std::vector;
Andrei Onea8714b022019-02-01 18:55:54 +000039class AidlNode;
40
41namespace android {
42namespace aidl {
43namespace mappings {
44std::string dump_location(const AidlNode& method);
45} // namespace mappings
Mathew Inwoodadb74672019-11-29 14:01:53 +000046namespace java {
47std::string dump_location(const AidlNode& method);
48} // namespace java
Andrei Onea8714b022019-02-01 18:55:54 +000049} // namespace aidl
50} // namespace android
51
Steven Moreland46e9da82018-07-27 15:45:29 -070052class AidlLocation {
Casey Dahlinbc7a50a2015-09-28 19:20:50 -070053 public:
Steven Moreland46e9da82018-07-27 15:45:29 -070054 struct Point {
Dan Willemsen609ba6d2019-12-30 10:44:00 -080055 int line;
56 int column;
Steven Moreland46e9da82018-07-27 15:45:29 -070057 };
58
Devin Mooredf93ebb2020-03-25 14:03:35 -070059 enum class Source {
60 // From internal aidl source code
61 INTERNAL = 0,
62 // From a parsed file
63 EXTERNAL = 1
64 };
65
66 AidlLocation(const std::string& file, Point begin, Point end, Source source);
Devin Moore5de18ed2020-04-02 13:52:29 -070067 AidlLocation(const std::string& file, Source source)
68 : AidlLocation(file, {0, 0}, {0, 0}, source) {}
Devin Mooredf93ebb2020-03-25 14:03:35 -070069
70 bool IsInternal() const { return source_ == Source::INTERNAL; }
Steven Moreland46e9da82018-07-27 15:45:29 -070071
Devin Moore5de18ed2020-04-02 13:52:29 -070072 // The first line of a file is line 1.
73 bool LocationKnown() const { return begin_.line != 0; }
74
Steven Moreland46e9da82018-07-27 15:45:29 -070075 friend std::ostream& operator<<(std::ostream& os, const AidlLocation& l);
Andrei Onea8714b022019-02-01 18:55:54 +000076 friend class AidlNode;
Casey Dahlinbc7a50a2015-09-28 19:20:50 -070077
78 private:
Steven Moreland541788d2020-05-21 22:05:52 +000079 // INTENTIONALLY HIDDEN: only operator<< should access details here.
80 // Otherwise, locations should only ever be copied around to construct new
81 // objects.
Steven Moreland46e9da82018-07-27 15:45:29 -070082 const std::string file_;
83 Point begin_;
84 Point end_;
Devin Mooredf93ebb2020-03-25 14:03:35 -070085 Source source_;
Steven Moreland46e9da82018-07-27 15:45:29 -070086};
87
Devin Mooredf93ebb2020-03-25 14:03:35 -070088#define AIDL_LOCATION_HERE \
89 AidlLocation { __FILE__, {__LINE__, 0}, {__LINE__, 0}, AidlLocation::Source::INTERNAL }
Steven Moreland02e012e2018-08-02 14:58:10 -070090
Steven Moreland46e9da82018-07-27 15:45:29 -070091std::ostream& operator<<(std::ostream& os, const AidlLocation& l);
92
93// Anything that is locatable in a .aidl file.
94class AidlNode {
95 public:
96 AidlNode(const AidlLocation& location);
Steven Moreland3f658cf2018-08-20 13:40:54 -070097
98 AidlNode(const AidlNode&) = default;
Steven Moreland46e9da82018-07-27 15:45:29 -070099 virtual ~AidlNode() = default;
100
Jiyong Parkd800fef2020-07-22 18:09:43 +0900101 AidlNode(AidlNode&&) = delete;
102 AidlNode& operator=(AidlNode&&) = delete;
103
Devin Mooredf93ebb2020-03-25 14:03:35 -0700104 // To be able to print AidlLocation
Steven Morelandb0d15a52020-03-31 14:03:47 -0700105 friend class AidlErrorLog;
Andrei Onea8714b022019-02-01 18:55:54 +0000106 friend std::string android::aidl::mappings::dump_location(const AidlNode&);
Mathew Inwoodadb74672019-11-29 14:01:53 +0000107 friend std::string android::aidl::java::dump_location(const AidlNode&);
Steven Moreland46e9da82018-07-27 15:45:29 -0700108
Devin Mooredf93ebb2020-03-25 14:03:35 -0700109 const AidlLocation& GetLocation() const { return location_; }
110
Steven Moreland46e9da82018-07-27 15:45:29 -0700111 private:
Mathew Inwoodadb74672019-11-29 14:01:53 +0000112 std::string PrintLine() const;
Andrei Onea8714b022019-02-01 18:55:54 +0000113 std::string PrintLocation() const;
Steven Moreland46e9da82018-07-27 15:45:29 -0700114 const AidlLocation location_;
Casey Dahlinbc7a50a2015-09-28 19:20:50 -0700115};
116
Casey Dahlina2f77c42015-12-01 18:26:02 -0800117namespace android {
118namespace aidl {
119
Jiyong Park1deecc32018-07-17 01:14:41 +0900120class AidlTypenames;
Casey Dahlina2f77c42015-12-01 18:26:02 -0800121
122} // namespace aidl
123} // namespace android
124
Jeongik Chadf76dc72019-11-28 00:08:47 +0900125// unique_ptr<AidlTypeSpecifier> for type arugment,
126// std::string for type parameter(T, U, and so on).
127template <typename T>
128class AidlParameterizable {
129 public:
130 AidlParameterizable(std::vector<T>* type_params) : type_params_(type_params) {}
131 virtual ~AidlParameterizable() = default;
132 bool IsGeneric() const { return type_params_ != nullptr; }
133 const std::vector<T>& GetTypeParameters() const { return *type_params_; }
134 bool CheckValid() const;
135
136 virtual const AidlNode& AsAidlNode() const = 0;
137
138 protected:
139 AidlParameterizable(const AidlParameterizable&);
140
141 private:
142 const unique_ptr<std::vector<T>> type_params_;
143 static_assert(std::is_same<T, unique_ptr<AidlTypeSpecifier>>::value ||
144 std::is_same<T, std::string>::value);
145};
146template <>
147bool AidlParameterizable<std::string>::CheckValid() const;
148
Andrei Onea9445fc62019-06-27 18:11:59 +0100149class AidlConstantValue;
150class AidlConstantDeclaration;
151
152// Transforms a value string into a language specific form. Raw value as produced by
153// AidlConstantValue.
154using ConstantValueDecorator =
155 std::function<std::string(const AidlTypeSpecifier& type, const std::string& raw_value)>;
156
Jiyong Park68bc77a2018-07-19 19:00:45 +0900157class AidlAnnotation : public AidlNode {
158 public:
Steven Moreland0cea4aa2020-04-20 21:06:02 -0700159 enum class Type {
160 BACKING = 1,
161 HIDE,
162 JAVA_STABLE_PARCELABLE,
163 UNSUPPORTED_APP_USAGE,
164 VINTF_STABILITY,
165 NULLABLE,
166 UTF8_IN_CPP,
Jiyong Parkbf5fd5c2020-06-05 19:48:05 +0900167 JAVA_PASSTHROUGH,
Jiyong Park43113fb2020-07-20 16:26:19 +0900168 JAVA_DEBUG,
Steven Moreland0cea4aa2020-04-20 21:06:02 -0700169 };
170 static std::string TypeToString(Type type);
171
Andrei Onea9445fc62019-06-27 18:11:59 +0100172 static AidlAnnotation* Parse(
173 const AidlLocation& location, const string& name,
174 std::map<std::string, std::shared_ptr<AidlConstantValue>>* parameter_list);
Steven Moreland46e9da82018-07-27 15:45:29 -0700175
Steven Moreland3f658cf2018-08-20 13:40:54 -0700176 AidlAnnotation(const AidlAnnotation&) = default;
Steven Moreland3be75772018-08-20 13:27:43 -0700177 AidlAnnotation(AidlAnnotation&&) = default;
Jiyong Park68bc77a2018-07-19 19:00:45 +0900178 virtual ~AidlAnnotation() = default;
Andrei Onea9445fc62019-06-27 18:11:59 +0100179 bool CheckValid() const;
Steven Moreland3be75772018-08-20 13:27:43 -0700180
Steven Moreland0cea4aa2020-04-20 21:06:02 -0700181 const string& GetName() const { return schema_.name; };
182 const Type& GetType() const { return schema_.type; }
Daniel Norman37d43dd2019-09-09 17:22:34 -0700183 string ToString(const ConstantValueDecorator& decorator) const;
Andrei Onea9445fc62019-06-27 18:11:59 +0100184 std::map<std::string, std::string> AnnotationParams(
185 const ConstantValueDecorator& decorator) const;
Jiyong Parka6605ab2018-11-11 14:30:21 +0900186 const string& GetComments() const { return comments_; }
187 void SetComments(const string& comments) { comments_ = comments; }
Jiyong Park68bc77a2018-07-19 19:00:45 +0900188
189 private:
Steven Moreland0cea4aa2020-04-20 21:06:02 -0700190 struct Schema {
191 AidlAnnotation::Type type;
192
193 // text name in .aidl file, e.g. "nullable"
194 std::string name;
195
196 // map from param name -> value type
197 std::map<std::string, std::string> supported_parameters;
198 };
199 static const std::vector<Schema>& AllSchemas();
200
201 AidlAnnotation(const AidlLocation& location, const Schema& schema,
Andrei Onea9445fc62019-06-27 18:11:59 +0100202 std::map<std::string, std::shared_ptr<AidlConstantValue>>&& parameters);
Steven Moreland0cea4aa2020-04-20 21:06:02 -0700203
204 const Schema& schema_;
Jiyong Parka6605ab2018-11-11 14:30:21 +0900205 string comments_;
Andrei Onea9445fc62019-06-27 18:11:59 +0100206 std::map<std::string, std::shared_ptr<AidlConstantValue>> parameters_;
Jiyong Park68bc77a2018-07-19 19:00:45 +0900207};
208
Steven Moreland3be75772018-08-20 13:27:43 -0700209static inline bool operator<(const AidlAnnotation& lhs, const AidlAnnotation& rhs) {
210 return lhs.GetName() < rhs.GetName();
211}
212static inline bool operator==(const AidlAnnotation& lhs, const AidlAnnotation& rhs) {
213 return lhs.GetName() == rhs.GetName();
214}
Jiyong Park3656c3c2018-08-01 20:02:01 +0900215
Casey Dahline7922932016-02-29 17:23:01 -0800216class AidlAnnotatable : public AidlNode {
Casey Dahlin0ee37582015-09-30 16:31:55 -0700217 public:
Steven Moreland46e9da82018-07-27 15:45:29 -0700218 AidlAnnotatable(const AidlLocation& location);
Steven Moreland3f658cf2018-08-20 13:40:54 -0700219
220 AidlAnnotatable(const AidlAnnotatable&) = default;
221 AidlAnnotatable(AidlAnnotatable&&) = default;
Casey Dahline7922932016-02-29 17:23:01 -0800222 virtual ~AidlAnnotatable() = default;
223
Artur Satayev91fe8712019-07-29 13:06:01 +0100224 void Annotate(vector<AidlAnnotation>&& annotations) {
225 for (auto& annotation : annotations) {
226 annotations_.emplace_back(std::move(annotation));
227 }
228 }
Jiyong Park68bc77a2018-07-19 19:00:45 +0900229 bool IsNullable() const;
Jiyong Park68bc77a2018-07-19 19:00:45 +0900230 bool IsUtf8InCpp() const;
Steven Morelanda57d0a62019-07-30 09:41:14 -0700231 bool IsVintfStability() const;
Jeongik Cha88f95a82020-01-15 13:02:16 +0900232 bool IsStableApiParcelable(Options::Language lang) const;
Makoto Onuki78a1c1c2020-03-04 16:57:23 -0800233 bool IsHide() const;
Jiyong Park43113fb2020-07-20 16:26:19 +0900234 bool IsJavaDebug() const;
Andrei Onea9445fc62019-06-27 18:11:59 +0100235
Steven Moreland7e4b9502020-02-20 18:10:42 -0800236 void DumpAnnotations(CodeWriter* writer) const;
237
Andrei Onea9445fc62019-06-27 18:11:59 +0100238 const AidlAnnotation* UnsupportedAppUsage() const;
Jiyong Parkbf5fd5c2020-06-05 19:48:05 +0900239 const AidlAnnotation* JavaPassthrough() const;
Daniel Norman716d3112019-09-10 13:11:56 -0700240 const AidlTypeSpecifier* BackingType(const AidlTypenames& typenames) const;
Jiyong Park68bc77a2018-07-19 19:00:45 +0900241 std::string ToString() const;
Casey Dahline7922932016-02-29 17:23:01 -0800242
Jiyong Parka6605ab2018-11-11 14:30:21 +0900243 const vector<AidlAnnotation>& GetAnnotations() const { return annotations_; }
Devin Moore24f68572020-02-26 13:20:59 -0800244 virtual bool CheckValid(const AidlTypenames&) const;
Jiyong Park3656c3c2018-08-01 20:02:01 +0900245
Steven Moreland181144c2020-04-20 19:57:56 -0700246 protected:
Steven Moreland0cea4aa2020-04-20 21:06:02 -0700247 virtual std::set<AidlAnnotation::Type> GetSupportedAnnotations() const = 0;
Steven Moreland181144c2020-04-20 19:57:56 -0700248
Casey Dahline7922932016-02-29 17:23:01 -0800249 private:
Jiyong Parka6605ab2018-11-11 14:30:21 +0900250 vector<AidlAnnotation> annotations_;
Casey Dahline7922932016-02-29 17:23:01 -0800251};
252
Jiyong Park1deecc32018-07-17 01:14:41 +0900253// AidlTypeSpecifier represents a reference to either a built-in type,
254// a defined type, or a variant (e.g., array of generic) of a type.
Jeongik Chadf76dc72019-11-28 00:08:47 +0900255class AidlTypeSpecifier final : public AidlAnnotatable,
256 public AidlParameterizable<unique_ptr<AidlTypeSpecifier>> {
Casey Dahline7922932016-02-29 17:23:01 -0800257 public:
Steven Moreland46e9da82018-07-27 15:45:29 -0700258 AidlTypeSpecifier(const AidlLocation& location, const string& unresolved_name, bool is_array,
259 vector<unique_ptr<AidlTypeSpecifier>>* type_params, const string& comments);
Jiyong Parkd59a10d2018-07-18 11:12:55 +0900260 virtual ~AidlTypeSpecifier() = default;
Casey Dahlin0ee37582015-09-30 16:31:55 -0700261
Steven Moreland3f658cf2018-08-20 13:40:54 -0700262 // Copy of this type which is not an array.
263 AidlTypeSpecifier ArrayBase() const;
264
Jiyong Park1deecc32018-07-17 01:14:41 +0900265 // Returns the full-qualified name of the base type.
266 // int -> int
267 // int[] -> int
268 // List<String> -> List
269 // IFoo -> foo.bar.IFoo (if IFoo is in package foo.bar)
270 const string& GetName() const {
271 if (IsResolved()) {
272 return fully_qualified_name_;
273 } else {
274 return GetUnresolvedName();
275 }
276 }
Casey Dahlin0ee37582015-09-30 16:31:55 -0700277
Jiyong Park1deecc32018-07-17 01:14:41 +0900278 // Returns string representation of this type specifier.
Artur Satayev91fe8712019-07-29 13:06:01 +0100279 // This is GetBaseTypeName() + array modifier or generic type parameters
Jiyong Park1deecc32018-07-17 01:14:41 +0900280 string ToString() const;
281
Jiyong Park02da7422018-07-16 16:00:26 +0900282 std::string Signature() const;
283
Jiyong Park1deecc32018-07-17 01:14:41 +0900284 const string& GetUnresolvedName() const { return unresolved_name_; }
285
Jeongik Cha997281d2020-01-16 15:23:59 +0900286 bool IsHidden() const;
287
Jiyong Park1deecc32018-07-17 01:14:41 +0900288 const string& GetComments() const { return comments_; }
289
Jeongik Cha1a7ab642019-07-29 17:31:02 +0900290 const std::vector<std::string> GetSplitName() const { return split_name_; }
291
Jiyong Parka6605ab2018-11-11 14:30:21 +0900292 void SetComments(const string& comment) { comments_ = comment; }
293
Jiyong Park1deecc32018-07-17 01:14:41 +0900294 bool IsResolved() const { return fully_qualified_name_ != ""; }
295
296 bool IsArray() const { return is_array_; }
297
Jiyong Park1deecc32018-07-17 01:14:41 +0900298 // Resolve the base type name to a fully-qualified name. Return false if the
299 // resolution fails.
Daniel Norman716d3112019-09-10 13:11:56 -0700300 bool Resolve(const AidlTypenames& typenames);
Casey Dahlin0ee37582015-09-30 16:31:55 -0700301
Steven Moreland0cea4aa2020-04-20 21:06:02 -0700302 std::set<AidlAnnotation::Type> GetSupportedAnnotations() const override;
Devin Moore24f68572020-02-26 13:20:59 -0800303 bool CheckValid(const AidlTypenames& typenames) const override;
Steven Morelandd59e3172020-05-11 16:42:09 -0700304 bool LanguageSpecificCheckValid(const AidlTypenames& typenames, Options::Language lang) const;
Jeongik Chadf76dc72019-11-28 00:08:47 +0900305 const AidlNode& AsAidlNode() const override { return *this; }
Jiyong Park1d2df7d2018-07-23 15:22:50 +0900306
Casey Dahlin0ee37582015-09-30 16:31:55 -0700307 private:
Steven Moreland3f658cf2018-08-20 13:40:54 -0700308 AidlTypeSpecifier(const AidlTypeSpecifier&) = default;
309
Jiyong Park1deecc32018-07-17 01:14:41 +0900310 const string unresolved_name_;
311 string fully_qualified_name_;
Steven Moreland3f658cf2018-08-20 13:40:54 -0700312 bool is_array_;
Jiyong Parka6605ab2018-11-11 14:30:21 +0900313 string comments_;
Jeongik Cha1a7ab642019-07-29 17:31:02 +0900314 vector<string> split_name_;
Casey Dahlin0ee37582015-09-30 16:31:55 -0700315};
316
Steven Moreland860b1942018-08-16 14:59:28 -0700317// Returns the universal value unaltered.
318std::string AidlConstantValueDecorator(const AidlTypeSpecifier& type, const std::string& raw_value);
319
Steven Moreland9ea10e32018-07-19 15:26:09 -0700320class AidlConstantValue;
Steven Moreland541788d2020-05-21 22:05:52 +0000321// TODO: This class is used for method arguments and also parcelable fields,
322// and it should be split up since default values don't apply to method
323// arguments
Steven Moreland5557f1c2018-07-02 13:50:23 -0700324class AidlVariableDeclaration : public AidlNode {
325 public:
Steven Moreland46e9da82018-07-27 15:45:29 -0700326 AidlVariableDeclaration(const AidlLocation& location, AidlTypeSpecifier* type,
327 const std::string& name);
328 AidlVariableDeclaration(const AidlLocation& location, AidlTypeSpecifier* type,
329 const std::string& name, AidlConstantValue* default_value);
Steven Moreland5557f1c2018-07-02 13:50:23 -0700330 virtual ~AidlVariableDeclaration() = default;
331
Jiyong Parkd800fef2020-07-22 18:09:43 +0900332 // non-copyable, non-movable
333 AidlVariableDeclaration(const AidlVariableDeclaration&) = delete;
334 AidlVariableDeclaration(AidlVariableDeclaration&&) = delete;
335 AidlVariableDeclaration& operator=(const AidlVariableDeclaration&) = delete;
336 AidlVariableDeclaration& operator=(AidlVariableDeclaration&&) = delete;
337
Steven Moreland5557f1c2018-07-02 13:50:23 -0700338 std::string GetName() const { return name_; }
Jiyong Parkd59a10d2018-07-18 11:12:55 +0900339 const AidlTypeSpecifier& GetType() const { return *type_; }
Steven Moreland541788d2020-05-21 22:05:52 +0000340 // if this was constructed explicitly with a default value
341 bool IsDefaultUserSpecified() const { return default_user_specified_; }
342 // will return the default value this is constructed with or a default value
343 // if one is available
Steven Moreland9ea10e32018-07-19 15:26:09 -0700344 const AidlConstantValue* GetDefaultValue() const { return default_value_.get(); }
345
Jiyong Parkd59a10d2018-07-18 11:12:55 +0900346 AidlTypeSpecifier* GetMutableType() { return type_.get(); }
Steven Moreland5557f1c2018-07-02 13:50:23 -0700347
Jeongik Chadb0f59e2018-11-01 18:11:21 +0900348 bool CheckValid(const AidlTypenames& typenames) const;
Steven Moreland5557f1c2018-07-02 13:50:23 -0700349 std::string ToString() const;
Jiyong Park02da7422018-07-16 16:00:26 +0900350 std::string Signature() const;
Steven Moreland5557f1c2018-07-02 13:50:23 -0700351
Steven Moreland860b1942018-08-16 14:59:28 -0700352 std::string ValueString(const ConstantValueDecorator& decorator) const;
Steven Moreland25294322018-08-07 18:13:55 -0700353
Steven Moreland5557f1c2018-07-02 13:50:23 -0700354 private:
Jiyong Parkd59a10d2018-07-18 11:12:55 +0900355 std::unique_ptr<AidlTypeSpecifier> type_;
Steven Moreland5557f1c2018-07-02 13:50:23 -0700356 std::string name_;
Steven Moreland541788d2020-05-21 22:05:52 +0000357 bool default_user_specified_;
Steven Moreland9ea10e32018-07-19 15:26:09 -0700358 std::unique_ptr<AidlConstantValue> default_value_;
Steven Moreland5557f1c2018-07-02 13:50:23 -0700359};
360
361class AidlArgument : public AidlVariableDeclaration {
Casey Dahlinbc7a50a2015-09-28 19:20:50 -0700362 public:
Casey Dahlinc378c992015-09-29 16:50:40 -0700363 enum Direction { IN_DIR = 1, OUT_DIR = 2, INOUT_DIR = 3 };
364
Steven Moreland46e9da82018-07-27 15:45:29 -0700365 AidlArgument(const AidlLocation& location, AidlArgument::Direction direction,
366 AidlTypeSpecifier* type, const std::string& name);
367 AidlArgument(const AidlLocation& location, AidlTypeSpecifier* type, const std::string& name);
Casey Dahlinbc7a50a2015-09-28 19:20:50 -0700368 virtual ~AidlArgument() = default;
369
Jiyong Parkd800fef2020-07-22 18:09:43 +0900370 // non-copyable, non-movable
371 AidlArgument(const AidlArgument&) = delete;
372 AidlArgument(AidlArgument&&) = delete;
373 AidlArgument& operator=(const AidlArgument&) = delete;
374 AidlArgument& operator=(AidlArgument&&) = delete;
375
Casey Dahlinc378c992015-09-29 16:50:40 -0700376 Direction GetDirection() const { return direction_; }
Christopher Wileyad339272015-10-05 19:11:58 -0700377 bool IsOut() const { return direction_ & OUT_DIR; }
378 bool IsIn() const { return direction_ & IN_DIR; }
Casey Dahlinc378c992015-09-29 16:50:40 -0700379 bool DirectionWasSpecified() const { return direction_specified_; }
Jiyong Park3656c3c2018-08-01 20:02:01 +0900380 string GetDirectionSpecifier() const;
Christopher Wileyad339272015-10-05 19:11:58 -0700381
Casey Dahlinc378c992015-09-29 16:50:40 -0700382 std::string ToString() const;
Jiyong Park02da7422018-07-16 16:00:26 +0900383 std::string Signature() const;
Casey Dahlinc378c992015-09-29 16:50:40 -0700384
Casey Dahlinbc7a50a2015-09-28 19:20:50 -0700385 private:
Casey Dahlinc378c992015-09-29 16:50:40 -0700386 Direction direction_;
387 bool direction_specified_;
Casey Dahlina834dd42015-09-23 11:52:15 -0700388};
Adam Lesinskiffa16862014-01-23 18:17:42 -0800389
Casey Dahlind40e2fe2015-11-24 14:06:52 -0800390class AidlMethod;
Steven Moreland693640b2018-07-19 13:46:27 -0700391class AidlConstantDeclaration;
Daniel Norman85aed542019-08-21 12:01:14 -0700392class AidlEnumDeclaration;
Casey Dahlind40e2fe2015-11-24 14:06:52 -0800393class AidlMember : public AidlNode {
394 public:
Steven Moreland46e9da82018-07-27 15:45:29 -0700395 AidlMember(const AidlLocation& location);
Casey Dahlind40e2fe2015-11-24 14:06:52 -0800396 virtual ~AidlMember() = default;
397
Jiyong Parkd800fef2020-07-22 18:09:43 +0900398 // non-copyable, non-movable
399 AidlMember(const AidlMember&) = delete;
400 AidlMember(AidlMember&&) = delete;
401 AidlMember& operator=(const AidlMember&) = delete;
402 AidlMember& operator=(AidlMember&&) = delete;
403
Casey Dahlind40e2fe2015-11-24 14:06:52 -0800404 virtual AidlMethod* AsMethod() { return nullptr; }
Steven Moreland693640b2018-07-19 13:46:27 -0700405 virtual AidlConstantDeclaration* AsConstantDeclaration() { return nullptr; }
Casey Dahlind40e2fe2015-11-24 14:06:52 -0800406};
407
Will McVickerd7d18df2019-09-12 13:40:50 -0700408class AidlUnaryConstExpression;
409class AidlBinaryConstExpression;
410
Steven Moreland693640b2018-07-19 13:46:27 -0700411class AidlConstantValue : public AidlNode {
Casey Dahlind40e2fe2015-11-24 14:06:52 -0800412 public:
Will McVickerd7d18df2019-09-12 13:40:50 -0700413 enum class Type {
414 // WARNING: Don't change this order! The order is used to determine type
415 // promotion during a binary expression.
416 BOOLEAN,
417 INT8,
418 INT32,
419 INT64,
420 ARRAY,
421 CHARACTER,
422 STRING,
423 FLOATING,
424 UNARY,
425 BINARY,
426 ERROR,
427 };
428
429 /*
430 * Return the value casted to the given type.
431 */
432 template <typename T>
433 T cast() const;
Casey Dahlind40e2fe2015-11-24 14:06:52 -0800434
Steven Moreland693640b2018-07-19 13:46:27 -0700435 virtual ~AidlConstantValue() = default;
Casey Dahlind40e2fe2015-11-24 14:06:52 -0800436
Jiyong Parkd800fef2020-07-22 18:09:43 +0900437 // non-copyable, non-movable
438 AidlConstantValue(const AidlConstantValue&) = delete;
439 AidlConstantValue(AidlConstantValue&&) = delete;
440 AidlConstantValue& operator=(const AidlConstantValue&) = delete;
441 AidlConstantValue& operator=(AidlConstantValue&&) = delete;
442
Steven Moreland541788d2020-05-21 22:05:52 +0000443 // creates default value, when one isn't specified
444 // nullptr if no default available
445 static AidlConstantValue* Default(const AidlTypeSpecifier& specifier);
446
Steven Moreland25294322018-08-07 18:13:55 -0700447 static AidlConstantValue* Boolean(const AidlLocation& location, bool value);
448 static AidlConstantValue* Character(const AidlLocation& location, char value);
Steven Moreland25294322018-08-07 18:13:55 -0700449 // example: 123, -5498, maybe any size
Will McVickerd7d18df2019-09-12 13:40:50 -0700450 static AidlConstantValue* Integral(const AidlLocation& location, const string& value);
451 static AidlConstantValue* Floating(const AidlLocation& location, const std::string& value);
Steven Moreland860b1942018-08-16 14:59:28 -0700452 static AidlConstantValue* Array(const AidlLocation& location,
Will McVickerd7d18df2019-09-12 13:40:50 -0700453 std::unique_ptr<vector<unique_ptr<AidlConstantValue>>> values);
Steven Moreland693640b2018-07-19 13:46:27 -0700454 // example: "\"asdf\""
Will McVickerd7d18df2019-09-12 13:40:50 -0700455 static AidlConstantValue* String(const AidlLocation& location, const string& value);
Steven Moreland693640b2018-07-19 13:46:27 -0700456
Daniel Normanf0ca44f2019-10-25 09:59:44 -0700457 // Construct an AidlConstantValue by evaluating the other integral constant's
458 // value string. This does not preserve the structure of the copied constant.
Steven Moreland59e53e42019-11-26 20:38:08 -0800459 // Returns nullptr and logs if value cannot be copied.
Daniel Normanf0ca44f2019-10-25 09:59:44 -0700460 static AidlConstantValue* ShallowIntegralCopy(const AidlConstantValue& other);
Daniel Normanb28684e2019-10-17 15:31:39 -0700461
Will McVickerd7d18df2019-09-12 13:40:50 -0700462 Type GetType() const { return final_type_; }
Steven Moreland25294322018-08-07 18:13:55 -0700463
Will McVickerd7d18df2019-09-12 13:40:50 -0700464 virtual bool CheckValid() const;
Steven Moreland860b1942018-08-16 14:59:28 -0700465
466 // Raw value of type (currently valid in C++ and Java). Empty string on error.
Steven Moreland4bcb05c2019-11-27 18:57:47 -0800467 string ValueString(const AidlTypeSpecifier& type, const ConstantValueDecorator& decorator) const;
Casey Dahlind40e2fe2015-11-24 14:06:52 -0800468
469 private:
Will McVickerd7d18df2019-09-12 13:40:50 -0700470 AidlConstantValue(const AidlLocation& location, Type parsed_type, int64_t parsed_value,
471 const string& checked_value);
472 AidlConstantValue(const AidlLocation& location, Type type, const string& checked_value);
Steven Moreland860b1942018-08-16 14:59:28 -0700473 AidlConstantValue(const AidlLocation& location, Type type,
Will McVickerd7d18df2019-09-12 13:40:50 -0700474 std::unique_ptr<vector<unique_ptr<AidlConstantValue>>> values);
Steven Moreland25294322018-08-07 18:13:55 -0700475 static string ToString(Type type);
Will McVickerd7d18df2019-09-12 13:40:50 -0700476 static bool ParseIntegral(const string& value, int64_t* parsed_value, Type* parsed_type);
477 static bool IsHex(const string& value);
Steven Moreland4bcb05c2019-11-27 18:57:47 -0800478
Will McVickerd7d18df2019-09-12 13:40:50 -0700479 virtual bool evaluate(const AidlTypeSpecifier& type) const;
Casey Dahlind40e2fe2015-11-24 14:06:52 -0800480
Steven Moreland693640b2018-07-19 13:46:27 -0700481 const Type type_ = Type::ERROR;
Will McVickerd7d18df2019-09-12 13:40:50 -0700482 const vector<unique_ptr<AidlConstantValue>> values_; // if type_ == ARRAY
483 const string value_; // otherwise
484
485 // State for tracking evaluation of expressions
Steven Moreland4bcb05c2019-11-27 18:57:47 -0800486 mutable bool is_valid_ = false; // cache of CheckValid, but may be marked false in evaluate
487 mutable bool is_evaluated_ = false; // whether evaluate has been called
Will McVickerd7d18df2019-09-12 13:40:50 -0700488 mutable Type final_type_;
489 mutable int64_t final_value_;
490 mutable string final_string_value_ = "";
Steven Moreland693640b2018-07-19 13:46:27 -0700491
Will McVickerd7d18df2019-09-12 13:40:50 -0700492 friend AidlUnaryConstExpression;
493 friend AidlBinaryConstExpression;
494};
495
496class AidlUnaryConstExpression : public AidlConstantValue {
497 public:
498 AidlUnaryConstExpression(const AidlLocation& location, const string& op,
499 std::unique_ptr<AidlConstantValue> rval);
500
501 static bool IsCompatibleType(Type type, const string& op);
502 bool CheckValid() const override;
Will McVickerd7d18df2019-09-12 13:40:50 -0700503 private:
504 bool evaluate(const AidlTypeSpecifier& type) const override;
505
506 std::unique_ptr<AidlConstantValue> unary_;
507 const string op_;
508};
509
510class AidlBinaryConstExpression : public AidlConstantValue {
511 public:
512 AidlBinaryConstExpression(const AidlLocation& location, std::unique_ptr<AidlConstantValue> lval,
513 const string& op, std::unique_ptr<AidlConstantValue> rval);
514
515 bool CheckValid() const override;
Will McVickerd7d18df2019-09-12 13:40:50 -0700516
517 static bool AreCompatibleTypes(Type t1, Type t2);
518 // Returns the promoted kind for both operands
519 static Type UsualArithmeticConversion(Type left, Type right);
520 // Returns the promoted integral type where INT32 is the smallest type
521 static Type IntegralPromotion(Type in);
522
523 private:
524 bool evaluate(const AidlTypeSpecifier& type) const override;
525
526 std::unique_ptr<AidlConstantValue> left_val_;
527 std::unique_ptr<AidlConstantValue> right_val_;
528 const string op_;
Christopher Wileyd6bdd8d2016-05-03 11:23:13 -0700529};
530
Andrei Onea9445fc62019-06-27 18:11:59 +0100531struct AidlAnnotationParameter {
532 std::string name;
533 std::unique_ptr<AidlConstantValue> value;
534};
535
Steven Moreland693640b2018-07-19 13:46:27 -0700536class AidlConstantDeclaration : public AidlMember {
Christopher Wileyd6bdd8d2016-05-03 11:23:13 -0700537 public:
Steven Moreland46e9da82018-07-27 15:45:29 -0700538 AidlConstantDeclaration(const AidlLocation& location, AidlTypeSpecifier* specifier,
Will McVickerd7d18df2019-09-12 13:40:50 -0700539 const string& name, AidlConstantValue* value);
Steven Moreland693640b2018-07-19 13:46:27 -0700540 virtual ~AidlConstantDeclaration() = default;
Christopher Wileyd6bdd8d2016-05-03 11:23:13 -0700541
Jiyong Parkd800fef2020-07-22 18:09:43 +0900542 // non-copyable, non-movable
543 AidlConstantDeclaration(const AidlConstantDeclaration&) = delete;
544 AidlConstantDeclaration(AidlConstantDeclaration&&) = delete;
545 AidlConstantDeclaration& operator=(const AidlConstantDeclaration&) = delete;
546 AidlConstantDeclaration& operator=(AidlConstantDeclaration&&) = delete;
547
Steven Moreland693640b2018-07-19 13:46:27 -0700548 const AidlTypeSpecifier& GetType() const { return *type_; }
Steven Moreland4d12f9a2018-10-31 14:30:55 -0700549 AidlTypeSpecifier* GetMutableType() { return type_.get(); }
Will McVickerd7d18df2019-09-12 13:40:50 -0700550 const string& GetName() const { return name_; }
Steven Moreland693640b2018-07-19 13:46:27 -0700551 const AidlConstantValue& GetValue() const { return *value_; }
Jeongik Chadb0f59e2018-11-01 18:11:21 +0900552 bool CheckValid(const AidlTypenames& typenames) const;
Christopher Wileyd6bdd8d2016-05-03 11:23:13 -0700553
Will McVickerd7d18df2019-09-12 13:40:50 -0700554 string ToString() const;
555 string Signature() const;
Steven Moreland860b1942018-08-16 14:59:28 -0700556 string ValueString(const ConstantValueDecorator& decorator) const {
Will McVickerd7d18df2019-09-12 13:40:50 -0700557 return value_->ValueString(GetType(), decorator);
Steven Moreland860b1942018-08-16 14:59:28 -0700558 }
Steven Moreland25294322018-08-07 18:13:55 -0700559
Steven Moreland693640b2018-07-19 13:46:27 -0700560 AidlConstantDeclaration* AsConstantDeclaration() override { return this; }
Christopher Wileyd6bdd8d2016-05-03 11:23:13 -0700561
562 private:
Steven Moreland693640b2018-07-19 13:46:27 -0700563 const unique_ptr<AidlTypeSpecifier> type_;
Will McVickerd7d18df2019-09-12 13:40:50 -0700564 const string name_;
565 unique_ptr<AidlConstantValue> value_;
Casey Dahlind40e2fe2015-11-24 14:06:52 -0800566};
567
568class AidlMethod : public AidlMember {
Casey Dahlin5c69deb2015-10-01 14:44:12 -0700569 public:
Will McVickerd7d18df2019-09-12 13:40:50 -0700570 AidlMethod(const AidlLocation& location, bool oneway, AidlTypeSpecifier* type, const string& name,
571 vector<unique_ptr<AidlArgument>>* args, const string& comments);
572 AidlMethod(const AidlLocation& location, bool oneway, AidlTypeSpecifier* type, const string& name,
573 vector<unique_ptr<AidlArgument>>* args, const string& comments, int id,
574 bool is_user_defined = true);
Casey Dahlin5c69deb2015-10-01 14:44:12 -0700575 virtual ~AidlMethod() = default;
576
Jiyong Parkd800fef2020-07-22 18:09:43 +0900577 // non-copyable, non-movable
578 AidlMethod(const AidlMethod&) = delete;
579 AidlMethod(AidlMethod&&) = delete;
580 AidlMethod& operator=(const AidlMethod&) = delete;
581 AidlMethod& operator=(AidlMethod&&) = delete;
582
Casey Dahlind40e2fe2015-11-24 14:06:52 -0800583 AidlMethod* AsMethod() override { return this; }
Jeongik Cha997281d2020-01-16 15:23:59 +0900584 bool IsHidden() const;
Will McVickerd7d18df2019-09-12 13:40:50 -0700585 const string& GetComments() const { return comments_; }
Jiyong Parkd59a10d2018-07-18 11:12:55 +0900586 const AidlTypeSpecifier& GetType() const { return *type_; }
587 AidlTypeSpecifier* GetMutableType() { return type_.get(); }
Steven Morelandacd53472018-12-14 10:17:26 -0800588
Steven Moreland8c70ba92018-12-17 10:20:31 -0800589 // set if this method is part of an interface that is marked oneway
590 void ApplyInterfaceOneway(bool oneway) { oneway_ = oneway_ || oneway; }
Casey Dahlinf4a93112015-10-05 16:58:09 -0700591 bool IsOneway() const { return oneway_; }
Steven Morelandacd53472018-12-14 10:17:26 -0800592
Casey Dahlinf4a93112015-10-05 16:58:09 -0700593 const std::string& GetName() const { return name_; }
Casey Dahlinf4a93112015-10-05 16:58:09 -0700594 bool HasId() const { return has_id_; }
Jiyong Parked65bf42018-08-28 15:43:27 +0900595 int GetId() const { return id_; }
Casey Dahlinf4a93112015-10-05 16:58:09 -0700596 void SetId(unsigned id) { id_ = id; }
Casey Dahlinf2d23f72015-10-02 16:19:19 -0700597
Jiyong Park309668e2018-07-28 16:55:44 +0900598 bool IsUserDefined() const { return is_user_defined_; }
599
Casey Dahlinf4a93112015-10-05 16:58:09 -0700600 const std::vector<std::unique_ptr<AidlArgument>>& GetArguments() const {
Christopher Wileyad339272015-10-05 19:11:58 -0700601 return arguments_;
602 }
603 // An inout parameter will appear in both GetInArguments()
604 // and GetOutArguments(). AidlMethod retains ownership of the argument
605 // pointers returned in this way.
606 const std::vector<const AidlArgument*>& GetInArguments() const {
607 return in_arguments_;
608 }
609 const std::vector<const AidlArgument*>& GetOutArguments() const {
610 return out_arguments_;
Casey Dahlinf4a93112015-10-05 16:58:09 -0700611 }
Casey Dahlin5c69deb2015-10-01 14:44:12 -0700612
Jiyong Park309668e2018-07-28 16:55:44 +0900613 // name + type parameter types
614 // i.e, foo(int, String)
Jiyong Park02da7422018-07-16 16:00:26 +0900615 std::string Signature() const;
616
Jiyong Park309668e2018-07-28 16:55:44 +0900617 // return type + name + type parameter types + annotations
618 // i.e, boolean foo(int, @Nullable String)
619 std::string ToString() const;
620
Casey Dahlin5c69deb2015-10-01 14:44:12 -0700621 private:
Casey Dahlinf4a93112015-10-05 16:58:09 -0700622 bool oneway_;
Casey Dahlinf2d23f72015-10-02 16:19:19 -0700623 std::string comments_;
Jiyong Parkd59a10d2018-07-18 11:12:55 +0900624 std::unique_ptr<AidlTypeSpecifier> type_;
Casey Dahlinf4a93112015-10-05 16:58:09 -0700625 std::string name_;
Christopher Wileyad339272015-10-05 19:11:58 -0700626 const std::vector<std::unique_ptr<AidlArgument>> arguments_;
627 std::vector<const AidlArgument*> in_arguments_;
628 std::vector<const AidlArgument*> out_arguments_;
Casey Dahlinf4a93112015-10-05 16:58:09 -0700629 bool has_id_;
630 int id_;
Jiyong Park309668e2018-07-28 16:55:44 +0900631 bool is_user_defined_ = true;
Casey Dahlin0a2f8be2015-09-28 16:15:29 -0700632};
Adam Lesinskiffa16862014-01-23 18:17:42 -0800633
Steven Morelandc258abc2018-07-10 14:03:38 -0700634class AidlDefinedType;
Jiyong Parkb034bf02018-07-30 17:44:33 +0900635class AidlInterface;
636class AidlParcelable;
637class AidlStructuredParcelable;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800638
Steven Moreland46e9da82018-07-27 15:45:29 -0700639class AidlInterface;
640class AidlParcelable;
641class AidlStructuredParcelable;
Daniel Norman85aed542019-08-21 12:01:14 -0700642// AidlDefinedType represents either an interface, parcelable, or enum that is
Jiyong Park1deecc32018-07-17 01:14:41 +0900643// defined in the source file.
644class AidlDefinedType : public AidlAnnotatable {
Steven Moreland787b0432018-07-03 09:00:58 -0700645 public:
Steven Moreland46e9da82018-07-27 15:45:29 -0700646 AidlDefinedType(const AidlLocation& location, const std::string& name,
Jiyong Park18132182020-06-08 20:24:40 +0900647 const std::string& comments, const std::string& package);
Steven Moreland787b0432018-07-03 09:00:58 -0700648 virtual ~AidlDefinedType() = default;
649
Jiyong Parkd800fef2020-07-22 18:09:43 +0900650 // non-copyable, non-movable
651 AidlDefinedType(const AidlDefinedType&) = delete;
652 AidlDefinedType(AidlDefinedType&&) = delete;
653 AidlDefinedType& operator=(const AidlDefinedType&) = delete;
654 AidlDefinedType& operator=(AidlDefinedType&&) = delete;
655
Jiyong Park1deecc32018-07-17 01:14:41 +0900656 const std::string& GetName() const { return name_; };
Jeongik Cha997281d2020-01-16 15:23:59 +0900657 bool IsHidden() const;
Jiyong Park1deecc32018-07-17 01:14:41 +0900658 const std::string& GetComments() const { return comments_; }
Jiyong Parka6605ab2018-11-11 14:30:21 +0900659 void SetComments(const std::string comments) { comments_ = comments; }
Jiyong Park1deecc32018-07-17 01:14:41 +0900660
Steven Moreland787b0432018-07-03 09:00:58 -0700661 /* dot joined package, example: "android.package.foo" */
Jiyong Park18132182020-06-08 20:24:40 +0900662 std::string GetPackage() const { return package_; }
Steven Moreland787b0432018-07-03 09:00:58 -0700663 /* dot joined package and name, example: "android.package.foo.IBar" */
664 std::string GetCanonicalName() const;
Jiyong Park18132182020-06-08 20:24:40 +0900665 const std::vector<std::string>& GetSplitPackage() const { return split_package_; }
Steven Moreland787b0432018-07-03 09:00:58 -0700666
Steven Morelanded83a282018-07-17 13:27:29 -0700667 virtual std::string GetPreprocessDeclarationName() const = 0;
Steven Morelandc258abc2018-07-10 14:03:38 -0700668
Steven Moreland5557f1c2018-07-02 13:50:23 -0700669 virtual const AidlStructuredParcelable* AsStructuredParcelable() const { return nullptr; }
Steven Morelandc258abc2018-07-10 14:03:38 -0700670 virtual const AidlParcelable* AsParcelable() const { return nullptr; }
Daniel Norman85aed542019-08-21 12:01:14 -0700671 virtual const AidlEnumDeclaration* AsEnumDeclaration() const { return nullptr; }
Steven Moreland5557f1c2018-07-02 13:50:23 -0700672 virtual const AidlInterface* AsInterface() const { return nullptr; }
Jeongik Chadf76dc72019-11-28 00:08:47 +0900673 virtual const AidlParameterizable<std::string>* AsParameterizable() const { return nullptr; }
Devin Moore24f68572020-02-26 13:20:59 -0800674 bool CheckValid(const AidlTypenames& typenames) const override;
Steven Morelandd59e3172020-05-11 16:42:09 -0700675 virtual bool LanguageSpecificCheckValid(const AidlTypenames& typenames,
676 Options::Language lang) const = 0;
Steven Morelandc258abc2018-07-10 14:03:38 -0700677 AidlStructuredParcelable* AsStructuredParcelable() {
678 return const_cast<AidlStructuredParcelable*>(
679 const_cast<const AidlDefinedType*>(this)->AsStructuredParcelable());
680 }
681 AidlParcelable* AsParcelable() {
682 return const_cast<AidlParcelable*>(const_cast<const AidlDefinedType*>(this)->AsParcelable());
683 }
Daniel Norman85aed542019-08-21 12:01:14 -0700684 AidlEnumDeclaration* AsEnumDeclaration() {
685 return const_cast<AidlEnumDeclaration*>(
686 const_cast<const AidlDefinedType*>(this)->AsEnumDeclaration());
687 }
Steven Morelandc258abc2018-07-10 14:03:38 -0700688 AidlInterface* AsInterface() {
689 return const_cast<AidlInterface*>(const_cast<const AidlDefinedType*>(this)->AsInterface());
690 }
691
Jeongik Chadf76dc72019-11-28 00:08:47 +0900692 AidlParameterizable<std::string>* AsParameterizable() {
693 return const_cast<AidlParameterizable<std::string>*>(
694 const_cast<const AidlDefinedType*>(this)->AsParameterizable());
695 }
696
Steven Moreland6cee3482018-07-18 14:39:58 -0700697 const AidlParcelable* AsUnstructuredParcelable() const {
698 if (this->AsStructuredParcelable() != nullptr) return nullptr;
699 return this->AsParcelable();
700 }
701 AidlParcelable* AsUnstructuredParcelable() {
702 return const_cast<AidlParcelable*>(
703 const_cast<const AidlDefinedType*>(this)->AsUnstructuredParcelable());
704 }
705
Jeongik Cha997281d2020-01-16 15:23:59 +0900706 virtual void Dump(CodeWriter* writer) const = 0;
Steven Morelanda5d9c5c2020-02-21 16:01:09 -0800707 void DumpHeader(CodeWriter* writer) const;
Jiyong Park02da7422018-07-16 16:00:26 +0900708
Steven Moreland787b0432018-07-03 09:00:58 -0700709 private:
Jiyong Park1deecc32018-07-17 01:14:41 +0900710 std::string name_;
Jiyong Park1deecc32018-07-17 01:14:41 +0900711 std::string comments_;
Jiyong Park18132182020-06-08 20:24:40 +0900712 const std::string package_;
713 const std::vector<std::string> split_package_;
Steven Moreland787b0432018-07-03 09:00:58 -0700714};
715
Jeongik Chadf76dc72019-11-28 00:08:47 +0900716class AidlParcelable : public AidlDefinedType, public AidlParameterizable<std::string> {
Casey Dahlin1ae2bc52015-10-07 18:49:10 -0700717 public:
Jiyong Park18132182020-06-08 20:24:40 +0900718 AidlParcelable(const AidlLocation& location, const std::string& name, const std::string& package,
719 const std::string& comments, const std::string& cpp_header = "",
Jeongik Chadf76dc72019-11-28 00:08:47 +0900720 std::vector<std::string>* type_params = nullptr);
Casey Dahlin1ae2bc52015-10-07 18:49:10 -0700721 virtual ~AidlParcelable() = default;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800722
Jiyong Parkd800fef2020-07-22 18:09:43 +0900723 // non-copyable, non-movable
724 AidlParcelable(const AidlParcelable&) = delete;
725 AidlParcelable(AidlParcelable&&) = delete;
726 AidlParcelable& operator=(const AidlParcelable&) = delete;
727 AidlParcelable& operator=(AidlParcelable&&) = delete;
728
Christopher Wiley8aa4d9f2015-11-16 19:10:45 -0800729 std::string GetCppHeader() const { return cpp_header_; }
Christopher Wiley8aa4d9f2015-11-16 19:10:45 -0800730
Steven Moreland0cea4aa2020-04-20 21:06:02 -0700731 std::set<AidlAnnotation::Type> GetSupportedAnnotations() const override;
Jeongik Cha82317dd2019-02-27 20:26:11 +0900732 bool CheckValid(const AidlTypenames& typenames) const override;
Steven Morelandd59e3172020-05-11 16:42:09 -0700733 bool LanguageSpecificCheckValid(const AidlTypenames& typenames,
734 Options::Language lang) const override;
Steven Morelandc258abc2018-07-10 14:03:38 -0700735 const AidlParcelable* AsParcelable() const override { return this; }
Jeongik Chadf76dc72019-11-28 00:08:47 +0900736 const AidlParameterizable<std::string>* AsParameterizable() const override { return this; }
737 const AidlNode& AsAidlNode() const override { return *this; }
Steven Morelanded83a282018-07-17 13:27:29 -0700738 std::string GetPreprocessDeclarationName() const override { return "parcelable"; }
Steven Morelandc258abc2018-07-10 14:03:38 -0700739
Jeongik Cha997281d2020-01-16 15:23:59 +0900740 void Dump(CodeWriter* writer) const override;
Jiyong Park02da7422018-07-16 16:00:26 +0900741
Casey Dahlin1ae2bc52015-10-07 18:49:10 -0700742 private:
Christopher Wiley8aa4d9f2015-11-16 19:10:45 -0800743 std::string cpp_header_;
Casey Dahlin0a2f8be2015-09-28 16:15:29 -0700744};
Adam Lesinskiffa16862014-01-23 18:17:42 -0800745
Steven Moreland5557f1c2018-07-02 13:50:23 -0700746class AidlStructuredParcelable : public AidlParcelable {
747 public:
Jiyong Park18132182020-06-08 20:24:40 +0900748 AidlStructuredParcelable(const AidlLocation& location, const std::string& name,
749 const std::string& package, const std::string& comments,
Steven Moreland5557f1c2018-07-02 13:50:23 -0700750 std::vector<std::unique_ptr<AidlVariableDeclaration>>* variables);
Jiyong Parkd800fef2020-07-22 18:09:43 +0900751 virtual ~AidlStructuredParcelable() = default;
752
753 // non-copyable, non-movable
754 AidlStructuredParcelable(const AidlStructuredParcelable&) = delete;
755 AidlStructuredParcelable(AidlStructuredParcelable&&) = delete;
756 AidlStructuredParcelable& operator=(const AidlStructuredParcelable&) = delete;
757 AidlStructuredParcelable& operator=(AidlStructuredParcelable&&) = delete;
Steven Moreland5557f1c2018-07-02 13:50:23 -0700758
759 const std::vector<std::unique_ptr<AidlVariableDeclaration>>& GetFields() const {
760 return variables_;
761 }
762
Steven Morelandc258abc2018-07-10 14:03:38 -0700763 const AidlStructuredParcelable* AsStructuredParcelable() const override { return this; }
Steven Morelanded83a282018-07-17 13:27:29 -0700764 std::string GetPreprocessDeclarationName() const override { return "structured_parcelable"; }
Steven Moreland5557f1c2018-07-02 13:50:23 -0700765
Jeongik Cha997281d2020-01-16 15:23:59 +0900766 void Dump(CodeWriter* writer) const override;
Jiyong Park02da7422018-07-16 16:00:26 +0900767
Steven Moreland0cea4aa2020-04-20 21:06:02 -0700768 std::set<AidlAnnotation::Type> GetSupportedAnnotations() const override;
Jeongik Chadb0f59e2018-11-01 18:11:21 +0900769 bool CheckValid(const AidlTypenames& typenames) const override;
Steven Morelandd59e3172020-05-11 16:42:09 -0700770 bool LanguageSpecificCheckValid(const AidlTypenames& typenames,
771 Options::Language lang) const override;
Jeongik Chadb0f59e2018-11-01 18:11:21 +0900772
Steven Moreland5557f1c2018-07-02 13:50:23 -0700773 private:
774 const std::vector<std::unique_ptr<AidlVariableDeclaration>> variables_;
Steven Moreland5557f1c2018-07-02 13:50:23 -0700775};
776
Daniel Norman85aed542019-08-21 12:01:14 -0700777class AidlEnumerator : public AidlNode {
778 public:
Daniel Norman2e4112d2019-10-03 10:22:35 -0700779 AidlEnumerator(const AidlLocation& location, const std::string& name, AidlConstantValue* value,
780 const std::string& comments);
Daniel Norman85aed542019-08-21 12:01:14 -0700781 virtual ~AidlEnumerator() = default;
782
Jiyong Parkd800fef2020-07-22 18:09:43 +0900783 // non-copyable, non-movable
784 AidlEnumerator(const AidlEnumerator&) = delete;
785 AidlEnumerator(AidlEnumerator&&) = delete;
786 AidlEnumerator& operator=(const AidlEnumerator&) = delete;
787 AidlEnumerator& operator=(AidlEnumerator&&) = delete;
788
Daniel Norman85aed542019-08-21 12:01:14 -0700789 const std::string& GetName() const { return name_; }
Will McVickerd7d18df2019-09-12 13:40:50 -0700790 AidlConstantValue* GetValue() const { return value_.get(); }
Daniel Norman2e4112d2019-10-03 10:22:35 -0700791 const std::string& GetComments() const { return comments_; }
Daniel Norman85aed542019-08-21 12:01:14 -0700792 bool CheckValid(const AidlTypeSpecifier& enum_backing_type) const;
793
794 string ValueString(const AidlTypeSpecifier& backing_type,
795 const ConstantValueDecorator& decorator) const;
796
Daniel Normanb28684e2019-10-17 15:31:39 -0700797 void SetValue(std::unique_ptr<AidlConstantValue> value) { value_ = std::move(value); }
798
Daniel Norman85aed542019-08-21 12:01:14 -0700799 private:
800 const std::string name_;
Will McVickerd7d18df2019-09-12 13:40:50 -0700801 unique_ptr<AidlConstantValue> value_;
Daniel Norman2e4112d2019-10-03 10:22:35 -0700802 const std::string comments_;
Daniel Norman85aed542019-08-21 12:01:14 -0700803};
804
805class AidlEnumDeclaration : public AidlDefinedType {
806 public:
Will McVickerd7d18df2019-09-12 13:40:50 -0700807 AidlEnumDeclaration(const AidlLocation& location, const string& name,
Daniel Norman85aed542019-08-21 12:01:14 -0700808 std::vector<std::unique_ptr<AidlEnumerator>>* enumerators,
Jiyong Park18132182020-06-08 20:24:40 +0900809 const std::string& package, const std::string& comments);
Daniel Norman85aed542019-08-21 12:01:14 -0700810 virtual ~AidlEnumDeclaration() = default;
811
Jiyong Parkd800fef2020-07-22 18:09:43 +0900812 // non-copyable, non-movable
813 AidlEnumDeclaration(const AidlEnumDeclaration&) = delete;
814 AidlEnumDeclaration(AidlEnumDeclaration&&) = delete;
815 AidlEnumDeclaration& operator=(const AidlEnumDeclaration&) = delete;
816 AidlEnumDeclaration& operator=(AidlEnumDeclaration&&) = delete;
817
Daniel Norman85aed542019-08-21 12:01:14 -0700818 void SetBackingType(std::unique_ptr<const AidlTypeSpecifier> type);
819 const AidlTypeSpecifier& GetBackingType() const { return *backing_type_; }
820 const std::vector<std::unique_ptr<AidlEnumerator>>& GetEnumerators() const {
821 return enumerators_;
822 }
Steven Moreland59e53e42019-11-26 20:38:08 -0800823 bool Autofill();
Steven Moreland0cea4aa2020-04-20 21:06:02 -0700824 std::set<AidlAnnotation::Type> GetSupportedAnnotations() const override;
Daniel Norman85aed542019-08-21 12:01:14 -0700825 bool CheckValid(const AidlTypenames& typenames) const override;
Steven Morelandd59e3172020-05-11 16:42:09 -0700826 bool LanguageSpecificCheckValid(const AidlTypenames& /*typenames*/,
827 Options::Language) const override {
828 return true;
829 }
Daniel Norman85aed542019-08-21 12:01:14 -0700830 std::string GetPreprocessDeclarationName() const override { return "enum"; }
Jeongik Cha997281d2020-01-16 15:23:59 +0900831 void Dump(CodeWriter* writer) const override;
Daniel Norman85aed542019-08-21 12:01:14 -0700832
833 const AidlEnumDeclaration* AsEnumDeclaration() const override { return this; }
834
835 private:
836 const std::string name_;
837 const std::vector<std::unique_ptr<AidlEnumerator>> enumerators_;
838 std::unique_ptr<const AidlTypeSpecifier> backing_type_;
Daniel Norman85aed542019-08-21 12:01:14 -0700839};
840
Jiyong Park1deecc32018-07-17 01:14:41 +0900841class AidlInterface final : public AidlDefinedType {
Casey Dahlin1ae2bc52015-10-07 18:49:10 -0700842 public:
Steven Moreland46e9da82018-07-27 15:45:29 -0700843 AidlInterface(const AidlLocation& location, const std::string& name, const std::string& comments,
844 bool oneway_, std::vector<std::unique_ptr<AidlMember>>* members,
Jiyong Park18132182020-06-08 20:24:40 +0900845 const std::string& package);
Casey Dahlin1ae2bc52015-10-07 18:49:10 -0700846 virtual ~AidlInterface() = default;
847
Jiyong Parkd800fef2020-07-22 18:09:43 +0900848 // non-copyable, non-movable
849 AidlInterface(const AidlInterface&) = delete;
850 AidlInterface(AidlInterface&&) = delete;
851 AidlInterface& operator=(const AidlInterface&) = delete;
852 AidlInterface& operator=(AidlInterface&&) = delete;
853
Casey Dahlinfb7da2e2015-10-08 17:26:09 -0700854 const std::vector<std::unique_ptr<AidlMethod>>& GetMethods() const
855 { return methods_; }
Jiyong Park309668e2018-07-28 16:55:44 +0900856 std::vector<std::unique_ptr<AidlMethod>>& GetMutableMethods() { return methods_; }
Steven Moreland693640b2018-07-19 13:46:27 -0700857 const std::vector<std::unique_ptr<AidlConstantDeclaration>>& GetConstantDeclarations() const {
858 return constants_;
859 }
Casey Dahlina2f77c42015-12-01 18:26:02 -0800860
Steven Morelandc258abc2018-07-10 14:03:38 -0700861 const AidlInterface* AsInterface() const override { return this; }
Steven Morelanded83a282018-07-17 13:27:29 -0700862 std::string GetPreprocessDeclarationName() const override { return "interface"; }
Steven Moreland5557f1c2018-07-02 13:50:23 -0700863
Jeongik Cha997281d2020-01-16 15:23:59 +0900864 void Dump(CodeWriter* writer) const override;
Jiyong Park02da7422018-07-16 16:00:26 +0900865
Steven Moreland0cea4aa2020-04-20 21:06:02 -0700866 std::set<AidlAnnotation::Type> GetSupportedAnnotations() const override;
Jeongik Chadb0f59e2018-11-01 18:11:21 +0900867 bool CheckValid(const AidlTypenames& typenames) const override;
Steven Morelandd59e3172020-05-11 16:42:09 -0700868 bool LanguageSpecificCheckValid(const AidlTypenames& typenames,
869 Options::Language lang) const override;
Jeongik Chadb0f59e2018-11-01 18:11:21 +0900870
Casey Dahlin1ae2bc52015-10-07 18:49:10 -0700871 private:
Casey Dahlinfb7da2e2015-10-08 17:26:09 -0700872 std::vector<std::unique_ptr<AidlMethod>> methods_;
Steven Moreland693640b2018-07-19 13:46:27 -0700873 std::vector<std::unique_ptr<AidlConstantDeclaration>> constants_;
Casey Dahlin0a2f8be2015-09-28 16:15:29 -0700874};
Adam Lesinskiffa16862014-01-23 18:17:42 -0800875
Casey Dahlin0edf3422015-10-07 12:34:59 -0700876class AidlImport : public AidlNode {
877 public:
Steven Moreland46e9da82018-07-27 15:45:29 -0700878 AidlImport(const AidlLocation& location, const std::string& needed_class);
Casey Dahlin0edf3422015-10-07 12:34:59 -0700879 virtual ~AidlImport() = default;
880
Jiyong Parkd800fef2020-07-22 18:09:43 +0900881 // non-copyable, non-movable
882 AidlImport(const AidlImport&) = delete;
883 AidlImport(AidlImport&&) = delete;
884 AidlImport& operator=(const AidlImport&) = delete;
885 AidlImport& operator=(AidlImport&&) = delete;
886
Casey Dahlin0edf3422015-10-07 12:34:59 -0700887 const std::string& GetFilename() const { return filename_; }
888 const std::string& GetNeededClass() const { return needed_class_; }
Casey Dahlin0edf3422015-10-07 12:34:59 -0700889
890 private:
Casey Dahlin0edf3422015-10-07 12:34:59 -0700891 std::string filename_;
892 std::string needed_class_;
Casey Dahline2507492015-09-14 17:11:20 -0700893};
894
Jiyong Park62515512020-06-08 15:57:11 +0900895// AidlDocument models an AIDL file
896class AidlDocument : public AidlNode {
897 public:
898 AidlDocument(const AidlLocation& location, std::vector<std::unique_ptr<AidlImport>>& imports,
Jiyong Park8e79b7f2020-07-20 20:52:38 +0900899 std::vector<std::unique_ptr<AidlDefinedType>>&& defined_types)
900 : AidlNode(location),
901 imports_(std::move(imports)),
902 defined_types_(std::move(defined_types)) {}
Jiyong Parkd800fef2020-07-22 18:09:43 +0900903 ~AidlDocument() = default;
904
905 // non-copyable, non-movable
Jiyong Park8e79b7f2020-07-20 20:52:38 +0900906 AidlDocument(const AidlDocument&) = delete;
907 AidlDocument(AidlDocument&&) = delete;
908 AidlDocument& operator=(const AidlDocument&) = delete;
909 AidlDocument& operator=(AidlDocument&&) = delete;
Jiyong Parkd800fef2020-07-22 18:09:43 +0900910
911 const std::vector<std::unique_ptr<AidlImport>>& Imports() const { return imports_; }
912 const std::vector<std::unique_ptr<AidlDefinedType>>& DefinedTypes() const {
913 return defined_types_;
914 }
Jiyong Park62515512020-06-08 15:57:11 +0900915
916 private:
917 const std::vector<std::unique_ptr<AidlImport>> imports_;
Jiyong Park8e79b7f2020-07-20 20:52:38 +0900918 const std::vector<std::unique_ptr<AidlDefinedType>> defined_types_;
Jiyong Park62515512020-06-08 15:57:11 +0900919};