blob: cec529850e83b78f277883d0aae7af162950a8ae [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 Moreland46e9da82018-07-27 15:45:29 -070094 const std::string file_;
95 Point begin_;
96 Point end_;
Devin Mooredf93ebb2020-03-25 14:03:35 -070097 Source source_;
Steven Moreland46e9da82018-07-27 15:45:29 -070098};
99
Devin Mooredf93ebb2020-03-25 14:03:35 -0700100#define AIDL_LOCATION_HERE \
101 AidlLocation { __FILE__, {__LINE__, 0}, {__LINE__, 0}, AidlLocation::Source::INTERNAL }
Steven Moreland02e012e2018-08-02 14:58:10 -0700102
Steven Moreland46e9da82018-07-27 15:45:29 -0700103std::ostream& operator<<(std::ostream& os, const AidlLocation& l);
104
105// Anything that is locatable in a .aidl file.
106class AidlNode {
107 public:
108 AidlNode(const AidlLocation& location);
Steven Moreland3f658cf2018-08-20 13:40:54 -0700109
110 AidlNode(const AidlNode&) = default;
Steven Moreland3be75772018-08-20 13:27:43 -0700111 AidlNode(AidlNode&&) = default;
Steven Moreland46e9da82018-07-27 15:45:29 -0700112 virtual ~AidlNode() = default;
113
Devin Mooredf93ebb2020-03-25 14:03:35 -0700114 // To be able to print AidlLocation
Steven Morelandb0d15a52020-03-31 14:03:47 -0700115 friend class AidlErrorLog;
Andrei Onea8714b022019-02-01 18:55:54 +0000116 friend std::string android::aidl::mappings::dump_location(const AidlNode&);
Mathew Inwoodadb74672019-11-29 14:01:53 +0000117 friend std::string android::aidl::java::dump_location(const AidlNode&);
Steven Moreland46e9da82018-07-27 15:45:29 -0700118
Devin Mooredf93ebb2020-03-25 14:03:35 -0700119 protected:
120 // This should only be used to construct implicit nodes related to existing nodes
121 const AidlLocation& GetLocation() const { return location_; }
122
Steven Moreland46e9da82018-07-27 15:45:29 -0700123 private:
Mathew Inwoodadb74672019-11-29 14:01:53 +0000124 std::string PrintLine() const;
Andrei Onea8714b022019-02-01 18:55:54 +0000125 std::string PrintLocation() const;
Steven Moreland46e9da82018-07-27 15:45:29 -0700126 const AidlLocation location_;
Casey Dahlinbc7a50a2015-09-28 19:20:50 -0700127};
128
Steven Moreland46e9da82018-07-27 15:45:29 -0700129// Generic point for printing any error in the AIDL compiler.
Steven Morelandb0d15a52020-03-31 14:03:47 -0700130class AidlErrorLog {
Steven Moreland46e9da82018-07-27 15:45:29 -0700131 public:
Devin Moore5de18ed2020-04-02 13:52:29 -0700132 AidlErrorLog(bool fatal, const std::string& filename)
133 : AidlErrorLog(fatal, AidlLocation(filename, AidlLocation::Source::EXTERNAL)) {}
Steven Morelandb0d15a52020-03-31 14:03:47 -0700134 AidlErrorLog(bool fatal, const AidlLocation& location);
135 AidlErrorLog(bool fatal, const AidlNode& node) : AidlErrorLog(fatal, node.location_) {}
136 AidlErrorLog(bool fatal, const AidlNode* node) : AidlErrorLog(fatal, *node) {}
Steven Moreland92c55f12018-07-31 14:08:37 -0700137
138 template <typename T>
Steven Morelandb0d15a52020-03-31 14:03:47 -0700139 AidlErrorLog(bool fatal, const std::unique_ptr<T>& node) : AidlErrorLog(fatal, *node) {}
140 ~AidlErrorLog() {
Steven Moreland46e9da82018-07-27 15:45:29 -0700141 os_ << std::endl;
142 if (fatal_) abort();
Devin Moore5de18ed2020-04-02 13:52:29 -0700143 if (location_.IsInternal()) {
144 os_ << "Logging an internal location should not happen. Offending location: " << location_
145 << std::endl;
146 abort();
147 }
Steven Moreland46e9da82018-07-27 15:45:29 -0700148 }
149
150 std::ostream& os_;
151
Steven Moreland33efcf62020-04-10 16:40:43 -0700152 static void clearError() { sHadError = false; }
Steven Morelandfdb57cd2020-01-08 20:03:30 -0800153 static bool hadError() { return sHadError; }
154
Steven Moreland46e9da82018-07-27 15:45:29 -0700155 private:
Steven Moreland46e9da82018-07-27 15:45:29 -0700156
157 bool fatal_;
158
Devin Moore5de18ed2020-04-02 13:52:29 -0700159 const AidlLocation location_;
160
Steven Morelandfdb57cd2020-01-08 20:03:30 -0800161 static bool sHadError;
162
Steven Morelandb0d15a52020-03-31 14:03:47 -0700163 DISALLOW_COPY_AND_ASSIGN(AidlErrorLog);
Steven Moreland46e9da82018-07-27 15:45:29 -0700164};
165
Steven Morelandb0d15a52020-03-31 14:03:47 -0700166#define AIDL_ERROR(CONTEXT) ::AidlErrorLog(false /*fatal*/, (CONTEXT)).os_
167#define AIDL_FATAL(CONTEXT) ::AidlErrorLog(true /*fatal*/, (CONTEXT)).os_
Steven Moreland3f658cf2018-08-20 13:40:54 -0700168#define AIDL_FATAL_IF(CONDITION, CONTEXT) \
169 if (CONDITION) AIDL_FATAL(CONTEXT) << "Bad internal state: " << #CONDITION << ": "
Steven Moreland46e9da82018-07-27 15:45:29 -0700170
Casey Dahlina2f77c42015-12-01 18:26:02 -0800171namespace android {
172namespace aidl {
173
Jiyong Park1deecc32018-07-17 01:14:41 +0900174class AidlTypenames;
Casey Dahlina2f77c42015-12-01 18:26:02 -0800175
176} // namespace aidl
177} // namespace android
178
Jeongik Chadf76dc72019-11-28 00:08:47 +0900179// unique_ptr<AidlTypeSpecifier> for type arugment,
180// std::string for type parameter(T, U, and so on).
181template <typename T>
182class AidlParameterizable {
183 public:
184 AidlParameterizable(std::vector<T>* type_params) : type_params_(type_params) {}
185 virtual ~AidlParameterizable() = default;
186 bool IsGeneric() const { return type_params_ != nullptr; }
187 const std::vector<T>& GetTypeParameters() const { return *type_params_; }
188 bool CheckValid() const;
189
190 virtual const AidlNode& AsAidlNode() const = 0;
191
192 protected:
193 AidlParameterizable(const AidlParameterizable&);
194
195 private:
196 const unique_ptr<std::vector<T>> type_params_;
197 static_assert(std::is_same<T, unique_ptr<AidlTypeSpecifier>>::value ||
198 std::is_same<T, std::string>::value);
199};
200template <>
201bool AidlParameterizable<std::string>::CheckValid() const;
202
Andrei Onea9445fc62019-06-27 18:11:59 +0100203class AidlConstantValue;
204class AidlConstantDeclaration;
205
206// Transforms a value string into a language specific form. Raw value as produced by
207// AidlConstantValue.
208using ConstantValueDecorator =
209 std::function<std::string(const AidlTypeSpecifier& type, const std::string& raw_value)>;
210
Jiyong Park68bc77a2018-07-19 19:00:45 +0900211class AidlAnnotation : public AidlNode {
212 public:
Steven Moreland0cea4aa2020-04-20 21:06:02 -0700213 enum class Type {
214 BACKING = 1,
215 HIDE,
216 JAVA_STABLE_PARCELABLE,
217 UNSUPPORTED_APP_USAGE,
218 VINTF_STABILITY,
219 NULLABLE,
220 UTF8_IN_CPP,
Jiyong Parkbf5fd5c2020-06-05 19:48:05 +0900221 JAVA_PASSTHROUGH,
Steven Moreland0cea4aa2020-04-20 21:06:02 -0700222 };
223 static std::string TypeToString(Type type);
224
Andrei Onea9445fc62019-06-27 18:11:59 +0100225 static AidlAnnotation* Parse(
226 const AidlLocation& location, const string& name,
227 std::map<std::string, std::shared_ptr<AidlConstantValue>>* parameter_list);
Steven Moreland46e9da82018-07-27 15:45:29 -0700228
Steven Moreland3f658cf2018-08-20 13:40:54 -0700229 AidlAnnotation(const AidlAnnotation&) = default;
Steven Moreland3be75772018-08-20 13:27:43 -0700230 AidlAnnotation(AidlAnnotation&&) = default;
Jiyong Park68bc77a2018-07-19 19:00:45 +0900231 virtual ~AidlAnnotation() = default;
Andrei Onea9445fc62019-06-27 18:11:59 +0100232 bool CheckValid() const;
Steven Moreland3be75772018-08-20 13:27:43 -0700233
Steven Moreland0cea4aa2020-04-20 21:06:02 -0700234 const string& GetName() const { return schema_.name; };
235 const Type& GetType() const { return schema_.type; }
Daniel Norman37d43dd2019-09-09 17:22:34 -0700236 string ToString(const ConstantValueDecorator& decorator) const;
Andrei Onea9445fc62019-06-27 18:11:59 +0100237 std::map<std::string, std::string> AnnotationParams(
238 const ConstantValueDecorator& decorator) const;
Jiyong Parka6605ab2018-11-11 14:30:21 +0900239 const string& GetComments() const { return comments_; }
240 void SetComments(const string& comments) { comments_ = comments; }
Jiyong Park68bc77a2018-07-19 19:00:45 +0900241
242 private:
Steven Moreland0cea4aa2020-04-20 21:06:02 -0700243 struct Schema {
244 AidlAnnotation::Type type;
245
246 // text name in .aidl file, e.g. "nullable"
247 std::string name;
248
249 // map from param name -> value type
250 std::map<std::string, std::string> supported_parameters;
251 };
252 static const std::vector<Schema>& AllSchemas();
253
254 AidlAnnotation(const AidlLocation& location, const Schema& schema,
Andrei Onea9445fc62019-06-27 18:11:59 +0100255 std::map<std::string, std::shared_ptr<AidlConstantValue>>&& parameters);
Steven Moreland0cea4aa2020-04-20 21:06:02 -0700256
257 const Schema& schema_;
Jiyong Parka6605ab2018-11-11 14:30:21 +0900258 string comments_;
Andrei Onea9445fc62019-06-27 18:11:59 +0100259 std::map<std::string, std::shared_ptr<AidlConstantValue>> parameters_;
Jiyong Park68bc77a2018-07-19 19:00:45 +0900260};
261
Steven Moreland3be75772018-08-20 13:27:43 -0700262static inline bool operator<(const AidlAnnotation& lhs, const AidlAnnotation& rhs) {
263 return lhs.GetName() < rhs.GetName();
264}
265static inline bool operator==(const AidlAnnotation& lhs, const AidlAnnotation& rhs) {
266 return lhs.GetName() == rhs.GetName();
267}
Jiyong Park3656c3c2018-08-01 20:02:01 +0900268
Casey Dahline7922932016-02-29 17:23:01 -0800269class AidlAnnotatable : public AidlNode {
Casey Dahlin0ee37582015-09-30 16:31:55 -0700270 public:
Steven Moreland46e9da82018-07-27 15:45:29 -0700271 AidlAnnotatable(const AidlLocation& location);
Steven Moreland3f658cf2018-08-20 13:40:54 -0700272
273 AidlAnnotatable(const AidlAnnotatable&) = default;
274 AidlAnnotatable(AidlAnnotatable&&) = default;
Casey Dahline7922932016-02-29 17:23:01 -0800275 virtual ~AidlAnnotatable() = default;
276
Artur Satayev91fe8712019-07-29 13:06:01 +0100277 void Annotate(vector<AidlAnnotation>&& annotations) {
278 for (auto& annotation : annotations) {
279 annotations_.emplace_back(std::move(annotation));
280 }
281 }
Jiyong Park68bc77a2018-07-19 19:00:45 +0900282 bool IsNullable() const;
Jiyong Park68bc77a2018-07-19 19:00:45 +0900283 bool IsUtf8InCpp() const;
Steven Morelanda57d0a62019-07-30 09:41:14 -0700284 bool IsVintfStability() const;
Jeongik Cha88f95a82020-01-15 13:02:16 +0900285 bool IsStableApiParcelable(Options::Language lang) const;
Makoto Onuki78a1c1c2020-03-04 16:57:23 -0800286 bool IsHide() const;
Andrei Onea9445fc62019-06-27 18:11:59 +0100287
Steven Moreland7e4b9502020-02-20 18:10:42 -0800288 void DumpAnnotations(CodeWriter* writer) const;
289
Andrei Onea9445fc62019-06-27 18:11:59 +0100290 const AidlAnnotation* UnsupportedAppUsage() const;
Jiyong Parkbf5fd5c2020-06-05 19:48:05 +0900291 const AidlAnnotation* JavaPassthrough() const;
Daniel Norman716d3112019-09-10 13:11:56 -0700292 const AidlTypeSpecifier* BackingType(const AidlTypenames& typenames) const;
Jiyong Park68bc77a2018-07-19 19:00:45 +0900293 std::string ToString() const;
Casey Dahline7922932016-02-29 17:23:01 -0800294
Jiyong Parka6605ab2018-11-11 14:30:21 +0900295 const vector<AidlAnnotation>& GetAnnotations() const { return annotations_; }
Devin Moore24f68572020-02-26 13:20:59 -0800296 virtual bool CheckValid(const AidlTypenames&) const;
Jiyong Park3656c3c2018-08-01 20:02:01 +0900297
Steven Moreland181144c2020-04-20 19:57:56 -0700298 protected:
Steven Moreland0cea4aa2020-04-20 21:06:02 -0700299 virtual std::set<AidlAnnotation::Type> GetSupportedAnnotations() const = 0;
Steven Moreland181144c2020-04-20 19:57:56 -0700300
Casey Dahline7922932016-02-29 17:23:01 -0800301 private:
Jiyong Parka6605ab2018-11-11 14:30:21 +0900302 vector<AidlAnnotation> annotations_;
Casey Dahline7922932016-02-29 17:23:01 -0800303};
304
Jiyong Park1deecc32018-07-17 01:14:41 +0900305class AidlQualifiedName;
306
307// AidlTypeSpecifier represents a reference to either a built-in type,
308// a defined type, or a variant (e.g., array of generic) of a type.
Jeongik Chadf76dc72019-11-28 00:08:47 +0900309class AidlTypeSpecifier final : public AidlAnnotatable,
310 public AidlParameterizable<unique_ptr<AidlTypeSpecifier>> {
Casey Dahline7922932016-02-29 17:23:01 -0800311 public:
Steven Moreland46e9da82018-07-27 15:45:29 -0700312 AidlTypeSpecifier(const AidlLocation& location, const string& unresolved_name, bool is_array,
313 vector<unique_ptr<AidlTypeSpecifier>>* type_params, const string& comments);
Jiyong Parkd59a10d2018-07-18 11:12:55 +0900314 virtual ~AidlTypeSpecifier() = default;
Casey Dahlin0ee37582015-09-30 16:31:55 -0700315
Steven Moreland3f658cf2018-08-20 13:40:54 -0700316 // Copy of this type which is not an array.
317 AidlTypeSpecifier ArrayBase() const;
318
Jiyong Park1deecc32018-07-17 01:14:41 +0900319 // Returns the full-qualified name of the base type.
320 // int -> int
321 // int[] -> int
322 // List<String> -> List
323 // IFoo -> foo.bar.IFoo (if IFoo is in package foo.bar)
324 const string& GetName() const {
325 if (IsResolved()) {
326 return fully_qualified_name_;
327 } else {
328 return GetUnresolvedName();
329 }
330 }
Casey Dahlin0ee37582015-09-30 16:31:55 -0700331
Jiyong Park1deecc32018-07-17 01:14:41 +0900332 // Returns string representation of this type specifier.
Artur Satayev91fe8712019-07-29 13:06:01 +0100333 // This is GetBaseTypeName() + array modifier or generic type parameters
Jiyong Park1deecc32018-07-17 01:14:41 +0900334 string ToString() const;
335
Jiyong Park02da7422018-07-16 16:00:26 +0900336 std::string Signature() const;
337
Jiyong Park1deecc32018-07-17 01:14:41 +0900338 const string& GetUnresolvedName() const { return unresolved_name_; }
339
Jeongik Cha997281d2020-01-16 15:23:59 +0900340 bool IsHidden() const;
341
Jiyong Park1deecc32018-07-17 01:14:41 +0900342 const string& GetComments() const { return comments_; }
343
Jeongik Cha1a7ab642019-07-29 17:31:02 +0900344 const std::vector<std::string> GetSplitName() const { return split_name_; }
345
Jiyong Parka6605ab2018-11-11 14:30:21 +0900346 void SetComments(const string& comment) { comments_ = comment; }
347
Jiyong Park1deecc32018-07-17 01:14:41 +0900348 bool IsResolved() const { return fully_qualified_name_ != ""; }
349
350 bool IsArray() const { return is_array_; }
351
Jiyong Park1deecc32018-07-17 01:14:41 +0900352 // Resolve the base type name to a fully-qualified name. Return false if the
353 // resolution fails.
Daniel Norman716d3112019-09-10 13:11:56 -0700354 bool Resolve(const AidlTypenames& typenames);
Casey Dahlin0ee37582015-09-30 16:31:55 -0700355
Steven Moreland0cea4aa2020-04-20 21:06:02 -0700356 std::set<AidlAnnotation::Type> GetSupportedAnnotations() const override;
Devin Moore24f68572020-02-26 13:20:59 -0800357 bool CheckValid(const AidlTypenames& typenames) const override;
Steven Morelandd59e3172020-05-11 16:42:09 -0700358 bool LanguageSpecificCheckValid(const AidlTypenames& typenames, Options::Language lang) const;
Jeongik Chadf76dc72019-11-28 00:08:47 +0900359 const AidlNode& AsAidlNode() const override { return *this; }
Jiyong Park1d2df7d2018-07-23 15:22:50 +0900360
Casey Dahlin0ee37582015-09-30 16:31:55 -0700361 private:
Steven Moreland3f658cf2018-08-20 13:40:54 -0700362 AidlTypeSpecifier(const AidlTypeSpecifier&) = default;
363
Jiyong Park1deecc32018-07-17 01:14:41 +0900364 const string unresolved_name_;
365 string fully_qualified_name_;
Steven Moreland3f658cf2018-08-20 13:40:54 -0700366 bool is_array_;
Jiyong Parka6605ab2018-11-11 14:30:21 +0900367 string comments_;
Jeongik Cha1a7ab642019-07-29 17:31:02 +0900368 vector<string> split_name_;
Casey Dahlin0ee37582015-09-30 16:31:55 -0700369};
370
Steven Moreland860b1942018-08-16 14:59:28 -0700371// Returns the universal value unaltered.
372std::string AidlConstantValueDecorator(const AidlTypeSpecifier& type, const std::string& raw_value);
373
Steven Moreland9ea10e32018-07-19 15:26:09 -0700374class AidlConstantValue;
Steven Moreland5557f1c2018-07-02 13:50:23 -0700375class AidlVariableDeclaration : public AidlNode {
376 public:
Steven Moreland46e9da82018-07-27 15:45:29 -0700377 AidlVariableDeclaration(const AidlLocation& location, AidlTypeSpecifier* type,
378 const std::string& name);
379 AidlVariableDeclaration(const AidlLocation& location, AidlTypeSpecifier* type,
380 const std::string& name, AidlConstantValue* default_value);
Steven Moreland5557f1c2018-07-02 13:50:23 -0700381 virtual ~AidlVariableDeclaration() = default;
382
383 std::string GetName() const { return name_; }
Jiyong Parkd59a10d2018-07-18 11:12:55 +0900384 const AidlTypeSpecifier& GetType() const { return *type_; }
Steven Moreland9ea10e32018-07-19 15:26:09 -0700385 const AidlConstantValue* GetDefaultValue() const { return default_value_.get(); }
386
Jiyong Parkd59a10d2018-07-18 11:12:55 +0900387 AidlTypeSpecifier* GetMutableType() { return type_.get(); }
Steven Moreland5557f1c2018-07-02 13:50:23 -0700388
Jeongik Chadb0f59e2018-11-01 18:11:21 +0900389 bool CheckValid(const AidlTypenames& typenames) const;
Steven Moreland5557f1c2018-07-02 13:50:23 -0700390 std::string ToString() const;
Jiyong Park02da7422018-07-16 16:00:26 +0900391 std::string Signature() const;
Steven Moreland5557f1c2018-07-02 13:50:23 -0700392
Steven Moreland860b1942018-08-16 14:59:28 -0700393 std::string ValueString(const ConstantValueDecorator& decorator) const;
Steven Moreland25294322018-08-07 18:13:55 -0700394
Steven Moreland5557f1c2018-07-02 13:50:23 -0700395 private:
Jiyong Parkd59a10d2018-07-18 11:12:55 +0900396 std::unique_ptr<AidlTypeSpecifier> type_;
Steven Moreland5557f1c2018-07-02 13:50:23 -0700397 std::string name_;
Steven Moreland9ea10e32018-07-19 15:26:09 -0700398 std::unique_ptr<AidlConstantValue> default_value_;
Steven Moreland5557f1c2018-07-02 13:50:23 -0700399
400 DISALLOW_COPY_AND_ASSIGN(AidlVariableDeclaration);
401};
402
403class AidlArgument : public AidlVariableDeclaration {
Casey Dahlinbc7a50a2015-09-28 19:20:50 -0700404 public:
Casey Dahlinc378c992015-09-29 16:50:40 -0700405 enum Direction { IN_DIR = 1, OUT_DIR = 2, INOUT_DIR = 3 };
406
Steven Moreland46e9da82018-07-27 15:45:29 -0700407 AidlArgument(const AidlLocation& location, AidlArgument::Direction direction,
408 AidlTypeSpecifier* type, const std::string& name);
409 AidlArgument(const AidlLocation& location, AidlTypeSpecifier* type, const std::string& name);
Casey Dahlinbc7a50a2015-09-28 19:20:50 -0700410 virtual ~AidlArgument() = default;
411
Casey Dahlinc378c992015-09-29 16:50:40 -0700412 Direction GetDirection() const { return direction_; }
Christopher Wileyad339272015-10-05 19:11:58 -0700413 bool IsOut() const { return direction_ & OUT_DIR; }
414 bool IsIn() const { return direction_ & IN_DIR; }
Casey Dahlinc378c992015-09-29 16:50:40 -0700415 bool DirectionWasSpecified() const { return direction_specified_; }
Jiyong Park3656c3c2018-08-01 20:02:01 +0900416 string GetDirectionSpecifier() const;
Christopher Wileyad339272015-10-05 19:11:58 -0700417
Casey Dahlinc378c992015-09-29 16:50:40 -0700418 std::string ToString() const;
Jiyong Park02da7422018-07-16 16:00:26 +0900419 std::string Signature() const;
Casey Dahlinc378c992015-09-29 16:50:40 -0700420
Casey Dahlinbc7a50a2015-09-28 19:20:50 -0700421 private:
Casey Dahlinc378c992015-09-29 16:50:40 -0700422 Direction direction_;
423 bool direction_specified_;
424
Casey Dahlinbc7a50a2015-09-28 19:20:50 -0700425 DISALLOW_COPY_AND_ASSIGN(AidlArgument);
Casey Dahlina834dd42015-09-23 11:52:15 -0700426};
Adam Lesinskiffa16862014-01-23 18:17:42 -0800427
Casey Dahlind40e2fe2015-11-24 14:06:52 -0800428class AidlMethod;
Steven Moreland693640b2018-07-19 13:46:27 -0700429class AidlConstantDeclaration;
Daniel Norman85aed542019-08-21 12:01:14 -0700430class AidlEnumDeclaration;
Casey Dahlind40e2fe2015-11-24 14:06:52 -0800431class AidlMember : public AidlNode {
432 public:
Steven Moreland46e9da82018-07-27 15:45:29 -0700433 AidlMember(const AidlLocation& location);
Casey Dahlind40e2fe2015-11-24 14:06:52 -0800434 virtual ~AidlMember() = default;
435
436 virtual AidlMethod* AsMethod() { return nullptr; }
Steven Moreland693640b2018-07-19 13:46:27 -0700437 virtual AidlConstantDeclaration* AsConstantDeclaration() { return nullptr; }
Casey Dahlind40e2fe2015-11-24 14:06:52 -0800438
439 private:
440 DISALLOW_COPY_AND_ASSIGN(AidlMember);
441};
442
Will McVickerd7d18df2019-09-12 13:40:50 -0700443class AidlUnaryConstExpression;
444class AidlBinaryConstExpression;
445
Steven Moreland693640b2018-07-19 13:46:27 -0700446class AidlConstantValue : public AidlNode {
Casey Dahlind40e2fe2015-11-24 14:06:52 -0800447 public:
Will McVickerd7d18df2019-09-12 13:40:50 -0700448 enum class Type {
449 // WARNING: Don't change this order! The order is used to determine type
450 // promotion during a binary expression.
451 BOOLEAN,
452 INT8,
453 INT32,
454 INT64,
455 ARRAY,
456 CHARACTER,
457 STRING,
458 FLOATING,
459 UNARY,
460 BINARY,
461 ERROR,
462 };
463
464 /*
465 * Return the value casted to the given type.
466 */
467 template <typename T>
468 T cast() const;
Casey Dahlind40e2fe2015-11-24 14:06:52 -0800469
Steven Moreland693640b2018-07-19 13:46:27 -0700470 virtual ~AidlConstantValue() = default;
Casey Dahlind40e2fe2015-11-24 14:06:52 -0800471
Steven Moreland25294322018-08-07 18:13:55 -0700472 static AidlConstantValue* Boolean(const AidlLocation& location, bool value);
473 static AidlConstantValue* Character(const AidlLocation& location, char value);
Steven Moreland25294322018-08-07 18:13:55 -0700474 // example: 123, -5498, maybe any size
Will McVickerd7d18df2019-09-12 13:40:50 -0700475 static AidlConstantValue* Integral(const AidlLocation& location, const string& value);
476 static AidlConstantValue* Floating(const AidlLocation& location, const std::string& value);
Steven Moreland860b1942018-08-16 14:59:28 -0700477 static AidlConstantValue* Array(const AidlLocation& location,
Will McVickerd7d18df2019-09-12 13:40:50 -0700478 std::unique_ptr<vector<unique_ptr<AidlConstantValue>>> values);
Steven Moreland693640b2018-07-19 13:46:27 -0700479 // example: "\"asdf\""
Will McVickerd7d18df2019-09-12 13:40:50 -0700480 static AidlConstantValue* String(const AidlLocation& location, const string& value);
Steven Moreland693640b2018-07-19 13:46:27 -0700481
Daniel Normanf0ca44f2019-10-25 09:59:44 -0700482 // Construct an AidlConstantValue by evaluating the other integral constant's
483 // value string. This does not preserve the structure of the copied constant.
Steven Moreland59e53e42019-11-26 20:38:08 -0800484 // Returns nullptr and logs if value cannot be copied.
Daniel Normanf0ca44f2019-10-25 09:59:44 -0700485 static AidlConstantValue* ShallowIntegralCopy(const AidlConstantValue& other);
Daniel Normanb28684e2019-10-17 15:31:39 -0700486
Will McVickerd7d18df2019-09-12 13:40:50 -0700487 Type GetType() const { return final_type_; }
Steven Moreland25294322018-08-07 18:13:55 -0700488
Will McVickerd7d18df2019-09-12 13:40:50 -0700489 virtual bool CheckValid() const;
Steven Moreland860b1942018-08-16 14:59:28 -0700490
491 // Raw value of type (currently valid in C++ and Java). Empty string on error.
Steven Moreland4bcb05c2019-11-27 18:57:47 -0800492 string ValueString(const AidlTypeSpecifier& type, const ConstantValueDecorator& decorator) const;
Casey Dahlind40e2fe2015-11-24 14:06:52 -0800493
494 private:
Will McVickerd7d18df2019-09-12 13:40:50 -0700495 AidlConstantValue(const AidlLocation& location, Type parsed_type, int64_t parsed_value,
496 const string& checked_value);
497 AidlConstantValue(const AidlLocation& location, Type type, const string& checked_value);
Steven Moreland860b1942018-08-16 14:59:28 -0700498 AidlConstantValue(const AidlLocation& location, Type type,
Will McVickerd7d18df2019-09-12 13:40:50 -0700499 std::unique_ptr<vector<unique_ptr<AidlConstantValue>>> values);
Steven Moreland25294322018-08-07 18:13:55 -0700500 static string ToString(Type type);
Will McVickerd7d18df2019-09-12 13:40:50 -0700501 static bool ParseIntegral(const string& value, int64_t* parsed_value, Type* parsed_type);
502 static bool IsHex(const string& value);
Steven Moreland4bcb05c2019-11-27 18:57:47 -0800503
Will McVickerd7d18df2019-09-12 13:40:50 -0700504 virtual bool evaluate(const AidlTypeSpecifier& type) const;
Casey Dahlind40e2fe2015-11-24 14:06:52 -0800505
Steven Moreland693640b2018-07-19 13:46:27 -0700506 const Type type_ = Type::ERROR;
Will McVickerd7d18df2019-09-12 13:40:50 -0700507 const vector<unique_ptr<AidlConstantValue>> values_; // if type_ == ARRAY
508 const string value_; // otherwise
509
510 // State for tracking evaluation of expressions
Steven Moreland4bcb05c2019-11-27 18:57:47 -0800511 mutable bool is_valid_ = false; // cache of CheckValid, but may be marked false in evaluate
512 mutable bool is_evaluated_ = false; // whether evaluate has been called
Will McVickerd7d18df2019-09-12 13:40:50 -0700513 mutable Type final_type_;
514 mutable int64_t final_value_;
515 mutable string final_string_value_ = "";
Steven Moreland693640b2018-07-19 13:46:27 -0700516
517 DISALLOW_COPY_AND_ASSIGN(AidlConstantValue);
Will McVickerd7d18df2019-09-12 13:40:50 -0700518
519 friend AidlUnaryConstExpression;
520 friend AidlBinaryConstExpression;
521};
522
523class AidlUnaryConstExpression : public AidlConstantValue {
524 public:
525 AidlUnaryConstExpression(const AidlLocation& location, const string& op,
526 std::unique_ptr<AidlConstantValue> rval);
527
528 static bool IsCompatibleType(Type type, const string& op);
529 bool CheckValid() const override;
Will McVickerd7d18df2019-09-12 13:40:50 -0700530 private:
531 bool evaluate(const AidlTypeSpecifier& type) const override;
532
533 std::unique_ptr<AidlConstantValue> unary_;
534 const string op_;
535};
536
537class AidlBinaryConstExpression : public AidlConstantValue {
538 public:
539 AidlBinaryConstExpression(const AidlLocation& location, std::unique_ptr<AidlConstantValue> lval,
540 const string& op, std::unique_ptr<AidlConstantValue> rval);
541
542 bool CheckValid() const override;
Will McVickerd7d18df2019-09-12 13:40:50 -0700543
544 static bool AreCompatibleTypes(Type t1, Type t2);
545 // Returns the promoted kind for both operands
546 static Type UsualArithmeticConversion(Type left, Type right);
547 // Returns the promoted integral type where INT32 is the smallest type
548 static Type IntegralPromotion(Type in);
549
550 private:
551 bool evaluate(const AidlTypeSpecifier& type) const override;
552
553 std::unique_ptr<AidlConstantValue> left_val_;
554 std::unique_ptr<AidlConstantValue> right_val_;
555 const string op_;
Christopher Wileyd6bdd8d2016-05-03 11:23:13 -0700556};
557
Andrei Onea9445fc62019-06-27 18:11:59 +0100558struct AidlAnnotationParameter {
559 std::string name;
560 std::unique_ptr<AidlConstantValue> value;
561};
562
Steven Moreland693640b2018-07-19 13:46:27 -0700563class AidlConstantDeclaration : public AidlMember {
Christopher Wileyd6bdd8d2016-05-03 11:23:13 -0700564 public:
Steven Moreland46e9da82018-07-27 15:45:29 -0700565 AidlConstantDeclaration(const AidlLocation& location, AidlTypeSpecifier* specifier,
Will McVickerd7d18df2019-09-12 13:40:50 -0700566 const string& name, AidlConstantValue* value);
Steven Moreland693640b2018-07-19 13:46:27 -0700567 virtual ~AidlConstantDeclaration() = default;
Christopher Wileyd6bdd8d2016-05-03 11:23:13 -0700568
Steven Moreland693640b2018-07-19 13:46:27 -0700569 const AidlTypeSpecifier& GetType() const { return *type_; }
Steven Moreland4d12f9a2018-10-31 14:30:55 -0700570 AidlTypeSpecifier* GetMutableType() { return type_.get(); }
Will McVickerd7d18df2019-09-12 13:40:50 -0700571 const string& GetName() const { return name_; }
Steven Moreland693640b2018-07-19 13:46:27 -0700572 const AidlConstantValue& GetValue() const { return *value_; }
Jeongik Chadb0f59e2018-11-01 18:11:21 +0900573 bool CheckValid(const AidlTypenames& typenames) const;
Christopher Wileyd6bdd8d2016-05-03 11:23:13 -0700574
Will McVickerd7d18df2019-09-12 13:40:50 -0700575 string ToString() const;
576 string Signature() const;
Steven Moreland860b1942018-08-16 14:59:28 -0700577 string ValueString(const ConstantValueDecorator& decorator) const {
Will McVickerd7d18df2019-09-12 13:40:50 -0700578 return value_->ValueString(GetType(), decorator);
Steven Moreland860b1942018-08-16 14:59:28 -0700579 }
Steven Moreland25294322018-08-07 18:13:55 -0700580
Steven Moreland693640b2018-07-19 13:46:27 -0700581 AidlConstantDeclaration* AsConstantDeclaration() override { return this; }
Christopher Wileyd6bdd8d2016-05-03 11:23:13 -0700582
583 private:
Steven Moreland693640b2018-07-19 13:46:27 -0700584 const unique_ptr<AidlTypeSpecifier> type_;
Will McVickerd7d18df2019-09-12 13:40:50 -0700585 const string name_;
586 unique_ptr<AidlConstantValue> value_;
Christopher Wileyd6bdd8d2016-05-03 11:23:13 -0700587
Steven Moreland693640b2018-07-19 13:46:27 -0700588 DISALLOW_COPY_AND_ASSIGN(AidlConstantDeclaration);
Casey Dahlind40e2fe2015-11-24 14:06:52 -0800589};
590
591class AidlMethod : public AidlMember {
Casey Dahlin5c69deb2015-10-01 14:44:12 -0700592 public:
Will McVickerd7d18df2019-09-12 13:40:50 -0700593 AidlMethod(const AidlLocation& location, bool oneway, AidlTypeSpecifier* type, const string& name,
594 vector<unique_ptr<AidlArgument>>* args, const string& comments);
595 AidlMethod(const AidlLocation& location, bool oneway, AidlTypeSpecifier* type, const string& name,
596 vector<unique_ptr<AidlArgument>>* args, const string& comments, int id,
597 bool is_user_defined = true);
Casey Dahlin5c69deb2015-10-01 14:44:12 -0700598 virtual ~AidlMethod() = default;
599
Casey Dahlind40e2fe2015-11-24 14:06:52 -0800600 AidlMethod* AsMethod() override { return this; }
Jeongik Cha997281d2020-01-16 15:23:59 +0900601 bool IsHidden() const;
Will McVickerd7d18df2019-09-12 13:40:50 -0700602 const string& GetComments() const { return comments_; }
Jiyong Parkd59a10d2018-07-18 11:12:55 +0900603 const AidlTypeSpecifier& GetType() const { return *type_; }
604 AidlTypeSpecifier* GetMutableType() { return type_.get(); }
Steven Morelandacd53472018-12-14 10:17:26 -0800605
Steven Moreland8c70ba92018-12-17 10:20:31 -0800606 // set if this method is part of an interface that is marked oneway
607 void ApplyInterfaceOneway(bool oneway) { oneway_ = oneway_ || oneway; }
Casey Dahlinf4a93112015-10-05 16:58:09 -0700608 bool IsOneway() const { return oneway_; }
Steven Morelandacd53472018-12-14 10:17:26 -0800609
Casey Dahlinf4a93112015-10-05 16:58:09 -0700610 const std::string& GetName() const { return name_; }
Casey Dahlinf4a93112015-10-05 16:58:09 -0700611 bool HasId() const { return has_id_; }
Jiyong Parked65bf42018-08-28 15:43:27 +0900612 int GetId() const { return id_; }
Casey Dahlinf4a93112015-10-05 16:58:09 -0700613 void SetId(unsigned id) { id_ = id; }
Casey Dahlinf2d23f72015-10-02 16:19:19 -0700614
Jiyong Park309668e2018-07-28 16:55:44 +0900615 bool IsUserDefined() const { return is_user_defined_; }
616
Casey Dahlinf4a93112015-10-05 16:58:09 -0700617 const std::vector<std::unique_ptr<AidlArgument>>& GetArguments() const {
Christopher Wileyad339272015-10-05 19:11:58 -0700618 return arguments_;
619 }
620 // An inout parameter will appear in both GetInArguments()
621 // and GetOutArguments(). AidlMethod retains ownership of the argument
622 // pointers returned in this way.
623 const std::vector<const AidlArgument*>& GetInArguments() const {
624 return in_arguments_;
625 }
626 const std::vector<const AidlArgument*>& GetOutArguments() const {
627 return out_arguments_;
Casey Dahlinf4a93112015-10-05 16:58:09 -0700628 }
Casey Dahlin5c69deb2015-10-01 14:44:12 -0700629
Jiyong Park309668e2018-07-28 16:55:44 +0900630 // name + type parameter types
631 // i.e, foo(int, String)
Jiyong Park02da7422018-07-16 16:00:26 +0900632 std::string Signature() const;
633
Jiyong Park309668e2018-07-28 16:55:44 +0900634 // return type + name + type parameter types + annotations
635 // i.e, boolean foo(int, @Nullable String)
636 std::string ToString() const;
637
Casey Dahlin5c69deb2015-10-01 14:44:12 -0700638 private:
Casey Dahlinf4a93112015-10-05 16:58:09 -0700639 bool oneway_;
Casey Dahlinf2d23f72015-10-02 16:19:19 -0700640 std::string comments_;
Jiyong Parkd59a10d2018-07-18 11:12:55 +0900641 std::unique_ptr<AidlTypeSpecifier> type_;
Casey Dahlinf4a93112015-10-05 16:58:09 -0700642 std::string name_;
Christopher Wileyad339272015-10-05 19:11:58 -0700643 const std::vector<std::unique_ptr<AidlArgument>> arguments_;
644 std::vector<const AidlArgument*> in_arguments_;
645 std::vector<const AidlArgument*> out_arguments_;
Casey Dahlinf4a93112015-10-05 16:58:09 -0700646 bool has_id_;
647 int id_;
Jiyong Park309668e2018-07-28 16:55:44 +0900648 bool is_user_defined_ = true;
Casey Dahlinf2d23f72015-10-02 16:19:19 -0700649
Casey Dahlin5c69deb2015-10-01 14:44:12 -0700650 DISALLOW_COPY_AND_ASSIGN(AidlMethod);
Casey Dahlin0a2f8be2015-09-28 16:15:29 -0700651};
Adam Lesinskiffa16862014-01-23 18:17:42 -0800652
Steven Morelandc258abc2018-07-10 14:03:38 -0700653class AidlDefinedType;
Jiyong Parkb034bf02018-07-30 17:44:33 +0900654class AidlInterface;
655class AidlParcelable;
656class AidlStructuredParcelable;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800657
Casey Dahlin2b2879b2015-10-13 16:59:44 -0700658class AidlQualifiedName : public AidlNode {
659 public:
Steven Moreland46e9da82018-07-27 15:45:29 -0700660 AidlQualifiedName(const AidlLocation& location, const std::string& term,
661 const std::string& comments);
Casey Dahlin2b2879b2015-10-13 16:59:44 -0700662 virtual ~AidlQualifiedName() = default;
Jiyong Park62515512020-06-08 15:57:11 +0900663 // Movable, but neither copiable nor assignable
664 AidlQualifiedName(AidlQualifiedName&&) = default;
665 AidlQualifiedName(const AidlQualifiedName&) = delete;
666 AidlQualifiedName& operator=(const AidlQualifiedName&) = delete;
Casey Dahlin2b2879b2015-10-13 16:59:44 -0700667
668 const std::vector<std::string>& GetTerms() const { return terms_; }
669 const std::string& GetComments() const { return comments_; }
670 std::string GetDotName() const { return android::base::Join(terms_, '.'); }
Ningyuan Wangd17c58b2016-09-29 14:33:14 -0700671 std::string GetColonName() const { return android::base::Join(terms_, "::"); }
Casey Dahlin2b2879b2015-10-13 16:59:44 -0700672
Chih-Hung Hsiehf05cc262016-07-27 11:42:51 -0700673 void AddTerm(const std::string& term);
Casey Dahlin2b2879b2015-10-13 16:59:44 -0700674
675 private:
676 std::vector<std::string> terms_;
677 std::string comments_;
Casey Dahlin2b2879b2015-10-13 16:59:44 -0700678};
679
Steven Moreland46e9da82018-07-27 15:45:29 -0700680class AidlInterface;
681class AidlParcelable;
682class AidlStructuredParcelable;
Daniel Norman85aed542019-08-21 12:01:14 -0700683// AidlDefinedType represents either an interface, parcelable, or enum that is
Jiyong Park1deecc32018-07-17 01:14:41 +0900684// defined in the source file.
685class AidlDefinedType : public AidlAnnotatable {
Steven Moreland787b0432018-07-03 09:00:58 -0700686 public:
Steven Moreland46e9da82018-07-27 15:45:29 -0700687 AidlDefinedType(const AidlLocation& location, const std::string& name,
688 const std::string& comments, const std::vector<std::string>& package);
Steven Moreland787b0432018-07-03 09:00:58 -0700689 virtual ~AidlDefinedType() = default;
690
Jiyong Park1deecc32018-07-17 01:14:41 +0900691 const std::string& GetName() const { return name_; };
Jeongik Cha997281d2020-01-16 15:23:59 +0900692 bool IsHidden() const;
Jiyong Park1deecc32018-07-17 01:14:41 +0900693 const std::string& GetComments() const { return comments_; }
Jiyong Parka6605ab2018-11-11 14:30:21 +0900694 void SetComments(const std::string comments) { comments_ = comments; }
Jiyong Park1deecc32018-07-17 01:14:41 +0900695
Steven Moreland787b0432018-07-03 09:00:58 -0700696 /* dot joined package, example: "android.package.foo" */
697 std::string GetPackage() const;
698 /* dot joined package and name, example: "android.package.foo.IBar" */
699 std::string GetCanonicalName() const;
700 const std::vector<std::string>& GetSplitPackage() const { return package_; }
701
Steven Morelanded83a282018-07-17 13:27:29 -0700702 virtual std::string GetPreprocessDeclarationName() const = 0;
Steven Morelandc258abc2018-07-10 14:03:38 -0700703
Steven Moreland5557f1c2018-07-02 13:50:23 -0700704 virtual const AidlStructuredParcelable* AsStructuredParcelable() const { return nullptr; }
Steven Morelandc258abc2018-07-10 14:03:38 -0700705 virtual const AidlParcelable* AsParcelable() const { return nullptr; }
Daniel Norman85aed542019-08-21 12:01:14 -0700706 virtual const AidlEnumDeclaration* AsEnumDeclaration() const { return nullptr; }
Steven Moreland5557f1c2018-07-02 13:50:23 -0700707 virtual const AidlInterface* AsInterface() const { return nullptr; }
Jeongik Chadf76dc72019-11-28 00:08:47 +0900708 virtual const AidlParameterizable<std::string>* AsParameterizable() const { return nullptr; }
Devin Moore24f68572020-02-26 13:20:59 -0800709 bool CheckValid(const AidlTypenames& typenames) const override;
Steven Morelandd59e3172020-05-11 16:42:09 -0700710 virtual bool LanguageSpecificCheckValid(const AidlTypenames& typenames,
711 Options::Language lang) const = 0;
Steven Morelandc258abc2018-07-10 14:03:38 -0700712 AidlStructuredParcelable* AsStructuredParcelable() {
713 return const_cast<AidlStructuredParcelable*>(
714 const_cast<const AidlDefinedType*>(this)->AsStructuredParcelable());
715 }
716 AidlParcelable* AsParcelable() {
717 return const_cast<AidlParcelable*>(const_cast<const AidlDefinedType*>(this)->AsParcelable());
718 }
Daniel Norman85aed542019-08-21 12:01:14 -0700719 AidlEnumDeclaration* AsEnumDeclaration() {
720 return const_cast<AidlEnumDeclaration*>(
721 const_cast<const AidlDefinedType*>(this)->AsEnumDeclaration());
722 }
Steven Morelandc258abc2018-07-10 14:03:38 -0700723 AidlInterface* AsInterface() {
724 return const_cast<AidlInterface*>(const_cast<const AidlDefinedType*>(this)->AsInterface());
725 }
726
Jeongik Chadf76dc72019-11-28 00:08:47 +0900727 AidlParameterizable<std::string>* AsParameterizable() {
728 return const_cast<AidlParameterizable<std::string>*>(
729 const_cast<const AidlDefinedType*>(this)->AsParameterizable());
730 }
731
Steven Moreland6cee3482018-07-18 14:39:58 -0700732 const AidlParcelable* AsUnstructuredParcelable() const {
733 if (this->AsStructuredParcelable() != nullptr) return nullptr;
734 return this->AsParcelable();
735 }
736 AidlParcelable* AsUnstructuredParcelable() {
737 return const_cast<AidlParcelable*>(
738 const_cast<const AidlDefinedType*>(this)->AsUnstructuredParcelable());
739 }
740
Jeongik Cha997281d2020-01-16 15:23:59 +0900741 virtual void Dump(CodeWriter* writer) const = 0;
Steven Morelanda5d9c5c2020-02-21 16:01:09 -0800742 void DumpHeader(CodeWriter* writer) const;
Jiyong Park02da7422018-07-16 16:00:26 +0900743
Steven Moreland787b0432018-07-03 09:00:58 -0700744 private:
Jiyong Park1deecc32018-07-17 01:14:41 +0900745 std::string name_;
Jiyong Park1deecc32018-07-17 01:14:41 +0900746 std::string comments_;
Steven Moreland787b0432018-07-03 09:00:58 -0700747 const std::vector<std::string> package_;
Steven Moreland5557f1c2018-07-02 13:50:23 -0700748
749 DISALLOW_COPY_AND_ASSIGN(AidlDefinedType);
Steven Moreland787b0432018-07-03 09:00:58 -0700750};
751
Jeongik Chadf76dc72019-11-28 00:08:47 +0900752class AidlParcelable : public AidlDefinedType, public AidlParameterizable<std::string> {
Casey Dahlin1ae2bc52015-10-07 18:49:10 -0700753 public:
Steven Moreland46e9da82018-07-27 15:45:29 -0700754 AidlParcelable(const AidlLocation& location, AidlQualifiedName* name,
Jiyong Parka6605ab2018-11-11 14:30:21 +0900755 const std::vector<std::string>& package, const std::string& comments,
Jeongik Chadf76dc72019-11-28 00:08:47 +0900756 const std::string& cpp_header = "",
757 std::vector<std::string>* type_params = nullptr);
Casey Dahlin1ae2bc52015-10-07 18:49:10 -0700758 virtual ~AidlParcelable() = default;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800759
Ningyuan Wangd17c58b2016-09-29 14:33:14 -0700760 // C++ uses "::" instead of "." to refer to a inner class.
761 std::string GetCppName() const { return name_->GetColonName(); }
Christopher Wiley8aa4d9f2015-11-16 19:10:45 -0800762 std::string GetCppHeader() const { return cpp_header_; }
Christopher Wiley8aa4d9f2015-11-16 19:10:45 -0800763
Steven Moreland0cea4aa2020-04-20 21:06:02 -0700764 std::set<AidlAnnotation::Type> GetSupportedAnnotations() const override;
Jeongik Cha82317dd2019-02-27 20:26:11 +0900765 bool CheckValid(const AidlTypenames& typenames) const override;
Steven Morelandd59e3172020-05-11 16:42:09 -0700766 bool LanguageSpecificCheckValid(const AidlTypenames& typenames,
767 Options::Language lang) const override;
Steven Morelandc258abc2018-07-10 14:03:38 -0700768 const AidlParcelable* AsParcelable() const override { return this; }
Jeongik Chadf76dc72019-11-28 00:08:47 +0900769 const AidlParameterizable<std::string>* AsParameterizable() const override { return this; }
770 const AidlNode& AsAidlNode() const override { return *this; }
Steven Morelanded83a282018-07-17 13:27:29 -0700771 std::string GetPreprocessDeclarationName() const override { return "parcelable"; }
Steven Morelandc258abc2018-07-10 14:03:38 -0700772
Jeongik Cha997281d2020-01-16 15:23:59 +0900773 void Dump(CodeWriter* writer) const override;
Jiyong Park02da7422018-07-16 16:00:26 +0900774
Casey Dahlin1ae2bc52015-10-07 18:49:10 -0700775 private:
Christopher Wiley8aa4d9f2015-11-16 19:10:45 -0800776 std::unique_ptr<AidlQualifiedName> name_;
Christopher Wiley8aa4d9f2015-11-16 19:10:45 -0800777 std::string cpp_header_;
Casey Dahlin59401da2015-10-09 18:16:45 -0700778
Casey Dahlin1ae2bc52015-10-07 18:49:10 -0700779 DISALLOW_COPY_AND_ASSIGN(AidlParcelable);
Casey Dahlin0a2f8be2015-09-28 16:15:29 -0700780};
Adam Lesinskiffa16862014-01-23 18:17:42 -0800781
Steven Moreland5557f1c2018-07-02 13:50:23 -0700782class AidlStructuredParcelable : public AidlParcelable {
783 public:
Steven Moreland46e9da82018-07-27 15:45:29 -0700784 AidlStructuredParcelable(const AidlLocation& location, AidlQualifiedName* name,
Jiyong Parka6605ab2018-11-11 14:30:21 +0900785 const std::vector<std::string>& package, const std::string& comments,
Steven Moreland5557f1c2018-07-02 13:50:23 -0700786 std::vector<std::unique_ptr<AidlVariableDeclaration>>* variables);
787
788 const std::vector<std::unique_ptr<AidlVariableDeclaration>>& GetFields() const {
789 return variables_;
790 }
791
Steven Morelandc258abc2018-07-10 14:03:38 -0700792 const AidlStructuredParcelable* AsStructuredParcelable() const override { return this; }
Steven Morelanded83a282018-07-17 13:27:29 -0700793 std::string GetPreprocessDeclarationName() const override { return "structured_parcelable"; }
Steven Moreland5557f1c2018-07-02 13:50:23 -0700794
Jeongik Cha997281d2020-01-16 15:23:59 +0900795 void Dump(CodeWriter* writer) const override;
Jiyong Park02da7422018-07-16 16:00:26 +0900796
Steven Moreland0cea4aa2020-04-20 21:06:02 -0700797 std::set<AidlAnnotation::Type> GetSupportedAnnotations() const override;
Jeongik Chadb0f59e2018-11-01 18:11:21 +0900798 bool CheckValid(const AidlTypenames& typenames) const override;
Steven Morelandd59e3172020-05-11 16:42:09 -0700799 bool LanguageSpecificCheckValid(const AidlTypenames& typenames,
800 Options::Language lang) const override;
Jeongik Chadb0f59e2018-11-01 18:11:21 +0900801
Steven Moreland5557f1c2018-07-02 13:50:23 -0700802 private:
803 const std::vector<std::unique_ptr<AidlVariableDeclaration>> variables_;
804
805 DISALLOW_COPY_AND_ASSIGN(AidlStructuredParcelable);
806};
807
Daniel Norman85aed542019-08-21 12:01:14 -0700808class AidlEnumerator : public AidlNode {
809 public:
Daniel Norman2e4112d2019-10-03 10:22:35 -0700810 AidlEnumerator(const AidlLocation& location, const std::string& name, AidlConstantValue* value,
811 const std::string& comments);
Daniel Norman85aed542019-08-21 12:01:14 -0700812 virtual ~AidlEnumerator() = default;
813
814 const std::string& GetName() const { return name_; }
Will McVickerd7d18df2019-09-12 13:40:50 -0700815 AidlConstantValue* GetValue() const { return value_.get(); }
Daniel Norman2e4112d2019-10-03 10:22:35 -0700816 const std::string& GetComments() const { return comments_; }
Daniel Norman85aed542019-08-21 12:01:14 -0700817 bool CheckValid(const AidlTypeSpecifier& enum_backing_type) const;
818
819 string ValueString(const AidlTypeSpecifier& backing_type,
820 const ConstantValueDecorator& decorator) const;
821
Daniel Normanb28684e2019-10-17 15:31:39 -0700822 void SetValue(std::unique_ptr<AidlConstantValue> value) { value_ = std::move(value); }
823
Daniel Norman85aed542019-08-21 12:01:14 -0700824 private:
825 const std::string name_;
Will McVickerd7d18df2019-09-12 13:40:50 -0700826 unique_ptr<AidlConstantValue> value_;
Daniel Norman2e4112d2019-10-03 10:22:35 -0700827 const std::string comments_;
Daniel Norman85aed542019-08-21 12:01:14 -0700828
829 DISALLOW_COPY_AND_ASSIGN(AidlEnumerator);
830};
831
832class AidlEnumDeclaration : public AidlDefinedType {
833 public:
Will McVickerd7d18df2019-09-12 13:40:50 -0700834 AidlEnumDeclaration(const AidlLocation& location, const string& name,
Daniel Norman85aed542019-08-21 12:01:14 -0700835 std::vector<std::unique_ptr<AidlEnumerator>>* enumerators,
Daniel Norman2e4112d2019-10-03 10:22:35 -0700836 const std::vector<std::string>& package, const std::string& comments);
Daniel Norman85aed542019-08-21 12:01:14 -0700837 virtual ~AidlEnumDeclaration() = default;
838
839 void SetBackingType(std::unique_ptr<const AidlTypeSpecifier> type);
840 const AidlTypeSpecifier& GetBackingType() const { return *backing_type_; }
841 const std::vector<std::unique_ptr<AidlEnumerator>>& GetEnumerators() const {
842 return enumerators_;
843 }
Steven Moreland59e53e42019-11-26 20:38:08 -0800844 bool Autofill();
Steven Moreland0cea4aa2020-04-20 21:06:02 -0700845 std::set<AidlAnnotation::Type> GetSupportedAnnotations() const override;
Daniel Norman85aed542019-08-21 12:01:14 -0700846 bool CheckValid(const AidlTypenames& typenames) const override;
Steven Morelandd59e3172020-05-11 16:42:09 -0700847 bool LanguageSpecificCheckValid(const AidlTypenames& /*typenames*/,
848 Options::Language) const override {
849 return true;
850 }
Daniel Norman85aed542019-08-21 12:01:14 -0700851 std::string GetPreprocessDeclarationName() const override { return "enum"; }
Jeongik Cha997281d2020-01-16 15:23:59 +0900852 void Dump(CodeWriter* writer) const override;
Daniel Norman85aed542019-08-21 12:01:14 -0700853
854 const AidlEnumDeclaration* AsEnumDeclaration() const override { return this; }
855
856 private:
857 const std::string name_;
858 const std::vector<std::unique_ptr<AidlEnumerator>> enumerators_;
859 std::unique_ptr<const AidlTypeSpecifier> backing_type_;
860
861 DISALLOW_COPY_AND_ASSIGN(AidlEnumDeclaration);
862};
863
Jiyong Park1deecc32018-07-17 01:14:41 +0900864class AidlInterface final : public AidlDefinedType {
Casey Dahlin1ae2bc52015-10-07 18:49:10 -0700865 public:
Steven Moreland46e9da82018-07-27 15:45:29 -0700866 AidlInterface(const AidlLocation& location, const std::string& name, const std::string& comments,
867 bool oneway_, std::vector<std::unique_ptr<AidlMember>>* members,
Christopher Wileyd76067c2015-10-19 17:00:13 -0700868 const std::vector<std::string>& package);
Casey Dahlin1ae2bc52015-10-07 18:49:10 -0700869 virtual ~AidlInterface() = default;
870
Casey Dahlinfb7da2e2015-10-08 17:26:09 -0700871 const std::vector<std::unique_ptr<AidlMethod>>& GetMethods() const
872 { return methods_; }
Jiyong Park309668e2018-07-28 16:55:44 +0900873 std::vector<std::unique_ptr<AidlMethod>>& GetMutableMethods() { return methods_; }
Steven Moreland693640b2018-07-19 13:46:27 -0700874 const std::vector<std::unique_ptr<AidlConstantDeclaration>>& GetConstantDeclarations() const {
875 return constants_;
876 }
Casey Dahlina2f77c42015-12-01 18:26:02 -0800877
Steven Morelandc258abc2018-07-10 14:03:38 -0700878 const AidlInterface* AsInterface() const override { return this; }
Steven Morelanded83a282018-07-17 13:27:29 -0700879 std::string GetPreprocessDeclarationName() const override { return "interface"; }
Steven Moreland5557f1c2018-07-02 13:50:23 -0700880
Jeongik Cha997281d2020-01-16 15:23:59 +0900881 void Dump(CodeWriter* writer) const override;
Jiyong Park02da7422018-07-16 16:00:26 +0900882
Steven Moreland0cea4aa2020-04-20 21:06:02 -0700883 std::set<AidlAnnotation::Type> GetSupportedAnnotations() const override;
Jeongik Chadb0f59e2018-11-01 18:11:21 +0900884 bool CheckValid(const AidlTypenames& typenames) const override;
Steven Morelandd59e3172020-05-11 16:42:09 -0700885 bool LanguageSpecificCheckValid(const AidlTypenames& typenames,
886 Options::Language lang) const override;
Jeongik Chadb0f59e2018-11-01 18:11:21 +0900887
Casey Dahlin1ae2bc52015-10-07 18:49:10 -0700888 private:
Casey Dahlinfb7da2e2015-10-08 17:26:09 -0700889 std::vector<std::unique_ptr<AidlMethod>> methods_;
Steven Moreland693640b2018-07-19 13:46:27 -0700890 std::vector<std::unique_ptr<AidlConstantDeclaration>> constants_;
Casey Dahlinfb7da2e2015-10-08 17:26:09 -0700891
Casey Dahlin1ae2bc52015-10-07 18:49:10 -0700892 DISALLOW_COPY_AND_ASSIGN(AidlInterface);
Casey Dahlin0a2f8be2015-09-28 16:15:29 -0700893};
Adam Lesinskiffa16862014-01-23 18:17:42 -0800894
Casey Dahlin0edf3422015-10-07 12:34:59 -0700895class AidlImport : public AidlNode {
896 public:
Steven Moreland46e9da82018-07-27 15:45:29 -0700897 AidlImport(const AidlLocation& location, const std::string& needed_class);
Casey Dahlin0edf3422015-10-07 12:34:59 -0700898 virtual ~AidlImport() = default;
899
Casey Dahlin0edf3422015-10-07 12:34:59 -0700900 const std::string& GetFilename() const { return filename_; }
901 const std::string& GetNeededClass() const { return needed_class_; }
Casey Dahlin0edf3422015-10-07 12:34:59 -0700902
903 private:
Casey Dahlin0edf3422015-10-07 12:34:59 -0700904 std::string filename_;
905 std::string needed_class_;
Casey Dahlin0edf3422015-10-07 12:34:59 -0700906
907 DISALLOW_COPY_AND_ASSIGN(AidlImport);
Casey Dahline2507492015-09-14 17:11:20 -0700908};
909
Jiyong Park62515512020-06-08 15:57:11 +0900910// AidlDocument models an AIDL file
911class AidlDocument : public AidlNode {
912 public:
913 AidlDocument(const AidlLocation& location, std::vector<std::unique_ptr<AidlImport>>& imports,
914 std::vector<AidlDefinedType*>& defined_types)
915 : AidlNode(location), imports_(std::move(imports)), defined_types_(defined_types) {}
916 const std::vector<std::unique_ptr<AidlImport>>& Imports() const { return imports_; }
917 const std::vector<AidlDefinedType*>& DefinedTypes() const { return defined_types_; }
918
919 private:
920 const std::vector<std::unique_ptr<AidlImport>> imports_;
921 const std::vector<AidlDefinedType*> defined_types_;
922};