blob: 44c807808b81ab3a63bd88113a11319123956275 [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
Casey Dahlin89d44842015-09-24 18:45:54 -070033struct yy_buffer_state;
34typedef yy_buffer_state* YY_BUFFER_STATE;
35
Jiyong Parkb034bf02018-07-30 17:44:33 +090036using android::aidl::AidlTypenames;
Jiyong Park02da7422018-07-16 16:00:26 +090037using android::aidl::CodeWriter;
Jeongik Cha047c5ee2019-08-07 23:16:49 +090038using android::aidl::Options;
Steven Moreland3f658cf2018-08-20 13:40:54 -070039using std::shared_ptr;
Jiyong Park1deecc32018-07-17 01:14:41 +090040using std::string;
41using std::unique_ptr;
42using std::vector;
Andrei Onea8714b022019-02-01 18:55:54 +000043class AidlNode;
44
45namespace android {
46namespace aidl {
47namespace mappings {
48std::string dump_location(const AidlNode& method);
49} // namespace mappings
Mathew Inwoodadb74672019-11-29 14:01:53 +000050namespace java {
51std::string dump_location(const AidlNode& method);
52} // namespace java
Andrei Onea8714b022019-02-01 18:55:54 +000053} // namespace aidl
54} // namespace android
55
Casey Dahlincdbbc8c2015-10-14 15:31:04 -070056class AidlToken {
57 public:
58 AidlToken(const std::string& text, const std::string& comments);
Adam Lesinskiffa16862014-01-23 18:17:42 -080059
Casey Dahlincdbbc8c2015-10-14 15:31:04 -070060 const std::string& GetText() const { return text_; }
61 const std::string& GetComments() const { return comments_; }
Adam Lesinskiffa16862014-01-23 18:17:42 -080062
Casey Dahlincdbbc8c2015-10-14 15:31:04 -070063 private:
64 std::string text_;
65 std::string comments_;
Casey Dahlin082f1d12015-09-21 14:06:25 -070066
Casey Dahlincdbbc8c2015-10-14 15:31:04 -070067 DISALLOW_COPY_AND_ASSIGN(AidlToken);
Casey Dahlin0a2f8be2015-09-28 16:15:29 -070068};
Adam Lesinskiffa16862014-01-23 18:17:42 -080069
Steven Moreland46e9da82018-07-27 15:45:29 -070070class AidlLocation {
Casey Dahlinbc7a50a2015-09-28 19:20:50 -070071 public:
Steven Moreland46e9da82018-07-27 15:45:29 -070072 struct Point {
Dan Willemsen609ba6d2019-12-30 10:44:00 -080073 int line;
74 int column;
Steven Moreland46e9da82018-07-27 15:45:29 -070075 };
76
Devin Mooredf93ebb2020-03-25 14:03:35 -070077 enum class Source {
78 // From internal aidl source code
79 INTERNAL = 0,
80 // From a parsed file
81 EXTERNAL = 1
82 };
83
84 AidlLocation(const std::string& file, Point begin, Point end, Source source);
Devin Moore5de18ed2020-04-02 13:52:29 -070085 AidlLocation(const std::string& file, Source source)
86 : AidlLocation(file, {0, 0}, {0, 0}, source) {}
Devin Mooredf93ebb2020-03-25 14:03:35 -070087
88 bool IsInternal() const { return source_ == Source::INTERNAL; }
Steven Moreland46e9da82018-07-27 15:45:29 -070089
Devin Moore5de18ed2020-04-02 13:52:29 -070090 // The first line of a file is line 1.
91 bool LocationKnown() const { return begin_.line != 0; }
92
Steven Moreland46e9da82018-07-27 15:45:29 -070093 friend std::ostream& operator<<(std::ostream& os, const AidlLocation& l);
Andrei Onea8714b022019-02-01 18:55:54 +000094 friend class AidlNode;
Casey Dahlinbc7a50a2015-09-28 19:20:50 -070095
96 private:
Steven Moreland46e9da82018-07-27 15:45:29 -070097 const std::string file_;
98 Point begin_;
99 Point end_;
Devin Mooredf93ebb2020-03-25 14:03:35 -0700100 Source source_;
Steven Moreland46e9da82018-07-27 15:45:29 -0700101};
102
Devin Mooredf93ebb2020-03-25 14:03:35 -0700103#define AIDL_LOCATION_HERE \
104 AidlLocation { __FILE__, {__LINE__, 0}, {__LINE__, 0}, AidlLocation::Source::INTERNAL }
Steven Moreland02e012e2018-08-02 14:58:10 -0700105
Steven Moreland46e9da82018-07-27 15:45:29 -0700106std::ostream& operator<<(std::ostream& os, const AidlLocation& l);
107
108// Anything that is locatable in a .aidl file.
109class AidlNode {
110 public:
111 AidlNode(const AidlLocation& location);
Steven Moreland3f658cf2018-08-20 13:40:54 -0700112
113 AidlNode(const AidlNode&) = default;
Steven Moreland3be75772018-08-20 13:27:43 -0700114 AidlNode(AidlNode&&) = default;
Steven Moreland46e9da82018-07-27 15:45:29 -0700115 virtual ~AidlNode() = default;
116
Devin Mooredf93ebb2020-03-25 14:03:35 -0700117 // To be able to print AidlLocation
Steven Morelandb0d15a52020-03-31 14:03:47 -0700118 friend class AidlErrorLog;
Andrei Onea8714b022019-02-01 18:55:54 +0000119 friend std::string android::aidl::mappings::dump_location(const AidlNode&);
Mathew Inwoodadb74672019-11-29 14:01:53 +0000120 friend std::string android::aidl::java::dump_location(const AidlNode&);
Steven Moreland46e9da82018-07-27 15:45:29 -0700121
Devin Mooredf93ebb2020-03-25 14:03:35 -0700122 protected:
123 // This should only be used to construct implicit nodes related to existing nodes
124 const AidlLocation& GetLocation() const { return location_; }
125
Steven Moreland46e9da82018-07-27 15:45:29 -0700126 private:
Mathew Inwoodadb74672019-11-29 14:01:53 +0000127 std::string PrintLine() const;
Andrei Onea8714b022019-02-01 18:55:54 +0000128 std::string PrintLocation() const;
Steven Moreland46e9da82018-07-27 15:45:29 -0700129 const AidlLocation location_;
Casey Dahlinbc7a50a2015-09-28 19:20:50 -0700130};
131
Steven Moreland46e9da82018-07-27 15:45:29 -0700132// Generic point for printing any error in the AIDL compiler.
Steven Morelandb0d15a52020-03-31 14:03:47 -0700133class AidlErrorLog {
Steven Moreland46e9da82018-07-27 15:45:29 -0700134 public:
Devin Moore5de18ed2020-04-02 13:52:29 -0700135 AidlErrorLog(bool fatal, const std::string& filename)
136 : AidlErrorLog(fatal, AidlLocation(filename, AidlLocation::Source::EXTERNAL)) {}
Steven Morelandb0d15a52020-03-31 14:03:47 -0700137 AidlErrorLog(bool fatal, const AidlLocation& location);
138 AidlErrorLog(bool fatal, const AidlNode& node) : AidlErrorLog(fatal, node.location_) {}
139 AidlErrorLog(bool fatal, const AidlNode* node) : AidlErrorLog(fatal, *node) {}
Steven Moreland92c55f12018-07-31 14:08:37 -0700140
141 template <typename T>
Steven Morelandb0d15a52020-03-31 14:03:47 -0700142 AidlErrorLog(bool fatal, const std::unique_ptr<T>& node) : AidlErrorLog(fatal, *node) {}
143 ~AidlErrorLog() {
Steven Moreland46e9da82018-07-27 15:45:29 -0700144 os_ << std::endl;
145 if (fatal_) abort();
Devin Moore5de18ed2020-04-02 13:52:29 -0700146 if (location_.IsInternal()) {
147 os_ << "Logging an internal location should not happen. Offending location: " << location_
148 << std::endl;
149 abort();
150 }
Steven Moreland46e9da82018-07-27 15:45:29 -0700151 }
152
153 std::ostream& os_;
154
Steven Moreland33efcf62020-04-10 16:40:43 -0700155 static void clearError() { sHadError = false; }
Steven Morelandfdb57cd2020-01-08 20:03:30 -0800156 static bool hadError() { return sHadError; }
157
Steven Moreland46e9da82018-07-27 15:45:29 -0700158 private:
Steven Moreland46e9da82018-07-27 15:45:29 -0700159
160 bool fatal_;
161
Devin Moore5de18ed2020-04-02 13:52:29 -0700162 const AidlLocation location_;
163
Steven Morelandfdb57cd2020-01-08 20:03:30 -0800164 static bool sHadError;
165
Steven Morelandb0d15a52020-03-31 14:03:47 -0700166 DISALLOW_COPY_AND_ASSIGN(AidlErrorLog);
Steven Moreland46e9da82018-07-27 15:45:29 -0700167};
168
Steven Morelandb0d15a52020-03-31 14:03:47 -0700169#define AIDL_ERROR(CONTEXT) ::AidlErrorLog(false /*fatal*/, (CONTEXT)).os_
170#define AIDL_FATAL(CONTEXT) ::AidlErrorLog(true /*fatal*/, (CONTEXT)).os_
Steven Moreland3f658cf2018-08-20 13:40:54 -0700171#define AIDL_FATAL_IF(CONDITION, CONTEXT) \
172 if (CONDITION) AIDL_FATAL(CONTEXT) << "Bad internal state: " << #CONDITION << ": "
Steven Moreland46e9da82018-07-27 15:45:29 -0700173
Casey Dahlina2f77c42015-12-01 18:26:02 -0800174namespace android {
175namespace aidl {
176
Jiyong Park1deecc32018-07-17 01:14:41 +0900177class AidlTypenames;
Casey Dahlina2f77c42015-12-01 18:26:02 -0800178
179} // namespace aidl
180} // namespace android
181
Jeongik Chadf76dc72019-11-28 00:08:47 +0900182// unique_ptr<AidlTypeSpecifier> for type arugment,
183// std::string for type parameter(T, U, and so on).
184template <typename T>
185class AidlParameterizable {
186 public:
187 AidlParameterizable(std::vector<T>* type_params) : type_params_(type_params) {}
188 virtual ~AidlParameterizable() = default;
189 bool IsGeneric() const { return type_params_ != nullptr; }
190 const std::vector<T>& GetTypeParameters() const { return *type_params_; }
191 bool CheckValid() const;
192
193 virtual const AidlNode& AsAidlNode() const = 0;
194
195 protected:
196 AidlParameterizable(const AidlParameterizable&);
197
198 private:
199 const unique_ptr<std::vector<T>> type_params_;
200 static_assert(std::is_same<T, unique_ptr<AidlTypeSpecifier>>::value ||
201 std::is_same<T, std::string>::value);
202};
203template <>
204bool AidlParameterizable<std::string>::CheckValid() const;
205
Andrei Onea9445fc62019-06-27 18:11:59 +0100206class AidlConstantValue;
207class AidlConstantDeclaration;
208
209// Transforms a value string into a language specific form. Raw value as produced by
210// AidlConstantValue.
211using ConstantValueDecorator =
212 std::function<std::string(const AidlTypeSpecifier& type, const std::string& raw_value)>;
213
Jiyong Park68bc77a2018-07-19 19:00:45 +0900214class AidlAnnotation : public AidlNode {
215 public:
Steven Moreland0cea4aa2020-04-20 21:06:02 -0700216 enum class Type {
217 BACKING = 1,
218 HIDE,
219 JAVA_STABLE_PARCELABLE,
220 UNSUPPORTED_APP_USAGE,
221 VINTF_STABILITY,
222 NULLABLE,
223 UTF8_IN_CPP,
224 };
225 static std::string TypeToString(Type type);
226
Andrei Onea9445fc62019-06-27 18:11:59 +0100227 static AidlAnnotation* Parse(
228 const AidlLocation& location, const string& name,
229 std::map<std::string, std::shared_ptr<AidlConstantValue>>* parameter_list);
Steven Moreland46e9da82018-07-27 15:45:29 -0700230
Steven Moreland3f658cf2018-08-20 13:40:54 -0700231 AidlAnnotation(const AidlAnnotation&) = default;
Steven Moreland3be75772018-08-20 13:27:43 -0700232 AidlAnnotation(AidlAnnotation&&) = default;
Jiyong Park68bc77a2018-07-19 19:00:45 +0900233 virtual ~AidlAnnotation() = default;
Andrei Onea9445fc62019-06-27 18:11:59 +0100234 bool CheckValid() const;
Steven Moreland3be75772018-08-20 13:27:43 -0700235
Steven Moreland0cea4aa2020-04-20 21:06:02 -0700236 const string& GetName() const { return schema_.name; };
237 const Type& GetType() const { return schema_.type; }
Daniel Norman37d43dd2019-09-09 17:22:34 -0700238 string ToString(const ConstantValueDecorator& decorator) const;
Andrei Onea9445fc62019-06-27 18:11:59 +0100239 std::map<std::string, std::string> AnnotationParams(
240 const ConstantValueDecorator& decorator) const;
Jiyong Parka6605ab2018-11-11 14:30:21 +0900241 const string& GetComments() const { return comments_; }
242 void SetComments(const string& comments) { comments_ = comments; }
Jiyong Park68bc77a2018-07-19 19:00:45 +0900243
244 private:
Steven Moreland0cea4aa2020-04-20 21:06:02 -0700245 struct Schema {
246 AidlAnnotation::Type type;
247
248 // text name in .aidl file, e.g. "nullable"
249 std::string name;
250
251 // map from param name -> value type
252 std::map<std::string, std::string> supported_parameters;
253 };
254 static const std::vector<Schema>& AllSchemas();
255
256 AidlAnnotation(const AidlLocation& location, const Schema& schema,
Andrei Onea9445fc62019-06-27 18:11:59 +0100257 std::map<std::string, std::shared_ptr<AidlConstantValue>>&& parameters);
Steven Moreland0cea4aa2020-04-20 21:06:02 -0700258
259 const Schema& schema_;
Jiyong Parka6605ab2018-11-11 14:30:21 +0900260 string comments_;
Andrei Onea9445fc62019-06-27 18:11:59 +0100261 std::map<std::string, std::shared_ptr<AidlConstantValue>> parameters_;
Jiyong Park68bc77a2018-07-19 19:00:45 +0900262};
263
Steven Moreland3be75772018-08-20 13:27:43 -0700264static inline bool operator<(const AidlAnnotation& lhs, const AidlAnnotation& rhs) {
265 return lhs.GetName() < rhs.GetName();
266}
267static inline bool operator==(const AidlAnnotation& lhs, const AidlAnnotation& rhs) {
268 return lhs.GetName() == rhs.GetName();
269}
Jiyong Park3656c3c2018-08-01 20:02:01 +0900270
Casey Dahline7922932016-02-29 17:23:01 -0800271class AidlAnnotatable : public AidlNode {
Casey Dahlin0ee37582015-09-30 16:31:55 -0700272 public:
Steven Moreland46e9da82018-07-27 15:45:29 -0700273 AidlAnnotatable(const AidlLocation& location);
Steven Moreland3f658cf2018-08-20 13:40:54 -0700274
275 AidlAnnotatable(const AidlAnnotatable&) = default;
276 AidlAnnotatable(AidlAnnotatable&&) = default;
Casey Dahline7922932016-02-29 17:23:01 -0800277 virtual ~AidlAnnotatable() = default;
278
Artur Satayev91fe8712019-07-29 13:06:01 +0100279 void Annotate(vector<AidlAnnotation>&& annotations) {
280 for (auto& annotation : annotations) {
281 annotations_.emplace_back(std::move(annotation));
282 }
283 }
Jiyong Park68bc77a2018-07-19 19:00:45 +0900284 bool IsNullable() const;
Jiyong Park68bc77a2018-07-19 19:00:45 +0900285 bool IsUtf8InCpp() const;
Steven Morelanda57d0a62019-07-30 09:41:14 -0700286 bool IsVintfStability() const;
Jeongik Cha88f95a82020-01-15 13:02:16 +0900287 bool IsStableApiParcelable(Options::Language lang) const;
Makoto Onuki78a1c1c2020-03-04 16:57:23 -0800288 bool IsHide() const;
Andrei Onea9445fc62019-06-27 18:11:59 +0100289
Steven Moreland7e4b9502020-02-20 18:10:42 -0800290 void DumpAnnotations(CodeWriter* writer) const;
291
Andrei Onea9445fc62019-06-27 18:11:59 +0100292 const AidlAnnotation* UnsupportedAppUsage() const;
Daniel Norman716d3112019-09-10 13:11:56 -0700293 const AidlTypeSpecifier* BackingType(const AidlTypenames& typenames) const;
Jiyong Park68bc77a2018-07-19 19:00:45 +0900294 std::string ToString() const;
Casey Dahline7922932016-02-29 17:23:01 -0800295
Jiyong Parka6605ab2018-11-11 14:30:21 +0900296 const vector<AidlAnnotation>& GetAnnotations() const { return annotations_; }
Devin Moore24f68572020-02-26 13:20:59 -0800297 virtual bool CheckValid(const AidlTypenames&) const;
Jiyong Park3656c3c2018-08-01 20:02:01 +0900298
Steven Moreland181144c2020-04-20 19:57:56 -0700299 protected:
Steven Moreland0cea4aa2020-04-20 21:06:02 -0700300 virtual std::set<AidlAnnotation::Type> GetSupportedAnnotations() const = 0;
Steven Moreland181144c2020-04-20 19:57:56 -0700301
Casey Dahline7922932016-02-29 17:23:01 -0800302 private:
Jiyong Parka6605ab2018-11-11 14:30:21 +0900303 vector<AidlAnnotation> annotations_;
Casey Dahline7922932016-02-29 17:23:01 -0800304};
305
Jiyong Park1deecc32018-07-17 01:14:41 +0900306class AidlQualifiedName;
307
308// AidlTypeSpecifier represents a reference to either a built-in type,
309// a defined type, or a variant (e.g., array of generic) of a type.
Jeongik Chadf76dc72019-11-28 00:08:47 +0900310class AidlTypeSpecifier final : public AidlAnnotatable,
311 public AidlParameterizable<unique_ptr<AidlTypeSpecifier>> {
Casey Dahline7922932016-02-29 17:23:01 -0800312 public:
Steven Moreland46e9da82018-07-27 15:45:29 -0700313 AidlTypeSpecifier(const AidlLocation& location, const string& unresolved_name, bool is_array,
314 vector<unique_ptr<AidlTypeSpecifier>>* type_params, const string& comments);
Jiyong Parkd59a10d2018-07-18 11:12:55 +0900315 virtual ~AidlTypeSpecifier() = default;
Casey Dahlin0ee37582015-09-30 16:31:55 -0700316
Steven Moreland3f658cf2018-08-20 13:40:54 -0700317 // Copy of this type which is not an array.
318 AidlTypeSpecifier ArrayBase() const;
319
Jiyong Park1deecc32018-07-17 01:14:41 +0900320 // Returns the full-qualified name of the base type.
321 // int -> int
322 // int[] -> int
323 // List<String> -> List
324 // IFoo -> foo.bar.IFoo (if IFoo is in package foo.bar)
325 const string& GetName() const {
326 if (IsResolved()) {
327 return fully_qualified_name_;
328 } else {
329 return GetUnresolvedName();
330 }
331 }
Casey Dahlin0ee37582015-09-30 16:31:55 -0700332
Jiyong Park1deecc32018-07-17 01:14:41 +0900333 // Returns string representation of this type specifier.
Artur Satayev91fe8712019-07-29 13:06:01 +0100334 // This is GetBaseTypeName() + array modifier or generic type parameters
Jiyong Park1deecc32018-07-17 01:14:41 +0900335 string ToString() const;
336
Jiyong Park02da7422018-07-16 16:00:26 +0900337 std::string Signature() const;
338
Jiyong Park1deecc32018-07-17 01:14:41 +0900339 const string& GetUnresolvedName() const { return unresolved_name_; }
340
Jeongik Cha997281d2020-01-16 15:23:59 +0900341 bool IsHidden() const;
342
Jiyong Park1deecc32018-07-17 01:14:41 +0900343 const string& GetComments() const { return comments_; }
344
Jeongik Cha1a7ab642019-07-29 17:31:02 +0900345 const std::vector<std::string> GetSplitName() const { return split_name_; }
346
Jiyong Parka6605ab2018-11-11 14:30:21 +0900347 void SetComments(const string& comment) { comments_ = comment; }
348
Jiyong Park1deecc32018-07-17 01:14:41 +0900349 bool IsResolved() const { return fully_qualified_name_ != ""; }
350
351 bool IsArray() const { return is_array_; }
352
Jiyong Park1deecc32018-07-17 01:14:41 +0900353 // Resolve the base type name to a fully-qualified name. Return false if the
354 // resolution fails.
Daniel Norman716d3112019-09-10 13:11:56 -0700355 bool Resolve(const AidlTypenames& typenames);
Casey Dahlin0ee37582015-09-30 16:31:55 -0700356
Steven Moreland0cea4aa2020-04-20 21:06:02 -0700357 std::set<AidlAnnotation::Type> GetSupportedAnnotations() const override;
Devin Moore24f68572020-02-26 13:20:59 -0800358 bool CheckValid(const AidlTypenames& typenames) const override;
Jeongik Cha047c5ee2019-08-07 23:16:49 +0900359 bool LanguageSpecificCheckValid(Options::Language lang) const;
Jeongik Chadf76dc72019-11-28 00:08:47 +0900360 const AidlNode& AsAidlNode() const override { return *this; }
Jiyong Park1d2df7d2018-07-23 15:22:50 +0900361
Casey Dahlin0ee37582015-09-30 16:31:55 -0700362 private:
Steven Moreland3f658cf2018-08-20 13:40:54 -0700363 AidlTypeSpecifier(const AidlTypeSpecifier&) = default;
364
Jiyong Park1deecc32018-07-17 01:14:41 +0900365 const string unresolved_name_;
366 string fully_qualified_name_;
Steven Moreland3f658cf2018-08-20 13:40:54 -0700367 bool is_array_;
Jiyong Parka6605ab2018-11-11 14:30:21 +0900368 string comments_;
Jeongik Cha1a7ab642019-07-29 17:31:02 +0900369 vector<string> split_name_;
Casey Dahlin0ee37582015-09-30 16:31:55 -0700370};
371
Steven Moreland860b1942018-08-16 14:59:28 -0700372// Returns the universal value unaltered.
373std::string AidlConstantValueDecorator(const AidlTypeSpecifier& type, const std::string& raw_value);
374
Steven Moreland9ea10e32018-07-19 15:26:09 -0700375class AidlConstantValue;
Steven Moreland5557f1c2018-07-02 13:50:23 -0700376class AidlVariableDeclaration : public AidlNode {
377 public:
Steven Moreland46e9da82018-07-27 15:45:29 -0700378 AidlVariableDeclaration(const AidlLocation& location, AidlTypeSpecifier* type,
379 const std::string& name);
380 AidlVariableDeclaration(const AidlLocation& location, AidlTypeSpecifier* type,
381 const std::string& name, AidlConstantValue* default_value);
Steven Moreland5557f1c2018-07-02 13:50:23 -0700382 virtual ~AidlVariableDeclaration() = default;
383
384 std::string GetName() const { return name_; }
Jiyong Parkd59a10d2018-07-18 11:12:55 +0900385 const AidlTypeSpecifier& GetType() const { return *type_; }
Steven Moreland9ea10e32018-07-19 15:26:09 -0700386 const AidlConstantValue* GetDefaultValue() const { return default_value_.get(); }
387
Jiyong Parkd59a10d2018-07-18 11:12:55 +0900388 AidlTypeSpecifier* GetMutableType() { return type_.get(); }
Steven Moreland5557f1c2018-07-02 13:50:23 -0700389
Jeongik Chadb0f59e2018-11-01 18:11:21 +0900390 bool CheckValid(const AidlTypenames& typenames) const;
Steven Moreland5557f1c2018-07-02 13:50:23 -0700391 std::string ToString() const;
Jiyong Park02da7422018-07-16 16:00:26 +0900392 std::string Signature() const;
Steven Moreland5557f1c2018-07-02 13:50:23 -0700393
Steven Moreland860b1942018-08-16 14:59:28 -0700394 std::string ValueString(const ConstantValueDecorator& decorator) const;
Steven Moreland25294322018-08-07 18:13:55 -0700395
Steven Moreland5557f1c2018-07-02 13:50:23 -0700396 private:
Jiyong Parkd59a10d2018-07-18 11:12:55 +0900397 std::unique_ptr<AidlTypeSpecifier> type_;
Steven Moreland5557f1c2018-07-02 13:50:23 -0700398 std::string name_;
Steven Moreland9ea10e32018-07-19 15:26:09 -0700399 std::unique_ptr<AidlConstantValue> default_value_;
Steven Moreland5557f1c2018-07-02 13:50:23 -0700400
401 DISALLOW_COPY_AND_ASSIGN(AidlVariableDeclaration);
402};
403
404class AidlArgument : public AidlVariableDeclaration {
Casey Dahlinbc7a50a2015-09-28 19:20:50 -0700405 public:
Casey Dahlinc378c992015-09-29 16:50:40 -0700406 enum Direction { IN_DIR = 1, OUT_DIR = 2, INOUT_DIR = 3 };
407
Steven Moreland46e9da82018-07-27 15:45:29 -0700408 AidlArgument(const AidlLocation& location, AidlArgument::Direction direction,
409 AidlTypeSpecifier* type, const std::string& name);
410 AidlArgument(const AidlLocation& location, AidlTypeSpecifier* type, const std::string& name);
Casey Dahlinbc7a50a2015-09-28 19:20:50 -0700411 virtual ~AidlArgument() = default;
412
Casey Dahlinc378c992015-09-29 16:50:40 -0700413 Direction GetDirection() const { return direction_; }
Christopher Wileyad339272015-10-05 19:11:58 -0700414 bool IsOut() const { return direction_ & OUT_DIR; }
415 bool IsIn() const { return direction_ & IN_DIR; }
Casey Dahlinc378c992015-09-29 16:50:40 -0700416 bool DirectionWasSpecified() const { return direction_specified_; }
Jiyong Park3656c3c2018-08-01 20:02:01 +0900417 string GetDirectionSpecifier() const;
Christopher Wileyad339272015-10-05 19:11:58 -0700418
Casey Dahlinc378c992015-09-29 16:50:40 -0700419 std::string ToString() const;
Jiyong Park02da7422018-07-16 16:00:26 +0900420 std::string Signature() const;
Casey Dahlinc378c992015-09-29 16:50:40 -0700421
Casey Dahlinbc7a50a2015-09-28 19:20:50 -0700422 private:
Casey Dahlinc378c992015-09-29 16:50:40 -0700423 Direction direction_;
424 bool direction_specified_;
425
Casey Dahlinbc7a50a2015-09-28 19:20:50 -0700426 DISALLOW_COPY_AND_ASSIGN(AidlArgument);
Casey Dahlina834dd42015-09-23 11:52:15 -0700427};
Adam Lesinskiffa16862014-01-23 18:17:42 -0800428
Casey Dahlind40e2fe2015-11-24 14:06:52 -0800429class AidlMethod;
Steven Moreland693640b2018-07-19 13:46:27 -0700430class AidlConstantDeclaration;
Daniel Norman85aed542019-08-21 12:01:14 -0700431class AidlEnumDeclaration;
Casey Dahlind40e2fe2015-11-24 14:06:52 -0800432class AidlMember : public AidlNode {
433 public:
Steven Moreland46e9da82018-07-27 15:45:29 -0700434 AidlMember(const AidlLocation& location);
Casey Dahlind40e2fe2015-11-24 14:06:52 -0800435 virtual ~AidlMember() = default;
436
437 virtual AidlMethod* AsMethod() { return nullptr; }
Steven Moreland693640b2018-07-19 13:46:27 -0700438 virtual AidlConstantDeclaration* AsConstantDeclaration() { return nullptr; }
Casey Dahlind40e2fe2015-11-24 14:06:52 -0800439
440 private:
441 DISALLOW_COPY_AND_ASSIGN(AidlMember);
442};
443
Will McVickerd7d18df2019-09-12 13:40:50 -0700444class AidlUnaryConstExpression;
445class AidlBinaryConstExpression;
446
Steven Moreland693640b2018-07-19 13:46:27 -0700447class AidlConstantValue : public AidlNode {
Casey Dahlind40e2fe2015-11-24 14:06:52 -0800448 public:
Will McVickerd7d18df2019-09-12 13:40:50 -0700449 enum class Type {
450 // WARNING: Don't change this order! The order is used to determine type
451 // promotion during a binary expression.
452 BOOLEAN,
453 INT8,
454 INT32,
455 INT64,
456 ARRAY,
457 CHARACTER,
458 STRING,
459 FLOATING,
460 UNARY,
461 BINARY,
462 ERROR,
463 };
464
465 /*
466 * Return the value casted to the given type.
467 */
468 template <typename T>
469 T cast() const;
Casey Dahlind40e2fe2015-11-24 14:06:52 -0800470
Steven Moreland693640b2018-07-19 13:46:27 -0700471 virtual ~AidlConstantValue() = default;
Casey Dahlind40e2fe2015-11-24 14:06:52 -0800472
Steven Moreland25294322018-08-07 18:13:55 -0700473 static AidlConstantValue* Boolean(const AidlLocation& location, bool value);
474 static AidlConstantValue* Character(const AidlLocation& location, char value);
Steven Moreland25294322018-08-07 18:13:55 -0700475 // example: 123, -5498, maybe any size
Will McVickerd7d18df2019-09-12 13:40:50 -0700476 static AidlConstantValue* Integral(const AidlLocation& location, const string& value);
477 static AidlConstantValue* Floating(const AidlLocation& location, const std::string& value);
Steven Moreland860b1942018-08-16 14:59:28 -0700478 static AidlConstantValue* Array(const AidlLocation& location,
Will McVickerd7d18df2019-09-12 13:40:50 -0700479 std::unique_ptr<vector<unique_ptr<AidlConstantValue>>> values);
Steven Moreland693640b2018-07-19 13:46:27 -0700480 // example: "\"asdf\""
Will McVickerd7d18df2019-09-12 13:40:50 -0700481 static AidlConstantValue* String(const AidlLocation& location, const string& value);
Steven Moreland693640b2018-07-19 13:46:27 -0700482
Daniel Normanf0ca44f2019-10-25 09:59:44 -0700483 // Construct an AidlConstantValue by evaluating the other integral constant's
484 // value string. This does not preserve the structure of the copied constant.
Steven Moreland59e53e42019-11-26 20:38:08 -0800485 // Returns nullptr and logs if value cannot be copied.
Daniel Normanf0ca44f2019-10-25 09:59:44 -0700486 static AidlConstantValue* ShallowIntegralCopy(const AidlConstantValue& other);
Daniel Normanb28684e2019-10-17 15:31:39 -0700487
Will McVickerd7d18df2019-09-12 13:40:50 -0700488 Type GetType() const { return final_type_; }
Steven Moreland25294322018-08-07 18:13:55 -0700489
Will McVickerd7d18df2019-09-12 13:40:50 -0700490 virtual bool CheckValid() const;
Steven Moreland860b1942018-08-16 14:59:28 -0700491
492 // Raw value of type (currently valid in C++ and Java). Empty string on error.
Steven Moreland4bcb05c2019-11-27 18:57:47 -0800493 string ValueString(const AidlTypeSpecifier& type, const ConstantValueDecorator& decorator) const;
Casey Dahlind40e2fe2015-11-24 14:06:52 -0800494
495 private:
Will McVickerd7d18df2019-09-12 13:40:50 -0700496 AidlConstantValue(const AidlLocation& location, Type parsed_type, int64_t parsed_value,
497 const string& checked_value);
498 AidlConstantValue(const AidlLocation& location, Type type, const string& checked_value);
Steven Moreland860b1942018-08-16 14:59:28 -0700499 AidlConstantValue(const AidlLocation& location, Type type,
Will McVickerd7d18df2019-09-12 13:40:50 -0700500 std::unique_ptr<vector<unique_ptr<AidlConstantValue>>> values);
Steven Moreland25294322018-08-07 18:13:55 -0700501 static string ToString(Type type);
Will McVickerd7d18df2019-09-12 13:40:50 -0700502 static bool ParseIntegral(const string& value, int64_t* parsed_value, Type* parsed_type);
503 static bool IsHex(const string& value);
Steven Moreland4bcb05c2019-11-27 18:57:47 -0800504
Will McVickerd7d18df2019-09-12 13:40:50 -0700505 virtual bool evaluate(const AidlTypeSpecifier& type) const;
Casey Dahlind40e2fe2015-11-24 14:06:52 -0800506
Steven Moreland693640b2018-07-19 13:46:27 -0700507 const Type type_ = Type::ERROR;
Will McVickerd7d18df2019-09-12 13:40:50 -0700508 const vector<unique_ptr<AidlConstantValue>> values_; // if type_ == ARRAY
509 const string value_; // otherwise
510
511 // State for tracking evaluation of expressions
Steven Moreland4bcb05c2019-11-27 18:57:47 -0800512 mutable bool is_valid_ = false; // cache of CheckValid, but may be marked false in evaluate
513 mutable bool is_evaluated_ = false; // whether evaluate has been called
Will McVickerd7d18df2019-09-12 13:40:50 -0700514 mutable Type final_type_;
515 mutable int64_t final_value_;
516 mutable string final_string_value_ = "";
Steven Moreland693640b2018-07-19 13:46:27 -0700517
518 DISALLOW_COPY_AND_ASSIGN(AidlConstantValue);
Will McVickerd7d18df2019-09-12 13:40:50 -0700519
520 friend AidlUnaryConstExpression;
521 friend AidlBinaryConstExpression;
522};
523
524class AidlUnaryConstExpression : public AidlConstantValue {
525 public:
526 AidlUnaryConstExpression(const AidlLocation& location, const string& op,
527 std::unique_ptr<AidlConstantValue> rval);
528
529 static bool IsCompatibleType(Type type, const string& op);
530 bool CheckValid() const override;
Will McVickerd7d18df2019-09-12 13:40:50 -0700531 private:
532 bool evaluate(const AidlTypeSpecifier& type) const override;
533
534 std::unique_ptr<AidlConstantValue> unary_;
535 const string op_;
536};
537
538class AidlBinaryConstExpression : public AidlConstantValue {
539 public:
540 AidlBinaryConstExpression(const AidlLocation& location, std::unique_ptr<AidlConstantValue> lval,
541 const string& op, std::unique_ptr<AidlConstantValue> rval);
542
543 bool CheckValid() const override;
Will McVickerd7d18df2019-09-12 13:40:50 -0700544
545 static bool AreCompatibleTypes(Type t1, Type t2);
546 // Returns the promoted kind for both operands
547 static Type UsualArithmeticConversion(Type left, Type right);
548 // Returns the promoted integral type where INT32 is the smallest type
549 static Type IntegralPromotion(Type in);
550
551 private:
552 bool evaluate(const AidlTypeSpecifier& type) const override;
553
554 std::unique_ptr<AidlConstantValue> left_val_;
555 std::unique_ptr<AidlConstantValue> right_val_;
556 const string op_;
Christopher Wileyd6bdd8d2016-05-03 11:23:13 -0700557};
558
Andrei Onea9445fc62019-06-27 18:11:59 +0100559struct AidlAnnotationParameter {
560 std::string name;
561 std::unique_ptr<AidlConstantValue> value;
562};
563
Steven Moreland693640b2018-07-19 13:46:27 -0700564class AidlConstantDeclaration : public AidlMember {
Christopher Wileyd6bdd8d2016-05-03 11:23:13 -0700565 public:
Steven Moreland46e9da82018-07-27 15:45:29 -0700566 AidlConstantDeclaration(const AidlLocation& location, AidlTypeSpecifier* specifier,
Will McVickerd7d18df2019-09-12 13:40:50 -0700567 const string& name, AidlConstantValue* value);
Steven Moreland693640b2018-07-19 13:46:27 -0700568 virtual ~AidlConstantDeclaration() = default;
Christopher Wileyd6bdd8d2016-05-03 11:23:13 -0700569
Steven Moreland693640b2018-07-19 13:46:27 -0700570 const AidlTypeSpecifier& GetType() const { return *type_; }
Steven Moreland4d12f9a2018-10-31 14:30:55 -0700571 AidlTypeSpecifier* GetMutableType() { return type_.get(); }
Will McVickerd7d18df2019-09-12 13:40:50 -0700572 const string& GetName() const { return name_; }
Steven Moreland693640b2018-07-19 13:46:27 -0700573 const AidlConstantValue& GetValue() const { return *value_; }
Jeongik Chadb0f59e2018-11-01 18:11:21 +0900574 bool CheckValid(const AidlTypenames& typenames) const;
Christopher Wileyd6bdd8d2016-05-03 11:23:13 -0700575
Will McVickerd7d18df2019-09-12 13:40:50 -0700576 string ToString() const;
577 string Signature() const;
Steven Moreland860b1942018-08-16 14:59:28 -0700578 string ValueString(const ConstantValueDecorator& decorator) const {
Will McVickerd7d18df2019-09-12 13:40:50 -0700579 return value_->ValueString(GetType(), decorator);
Steven Moreland860b1942018-08-16 14:59:28 -0700580 }
Steven Moreland25294322018-08-07 18:13:55 -0700581
Steven Moreland693640b2018-07-19 13:46:27 -0700582 AidlConstantDeclaration* AsConstantDeclaration() override { return this; }
Christopher Wileyd6bdd8d2016-05-03 11:23:13 -0700583
584 private:
Steven Moreland693640b2018-07-19 13:46:27 -0700585 const unique_ptr<AidlTypeSpecifier> type_;
Will McVickerd7d18df2019-09-12 13:40:50 -0700586 const string name_;
587 unique_ptr<AidlConstantValue> value_;
Christopher Wileyd6bdd8d2016-05-03 11:23:13 -0700588
Steven Moreland693640b2018-07-19 13:46:27 -0700589 DISALLOW_COPY_AND_ASSIGN(AidlConstantDeclaration);
Casey Dahlind40e2fe2015-11-24 14:06:52 -0800590};
591
592class AidlMethod : public AidlMember {
Casey Dahlin5c69deb2015-10-01 14:44:12 -0700593 public:
Will McVickerd7d18df2019-09-12 13:40:50 -0700594 AidlMethod(const AidlLocation& location, bool oneway, AidlTypeSpecifier* type, const string& name,
595 vector<unique_ptr<AidlArgument>>* args, const string& comments);
596 AidlMethod(const AidlLocation& location, bool oneway, AidlTypeSpecifier* type, const string& name,
597 vector<unique_ptr<AidlArgument>>* args, const string& comments, int id,
598 bool is_user_defined = true);
Casey Dahlin5c69deb2015-10-01 14:44:12 -0700599 virtual ~AidlMethod() = default;
600
Casey Dahlind40e2fe2015-11-24 14:06:52 -0800601 AidlMethod* AsMethod() override { return this; }
Jeongik Cha997281d2020-01-16 15:23:59 +0900602 bool IsHidden() const;
Will McVickerd7d18df2019-09-12 13:40:50 -0700603 const string& GetComments() const { return comments_; }
Jiyong Parkd59a10d2018-07-18 11:12:55 +0900604 const AidlTypeSpecifier& GetType() const { return *type_; }
605 AidlTypeSpecifier* GetMutableType() { return type_.get(); }
Steven Morelandacd53472018-12-14 10:17:26 -0800606
Steven Moreland8c70ba92018-12-17 10:20:31 -0800607 // set if this method is part of an interface that is marked oneway
608 void ApplyInterfaceOneway(bool oneway) { oneway_ = oneway_ || oneway; }
Casey Dahlinf4a93112015-10-05 16:58:09 -0700609 bool IsOneway() const { return oneway_; }
Steven Morelandacd53472018-12-14 10:17:26 -0800610
Casey Dahlinf4a93112015-10-05 16:58:09 -0700611 const std::string& GetName() const { return name_; }
Casey Dahlinf4a93112015-10-05 16:58:09 -0700612 bool HasId() const { return has_id_; }
Jiyong Parked65bf42018-08-28 15:43:27 +0900613 int GetId() const { return id_; }
Casey Dahlinf4a93112015-10-05 16:58:09 -0700614 void SetId(unsigned id) { id_ = id; }
Casey Dahlinf2d23f72015-10-02 16:19:19 -0700615
Jiyong Park309668e2018-07-28 16:55:44 +0900616 bool IsUserDefined() const { return is_user_defined_; }
617
Casey Dahlinf4a93112015-10-05 16:58:09 -0700618 const std::vector<std::unique_ptr<AidlArgument>>& GetArguments() const {
Christopher Wileyad339272015-10-05 19:11:58 -0700619 return arguments_;
620 }
621 // An inout parameter will appear in both GetInArguments()
622 // and GetOutArguments(). AidlMethod retains ownership of the argument
623 // pointers returned in this way.
624 const std::vector<const AidlArgument*>& GetInArguments() const {
625 return in_arguments_;
626 }
627 const std::vector<const AidlArgument*>& GetOutArguments() const {
628 return out_arguments_;
Casey Dahlinf4a93112015-10-05 16:58:09 -0700629 }
Casey Dahlin5c69deb2015-10-01 14:44:12 -0700630
Jiyong Park309668e2018-07-28 16:55:44 +0900631 // name + type parameter types
632 // i.e, foo(int, String)
Jiyong Park02da7422018-07-16 16:00:26 +0900633 std::string Signature() const;
634
Jiyong Park309668e2018-07-28 16:55:44 +0900635 // return type + name + type parameter types + annotations
636 // i.e, boolean foo(int, @Nullable String)
637 std::string ToString() const;
638
Casey Dahlin5c69deb2015-10-01 14:44:12 -0700639 private:
Casey Dahlinf4a93112015-10-05 16:58:09 -0700640 bool oneway_;
Casey Dahlinf2d23f72015-10-02 16:19:19 -0700641 std::string comments_;
Jiyong Parkd59a10d2018-07-18 11:12:55 +0900642 std::unique_ptr<AidlTypeSpecifier> type_;
Casey Dahlinf4a93112015-10-05 16:58:09 -0700643 std::string name_;
Christopher Wileyad339272015-10-05 19:11:58 -0700644 const std::vector<std::unique_ptr<AidlArgument>> arguments_;
645 std::vector<const AidlArgument*> in_arguments_;
646 std::vector<const AidlArgument*> out_arguments_;
Casey Dahlinf4a93112015-10-05 16:58:09 -0700647 bool has_id_;
648 int id_;
Jiyong Park309668e2018-07-28 16:55:44 +0900649 bool is_user_defined_ = true;
Casey Dahlinf2d23f72015-10-02 16:19:19 -0700650
Casey Dahlin5c69deb2015-10-01 14:44:12 -0700651 DISALLOW_COPY_AND_ASSIGN(AidlMethod);
Casey Dahlin0a2f8be2015-09-28 16:15:29 -0700652};
Adam Lesinskiffa16862014-01-23 18:17:42 -0800653
Steven Morelandc258abc2018-07-10 14:03:38 -0700654class AidlDefinedType;
Jiyong Parkb034bf02018-07-30 17:44:33 +0900655class AidlInterface;
656class AidlParcelable;
657class AidlStructuredParcelable;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800658
Casey Dahlin2b2879b2015-10-13 16:59:44 -0700659class AidlQualifiedName : public AidlNode {
660 public:
Steven Moreland46e9da82018-07-27 15:45:29 -0700661 AidlQualifiedName(const AidlLocation& location, const std::string& term,
662 const std::string& comments);
Casey Dahlin2b2879b2015-10-13 16:59:44 -0700663 virtual ~AidlQualifiedName() = default;
664
665 const std::vector<std::string>& GetTerms() const { return terms_; }
666 const std::string& GetComments() const { return comments_; }
667 std::string GetDotName() const { return android::base::Join(terms_, '.'); }
Ningyuan Wangd17c58b2016-09-29 14:33:14 -0700668 std::string GetColonName() const { return android::base::Join(terms_, "::"); }
Casey Dahlin2b2879b2015-10-13 16:59:44 -0700669
Chih-Hung Hsiehf05cc262016-07-27 11:42:51 -0700670 void AddTerm(const std::string& term);
Casey Dahlin2b2879b2015-10-13 16:59:44 -0700671
672 private:
673 std::vector<std::string> terms_;
674 std::string comments_;
675
676 DISALLOW_COPY_AND_ASSIGN(AidlQualifiedName);
677};
678
Steven Moreland46e9da82018-07-27 15:45:29 -0700679class AidlInterface;
680class AidlParcelable;
681class AidlStructuredParcelable;
Daniel Norman85aed542019-08-21 12:01:14 -0700682// AidlDefinedType represents either an interface, parcelable, or enum that is
Jiyong Park1deecc32018-07-17 01:14:41 +0900683// defined in the source file.
684class AidlDefinedType : public AidlAnnotatable {
Steven Moreland787b0432018-07-03 09:00:58 -0700685 public:
Steven Moreland46e9da82018-07-27 15:45:29 -0700686 AidlDefinedType(const AidlLocation& location, const std::string& name,
687 const std::string& comments, const std::vector<std::string>& package);
Steven Moreland787b0432018-07-03 09:00:58 -0700688 virtual ~AidlDefinedType() = default;
689
Jiyong Park1deecc32018-07-17 01:14:41 +0900690 const std::string& GetName() const { return name_; };
Jeongik Cha997281d2020-01-16 15:23:59 +0900691 bool IsHidden() const;
Jiyong Park1deecc32018-07-17 01:14:41 +0900692 const std::string& GetComments() const { return comments_; }
Jiyong Parka6605ab2018-11-11 14:30:21 +0900693 void SetComments(const std::string comments) { comments_ = comments; }
Jiyong Park1deecc32018-07-17 01:14:41 +0900694
Steven Moreland787b0432018-07-03 09:00:58 -0700695 /* dot joined package, example: "android.package.foo" */
696 std::string GetPackage() const;
697 /* dot joined package and name, example: "android.package.foo.IBar" */
698 std::string GetCanonicalName() const;
699 const std::vector<std::string>& GetSplitPackage() const { return package_; }
700
Steven Morelanded83a282018-07-17 13:27:29 -0700701 virtual std::string GetPreprocessDeclarationName() const = 0;
Steven Morelandc258abc2018-07-10 14:03:38 -0700702
Steven Moreland5557f1c2018-07-02 13:50:23 -0700703 virtual const AidlStructuredParcelable* AsStructuredParcelable() const { return nullptr; }
Steven Morelandc258abc2018-07-10 14:03:38 -0700704 virtual const AidlParcelable* AsParcelable() const { return nullptr; }
Daniel Norman85aed542019-08-21 12:01:14 -0700705 virtual const AidlEnumDeclaration* AsEnumDeclaration() const { return nullptr; }
Steven Moreland5557f1c2018-07-02 13:50:23 -0700706 virtual const AidlInterface* AsInterface() const { return nullptr; }
Jeongik Chadf76dc72019-11-28 00:08:47 +0900707 virtual const AidlParameterizable<std::string>* AsParameterizable() const { return nullptr; }
Devin Moore24f68572020-02-26 13:20:59 -0800708 bool CheckValid(const AidlTypenames& typenames) const override;
Jeongik Cha047c5ee2019-08-07 23:16:49 +0900709 virtual bool LanguageSpecificCheckValid(Options::Language lang) const = 0;
Steven Morelandc258abc2018-07-10 14:03:38 -0700710 AidlStructuredParcelable* AsStructuredParcelable() {
711 return const_cast<AidlStructuredParcelable*>(
712 const_cast<const AidlDefinedType*>(this)->AsStructuredParcelable());
713 }
714 AidlParcelable* AsParcelable() {
715 return const_cast<AidlParcelable*>(const_cast<const AidlDefinedType*>(this)->AsParcelable());
716 }
Daniel Norman85aed542019-08-21 12:01:14 -0700717 AidlEnumDeclaration* AsEnumDeclaration() {
718 return const_cast<AidlEnumDeclaration*>(
719 const_cast<const AidlDefinedType*>(this)->AsEnumDeclaration());
720 }
Steven Morelandc258abc2018-07-10 14:03:38 -0700721 AidlInterface* AsInterface() {
722 return const_cast<AidlInterface*>(const_cast<const AidlDefinedType*>(this)->AsInterface());
723 }
724
Jeongik Chadf76dc72019-11-28 00:08:47 +0900725 AidlParameterizable<std::string>* AsParameterizable() {
726 return const_cast<AidlParameterizable<std::string>*>(
727 const_cast<const AidlDefinedType*>(this)->AsParameterizable());
728 }
729
Steven Moreland6cee3482018-07-18 14:39:58 -0700730 const AidlParcelable* AsUnstructuredParcelable() const {
731 if (this->AsStructuredParcelable() != nullptr) return nullptr;
732 return this->AsParcelable();
733 }
734 AidlParcelable* AsUnstructuredParcelable() {
735 return const_cast<AidlParcelable*>(
736 const_cast<const AidlDefinedType*>(this)->AsUnstructuredParcelable());
737 }
738
Jeongik Cha997281d2020-01-16 15:23:59 +0900739 virtual void Dump(CodeWriter* writer) const = 0;
Steven Morelanda5d9c5c2020-02-21 16:01:09 -0800740 void DumpHeader(CodeWriter* writer) const;
Jiyong Park02da7422018-07-16 16:00:26 +0900741
Steven Moreland787b0432018-07-03 09:00:58 -0700742 private:
Jiyong Park1deecc32018-07-17 01:14:41 +0900743 std::string name_;
Jiyong Park1deecc32018-07-17 01:14:41 +0900744 std::string comments_;
Steven Moreland787b0432018-07-03 09:00:58 -0700745 const std::vector<std::string> package_;
Steven Moreland5557f1c2018-07-02 13:50:23 -0700746
747 DISALLOW_COPY_AND_ASSIGN(AidlDefinedType);
Steven Moreland787b0432018-07-03 09:00:58 -0700748};
749
Jeongik Chadf76dc72019-11-28 00:08:47 +0900750class AidlParcelable : public AidlDefinedType, public AidlParameterizable<std::string> {
Casey Dahlin1ae2bc52015-10-07 18:49:10 -0700751 public:
Steven Moreland46e9da82018-07-27 15:45:29 -0700752 AidlParcelable(const AidlLocation& location, AidlQualifiedName* name,
Jiyong Parka6605ab2018-11-11 14:30:21 +0900753 const std::vector<std::string>& package, const std::string& comments,
Jeongik Chadf76dc72019-11-28 00:08:47 +0900754 const std::string& cpp_header = "",
755 std::vector<std::string>* type_params = nullptr);
Casey Dahlin1ae2bc52015-10-07 18:49:10 -0700756 virtual ~AidlParcelable() = default;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800757
Ningyuan Wangd17c58b2016-09-29 14:33:14 -0700758 // C++ uses "::" instead of "." to refer to a inner class.
759 std::string GetCppName() const { return name_->GetColonName(); }
Christopher Wiley8aa4d9f2015-11-16 19:10:45 -0800760 std::string GetCppHeader() const { return cpp_header_; }
Christopher Wiley8aa4d9f2015-11-16 19:10:45 -0800761
Steven Moreland0cea4aa2020-04-20 21:06:02 -0700762 std::set<AidlAnnotation::Type> GetSupportedAnnotations() const override;
Jeongik Cha82317dd2019-02-27 20:26:11 +0900763 bool CheckValid(const AidlTypenames& typenames) const override;
Jeongik Cha047c5ee2019-08-07 23:16:49 +0900764 bool LanguageSpecificCheckValid(Options::Language lang) const override;
Steven Morelandc258abc2018-07-10 14:03:38 -0700765 const AidlParcelable* AsParcelable() const override { return this; }
Jeongik Chadf76dc72019-11-28 00:08:47 +0900766 const AidlParameterizable<std::string>* AsParameterizable() const override { return this; }
767 const AidlNode& AsAidlNode() const override { return *this; }
Steven Morelanded83a282018-07-17 13:27:29 -0700768 std::string GetPreprocessDeclarationName() const override { return "parcelable"; }
Steven Morelandc258abc2018-07-10 14:03:38 -0700769
Jeongik Cha997281d2020-01-16 15:23:59 +0900770 void Dump(CodeWriter* writer) const override;
Jiyong Park02da7422018-07-16 16:00:26 +0900771
Casey Dahlin1ae2bc52015-10-07 18:49:10 -0700772 private:
Christopher Wiley8aa4d9f2015-11-16 19:10:45 -0800773 std::unique_ptr<AidlQualifiedName> name_;
Christopher Wiley8aa4d9f2015-11-16 19:10:45 -0800774 std::string cpp_header_;
Casey Dahlin59401da2015-10-09 18:16:45 -0700775
Casey Dahlin1ae2bc52015-10-07 18:49:10 -0700776 DISALLOW_COPY_AND_ASSIGN(AidlParcelable);
Casey Dahlin0a2f8be2015-09-28 16:15:29 -0700777};
Adam Lesinskiffa16862014-01-23 18:17:42 -0800778
Steven Moreland5557f1c2018-07-02 13:50:23 -0700779class AidlStructuredParcelable : public AidlParcelable {
780 public:
Steven Moreland46e9da82018-07-27 15:45:29 -0700781 AidlStructuredParcelable(const AidlLocation& location, AidlQualifiedName* name,
Jiyong Parka6605ab2018-11-11 14:30:21 +0900782 const std::vector<std::string>& package, const std::string& comments,
Steven Moreland5557f1c2018-07-02 13:50:23 -0700783 std::vector<std::unique_ptr<AidlVariableDeclaration>>* variables);
784
785 const std::vector<std::unique_ptr<AidlVariableDeclaration>>& GetFields() const {
786 return variables_;
787 }
788
Steven Morelandc258abc2018-07-10 14:03:38 -0700789 const AidlStructuredParcelable* AsStructuredParcelable() const override { return this; }
Steven Morelanded83a282018-07-17 13:27:29 -0700790 std::string GetPreprocessDeclarationName() const override { return "structured_parcelable"; }
Steven Moreland5557f1c2018-07-02 13:50:23 -0700791
Jeongik Cha997281d2020-01-16 15:23:59 +0900792 void Dump(CodeWriter* writer) const override;
Jiyong Park02da7422018-07-16 16:00:26 +0900793
Steven Moreland0cea4aa2020-04-20 21:06:02 -0700794 std::set<AidlAnnotation::Type> GetSupportedAnnotations() const override;
Jeongik Chadb0f59e2018-11-01 18:11:21 +0900795 bool CheckValid(const AidlTypenames& typenames) const override;
Jeongik Cha047c5ee2019-08-07 23:16:49 +0900796 bool LanguageSpecificCheckValid(Options::Language lang) const override;
Jeongik Chadb0f59e2018-11-01 18:11:21 +0900797
Steven Moreland5557f1c2018-07-02 13:50:23 -0700798 private:
799 const std::vector<std::unique_ptr<AidlVariableDeclaration>> variables_;
800
801 DISALLOW_COPY_AND_ASSIGN(AidlStructuredParcelable);
802};
803
Daniel Norman85aed542019-08-21 12:01:14 -0700804class AidlEnumerator : public AidlNode {
805 public:
Daniel Norman2e4112d2019-10-03 10:22:35 -0700806 AidlEnumerator(const AidlLocation& location, const std::string& name, AidlConstantValue* value,
807 const std::string& comments);
Daniel Norman85aed542019-08-21 12:01:14 -0700808 virtual ~AidlEnumerator() = default;
809
810 const std::string& GetName() const { return name_; }
Will McVickerd7d18df2019-09-12 13:40:50 -0700811 AidlConstantValue* GetValue() const { return value_.get(); }
Daniel Norman2e4112d2019-10-03 10:22:35 -0700812 const std::string& GetComments() const { return comments_; }
Daniel Norman85aed542019-08-21 12:01:14 -0700813 bool CheckValid(const AidlTypeSpecifier& enum_backing_type) const;
814
815 string ValueString(const AidlTypeSpecifier& backing_type,
816 const ConstantValueDecorator& decorator) const;
817
Daniel Normanb28684e2019-10-17 15:31:39 -0700818 void SetValue(std::unique_ptr<AidlConstantValue> value) { value_ = std::move(value); }
819
Daniel Norman85aed542019-08-21 12:01:14 -0700820 private:
821 const std::string name_;
Will McVickerd7d18df2019-09-12 13:40:50 -0700822 unique_ptr<AidlConstantValue> value_;
Daniel Norman2e4112d2019-10-03 10:22:35 -0700823 const std::string comments_;
Daniel Norman85aed542019-08-21 12:01:14 -0700824
825 DISALLOW_COPY_AND_ASSIGN(AidlEnumerator);
826};
827
828class AidlEnumDeclaration : public AidlDefinedType {
829 public:
Will McVickerd7d18df2019-09-12 13:40:50 -0700830 AidlEnumDeclaration(const AidlLocation& location, const string& name,
Daniel Norman85aed542019-08-21 12:01:14 -0700831 std::vector<std::unique_ptr<AidlEnumerator>>* enumerators,
Daniel Norman2e4112d2019-10-03 10:22:35 -0700832 const std::vector<std::string>& package, const std::string& comments);
Daniel Norman85aed542019-08-21 12:01:14 -0700833 virtual ~AidlEnumDeclaration() = default;
834
835 void SetBackingType(std::unique_ptr<const AidlTypeSpecifier> type);
836 const AidlTypeSpecifier& GetBackingType() const { return *backing_type_; }
837 const std::vector<std::unique_ptr<AidlEnumerator>>& GetEnumerators() const {
838 return enumerators_;
839 }
Steven Moreland59e53e42019-11-26 20:38:08 -0800840 bool Autofill();
Steven Moreland0cea4aa2020-04-20 21:06:02 -0700841 std::set<AidlAnnotation::Type> GetSupportedAnnotations() const override;
Daniel Norman85aed542019-08-21 12:01:14 -0700842 bool CheckValid(const AidlTypenames& typenames) const override;
843 bool LanguageSpecificCheckValid(Options::Language) const override { return true; }
844 std::string GetPreprocessDeclarationName() const override { return "enum"; }
Jeongik Cha997281d2020-01-16 15:23:59 +0900845 void Dump(CodeWriter* writer) const override;
Daniel Norman85aed542019-08-21 12:01:14 -0700846
847 const AidlEnumDeclaration* AsEnumDeclaration() const override { return this; }
848
849 private:
850 const std::string name_;
851 const std::vector<std::unique_ptr<AidlEnumerator>> enumerators_;
852 std::unique_ptr<const AidlTypeSpecifier> backing_type_;
853
854 DISALLOW_COPY_AND_ASSIGN(AidlEnumDeclaration);
855};
856
Jiyong Park1deecc32018-07-17 01:14:41 +0900857class AidlInterface final : public AidlDefinedType {
Casey Dahlin1ae2bc52015-10-07 18:49:10 -0700858 public:
Steven Moreland46e9da82018-07-27 15:45:29 -0700859 AidlInterface(const AidlLocation& location, const std::string& name, const std::string& comments,
860 bool oneway_, std::vector<std::unique_ptr<AidlMember>>* members,
Christopher Wileyd76067c2015-10-19 17:00:13 -0700861 const std::vector<std::string>& package);
Casey Dahlin1ae2bc52015-10-07 18:49:10 -0700862 virtual ~AidlInterface() = default;
863
Casey Dahlinfb7da2e2015-10-08 17:26:09 -0700864 const std::vector<std::unique_ptr<AidlMethod>>& GetMethods() const
865 { return methods_; }
Jiyong Park309668e2018-07-28 16:55:44 +0900866 std::vector<std::unique_ptr<AidlMethod>>& GetMutableMethods() { return methods_; }
Steven Moreland693640b2018-07-19 13:46:27 -0700867 const std::vector<std::unique_ptr<AidlConstantDeclaration>>& GetConstantDeclarations() const {
868 return constants_;
869 }
Casey Dahlina2f77c42015-12-01 18:26:02 -0800870
Steven Morelandc258abc2018-07-10 14:03:38 -0700871 const AidlInterface* AsInterface() const override { return this; }
Steven Morelanded83a282018-07-17 13:27:29 -0700872 std::string GetPreprocessDeclarationName() const override { return "interface"; }
Steven Moreland5557f1c2018-07-02 13:50:23 -0700873
Jeongik Cha997281d2020-01-16 15:23:59 +0900874 void Dump(CodeWriter* writer) const override;
Jiyong Park02da7422018-07-16 16:00:26 +0900875
Steven Moreland0cea4aa2020-04-20 21:06:02 -0700876 std::set<AidlAnnotation::Type> GetSupportedAnnotations() const override;
Jeongik Chadb0f59e2018-11-01 18:11:21 +0900877 bool CheckValid(const AidlTypenames& typenames) const override;
Jeongik Cha047c5ee2019-08-07 23:16:49 +0900878 bool LanguageSpecificCheckValid(Options::Language lang) const override;
Jeongik Chadb0f59e2018-11-01 18:11:21 +0900879
Casey Dahlin1ae2bc52015-10-07 18:49:10 -0700880 private:
Casey Dahlinfb7da2e2015-10-08 17:26:09 -0700881 std::vector<std::unique_ptr<AidlMethod>> methods_;
Steven Moreland693640b2018-07-19 13:46:27 -0700882 std::vector<std::unique_ptr<AidlConstantDeclaration>> constants_;
Casey Dahlinfb7da2e2015-10-08 17:26:09 -0700883
Casey Dahlin1ae2bc52015-10-07 18:49:10 -0700884 DISALLOW_COPY_AND_ASSIGN(AidlInterface);
Casey Dahlin0a2f8be2015-09-28 16:15:29 -0700885};
Adam Lesinskiffa16862014-01-23 18:17:42 -0800886
Casey Dahlin0edf3422015-10-07 12:34:59 -0700887class AidlImport : public AidlNode {
888 public:
Steven Moreland46e9da82018-07-27 15:45:29 -0700889 AidlImport(const AidlLocation& location, const std::string& needed_class);
Casey Dahlin0edf3422015-10-07 12:34:59 -0700890 virtual ~AidlImport() = default;
891
Casey Dahlin0edf3422015-10-07 12:34:59 -0700892 const std::string& GetFilename() const { return filename_; }
893 const std::string& GetNeededClass() const { return needed_class_; }
Casey Dahlin0edf3422015-10-07 12:34:59 -0700894
895 private:
Casey Dahlin0edf3422015-10-07 12:34:59 -0700896 std::string filename_;
897 std::string needed_class_;
Casey Dahlin0edf3422015-10-07 12:34:59 -0700898
899 DISALLOW_COPY_AND_ASSIGN(AidlImport);
Casey Dahline2507492015-09-14 17:11:20 -0700900};
901
902class Parser {
Casey Dahlindd691812015-09-09 17:59:06 -0700903 public:
Casey Dahline2507492015-09-14 17:11:20 -0700904 ~Parser();
Casey Dahlindd691812015-09-09 17:59:06 -0700905
Steven Moreland64e29be2018-08-08 18:52:19 -0700906 // Parse contents of file |filename|. Should only be called once.
907 static std::unique_ptr<Parser> Parse(const std::string& filename,
908 const android::aidl::IoDelegate& io_delegate,
909 AidlTypenames& typenames);
Casey Dahlin89d44842015-09-24 18:45:54 -0700910
Steven Moreland2ca4fcb2018-06-27 16:01:01 -0700911 void AddError() { error_++; }
Steven Moreland64e29be2018-08-08 18:52:19 -0700912 bool HasError() { return error_ != 0; }
Casey Dahlindd691812015-09-09 17:59:06 -0700913
Casey Dahlin3c6df362015-10-06 15:48:35 -0700914 const std::string& FileName() const { return filename_; }
Casey Dahlin42727f82015-10-12 19:23:40 -0700915 void* Scanner() const { return scanner_; }
Casey Dahlindd691812015-09-09 17:59:06 -0700916
Steven Morelandd1039a92020-01-23 09:49:43 -0800917 void AddImport(std::unique_ptr<AidlImport>&& import);
Christopher Wileyd76067c2015-10-19 17:00:13 -0700918 const std::vector<std::unique_ptr<AidlImport>>& GetImports() {
919 return imports_;
920 }
Casey Dahlindd691812015-09-09 17:59:06 -0700921
Jiyong Parkb034bf02018-07-30 17:44:33 +0900922 void SetPackage(unique_ptr<AidlQualifiedName> name) { package_ = std::move(name); }
923 std::vector<std::string> Package() const;
Jiyong Park1deecc32018-07-17 01:14:41 +0900924
925 void DeferResolution(AidlTypeSpecifier* typespec) {
926 unresolved_typespecs_.emplace_back(typespec);
927 }
928
Jiyong Parke59c3682018-09-11 23:10:25 +0900929 const vector<AidlTypeSpecifier*>& GetUnresolvedTypespecs() const { return unresolved_typespecs_; }
930
Jiyong Park1deecc32018-07-17 01:14:41 +0900931 bool Resolve();
932
Jiyong Parkb034bf02018-07-30 17:44:33 +0900933 void AddDefinedType(unique_ptr<AidlDefinedType> type) {
934 // Parser does NOT own AidlDefinedType, it just has references to the types
935 // that it encountered while parsing the input file.
936 defined_types_.emplace_back(type.get());
937
938 // AidlDefinedType IS owned by AidlTypenames
939 if (!typenames_.AddDefinedType(std::move(type))) {
940 AddError();
941 }
942 }
943
944 vector<AidlDefinedType*>& GetDefinedTypes() { return defined_types_; }
945
Casey Dahlindd691812015-09-09 17:59:06 -0700946 private:
Steven Moreland64e29be2018-08-08 18:52:19 -0700947 explicit Parser(const std::string& filename, std::string& raw_buffer,
948 android::aidl::AidlTypenames& typenames);
949
Casey Dahlindd691812015-09-09 17:59:06 -0700950 std::string filename_;
Christopher Wileyd76067c2015-10-19 17:00:13 -0700951 std::unique_ptr<AidlQualifiedName> package_;
Jiyong Parkb034bf02018-07-30 17:44:33 +0900952 AidlTypenames& typenames_;
Steven Moreland64e29be2018-08-08 18:52:19 -0700953
954 void* scanner_ = nullptr;
955 YY_BUFFER_STATE buffer_;
956 int error_ = 0;
957
958 std::vector<std::unique_ptr<AidlImport>> imports_;
Jiyong Parkb034bf02018-07-30 17:44:33 +0900959 vector<AidlDefinedType*> defined_types_;
Jiyong Park1deecc32018-07-17 01:14:41 +0900960 vector<AidlTypeSpecifier*> unresolved_typespecs_;
Casey Dahlindd691812015-09-09 17:59:06 -0700961
Casey Dahline2507492015-09-14 17:11:20 -0700962 DISALLOW_COPY_AND_ASSIGN(Parser);
Casey Dahlindd691812015-09-09 17:59:06 -0700963};