blob: 4edcc43fff4dfa9fd413c192c52fd229606dfbe9 [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 +0900305// AidlTypeSpecifier represents a reference to either a built-in type,
306// a defined type, or a variant (e.g., array of generic) of a type.
Jeongik Chadf76dc72019-11-28 00:08:47 +0900307class AidlTypeSpecifier final : public AidlAnnotatable,
308 public AidlParameterizable<unique_ptr<AidlTypeSpecifier>> {
Casey Dahline7922932016-02-29 17:23:01 -0800309 public:
Steven Moreland46e9da82018-07-27 15:45:29 -0700310 AidlTypeSpecifier(const AidlLocation& location, const string& unresolved_name, bool is_array,
311 vector<unique_ptr<AidlTypeSpecifier>>* type_params, const string& comments);
Jiyong Parkd59a10d2018-07-18 11:12:55 +0900312 virtual ~AidlTypeSpecifier() = default;
Casey Dahlin0ee37582015-09-30 16:31:55 -0700313
Steven Moreland3f658cf2018-08-20 13:40:54 -0700314 // Copy of this type which is not an array.
315 AidlTypeSpecifier ArrayBase() const;
316
Jiyong Park1deecc32018-07-17 01:14:41 +0900317 // Returns the full-qualified name of the base type.
318 // int -> int
319 // int[] -> int
320 // List<String> -> List
321 // IFoo -> foo.bar.IFoo (if IFoo is in package foo.bar)
322 const string& GetName() const {
323 if (IsResolved()) {
324 return fully_qualified_name_;
325 } else {
326 return GetUnresolvedName();
327 }
328 }
Casey Dahlin0ee37582015-09-30 16:31:55 -0700329
Jiyong Park1deecc32018-07-17 01:14:41 +0900330 // Returns string representation of this type specifier.
Artur Satayev91fe8712019-07-29 13:06:01 +0100331 // This is GetBaseTypeName() + array modifier or generic type parameters
Jiyong Park1deecc32018-07-17 01:14:41 +0900332 string ToString() const;
333
Jiyong Park02da7422018-07-16 16:00:26 +0900334 std::string Signature() const;
335
Jiyong Park1deecc32018-07-17 01:14:41 +0900336 const string& GetUnresolvedName() const { return unresolved_name_; }
337
Jeongik Cha997281d2020-01-16 15:23:59 +0900338 bool IsHidden() const;
339
Jiyong Park1deecc32018-07-17 01:14:41 +0900340 const string& GetComments() const { return comments_; }
341
Jeongik Cha1a7ab642019-07-29 17:31:02 +0900342 const std::vector<std::string> GetSplitName() const { return split_name_; }
343
Jiyong Parka6605ab2018-11-11 14:30:21 +0900344 void SetComments(const string& comment) { comments_ = comment; }
345
Jiyong Park1deecc32018-07-17 01:14:41 +0900346 bool IsResolved() const { return fully_qualified_name_ != ""; }
347
348 bool IsArray() const { return is_array_; }
349
Jiyong Park1deecc32018-07-17 01:14:41 +0900350 // Resolve the base type name to a fully-qualified name. Return false if the
351 // resolution fails.
Daniel Norman716d3112019-09-10 13:11:56 -0700352 bool Resolve(const AidlTypenames& typenames);
Casey Dahlin0ee37582015-09-30 16:31:55 -0700353
Steven Moreland0cea4aa2020-04-20 21:06:02 -0700354 std::set<AidlAnnotation::Type> GetSupportedAnnotations() const override;
Devin Moore24f68572020-02-26 13:20:59 -0800355 bool CheckValid(const AidlTypenames& typenames) const override;
Steven Morelandd59e3172020-05-11 16:42:09 -0700356 bool LanguageSpecificCheckValid(const AidlTypenames& typenames, Options::Language lang) const;
Jeongik Chadf76dc72019-11-28 00:08:47 +0900357 const AidlNode& AsAidlNode() const override { return *this; }
Jiyong Park1d2df7d2018-07-23 15:22:50 +0900358
Casey Dahlin0ee37582015-09-30 16:31:55 -0700359 private:
Steven Moreland3f658cf2018-08-20 13:40:54 -0700360 AidlTypeSpecifier(const AidlTypeSpecifier&) = default;
361
Jiyong Park1deecc32018-07-17 01:14:41 +0900362 const string unresolved_name_;
363 string fully_qualified_name_;
Steven Moreland3f658cf2018-08-20 13:40:54 -0700364 bool is_array_;
Jiyong Parka6605ab2018-11-11 14:30:21 +0900365 string comments_;
Jeongik Cha1a7ab642019-07-29 17:31:02 +0900366 vector<string> split_name_;
Casey Dahlin0ee37582015-09-30 16:31:55 -0700367};
368
Steven Moreland860b1942018-08-16 14:59:28 -0700369// Returns the universal value unaltered.
370std::string AidlConstantValueDecorator(const AidlTypeSpecifier& type, const std::string& raw_value);
371
Steven Moreland9ea10e32018-07-19 15:26:09 -0700372class AidlConstantValue;
Steven Moreland5557f1c2018-07-02 13:50:23 -0700373class AidlVariableDeclaration : public AidlNode {
374 public:
Steven Moreland46e9da82018-07-27 15:45:29 -0700375 AidlVariableDeclaration(const AidlLocation& location, AidlTypeSpecifier* type,
376 const std::string& name);
377 AidlVariableDeclaration(const AidlLocation& location, AidlTypeSpecifier* type,
378 const std::string& name, AidlConstantValue* default_value);
Steven Moreland5557f1c2018-07-02 13:50:23 -0700379 virtual ~AidlVariableDeclaration() = default;
380
381 std::string GetName() const { return name_; }
Jiyong Parkd59a10d2018-07-18 11:12:55 +0900382 const AidlTypeSpecifier& GetType() const { return *type_; }
Steven Moreland9ea10e32018-07-19 15:26:09 -0700383 const AidlConstantValue* GetDefaultValue() const { return default_value_.get(); }
384
Jiyong Parkd59a10d2018-07-18 11:12:55 +0900385 AidlTypeSpecifier* GetMutableType() { return type_.get(); }
Steven Moreland5557f1c2018-07-02 13:50:23 -0700386
Jeongik Chadb0f59e2018-11-01 18:11:21 +0900387 bool CheckValid(const AidlTypenames& typenames) const;
Steven Moreland5557f1c2018-07-02 13:50:23 -0700388 std::string ToString() const;
Jiyong Park02da7422018-07-16 16:00:26 +0900389 std::string Signature() const;
Steven Moreland5557f1c2018-07-02 13:50:23 -0700390
Steven Moreland860b1942018-08-16 14:59:28 -0700391 std::string ValueString(const ConstantValueDecorator& decorator) const;
Steven Moreland25294322018-08-07 18:13:55 -0700392
Steven Moreland5557f1c2018-07-02 13:50:23 -0700393 private:
Jiyong Parkd59a10d2018-07-18 11:12:55 +0900394 std::unique_ptr<AidlTypeSpecifier> type_;
Steven Moreland5557f1c2018-07-02 13:50:23 -0700395 std::string name_;
Steven Moreland9ea10e32018-07-19 15:26:09 -0700396 std::unique_ptr<AidlConstantValue> default_value_;
Steven Moreland5557f1c2018-07-02 13:50:23 -0700397
398 DISALLOW_COPY_AND_ASSIGN(AidlVariableDeclaration);
399};
400
401class AidlArgument : public AidlVariableDeclaration {
Casey Dahlinbc7a50a2015-09-28 19:20:50 -0700402 public:
Casey Dahlinc378c992015-09-29 16:50:40 -0700403 enum Direction { IN_DIR = 1, OUT_DIR = 2, INOUT_DIR = 3 };
404
Steven Moreland46e9da82018-07-27 15:45:29 -0700405 AidlArgument(const AidlLocation& location, AidlArgument::Direction direction,
406 AidlTypeSpecifier* type, const std::string& name);
407 AidlArgument(const AidlLocation& location, AidlTypeSpecifier* type, const std::string& name);
Casey Dahlinbc7a50a2015-09-28 19:20:50 -0700408 virtual ~AidlArgument() = default;
409
Casey Dahlinc378c992015-09-29 16:50:40 -0700410 Direction GetDirection() const { return direction_; }
Christopher Wileyad339272015-10-05 19:11:58 -0700411 bool IsOut() const { return direction_ & OUT_DIR; }
412 bool IsIn() const { return direction_ & IN_DIR; }
Casey Dahlinc378c992015-09-29 16:50:40 -0700413 bool DirectionWasSpecified() const { return direction_specified_; }
Jiyong Park3656c3c2018-08-01 20:02:01 +0900414 string GetDirectionSpecifier() const;
Christopher Wileyad339272015-10-05 19:11:58 -0700415
Casey Dahlinc378c992015-09-29 16:50:40 -0700416 std::string ToString() const;
Jiyong Park02da7422018-07-16 16:00:26 +0900417 std::string Signature() const;
Casey Dahlinc378c992015-09-29 16:50:40 -0700418
Casey Dahlinbc7a50a2015-09-28 19:20:50 -0700419 private:
Casey Dahlinc378c992015-09-29 16:50:40 -0700420 Direction direction_;
421 bool direction_specified_;
422
Casey Dahlinbc7a50a2015-09-28 19:20:50 -0700423 DISALLOW_COPY_AND_ASSIGN(AidlArgument);
Casey Dahlina834dd42015-09-23 11:52:15 -0700424};
Adam Lesinskiffa16862014-01-23 18:17:42 -0800425
Casey Dahlind40e2fe2015-11-24 14:06:52 -0800426class AidlMethod;
Steven Moreland693640b2018-07-19 13:46:27 -0700427class AidlConstantDeclaration;
Daniel Norman85aed542019-08-21 12:01:14 -0700428class AidlEnumDeclaration;
Casey Dahlind40e2fe2015-11-24 14:06:52 -0800429class AidlMember : public AidlNode {
430 public:
Steven Moreland46e9da82018-07-27 15:45:29 -0700431 AidlMember(const AidlLocation& location);
Casey Dahlind40e2fe2015-11-24 14:06:52 -0800432 virtual ~AidlMember() = default;
433
434 virtual AidlMethod* AsMethod() { return nullptr; }
Steven Moreland693640b2018-07-19 13:46:27 -0700435 virtual AidlConstantDeclaration* AsConstantDeclaration() { return nullptr; }
Casey Dahlind40e2fe2015-11-24 14:06:52 -0800436
437 private:
438 DISALLOW_COPY_AND_ASSIGN(AidlMember);
439};
440
Will McVickerd7d18df2019-09-12 13:40:50 -0700441class AidlUnaryConstExpression;
442class AidlBinaryConstExpression;
443
Steven Moreland693640b2018-07-19 13:46:27 -0700444class AidlConstantValue : public AidlNode {
Casey Dahlind40e2fe2015-11-24 14:06:52 -0800445 public:
Will McVickerd7d18df2019-09-12 13:40:50 -0700446 enum class Type {
447 // WARNING: Don't change this order! The order is used to determine type
448 // promotion during a binary expression.
449 BOOLEAN,
450 INT8,
451 INT32,
452 INT64,
453 ARRAY,
454 CHARACTER,
455 STRING,
456 FLOATING,
457 UNARY,
458 BINARY,
459 ERROR,
460 };
461
462 /*
463 * Return the value casted to the given type.
464 */
465 template <typename T>
466 T cast() const;
Casey Dahlind40e2fe2015-11-24 14:06:52 -0800467
Steven Moreland693640b2018-07-19 13:46:27 -0700468 virtual ~AidlConstantValue() = default;
Casey Dahlind40e2fe2015-11-24 14:06:52 -0800469
Steven Moreland25294322018-08-07 18:13:55 -0700470 static AidlConstantValue* Boolean(const AidlLocation& location, bool value);
471 static AidlConstantValue* Character(const AidlLocation& location, char value);
Steven Moreland25294322018-08-07 18:13:55 -0700472 // example: 123, -5498, maybe any size
Will McVickerd7d18df2019-09-12 13:40:50 -0700473 static AidlConstantValue* Integral(const AidlLocation& location, const string& value);
474 static AidlConstantValue* Floating(const AidlLocation& location, const std::string& value);
Steven Moreland860b1942018-08-16 14:59:28 -0700475 static AidlConstantValue* Array(const AidlLocation& location,
Will McVickerd7d18df2019-09-12 13:40:50 -0700476 std::unique_ptr<vector<unique_ptr<AidlConstantValue>>> values);
Steven Moreland693640b2018-07-19 13:46:27 -0700477 // example: "\"asdf\""
Will McVickerd7d18df2019-09-12 13:40:50 -0700478 static AidlConstantValue* String(const AidlLocation& location, const string& value);
Steven Moreland693640b2018-07-19 13:46:27 -0700479
Daniel Normanf0ca44f2019-10-25 09:59:44 -0700480 // Construct an AidlConstantValue by evaluating the other integral constant's
481 // value string. This does not preserve the structure of the copied constant.
Steven Moreland59e53e42019-11-26 20:38:08 -0800482 // Returns nullptr and logs if value cannot be copied.
Daniel Normanf0ca44f2019-10-25 09:59:44 -0700483 static AidlConstantValue* ShallowIntegralCopy(const AidlConstantValue& other);
Daniel Normanb28684e2019-10-17 15:31:39 -0700484
Will McVickerd7d18df2019-09-12 13:40:50 -0700485 Type GetType() const { return final_type_; }
Steven Moreland25294322018-08-07 18:13:55 -0700486
Will McVickerd7d18df2019-09-12 13:40:50 -0700487 virtual bool CheckValid() const;
Steven Moreland860b1942018-08-16 14:59:28 -0700488
489 // Raw value of type (currently valid in C++ and Java). Empty string on error.
Steven Moreland4bcb05c2019-11-27 18:57:47 -0800490 string ValueString(const AidlTypeSpecifier& type, const ConstantValueDecorator& decorator) const;
Casey Dahlind40e2fe2015-11-24 14:06:52 -0800491
492 private:
Will McVickerd7d18df2019-09-12 13:40:50 -0700493 AidlConstantValue(const AidlLocation& location, Type parsed_type, int64_t parsed_value,
494 const string& checked_value);
495 AidlConstantValue(const AidlLocation& location, Type type, const string& checked_value);
Steven Moreland860b1942018-08-16 14:59:28 -0700496 AidlConstantValue(const AidlLocation& location, Type type,
Will McVickerd7d18df2019-09-12 13:40:50 -0700497 std::unique_ptr<vector<unique_ptr<AidlConstantValue>>> values);
Steven Moreland25294322018-08-07 18:13:55 -0700498 static string ToString(Type type);
Will McVickerd7d18df2019-09-12 13:40:50 -0700499 static bool ParseIntegral(const string& value, int64_t* parsed_value, Type* parsed_type);
500 static bool IsHex(const string& value);
Steven Moreland4bcb05c2019-11-27 18:57:47 -0800501
Will McVickerd7d18df2019-09-12 13:40:50 -0700502 virtual bool evaluate(const AidlTypeSpecifier& type) const;
Casey Dahlind40e2fe2015-11-24 14:06:52 -0800503
Steven Moreland693640b2018-07-19 13:46:27 -0700504 const Type type_ = Type::ERROR;
Will McVickerd7d18df2019-09-12 13:40:50 -0700505 const vector<unique_ptr<AidlConstantValue>> values_; // if type_ == ARRAY
506 const string value_; // otherwise
507
508 // State for tracking evaluation of expressions
Steven Moreland4bcb05c2019-11-27 18:57:47 -0800509 mutable bool is_valid_ = false; // cache of CheckValid, but may be marked false in evaluate
510 mutable bool is_evaluated_ = false; // whether evaluate has been called
Will McVickerd7d18df2019-09-12 13:40:50 -0700511 mutable Type final_type_;
512 mutable int64_t final_value_;
513 mutable string final_string_value_ = "";
Steven Moreland693640b2018-07-19 13:46:27 -0700514
515 DISALLOW_COPY_AND_ASSIGN(AidlConstantValue);
Will McVickerd7d18df2019-09-12 13:40:50 -0700516
517 friend AidlUnaryConstExpression;
518 friend AidlBinaryConstExpression;
519};
520
521class AidlUnaryConstExpression : public AidlConstantValue {
522 public:
523 AidlUnaryConstExpression(const AidlLocation& location, const string& op,
524 std::unique_ptr<AidlConstantValue> rval);
525
526 static bool IsCompatibleType(Type type, const string& op);
527 bool CheckValid() const override;
Will McVickerd7d18df2019-09-12 13:40:50 -0700528 private:
529 bool evaluate(const AidlTypeSpecifier& type) const override;
530
531 std::unique_ptr<AidlConstantValue> unary_;
532 const string op_;
533};
534
535class AidlBinaryConstExpression : public AidlConstantValue {
536 public:
537 AidlBinaryConstExpression(const AidlLocation& location, std::unique_ptr<AidlConstantValue> lval,
538 const string& op, std::unique_ptr<AidlConstantValue> rval);
539
540 bool CheckValid() const override;
Will McVickerd7d18df2019-09-12 13:40:50 -0700541
542 static bool AreCompatibleTypes(Type t1, Type t2);
543 // Returns the promoted kind for both operands
544 static Type UsualArithmeticConversion(Type left, Type right);
545 // Returns the promoted integral type where INT32 is the smallest type
546 static Type IntegralPromotion(Type in);
547
548 private:
549 bool evaluate(const AidlTypeSpecifier& type) const override;
550
551 std::unique_ptr<AidlConstantValue> left_val_;
552 std::unique_ptr<AidlConstantValue> right_val_;
553 const string op_;
Christopher Wileyd6bdd8d2016-05-03 11:23:13 -0700554};
555
Andrei Onea9445fc62019-06-27 18:11:59 +0100556struct AidlAnnotationParameter {
557 std::string name;
558 std::unique_ptr<AidlConstantValue> value;
559};
560
Steven Moreland693640b2018-07-19 13:46:27 -0700561class AidlConstantDeclaration : public AidlMember {
Christopher Wileyd6bdd8d2016-05-03 11:23:13 -0700562 public:
Steven Moreland46e9da82018-07-27 15:45:29 -0700563 AidlConstantDeclaration(const AidlLocation& location, AidlTypeSpecifier* specifier,
Will McVickerd7d18df2019-09-12 13:40:50 -0700564 const string& name, AidlConstantValue* value);
Steven Moreland693640b2018-07-19 13:46:27 -0700565 virtual ~AidlConstantDeclaration() = default;
Christopher Wileyd6bdd8d2016-05-03 11:23:13 -0700566
Steven Moreland693640b2018-07-19 13:46:27 -0700567 const AidlTypeSpecifier& GetType() const { return *type_; }
Steven Moreland4d12f9a2018-10-31 14:30:55 -0700568 AidlTypeSpecifier* GetMutableType() { return type_.get(); }
Will McVickerd7d18df2019-09-12 13:40:50 -0700569 const string& GetName() const { return name_; }
Steven Moreland693640b2018-07-19 13:46:27 -0700570 const AidlConstantValue& GetValue() const { return *value_; }
Jeongik Chadb0f59e2018-11-01 18:11:21 +0900571 bool CheckValid(const AidlTypenames& typenames) const;
Christopher Wileyd6bdd8d2016-05-03 11:23:13 -0700572
Will McVickerd7d18df2019-09-12 13:40:50 -0700573 string ToString() const;
574 string Signature() const;
Steven Moreland860b1942018-08-16 14:59:28 -0700575 string ValueString(const ConstantValueDecorator& decorator) const {
Will McVickerd7d18df2019-09-12 13:40:50 -0700576 return value_->ValueString(GetType(), decorator);
Steven Moreland860b1942018-08-16 14:59:28 -0700577 }
Steven Moreland25294322018-08-07 18:13:55 -0700578
Steven Moreland693640b2018-07-19 13:46:27 -0700579 AidlConstantDeclaration* AsConstantDeclaration() override { return this; }
Christopher Wileyd6bdd8d2016-05-03 11:23:13 -0700580
581 private:
Steven Moreland693640b2018-07-19 13:46:27 -0700582 const unique_ptr<AidlTypeSpecifier> type_;
Will McVickerd7d18df2019-09-12 13:40:50 -0700583 const string name_;
584 unique_ptr<AidlConstantValue> value_;
Christopher Wileyd6bdd8d2016-05-03 11:23:13 -0700585
Steven Moreland693640b2018-07-19 13:46:27 -0700586 DISALLOW_COPY_AND_ASSIGN(AidlConstantDeclaration);
Casey Dahlind40e2fe2015-11-24 14:06:52 -0800587};
588
589class AidlMethod : public AidlMember {
Casey Dahlin5c69deb2015-10-01 14:44:12 -0700590 public:
Will McVickerd7d18df2019-09-12 13:40:50 -0700591 AidlMethod(const AidlLocation& location, bool oneway, AidlTypeSpecifier* type, const string& name,
592 vector<unique_ptr<AidlArgument>>* args, const string& comments);
593 AidlMethod(const AidlLocation& location, bool oneway, AidlTypeSpecifier* type, const string& name,
594 vector<unique_ptr<AidlArgument>>* args, const string& comments, int id,
595 bool is_user_defined = true);
Casey Dahlin5c69deb2015-10-01 14:44:12 -0700596 virtual ~AidlMethod() = default;
597
Casey Dahlind40e2fe2015-11-24 14:06:52 -0800598 AidlMethod* AsMethod() override { return this; }
Jeongik Cha997281d2020-01-16 15:23:59 +0900599 bool IsHidden() const;
Will McVickerd7d18df2019-09-12 13:40:50 -0700600 const string& GetComments() const { return comments_; }
Jiyong Parkd59a10d2018-07-18 11:12:55 +0900601 const AidlTypeSpecifier& GetType() const { return *type_; }
602 AidlTypeSpecifier* GetMutableType() { return type_.get(); }
Steven Morelandacd53472018-12-14 10:17:26 -0800603
Steven Moreland8c70ba92018-12-17 10:20:31 -0800604 // set if this method is part of an interface that is marked oneway
605 void ApplyInterfaceOneway(bool oneway) { oneway_ = oneway_ || oneway; }
Casey Dahlinf4a93112015-10-05 16:58:09 -0700606 bool IsOneway() const { return oneway_; }
Steven Morelandacd53472018-12-14 10:17:26 -0800607
Casey Dahlinf4a93112015-10-05 16:58:09 -0700608 const std::string& GetName() const { return name_; }
Casey Dahlinf4a93112015-10-05 16:58:09 -0700609 bool HasId() const { return has_id_; }
Jiyong Parked65bf42018-08-28 15:43:27 +0900610 int GetId() const { return id_; }
Casey Dahlinf4a93112015-10-05 16:58:09 -0700611 void SetId(unsigned id) { id_ = id; }
Casey Dahlinf2d23f72015-10-02 16:19:19 -0700612
Jiyong Park309668e2018-07-28 16:55:44 +0900613 bool IsUserDefined() const { return is_user_defined_; }
614
Casey Dahlinf4a93112015-10-05 16:58:09 -0700615 const std::vector<std::unique_ptr<AidlArgument>>& GetArguments() const {
Christopher Wileyad339272015-10-05 19:11:58 -0700616 return arguments_;
617 }
618 // An inout parameter will appear in both GetInArguments()
619 // and GetOutArguments(). AidlMethod retains ownership of the argument
620 // pointers returned in this way.
621 const std::vector<const AidlArgument*>& GetInArguments() const {
622 return in_arguments_;
623 }
624 const std::vector<const AidlArgument*>& GetOutArguments() const {
625 return out_arguments_;
Casey Dahlinf4a93112015-10-05 16:58:09 -0700626 }
Casey Dahlin5c69deb2015-10-01 14:44:12 -0700627
Jiyong Park309668e2018-07-28 16:55:44 +0900628 // name + type parameter types
629 // i.e, foo(int, String)
Jiyong Park02da7422018-07-16 16:00:26 +0900630 std::string Signature() const;
631
Jiyong Park309668e2018-07-28 16:55:44 +0900632 // return type + name + type parameter types + annotations
633 // i.e, boolean foo(int, @Nullable String)
634 std::string ToString() const;
635
Casey Dahlin5c69deb2015-10-01 14:44:12 -0700636 private:
Casey Dahlinf4a93112015-10-05 16:58:09 -0700637 bool oneway_;
Casey Dahlinf2d23f72015-10-02 16:19:19 -0700638 std::string comments_;
Jiyong Parkd59a10d2018-07-18 11:12:55 +0900639 std::unique_ptr<AidlTypeSpecifier> type_;
Casey Dahlinf4a93112015-10-05 16:58:09 -0700640 std::string name_;
Christopher Wileyad339272015-10-05 19:11:58 -0700641 const std::vector<std::unique_ptr<AidlArgument>> arguments_;
642 std::vector<const AidlArgument*> in_arguments_;
643 std::vector<const AidlArgument*> out_arguments_;
Casey Dahlinf4a93112015-10-05 16:58:09 -0700644 bool has_id_;
645 int id_;
Jiyong Park309668e2018-07-28 16:55:44 +0900646 bool is_user_defined_ = true;
Casey Dahlinf2d23f72015-10-02 16:19:19 -0700647
Casey Dahlin5c69deb2015-10-01 14:44:12 -0700648 DISALLOW_COPY_AND_ASSIGN(AidlMethod);
Casey Dahlin0a2f8be2015-09-28 16:15:29 -0700649};
Adam Lesinskiffa16862014-01-23 18:17:42 -0800650
Steven Morelandc258abc2018-07-10 14:03:38 -0700651class AidlDefinedType;
Jiyong Parkb034bf02018-07-30 17:44:33 +0900652class AidlInterface;
653class AidlParcelable;
654class AidlStructuredParcelable;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800655
Steven Moreland46e9da82018-07-27 15:45:29 -0700656class AidlInterface;
657class AidlParcelable;
658class AidlStructuredParcelable;
Daniel Norman85aed542019-08-21 12:01:14 -0700659// AidlDefinedType represents either an interface, parcelable, or enum that is
Jiyong Park1deecc32018-07-17 01:14:41 +0900660// defined in the source file.
661class AidlDefinedType : public AidlAnnotatable {
Steven Moreland787b0432018-07-03 09:00:58 -0700662 public:
Steven Moreland46e9da82018-07-27 15:45:29 -0700663 AidlDefinedType(const AidlLocation& location, const std::string& name,
Jiyong Park18132182020-06-08 20:24:40 +0900664 const std::string& comments, const std::string& package);
Steven Moreland787b0432018-07-03 09:00:58 -0700665 virtual ~AidlDefinedType() = default;
666
Jiyong Park1deecc32018-07-17 01:14:41 +0900667 const std::string& GetName() const { return name_; };
Jeongik Cha997281d2020-01-16 15:23:59 +0900668 bool IsHidden() const;
Jiyong Park1deecc32018-07-17 01:14:41 +0900669 const std::string& GetComments() const { return comments_; }
Jiyong Parka6605ab2018-11-11 14:30:21 +0900670 void SetComments(const std::string comments) { comments_ = comments; }
Jiyong Park1deecc32018-07-17 01:14:41 +0900671
Steven Moreland787b0432018-07-03 09:00:58 -0700672 /* dot joined package, example: "android.package.foo" */
Jiyong Park18132182020-06-08 20:24:40 +0900673 std::string GetPackage() const { return package_; }
Steven Moreland787b0432018-07-03 09:00:58 -0700674 /* dot joined package and name, example: "android.package.foo.IBar" */
675 std::string GetCanonicalName() const;
Jiyong Park18132182020-06-08 20:24:40 +0900676 const std::vector<std::string>& GetSplitPackage() const { return split_package_; }
Steven Moreland787b0432018-07-03 09:00:58 -0700677
Steven Morelanded83a282018-07-17 13:27:29 -0700678 virtual std::string GetPreprocessDeclarationName() const = 0;
Steven Morelandc258abc2018-07-10 14:03:38 -0700679
Steven Moreland5557f1c2018-07-02 13:50:23 -0700680 virtual const AidlStructuredParcelable* AsStructuredParcelable() const { return nullptr; }
Steven Morelandc258abc2018-07-10 14:03:38 -0700681 virtual const AidlParcelable* AsParcelable() const { return nullptr; }
Daniel Norman85aed542019-08-21 12:01:14 -0700682 virtual const AidlEnumDeclaration* AsEnumDeclaration() const { return nullptr; }
Steven Moreland5557f1c2018-07-02 13:50:23 -0700683 virtual const AidlInterface* AsInterface() const { return nullptr; }
Jeongik Chadf76dc72019-11-28 00:08:47 +0900684 virtual const AidlParameterizable<std::string>* AsParameterizable() const { return nullptr; }
Devin Moore24f68572020-02-26 13:20:59 -0800685 bool CheckValid(const AidlTypenames& typenames) const override;
Steven Morelandd59e3172020-05-11 16:42:09 -0700686 virtual bool LanguageSpecificCheckValid(const AidlTypenames& typenames,
687 Options::Language lang) const = 0;
Steven Morelandc258abc2018-07-10 14:03:38 -0700688 AidlStructuredParcelable* AsStructuredParcelable() {
689 return const_cast<AidlStructuredParcelable*>(
690 const_cast<const AidlDefinedType*>(this)->AsStructuredParcelable());
691 }
692 AidlParcelable* AsParcelable() {
693 return const_cast<AidlParcelable*>(const_cast<const AidlDefinedType*>(this)->AsParcelable());
694 }
Daniel Norman85aed542019-08-21 12:01:14 -0700695 AidlEnumDeclaration* AsEnumDeclaration() {
696 return const_cast<AidlEnumDeclaration*>(
697 const_cast<const AidlDefinedType*>(this)->AsEnumDeclaration());
698 }
Steven Morelandc258abc2018-07-10 14:03:38 -0700699 AidlInterface* AsInterface() {
700 return const_cast<AidlInterface*>(const_cast<const AidlDefinedType*>(this)->AsInterface());
701 }
702
Jeongik Chadf76dc72019-11-28 00:08:47 +0900703 AidlParameterizable<std::string>* AsParameterizable() {
704 return const_cast<AidlParameterizable<std::string>*>(
705 const_cast<const AidlDefinedType*>(this)->AsParameterizable());
706 }
707
Steven Moreland6cee3482018-07-18 14:39:58 -0700708 const AidlParcelable* AsUnstructuredParcelable() const {
709 if (this->AsStructuredParcelable() != nullptr) return nullptr;
710 return this->AsParcelable();
711 }
712 AidlParcelable* AsUnstructuredParcelable() {
713 return const_cast<AidlParcelable*>(
714 const_cast<const AidlDefinedType*>(this)->AsUnstructuredParcelable());
715 }
716
Jeongik Cha997281d2020-01-16 15:23:59 +0900717 virtual void Dump(CodeWriter* writer) const = 0;
Steven Morelanda5d9c5c2020-02-21 16:01:09 -0800718 void DumpHeader(CodeWriter* writer) const;
Jiyong Park02da7422018-07-16 16:00:26 +0900719
Steven Moreland787b0432018-07-03 09:00:58 -0700720 private:
Jiyong Park1deecc32018-07-17 01:14:41 +0900721 std::string name_;
Jiyong Park1deecc32018-07-17 01:14:41 +0900722 std::string comments_;
Jiyong Park18132182020-06-08 20:24:40 +0900723 const std::string package_;
724 const std::vector<std::string> split_package_;
Steven Moreland5557f1c2018-07-02 13:50:23 -0700725
726 DISALLOW_COPY_AND_ASSIGN(AidlDefinedType);
Steven Moreland787b0432018-07-03 09:00:58 -0700727};
728
Jeongik Chadf76dc72019-11-28 00:08:47 +0900729class AidlParcelable : public AidlDefinedType, public AidlParameterizable<std::string> {
Casey Dahlin1ae2bc52015-10-07 18:49:10 -0700730 public:
Jiyong Park18132182020-06-08 20:24:40 +0900731 AidlParcelable(const AidlLocation& location, const std::string& name, const std::string& package,
732 const std::string& comments, const std::string& cpp_header = "",
Jeongik Chadf76dc72019-11-28 00:08:47 +0900733 std::vector<std::string>* type_params = nullptr);
Casey Dahlin1ae2bc52015-10-07 18:49:10 -0700734 virtual ~AidlParcelable() = default;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800735
Christopher Wiley8aa4d9f2015-11-16 19:10:45 -0800736 std::string GetCppHeader() const { return cpp_header_; }
Christopher Wiley8aa4d9f2015-11-16 19:10:45 -0800737
Steven Moreland0cea4aa2020-04-20 21:06:02 -0700738 std::set<AidlAnnotation::Type> GetSupportedAnnotations() const override;
Jeongik Cha82317dd2019-02-27 20:26:11 +0900739 bool CheckValid(const AidlTypenames& typenames) const override;
Steven Morelandd59e3172020-05-11 16:42:09 -0700740 bool LanguageSpecificCheckValid(const AidlTypenames& typenames,
741 Options::Language lang) const override;
Steven Morelandc258abc2018-07-10 14:03:38 -0700742 const AidlParcelable* AsParcelable() const override { return this; }
Jeongik Chadf76dc72019-11-28 00:08:47 +0900743 const AidlParameterizable<std::string>* AsParameterizable() const override { return this; }
744 const AidlNode& AsAidlNode() const override { return *this; }
Steven Morelanded83a282018-07-17 13:27:29 -0700745 std::string GetPreprocessDeclarationName() const override { return "parcelable"; }
Steven Morelandc258abc2018-07-10 14:03:38 -0700746
Jeongik Cha997281d2020-01-16 15:23:59 +0900747 void Dump(CodeWriter* writer) const override;
Jiyong Park02da7422018-07-16 16:00:26 +0900748
Casey Dahlin1ae2bc52015-10-07 18:49:10 -0700749 private:
Christopher Wiley8aa4d9f2015-11-16 19:10:45 -0800750 std::string cpp_header_;
Casey Dahlin59401da2015-10-09 18:16:45 -0700751
Casey Dahlin1ae2bc52015-10-07 18:49:10 -0700752 DISALLOW_COPY_AND_ASSIGN(AidlParcelable);
Casey Dahlin0a2f8be2015-09-28 16:15:29 -0700753};
Adam Lesinskiffa16862014-01-23 18:17:42 -0800754
Steven Moreland5557f1c2018-07-02 13:50:23 -0700755class AidlStructuredParcelable : public AidlParcelable {
756 public:
Jiyong Park18132182020-06-08 20:24:40 +0900757 AidlStructuredParcelable(const AidlLocation& location, const std::string& name,
758 const std::string& package, const std::string& comments,
Steven Moreland5557f1c2018-07-02 13:50:23 -0700759 std::vector<std::unique_ptr<AidlVariableDeclaration>>* variables);
760
761 const std::vector<std::unique_ptr<AidlVariableDeclaration>>& GetFields() const {
762 return variables_;
763 }
764
Steven Morelandc258abc2018-07-10 14:03:38 -0700765 const AidlStructuredParcelable* AsStructuredParcelable() const override { return this; }
Steven Morelanded83a282018-07-17 13:27:29 -0700766 std::string GetPreprocessDeclarationName() const override { return "structured_parcelable"; }
Steven Moreland5557f1c2018-07-02 13:50:23 -0700767
Jeongik Cha997281d2020-01-16 15:23:59 +0900768 void Dump(CodeWriter* writer) const override;
Jiyong Park02da7422018-07-16 16:00:26 +0900769
Steven Moreland0cea4aa2020-04-20 21:06:02 -0700770 std::set<AidlAnnotation::Type> GetSupportedAnnotations() const override;
Jeongik Chadb0f59e2018-11-01 18:11:21 +0900771 bool CheckValid(const AidlTypenames& typenames) const override;
Steven Morelandd59e3172020-05-11 16:42:09 -0700772 bool LanguageSpecificCheckValid(const AidlTypenames& typenames,
773 Options::Language lang) const override;
Jeongik Chadb0f59e2018-11-01 18:11:21 +0900774
Steven Moreland5557f1c2018-07-02 13:50:23 -0700775 private:
776 const std::vector<std::unique_ptr<AidlVariableDeclaration>> variables_;
777
778 DISALLOW_COPY_AND_ASSIGN(AidlStructuredParcelable);
779};
780
Daniel Norman85aed542019-08-21 12:01:14 -0700781class AidlEnumerator : public AidlNode {
782 public:
Daniel Norman2e4112d2019-10-03 10:22:35 -0700783 AidlEnumerator(const AidlLocation& location, const std::string& name, AidlConstantValue* value,
784 const std::string& comments);
Daniel Norman85aed542019-08-21 12:01:14 -0700785 virtual ~AidlEnumerator() = default;
786
787 const std::string& GetName() const { return name_; }
Will McVickerd7d18df2019-09-12 13:40:50 -0700788 AidlConstantValue* GetValue() const { return value_.get(); }
Daniel Norman2e4112d2019-10-03 10:22:35 -0700789 const std::string& GetComments() const { return comments_; }
Daniel Norman85aed542019-08-21 12:01:14 -0700790 bool CheckValid(const AidlTypeSpecifier& enum_backing_type) const;
791
792 string ValueString(const AidlTypeSpecifier& backing_type,
793 const ConstantValueDecorator& decorator) const;
794
Daniel Normanb28684e2019-10-17 15:31:39 -0700795 void SetValue(std::unique_ptr<AidlConstantValue> value) { value_ = std::move(value); }
796
Daniel Norman85aed542019-08-21 12:01:14 -0700797 private:
798 const std::string name_;
Will McVickerd7d18df2019-09-12 13:40:50 -0700799 unique_ptr<AidlConstantValue> value_;
Daniel Norman2e4112d2019-10-03 10:22:35 -0700800 const std::string comments_;
Daniel Norman85aed542019-08-21 12:01:14 -0700801
802 DISALLOW_COPY_AND_ASSIGN(AidlEnumerator);
803};
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
812 void SetBackingType(std::unique_ptr<const AidlTypeSpecifier> type);
813 const AidlTypeSpecifier& GetBackingType() const { return *backing_type_; }
814 const std::vector<std::unique_ptr<AidlEnumerator>>& GetEnumerators() const {
815 return enumerators_;
816 }
Steven Moreland59e53e42019-11-26 20:38:08 -0800817 bool Autofill();
Steven Moreland0cea4aa2020-04-20 21:06:02 -0700818 std::set<AidlAnnotation::Type> GetSupportedAnnotations() const override;
Daniel Norman85aed542019-08-21 12:01:14 -0700819 bool CheckValid(const AidlTypenames& typenames) const override;
Steven Morelandd59e3172020-05-11 16:42:09 -0700820 bool LanguageSpecificCheckValid(const AidlTypenames& /*typenames*/,
821 Options::Language) const override {
822 return true;
823 }
Daniel Norman85aed542019-08-21 12:01:14 -0700824 std::string GetPreprocessDeclarationName() const override { return "enum"; }
Jeongik Cha997281d2020-01-16 15:23:59 +0900825 void Dump(CodeWriter* writer) const override;
Daniel Norman85aed542019-08-21 12:01:14 -0700826
827 const AidlEnumDeclaration* AsEnumDeclaration() const override { return this; }
828
829 private:
830 const std::string name_;
831 const std::vector<std::unique_ptr<AidlEnumerator>> enumerators_;
832 std::unique_ptr<const AidlTypeSpecifier> backing_type_;
833
834 DISALLOW_COPY_AND_ASSIGN(AidlEnumDeclaration);
835};
836
Jiyong Park1deecc32018-07-17 01:14:41 +0900837class AidlInterface final : public AidlDefinedType {
Casey Dahlin1ae2bc52015-10-07 18:49:10 -0700838 public:
Steven Moreland46e9da82018-07-27 15:45:29 -0700839 AidlInterface(const AidlLocation& location, const std::string& name, const std::string& comments,
840 bool oneway_, std::vector<std::unique_ptr<AidlMember>>* members,
Jiyong Park18132182020-06-08 20:24:40 +0900841 const std::string& package);
Casey Dahlin1ae2bc52015-10-07 18:49:10 -0700842 virtual ~AidlInterface() = default;
843
Casey Dahlinfb7da2e2015-10-08 17:26:09 -0700844 const std::vector<std::unique_ptr<AidlMethod>>& GetMethods() const
845 { return methods_; }
Jiyong Park309668e2018-07-28 16:55:44 +0900846 std::vector<std::unique_ptr<AidlMethod>>& GetMutableMethods() { return methods_; }
Steven Moreland693640b2018-07-19 13:46:27 -0700847 const std::vector<std::unique_ptr<AidlConstantDeclaration>>& GetConstantDeclarations() const {
848 return constants_;
849 }
Casey Dahlina2f77c42015-12-01 18:26:02 -0800850
Steven Morelandc258abc2018-07-10 14:03:38 -0700851 const AidlInterface* AsInterface() const override { return this; }
Steven Morelanded83a282018-07-17 13:27:29 -0700852 std::string GetPreprocessDeclarationName() const override { return "interface"; }
Steven Moreland5557f1c2018-07-02 13:50:23 -0700853
Jeongik Cha997281d2020-01-16 15:23:59 +0900854 void Dump(CodeWriter* writer) const override;
Jiyong Park02da7422018-07-16 16:00:26 +0900855
Steven Moreland0cea4aa2020-04-20 21:06:02 -0700856 std::set<AidlAnnotation::Type> GetSupportedAnnotations() const override;
Jeongik Chadb0f59e2018-11-01 18:11:21 +0900857 bool CheckValid(const AidlTypenames& typenames) const override;
Steven Morelandd59e3172020-05-11 16:42:09 -0700858 bool LanguageSpecificCheckValid(const AidlTypenames& typenames,
859 Options::Language lang) const override;
Jeongik Chadb0f59e2018-11-01 18:11:21 +0900860
Casey Dahlin1ae2bc52015-10-07 18:49:10 -0700861 private:
Casey Dahlinfb7da2e2015-10-08 17:26:09 -0700862 std::vector<std::unique_ptr<AidlMethod>> methods_;
Steven Moreland693640b2018-07-19 13:46:27 -0700863 std::vector<std::unique_ptr<AidlConstantDeclaration>> constants_;
Casey Dahlinfb7da2e2015-10-08 17:26:09 -0700864
Casey Dahlin1ae2bc52015-10-07 18:49:10 -0700865 DISALLOW_COPY_AND_ASSIGN(AidlInterface);
Casey Dahlin0a2f8be2015-09-28 16:15:29 -0700866};
Adam Lesinskiffa16862014-01-23 18:17:42 -0800867
Casey Dahlin0edf3422015-10-07 12:34:59 -0700868class AidlImport : public AidlNode {
869 public:
Steven Moreland46e9da82018-07-27 15:45:29 -0700870 AidlImport(const AidlLocation& location, const std::string& needed_class);
Casey Dahlin0edf3422015-10-07 12:34:59 -0700871 virtual ~AidlImport() = default;
872
Casey Dahlin0edf3422015-10-07 12:34:59 -0700873 const std::string& GetFilename() const { return filename_; }
874 const std::string& GetNeededClass() const { return needed_class_; }
Casey Dahlin0edf3422015-10-07 12:34:59 -0700875
876 private:
Casey Dahlin0edf3422015-10-07 12:34:59 -0700877 std::string filename_;
878 std::string needed_class_;
Casey Dahlin0edf3422015-10-07 12:34:59 -0700879
880 DISALLOW_COPY_AND_ASSIGN(AidlImport);
Casey Dahline2507492015-09-14 17:11:20 -0700881};
882
Jiyong Park62515512020-06-08 15:57:11 +0900883// AidlDocument models an AIDL file
884class AidlDocument : public AidlNode {
885 public:
886 AidlDocument(const AidlLocation& location, std::vector<std::unique_ptr<AidlImport>>& imports,
887 std::vector<AidlDefinedType*>& defined_types)
888 : AidlNode(location), imports_(std::move(imports)), defined_types_(defined_types) {}
889 const std::vector<std::unique_ptr<AidlImport>>& Imports() const { return imports_; }
890 const std::vector<AidlDefinedType*>& DefinedTypes() const { return defined_types_; }
891
892 private:
893 const std::vector<std::unique_ptr<AidlImport>> imports_;
894 const std::vector<AidlDefinedType*> defined_types_;
895};