blob: 7a40427805e74a6854dee2edb22b8ecf60233d05 [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
Steven Moreland46e9da82018-07-27 15:45:29 -070077 AidlLocation(const std::string& file, Point begin, Point end);
78
Steven Moreland46e9da82018-07-27 15:45:29 -070079 friend std::ostream& operator<<(std::ostream& os, const AidlLocation& l);
Andrei Onea8714b022019-02-01 18:55:54 +000080 friend class AidlNode;
Casey Dahlinbc7a50a2015-09-28 19:20:50 -070081
82 private:
Steven Moreland46e9da82018-07-27 15:45:29 -070083 const std::string file_;
84 Point begin_;
85 Point end_;
86};
87
Steven Moreland02e012e2018-08-02 14:58:10 -070088#define AIDL_LOCATION_HERE \
89 AidlLocation { \
90 __FILE__, {__LINE__, 0}, { __LINE__, 0 } \
91 }
92
Steven Moreland46e9da82018-07-27 15:45:29 -070093std::ostream& operator<<(std::ostream& os, const AidlLocation& l);
94
95// Anything that is locatable in a .aidl file.
96class AidlNode {
97 public:
98 AidlNode(const AidlLocation& location);
Steven Moreland3f658cf2018-08-20 13:40:54 -070099
100 AidlNode(const AidlNode&) = default;
Steven Moreland3be75772018-08-20 13:27:43 -0700101 AidlNode(AidlNode&&) = default;
Steven Moreland46e9da82018-07-27 15:45:29 -0700102 virtual ~AidlNode() = default;
103
Steven Moreland46e9da82018-07-27 15:45:29 -0700104 // DO NOT ADD. This is intentionally omitted. Nothing should refer to the location
105 // for a functional purpose. It is only for error messages.
106 // NO const AidlLocation& GetLocation() const { return location_; } NO
107
108 // To be able to print AidlLocation (nothing else should use this information)
109 friend class AidlError;
Andrei Onea8714b022019-02-01 18:55:54 +0000110 friend std::string android::aidl::mappings::dump_location(const AidlNode&);
Mathew Inwoodadb74672019-11-29 14:01:53 +0000111 friend std::string android::aidl::java::dump_location(const AidlNode&);
Steven Moreland46e9da82018-07-27 15:45:29 -0700112
113 private:
Mathew Inwoodadb74672019-11-29 14:01:53 +0000114 std::string PrintLine() const;
Andrei Onea8714b022019-02-01 18:55:54 +0000115 std::string PrintLocation() const;
Steven Moreland46e9da82018-07-27 15:45:29 -0700116 const AidlLocation location_;
Casey Dahlinbc7a50a2015-09-28 19:20:50 -0700117};
118
Steven Moreland46e9da82018-07-27 15:45:29 -0700119// Generic point for printing any error in the AIDL compiler.
120class AidlError {
121 public:
Steven Moreland92c55f12018-07-31 14:08:37 -0700122 AidlError(bool fatal, const std::string& filename) : AidlError(fatal) { os_ << filename << ": "; }
123 AidlError(bool fatal, const AidlLocation& location) : AidlError(fatal) {
Steven Moreland46e9da82018-07-27 15:45:29 -0700124 os_ << location << ": ";
125 }
Steven Moreland92c55f12018-07-31 14:08:37 -0700126 AidlError(bool fatal, const AidlNode& node) : AidlError(fatal, node.location_) {}
127 AidlError(bool fatal, const AidlNode* node) : AidlError(fatal, *node) {}
128
129 template <typename T>
130 AidlError(bool fatal, const std::unique_ptr<T>& node) : AidlError(fatal, *node) {}
Steven Moreland46e9da82018-07-27 15:45:29 -0700131 ~AidlError() {
132 os_ << std::endl;
133 if (fatal_) abort();
134 }
135
136 std::ostream& os_;
137
Steven Morelandfdb57cd2020-01-08 20:03:30 -0800138 static bool hadError() { return sHadError; }
139
Steven Moreland46e9da82018-07-27 15:45:29 -0700140 private:
Steven Moreland92c55f12018-07-31 14:08:37 -0700141 AidlError(bool fatal);
Steven Moreland46e9da82018-07-27 15:45:29 -0700142
143 bool fatal_;
144
Steven Morelandfdb57cd2020-01-08 20:03:30 -0800145 static bool sHadError;
146
Steven Moreland46e9da82018-07-27 15:45:29 -0700147 DISALLOW_COPY_AND_ASSIGN(AidlError);
148};
149
Steven Moreland92c55f12018-07-31 14:08:37 -0700150#define AIDL_ERROR(CONTEXT) ::AidlError(false /*fatal*/, (CONTEXT)).os_
151#define AIDL_FATAL(CONTEXT) ::AidlError(true /*fatal*/, (CONTEXT)).os_
Steven Moreland3f658cf2018-08-20 13:40:54 -0700152#define AIDL_FATAL_IF(CONDITION, CONTEXT) \
153 if (CONDITION) AIDL_FATAL(CONTEXT) << "Bad internal state: " << #CONDITION << ": "
Steven Moreland46e9da82018-07-27 15:45:29 -0700154
Casey Dahlina2f77c42015-12-01 18:26:02 -0800155namespace android {
156namespace aidl {
157
Jiyong Park1deecc32018-07-17 01:14:41 +0900158class AidlTypenames;
Casey Dahlina2f77c42015-12-01 18:26:02 -0800159
160} // namespace aidl
161} // namespace android
162
Jeongik Chadf76dc72019-11-28 00:08:47 +0900163// unique_ptr<AidlTypeSpecifier> for type arugment,
164// std::string for type parameter(T, U, and so on).
165template <typename T>
166class AidlParameterizable {
167 public:
168 AidlParameterizable(std::vector<T>* type_params) : type_params_(type_params) {}
169 virtual ~AidlParameterizable() = default;
170 bool IsGeneric() const { return type_params_ != nullptr; }
171 const std::vector<T>& GetTypeParameters() const { return *type_params_; }
172 bool CheckValid() const;
173
174 virtual const AidlNode& AsAidlNode() const = 0;
175
176 protected:
177 AidlParameterizable(const AidlParameterizable&);
178
179 private:
180 const unique_ptr<std::vector<T>> type_params_;
181 static_assert(std::is_same<T, unique_ptr<AidlTypeSpecifier>>::value ||
182 std::is_same<T, std::string>::value);
183};
184template <>
185bool AidlParameterizable<std::string>::CheckValid() const;
186
Andrei Onea9445fc62019-06-27 18:11:59 +0100187class AidlConstantValue;
188class AidlConstantDeclaration;
189
190// Transforms a value string into a language specific form. Raw value as produced by
191// AidlConstantValue.
192using ConstantValueDecorator =
193 std::function<std::string(const AidlTypeSpecifier& type, const std::string& raw_value)>;
194
Jiyong Park68bc77a2018-07-19 19:00:45 +0900195class AidlAnnotation : public AidlNode {
196 public:
Andrei Onea9445fc62019-06-27 18:11:59 +0100197 static AidlAnnotation* Parse(
198 const AidlLocation& location, const string& name,
199 std::map<std::string, std::shared_ptr<AidlConstantValue>>* parameter_list);
Steven Moreland46e9da82018-07-27 15:45:29 -0700200
Steven Moreland3f658cf2018-08-20 13:40:54 -0700201 AidlAnnotation(const AidlAnnotation&) = default;
Steven Moreland3be75772018-08-20 13:27:43 -0700202 AidlAnnotation(AidlAnnotation&&) = default;
Jiyong Park68bc77a2018-07-19 19:00:45 +0900203 virtual ~AidlAnnotation() = default;
Andrei Onea9445fc62019-06-27 18:11:59 +0100204 bool CheckValid() const;
Steven Moreland3be75772018-08-20 13:27:43 -0700205
Jiyong Park68bc77a2018-07-19 19:00:45 +0900206 const string& GetName() const { return name_; }
Daniel Norman37d43dd2019-09-09 17:22:34 -0700207 string ToString(const ConstantValueDecorator& decorator) const;
Andrei Onea9445fc62019-06-27 18:11:59 +0100208 std::map<std::string, std::string> AnnotationParams(
209 const ConstantValueDecorator& decorator) const;
Jiyong Parka6605ab2018-11-11 14:30:21 +0900210 const string& GetComments() const { return comments_; }
211 void SetComments(const string& comments) { comments_ = comments; }
Jiyong Park68bc77a2018-07-19 19:00:45 +0900212
213 private:
Steven Moreland46e9da82018-07-27 15:45:29 -0700214 AidlAnnotation(const AidlLocation& location, const string& name);
Andrei Onea9445fc62019-06-27 18:11:59 +0100215 AidlAnnotation(const AidlLocation& location, const string& name,
216 std::map<std::string, std::shared_ptr<AidlConstantValue>>&& parameters);
Jiyong Park68bc77a2018-07-19 19:00:45 +0900217 const string name_;
Jiyong Parka6605ab2018-11-11 14:30:21 +0900218 string comments_;
Andrei Onea9445fc62019-06-27 18:11:59 +0100219 std::map<std::string, std::shared_ptr<AidlConstantValue>> parameters_;
Jiyong Park68bc77a2018-07-19 19:00:45 +0900220};
221
Steven Moreland3be75772018-08-20 13:27:43 -0700222static inline bool operator<(const AidlAnnotation& lhs, const AidlAnnotation& rhs) {
223 return lhs.GetName() < rhs.GetName();
224}
225static inline bool operator==(const AidlAnnotation& lhs, const AidlAnnotation& rhs) {
226 return lhs.GetName() == rhs.GetName();
227}
Jiyong Park3656c3c2018-08-01 20:02:01 +0900228
Casey Dahline7922932016-02-29 17:23:01 -0800229class AidlAnnotatable : public AidlNode {
Casey Dahlin0ee37582015-09-30 16:31:55 -0700230 public:
Steven Moreland46e9da82018-07-27 15:45:29 -0700231 AidlAnnotatable(const AidlLocation& location);
Steven Moreland3f658cf2018-08-20 13:40:54 -0700232
233 AidlAnnotatable(const AidlAnnotatable&) = default;
234 AidlAnnotatable(AidlAnnotatable&&) = default;
Casey Dahline7922932016-02-29 17:23:01 -0800235 virtual ~AidlAnnotatable() = default;
236
Artur Satayev91fe8712019-07-29 13:06:01 +0100237 void Annotate(vector<AidlAnnotation>&& annotations) {
238 for (auto& annotation : annotations) {
239 annotations_.emplace_back(std::move(annotation));
240 }
241 }
Jiyong Park68bc77a2018-07-19 19:00:45 +0900242 bool IsNullable() const;
Jiyong Park68bc77a2018-07-19 19:00:45 +0900243 bool IsUtf8InCpp() const;
Steven Morelanda57d0a62019-07-30 09:41:14 -0700244 bool IsVintfStability() const;
Jeongik Cha88f95a82020-01-15 13:02:16 +0900245 bool IsStableApiParcelable(Options::Language lang) const;
Makoto Onuki00be5c72020-03-04 16:57:23 -0800246 bool IsHide() const;
Andrei Onea9445fc62019-06-27 18:11:59 +0100247
Steven Moreland71998bf2020-02-20 18:10:42 -0800248 void DumpAnnotations(CodeWriter* writer) const;
249
Andrei Onea9445fc62019-06-27 18:11:59 +0100250 const AidlAnnotation* UnsupportedAppUsage() const;
Daniel Norman716d3112019-09-10 13:11:56 -0700251 const AidlTypeSpecifier* BackingType(const AidlTypenames& typenames) const;
Jiyong Park68bc77a2018-07-19 19:00:45 +0900252 std::string ToString() const;
Casey Dahline7922932016-02-29 17:23:01 -0800253
Jiyong Parka6605ab2018-11-11 14:30:21 +0900254 const vector<AidlAnnotation>& GetAnnotations() const { return annotations_; }
Andrei Onea9445fc62019-06-27 18:11:59 +0100255 bool CheckValidAnnotations() const;
Jiyong Park3656c3c2018-08-01 20:02:01 +0900256
Casey Dahline7922932016-02-29 17:23:01 -0800257 private:
Jiyong Parka6605ab2018-11-11 14:30:21 +0900258 vector<AidlAnnotation> annotations_;
Casey Dahline7922932016-02-29 17:23:01 -0800259};
260
Jiyong Park1deecc32018-07-17 01:14:41 +0900261class AidlQualifiedName;
262
263// AidlTypeSpecifier represents a reference to either a built-in type,
264// a defined type, or a variant (e.g., array of generic) of a type.
Jeongik Chadf76dc72019-11-28 00:08:47 +0900265class AidlTypeSpecifier final : public AidlAnnotatable,
266 public AidlParameterizable<unique_ptr<AidlTypeSpecifier>> {
Casey Dahline7922932016-02-29 17:23:01 -0800267 public:
Steven Moreland46e9da82018-07-27 15:45:29 -0700268 AidlTypeSpecifier(const AidlLocation& location, const string& unresolved_name, bool is_array,
269 vector<unique_ptr<AidlTypeSpecifier>>* type_params, const string& comments);
Jiyong Parkd59a10d2018-07-18 11:12:55 +0900270 virtual ~AidlTypeSpecifier() = default;
Casey Dahlin0ee37582015-09-30 16:31:55 -0700271
Steven Moreland3f658cf2018-08-20 13:40:54 -0700272 // Copy of this type which is not an array.
273 AidlTypeSpecifier ArrayBase() const;
274
Jiyong Park1deecc32018-07-17 01:14:41 +0900275 // Returns the full-qualified name of the base type.
276 // int -> int
277 // int[] -> int
278 // List<String> -> List
279 // IFoo -> foo.bar.IFoo (if IFoo is in package foo.bar)
280 const string& GetName() const {
281 if (IsResolved()) {
282 return fully_qualified_name_;
283 } else {
284 return GetUnresolvedName();
285 }
286 }
Casey Dahlin0ee37582015-09-30 16:31:55 -0700287
Jiyong Park1deecc32018-07-17 01:14:41 +0900288 // Returns string representation of this type specifier.
Artur Satayev91fe8712019-07-29 13:06:01 +0100289 // This is GetBaseTypeName() + array modifier or generic type parameters
Jiyong Park1deecc32018-07-17 01:14:41 +0900290 string ToString() const;
291
Jiyong Park02da7422018-07-16 16:00:26 +0900292 std::string Signature() const;
293
Jiyong Park1deecc32018-07-17 01:14:41 +0900294 const string& GetUnresolvedName() const { return unresolved_name_; }
295
Jeongik Cha997281d2020-01-16 15:23:59 +0900296 bool IsHidden() const;
297
Jiyong Park1deecc32018-07-17 01:14:41 +0900298 const string& GetComments() const { return comments_; }
299
Jeongik Cha1a7ab642019-07-29 17:31:02 +0900300 const std::vector<std::string> GetSplitName() const { return split_name_; }
301
Jiyong Parka6605ab2018-11-11 14:30:21 +0900302 void SetComments(const string& comment) { comments_ = comment; }
303
Jiyong Park1deecc32018-07-17 01:14:41 +0900304 bool IsResolved() const { return fully_qualified_name_ != ""; }
305
306 bool IsArray() const { return is_array_; }
307
Jiyong Park1deecc32018-07-17 01:14:41 +0900308 // Resolve the base type name to a fully-qualified name. Return false if the
309 // resolution fails.
Daniel Norman716d3112019-09-10 13:11:56 -0700310 bool Resolve(const AidlTypenames& typenames);
Casey Dahlin0ee37582015-09-30 16:31:55 -0700311
Jeongik Chadb0f59e2018-11-01 18:11:21 +0900312 bool CheckValid(const AidlTypenames& typenames) const;
Jeongik Cha047c5ee2019-08-07 23:16:49 +0900313 bool LanguageSpecificCheckValid(Options::Language lang) const;
Jeongik Chadf76dc72019-11-28 00:08:47 +0900314 const AidlNode& AsAidlNode() const override { return *this; }
Jiyong Park1d2df7d2018-07-23 15:22:50 +0900315
Casey Dahlin0ee37582015-09-30 16:31:55 -0700316 private:
Steven Moreland3f658cf2018-08-20 13:40:54 -0700317 AidlTypeSpecifier(const AidlTypeSpecifier&) = default;
318
Jiyong Park1deecc32018-07-17 01:14:41 +0900319 const string unresolved_name_;
320 string fully_qualified_name_;
Steven Moreland3f658cf2018-08-20 13:40:54 -0700321 bool is_array_;
Jiyong Parka6605ab2018-11-11 14:30:21 +0900322 string comments_;
Jeongik Cha1a7ab642019-07-29 17:31:02 +0900323 vector<string> split_name_;
Casey Dahlin0ee37582015-09-30 16:31:55 -0700324};
325
Steven Moreland860b1942018-08-16 14:59:28 -0700326// Returns the universal value unaltered.
327std::string AidlConstantValueDecorator(const AidlTypeSpecifier& type, const std::string& raw_value);
328
Steven Moreland9ea10e32018-07-19 15:26:09 -0700329class AidlConstantValue;
Steven Moreland5557f1c2018-07-02 13:50:23 -0700330class AidlVariableDeclaration : public AidlNode {
331 public:
Steven Moreland46e9da82018-07-27 15:45:29 -0700332 AidlVariableDeclaration(const AidlLocation& location, AidlTypeSpecifier* type,
333 const std::string& name);
334 AidlVariableDeclaration(const AidlLocation& location, AidlTypeSpecifier* type,
335 const std::string& name, AidlConstantValue* default_value);
Steven Moreland5557f1c2018-07-02 13:50:23 -0700336 virtual ~AidlVariableDeclaration() = default;
337
338 std::string GetName() const { return name_; }
Jiyong Parkd59a10d2018-07-18 11:12:55 +0900339 const AidlTypeSpecifier& GetType() const { return *type_; }
Steven Moreland9ea10e32018-07-19 15:26:09 -0700340 const AidlConstantValue* GetDefaultValue() const { return default_value_.get(); }
341
Jiyong Parkd59a10d2018-07-18 11:12:55 +0900342 AidlTypeSpecifier* GetMutableType() { return type_.get(); }
Steven Moreland5557f1c2018-07-02 13:50:23 -0700343
Jeongik Chadb0f59e2018-11-01 18:11:21 +0900344 bool CheckValid(const AidlTypenames& typenames) const;
Steven Moreland5557f1c2018-07-02 13:50:23 -0700345 std::string ToString() const;
Jiyong Park02da7422018-07-16 16:00:26 +0900346 std::string Signature() const;
Steven Moreland5557f1c2018-07-02 13:50:23 -0700347
Steven Moreland860b1942018-08-16 14:59:28 -0700348 std::string ValueString(const ConstantValueDecorator& decorator) const;
Steven Moreland25294322018-08-07 18:13:55 -0700349
Steven Moreland5557f1c2018-07-02 13:50:23 -0700350 private:
Jiyong Parkd59a10d2018-07-18 11:12:55 +0900351 std::unique_ptr<AidlTypeSpecifier> type_;
Steven Moreland5557f1c2018-07-02 13:50:23 -0700352 std::string name_;
Steven Moreland9ea10e32018-07-19 15:26:09 -0700353 std::unique_ptr<AidlConstantValue> default_value_;
Steven Moreland5557f1c2018-07-02 13:50:23 -0700354
355 DISALLOW_COPY_AND_ASSIGN(AidlVariableDeclaration);
356};
357
358class AidlArgument : public AidlVariableDeclaration {
Casey Dahlinbc7a50a2015-09-28 19:20:50 -0700359 public:
Casey Dahlinc378c992015-09-29 16:50:40 -0700360 enum Direction { IN_DIR = 1, OUT_DIR = 2, INOUT_DIR = 3 };
361
Steven Moreland46e9da82018-07-27 15:45:29 -0700362 AidlArgument(const AidlLocation& location, AidlArgument::Direction direction,
363 AidlTypeSpecifier* type, const std::string& name);
364 AidlArgument(const AidlLocation& location, AidlTypeSpecifier* type, const std::string& name);
Casey Dahlinbc7a50a2015-09-28 19:20:50 -0700365 virtual ~AidlArgument() = default;
366
Casey Dahlinc378c992015-09-29 16:50:40 -0700367 Direction GetDirection() const { return direction_; }
Christopher Wileyad339272015-10-05 19:11:58 -0700368 bool IsOut() const { return direction_ & OUT_DIR; }
369 bool IsIn() const { return direction_ & IN_DIR; }
Casey Dahlinc378c992015-09-29 16:50:40 -0700370 bool DirectionWasSpecified() const { return direction_specified_; }
Jiyong Park3656c3c2018-08-01 20:02:01 +0900371 string GetDirectionSpecifier() const;
Christopher Wileyad339272015-10-05 19:11:58 -0700372
Casey Dahlinc378c992015-09-29 16:50:40 -0700373 std::string ToString() const;
Jiyong Park02da7422018-07-16 16:00:26 +0900374 std::string Signature() const;
Casey Dahlinc378c992015-09-29 16:50:40 -0700375
Casey Dahlinbc7a50a2015-09-28 19:20:50 -0700376 private:
Casey Dahlinc378c992015-09-29 16:50:40 -0700377 Direction direction_;
378 bool direction_specified_;
379
Casey Dahlinbc7a50a2015-09-28 19:20:50 -0700380 DISALLOW_COPY_AND_ASSIGN(AidlArgument);
Casey Dahlina834dd42015-09-23 11:52:15 -0700381};
Adam Lesinskiffa16862014-01-23 18:17:42 -0800382
Casey Dahlind40e2fe2015-11-24 14:06:52 -0800383class AidlMethod;
Steven Moreland693640b2018-07-19 13:46:27 -0700384class AidlConstantDeclaration;
Daniel Norman85aed542019-08-21 12:01:14 -0700385class AidlEnumDeclaration;
Casey Dahlind40e2fe2015-11-24 14:06:52 -0800386class AidlMember : public AidlNode {
387 public:
Steven Moreland46e9da82018-07-27 15:45:29 -0700388 AidlMember(const AidlLocation& location);
Casey Dahlind40e2fe2015-11-24 14:06:52 -0800389 virtual ~AidlMember() = default;
390
391 virtual AidlMethod* AsMethod() { return nullptr; }
Steven Moreland693640b2018-07-19 13:46:27 -0700392 virtual AidlConstantDeclaration* AsConstantDeclaration() { return nullptr; }
Casey Dahlind40e2fe2015-11-24 14:06:52 -0800393
394 private:
395 DISALLOW_COPY_AND_ASSIGN(AidlMember);
396};
397
Will McVickerd7d18df2019-09-12 13:40:50 -0700398class AidlUnaryConstExpression;
399class AidlBinaryConstExpression;
400
Steven Moreland693640b2018-07-19 13:46:27 -0700401class AidlConstantValue : public AidlNode {
Casey Dahlind40e2fe2015-11-24 14:06:52 -0800402 public:
Will McVickerd7d18df2019-09-12 13:40:50 -0700403 enum class Type {
404 // WARNING: Don't change this order! The order is used to determine type
405 // promotion during a binary expression.
406 BOOLEAN,
407 INT8,
408 INT32,
409 INT64,
410 ARRAY,
411 CHARACTER,
412 STRING,
413 FLOATING,
414 UNARY,
415 BINARY,
416 ERROR,
417 };
418
419 /*
420 * Return the value casted to the given type.
421 */
422 template <typename T>
423 T cast() const;
Casey Dahlind40e2fe2015-11-24 14:06:52 -0800424
Steven Moreland693640b2018-07-19 13:46:27 -0700425 virtual ~AidlConstantValue() = default;
Casey Dahlind40e2fe2015-11-24 14:06:52 -0800426
Steven Moreland25294322018-08-07 18:13:55 -0700427 static AidlConstantValue* Boolean(const AidlLocation& location, bool value);
428 static AidlConstantValue* Character(const AidlLocation& location, char value);
Steven Moreland25294322018-08-07 18:13:55 -0700429 // example: 123, -5498, maybe any size
Will McVickerd7d18df2019-09-12 13:40:50 -0700430 static AidlConstantValue* Integral(const AidlLocation& location, const string& value);
431 static AidlConstantValue* Floating(const AidlLocation& location, const std::string& value);
Steven Moreland860b1942018-08-16 14:59:28 -0700432 static AidlConstantValue* Array(const AidlLocation& location,
Will McVickerd7d18df2019-09-12 13:40:50 -0700433 std::unique_ptr<vector<unique_ptr<AidlConstantValue>>> values);
Steven Moreland693640b2018-07-19 13:46:27 -0700434 // example: "\"asdf\""
Will McVickerd7d18df2019-09-12 13:40:50 -0700435 static AidlConstantValue* String(const AidlLocation& location, const string& value);
Steven Moreland693640b2018-07-19 13:46:27 -0700436
Daniel Normanf0ca44f2019-10-25 09:59:44 -0700437 // Construct an AidlConstantValue by evaluating the other integral constant's
438 // value string. This does not preserve the structure of the copied constant.
Steven Moreland59e53e42019-11-26 20:38:08 -0800439 // Returns nullptr and logs if value cannot be copied.
Daniel Normanf0ca44f2019-10-25 09:59:44 -0700440 static AidlConstantValue* ShallowIntegralCopy(const AidlConstantValue& other);
Daniel Normanb28684e2019-10-17 15:31:39 -0700441
Will McVickerd7d18df2019-09-12 13:40:50 -0700442 Type GetType() const { return final_type_; }
Steven Moreland25294322018-08-07 18:13:55 -0700443
Will McVickerd7d18df2019-09-12 13:40:50 -0700444 virtual bool CheckValid() const;
Steven Moreland860b1942018-08-16 14:59:28 -0700445
446 // Raw value of type (currently valid in C++ and Java). Empty string on error.
Steven Moreland4bcb05c2019-11-27 18:57:47 -0800447 string ValueString(const AidlTypeSpecifier& type, const ConstantValueDecorator& decorator) const;
Casey Dahlind40e2fe2015-11-24 14:06:52 -0800448
449 private:
Will McVickerd7d18df2019-09-12 13:40:50 -0700450 AidlConstantValue(const AidlLocation& location, Type parsed_type, int64_t parsed_value,
451 const string& checked_value);
452 AidlConstantValue(const AidlLocation& location, Type type, const string& checked_value);
Steven Moreland860b1942018-08-16 14:59:28 -0700453 AidlConstantValue(const AidlLocation& location, Type type,
Will McVickerd7d18df2019-09-12 13:40:50 -0700454 std::unique_ptr<vector<unique_ptr<AidlConstantValue>>> values);
Steven Moreland25294322018-08-07 18:13:55 -0700455 static string ToString(Type type);
Will McVickerd7d18df2019-09-12 13:40:50 -0700456 static bool ParseIntegral(const string& value, int64_t* parsed_value, Type* parsed_type);
457 static bool IsHex(const string& value);
Steven Moreland4bcb05c2019-11-27 18:57:47 -0800458
Will McVickerd7d18df2019-09-12 13:40:50 -0700459 virtual bool evaluate(const AidlTypeSpecifier& type) const;
Casey Dahlind40e2fe2015-11-24 14:06:52 -0800460
Steven Moreland693640b2018-07-19 13:46:27 -0700461 const Type type_ = Type::ERROR;
Will McVickerd7d18df2019-09-12 13:40:50 -0700462 const vector<unique_ptr<AidlConstantValue>> values_; // if type_ == ARRAY
463 const string value_; // otherwise
464
465 // State for tracking evaluation of expressions
Steven Moreland4bcb05c2019-11-27 18:57:47 -0800466 mutable bool is_valid_ = false; // cache of CheckValid, but may be marked false in evaluate
467 mutable bool is_evaluated_ = false; // whether evaluate has been called
Will McVickerd7d18df2019-09-12 13:40:50 -0700468 mutable Type final_type_;
469 mutable int64_t final_value_;
470 mutable string final_string_value_ = "";
Steven Moreland693640b2018-07-19 13:46:27 -0700471
472 DISALLOW_COPY_AND_ASSIGN(AidlConstantValue);
Will McVickerd7d18df2019-09-12 13:40:50 -0700473
474 friend AidlUnaryConstExpression;
475 friend AidlBinaryConstExpression;
476};
477
478class AidlUnaryConstExpression : public AidlConstantValue {
479 public:
480 AidlUnaryConstExpression(const AidlLocation& location, const string& op,
481 std::unique_ptr<AidlConstantValue> rval);
482
483 static bool IsCompatibleType(Type type, const string& op);
484 bool CheckValid() const override;
Will McVickerd7d18df2019-09-12 13:40:50 -0700485 private:
486 bool evaluate(const AidlTypeSpecifier& type) const override;
487
488 std::unique_ptr<AidlConstantValue> unary_;
489 const string op_;
490};
491
492class AidlBinaryConstExpression : public AidlConstantValue {
493 public:
494 AidlBinaryConstExpression(const AidlLocation& location, std::unique_ptr<AidlConstantValue> lval,
495 const string& op, std::unique_ptr<AidlConstantValue> rval);
496
497 bool CheckValid() const override;
Will McVickerd7d18df2019-09-12 13:40:50 -0700498
499 static bool AreCompatibleTypes(Type t1, Type t2);
500 // Returns the promoted kind for both operands
501 static Type UsualArithmeticConversion(Type left, Type right);
502 // Returns the promoted integral type where INT32 is the smallest type
503 static Type IntegralPromotion(Type in);
504
505 private:
506 bool evaluate(const AidlTypeSpecifier& type) const override;
507
508 std::unique_ptr<AidlConstantValue> left_val_;
509 std::unique_ptr<AidlConstantValue> right_val_;
510 const string op_;
Christopher Wileyd6bdd8d2016-05-03 11:23:13 -0700511};
512
Andrei Onea9445fc62019-06-27 18:11:59 +0100513struct AidlAnnotationParameter {
514 std::string name;
515 std::unique_ptr<AidlConstantValue> value;
516};
517
Steven Moreland693640b2018-07-19 13:46:27 -0700518class AidlConstantDeclaration : public AidlMember {
Christopher Wileyd6bdd8d2016-05-03 11:23:13 -0700519 public:
Steven Moreland46e9da82018-07-27 15:45:29 -0700520 AidlConstantDeclaration(const AidlLocation& location, AidlTypeSpecifier* specifier,
Will McVickerd7d18df2019-09-12 13:40:50 -0700521 const string& name, AidlConstantValue* value);
Steven Moreland693640b2018-07-19 13:46:27 -0700522 virtual ~AidlConstantDeclaration() = default;
Christopher Wileyd6bdd8d2016-05-03 11:23:13 -0700523
Steven Moreland693640b2018-07-19 13:46:27 -0700524 const AidlTypeSpecifier& GetType() const { return *type_; }
Steven Moreland4d12f9a2018-10-31 14:30:55 -0700525 AidlTypeSpecifier* GetMutableType() { return type_.get(); }
Will McVickerd7d18df2019-09-12 13:40:50 -0700526 const string& GetName() const { return name_; }
Steven Moreland693640b2018-07-19 13:46:27 -0700527 const AidlConstantValue& GetValue() const { return *value_; }
Jeongik Chadb0f59e2018-11-01 18:11:21 +0900528 bool CheckValid(const AidlTypenames& typenames) const;
Christopher Wileyd6bdd8d2016-05-03 11:23:13 -0700529
Will McVickerd7d18df2019-09-12 13:40:50 -0700530 string ToString() const;
531 string Signature() const;
Steven Moreland860b1942018-08-16 14:59:28 -0700532 string ValueString(const ConstantValueDecorator& decorator) const {
Will McVickerd7d18df2019-09-12 13:40:50 -0700533 return value_->ValueString(GetType(), decorator);
Steven Moreland860b1942018-08-16 14:59:28 -0700534 }
Steven Moreland25294322018-08-07 18:13:55 -0700535
Steven Moreland693640b2018-07-19 13:46:27 -0700536 AidlConstantDeclaration* AsConstantDeclaration() override { return this; }
Christopher Wileyd6bdd8d2016-05-03 11:23:13 -0700537
538 private:
Steven Moreland693640b2018-07-19 13:46:27 -0700539 const unique_ptr<AidlTypeSpecifier> type_;
Will McVickerd7d18df2019-09-12 13:40:50 -0700540 const string name_;
541 unique_ptr<AidlConstantValue> value_;
Christopher Wileyd6bdd8d2016-05-03 11:23:13 -0700542
Steven Moreland693640b2018-07-19 13:46:27 -0700543 DISALLOW_COPY_AND_ASSIGN(AidlConstantDeclaration);
Casey Dahlind40e2fe2015-11-24 14:06:52 -0800544};
545
546class AidlMethod : public AidlMember {
Casey Dahlin5c69deb2015-10-01 14:44:12 -0700547 public:
Will McVickerd7d18df2019-09-12 13:40:50 -0700548 AidlMethod(const AidlLocation& location, bool oneway, AidlTypeSpecifier* type, const string& name,
549 vector<unique_ptr<AidlArgument>>* args, const string& comments);
550 AidlMethod(const AidlLocation& location, bool oneway, AidlTypeSpecifier* type, const string& name,
551 vector<unique_ptr<AidlArgument>>* args, const string& comments, int id,
552 bool is_user_defined = true);
Casey Dahlin5c69deb2015-10-01 14:44:12 -0700553 virtual ~AidlMethod() = default;
554
Casey Dahlind40e2fe2015-11-24 14:06:52 -0800555 AidlMethod* AsMethod() override { return this; }
Jeongik Cha997281d2020-01-16 15:23:59 +0900556 bool IsHidden() const;
Will McVickerd7d18df2019-09-12 13:40:50 -0700557 const string& GetComments() const { return comments_; }
Jiyong Parkd59a10d2018-07-18 11:12:55 +0900558 const AidlTypeSpecifier& GetType() const { return *type_; }
559 AidlTypeSpecifier* GetMutableType() { return type_.get(); }
Steven Morelandacd53472018-12-14 10:17:26 -0800560
Steven Moreland8c70ba92018-12-17 10:20:31 -0800561 // set if this method is part of an interface that is marked oneway
562 void ApplyInterfaceOneway(bool oneway) { oneway_ = oneway_ || oneway; }
Casey Dahlinf4a93112015-10-05 16:58:09 -0700563 bool IsOneway() const { return oneway_; }
Steven Morelandacd53472018-12-14 10:17:26 -0800564
Casey Dahlinf4a93112015-10-05 16:58:09 -0700565 const std::string& GetName() const { return name_; }
Casey Dahlinf4a93112015-10-05 16:58:09 -0700566 bool HasId() const { return has_id_; }
Jiyong Parked65bf42018-08-28 15:43:27 +0900567 int GetId() const { return id_; }
Casey Dahlinf4a93112015-10-05 16:58:09 -0700568 void SetId(unsigned id) { id_ = id; }
Casey Dahlinf2d23f72015-10-02 16:19:19 -0700569
Jiyong Park309668e2018-07-28 16:55:44 +0900570 bool IsUserDefined() const { return is_user_defined_; }
571
Casey Dahlinf4a93112015-10-05 16:58:09 -0700572 const std::vector<std::unique_ptr<AidlArgument>>& GetArguments() const {
Christopher Wileyad339272015-10-05 19:11:58 -0700573 return arguments_;
574 }
575 // An inout parameter will appear in both GetInArguments()
576 // and GetOutArguments(). AidlMethod retains ownership of the argument
577 // pointers returned in this way.
578 const std::vector<const AidlArgument*>& GetInArguments() const {
579 return in_arguments_;
580 }
581 const std::vector<const AidlArgument*>& GetOutArguments() const {
582 return out_arguments_;
Casey Dahlinf4a93112015-10-05 16:58:09 -0700583 }
Casey Dahlin5c69deb2015-10-01 14:44:12 -0700584
Jiyong Park309668e2018-07-28 16:55:44 +0900585 // name + type parameter types
586 // i.e, foo(int, String)
Jiyong Park02da7422018-07-16 16:00:26 +0900587 std::string Signature() const;
588
Jiyong Park309668e2018-07-28 16:55:44 +0900589 // return type + name + type parameter types + annotations
590 // i.e, boolean foo(int, @Nullable String)
591 std::string ToString() const;
592
Casey Dahlin5c69deb2015-10-01 14:44:12 -0700593 private:
Casey Dahlinf4a93112015-10-05 16:58:09 -0700594 bool oneway_;
Casey Dahlinf2d23f72015-10-02 16:19:19 -0700595 std::string comments_;
Jiyong Parkd59a10d2018-07-18 11:12:55 +0900596 std::unique_ptr<AidlTypeSpecifier> type_;
Casey Dahlinf4a93112015-10-05 16:58:09 -0700597 std::string name_;
Christopher Wileyad339272015-10-05 19:11:58 -0700598 const std::vector<std::unique_ptr<AidlArgument>> arguments_;
599 std::vector<const AidlArgument*> in_arguments_;
600 std::vector<const AidlArgument*> out_arguments_;
Casey Dahlinf4a93112015-10-05 16:58:09 -0700601 bool has_id_;
602 int id_;
Jiyong Park309668e2018-07-28 16:55:44 +0900603 bool is_user_defined_ = true;
Casey Dahlinf2d23f72015-10-02 16:19:19 -0700604
Casey Dahlin5c69deb2015-10-01 14:44:12 -0700605 DISALLOW_COPY_AND_ASSIGN(AidlMethod);
Casey Dahlin0a2f8be2015-09-28 16:15:29 -0700606};
Adam Lesinskiffa16862014-01-23 18:17:42 -0800607
Steven Morelandc258abc2018-07-10 14:03:38 -0700608class AidlDefinedType;
Jiyong Parkb034bf02018-07-30 17:44:33 +0900609class AidlInterface;
610class AidlParcelable;
611class AidlStructuredParcelable;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800612
Casey Dahlin2b2879b2015-10-13 16:59:44 -0700613class AidlQualifiedName : public AidlNode {
614 public:
Steven Moreland46e9da82018-07-27 15:45:29 -0700615 AidlQualifiedName(const AidlLocation& location, const std::string& term,
616 const std::string& comments);
Casey Dahlin2b2879b2015-10-13 16:59:44 -0700617 virtual ~AidlQualifiedName() = default;
618
619 const std::vector<std::string>& GetTerms() const { return terms_; }
620 const std::string& GetComments() const { return comments_; }
621 std::string GetDotName() const { return android::base::Join(terms_, '.'); }
Ningyuan Wangd17c58b2016-09-29 14:33:14 -0700622 std::string GetColonName() const { return android::base::Join(terms_, "::"); }
Casey Dahlin2b2879b2015-10-13 16:59:44 -0700623
Chih-Hung Hsiehf05cc262016-07-27 11:42:51 -0700624 void AddTerm(const std::string& term);
Casey Dahlin2b2879b2015-10-13 16:59:44 -0700625
626 private:
627 std::vector<std::string> terms_;
628 std::string comments_;
629
630 DISALLOW_COPY_AND_ASSIGN(AidlQualifiedName);
631};
632
Steven Moreland46e9da82018-07-27 15:45:29 -0700633class AidlInterface;
634class AidlParcelable;
635class AidlStructuredParcelable;
Daniel Norman85aed542019-08-21 12:01:14 -0700636// AidlDefinedType represents either an interface, parcelable, or enum that is
Jiyong Park1deecc32018-07-17 01:14:41 +0900637// defined in the source file.
638class AidlDefinedType : public AidlAnnotatable {
Steven Moreland787b0432018-07-03 09:00:58 -0700639 public:
Steven Moreland46e9da82018-07-27 15:45:29 -0700640 AidlDefinedType(const AidlLocation& location, const std::string& name,
641 const std::string& comments, const std::vector<std::string>& package);
Steven Moreland787b0432018-07-03 09:00:58 -0700642 virtual ~AidlDefinedType() = default;
643
Jiyong Park1deecc32018-07-17 01:14:41 +0900644 const std::string& GetName() const { return name_; };
Jeongik Cha997281d2020-01-16 15:23:59 +0900645 bool IsHidden() const;
Jiyong Park1deecc32018-07-17 01:14:41 +0900646 const std::string& GetComments() const { return comments_; }
Jiyong Parka6605ab2018-11-11 14:30:21 +0900647 void SetComments(const std::string comments) { comments_ = comments; }
Jiyong Park1deecc32018-07-17 01:14:41 +0900648
Steven Moreland787b0432018-07-03 09:00:58 -0700649 /* dot joined package, example: "android.package.foo" */
650 std::string GetPackage() const;
651 /* dot joined package and name, example: "android.package.foo.IBar" */
652 std::string GetCanonicalName() const;
653 const std::vector<std::string>& GetSplitPackage() const { return package_; }
654
Steven Morelanded83a282018-07-17 13:27:29 -0700655 virtual std::string GetPreprocessDeclarationName() const = 0;
Steven Morelandc258abc2018-07-10 14:03:38 -0700656
Steven Moreland5557f1c2018-07-02 13:50:23 -0700657 virtual const AidlStructuredParcelable* AsStructuredParcelable() const { return nullptr; }
Steven Morelandc258abc2018-07-10 14:03:38 -0700658 virtual const AidlParcelable* AsParcelable() const { return nullptr; }
Daniel Norman85aed542019-08-21 12:01:14 -0700659 virtual const AidlEnumDeclaration* AsEnumDeclaration() const { return nullptr; }
Steven Moreland5557f1c2018-07-02 13:50:23 -0700660 virtual const AidlInterface* AsInterface() const { return nullptr; }
Jeongik Chadf76dc72019-11-28 00:08:47 +0900661 virtual const AidlParameterizable<std::string>* AsParameterizable() const { return nullptr; }
Andrei Onea9445fc62019-06-27 18:11:59 +0100662 virtual bool CheckValid(const AidlTypenames&) const { return CheckValidAnnotations(); }
Jeongik Cha047c5ee2019-08-07 23:16:49 +0900663 virtual bool LanguageSpecificCheckValid(Options::Language lang) const = 0;
Steven Morelandc258abc2018-07-10 14:03:38 -0700664 AidlStructuredParcelable* AsStructuredParcelable() {
665 return const_cast<AidlStructuredParcelable*>(
666 const_cast<const AidlDefinedType*>(this)->AsStructuredParcelable());
667 }
668 AidlParcelable* AsParcelable() {
669 return const_cast<AidlParcelable*>(const_cast<const AidlDefinedType*>(this)->AsParcelable());
670 }
Daniel Norman85aed542019-08-21 12:01:14 -0700671 AidlEnumDeclaration* AsEnumDeclaration() {
672 return const_cast<AidlEnumDeclaration*>(
673 const_cast<const AidlDefinedType*>(this)->AsEnumDeclaration());
674 }
Steven Morelandc258abc2018-07-10 14:03:38 -0700675 AidlInterface* AsInterface() {
676 return const_cast<AidlInterface*>(const_cast<const AidlDefinedType*>(this)->AsInterface());
677 }
678
Jeongik Chadf76dc72019-11-28 00:08:47 +0900679 AidlParameterizable<std::string>* AsParameterizable() {
680 return const_cast<AidlParameterizable<std::string>*>(
681 const_cast<const AidlDefinedType*>(this)->AsParameterizable());
682 }
683
Steven Moreland6cee3482018-07-18 14:39:58 -0700684 const AidlParcelable* AsUnstructuredParcelable() const {
685 if (this->AsStructuredParcelable() != nullptr) return nullptr;
686 return this->AsParcelable();
687 }
688 AidlParcelable* AsUnstructuredParcelable() {
689 return const_cast<AidlParcelable*>(
690 const_cast<const AidlDefinedType*>(this)->AsUnstructuredParcelable());
691 }
692
Jeongik Cha997281d2020-01-16 15:23:59 +0900693 virtual void Dump(CodeWriter* writer) const = 0;
Steven Moreland9f2a4332020-02-21 16:01:09 -0800694 void DumpHeader(CodeWriter* writer) const;
Jiyong Park02da7422018-07-16 16:00:26 +0900695
Steven Moreland787b0432018-07-03 09:00:58 -0700696 private:
Jiyong Park1deecc32018-07-17 01:14:41 +0900697 std::string name_;
Jiyong Park1deecc32018-07-17 01:14:41 +0900698 std::string comments_;
Steven Moreland787b0432018-07-03 09:00:58 -0700699 const std::vector<std::string> package_;
Steven Moreland5557f1c2018-07-02 13:50:23 -0700700
701 DISALLOW_COPY_AND_ASSIGN(AidlDefinedType);
Steven Moreland787b0432018-07-03 09:00:58 -0700702};
703
Jeongik Chadf76dc72019-11-28 00:08:47 +0900704class AidlParcelable : public AidlDefinedType, public AidlParameterizable<std::string> {
Casey Dahlin1ae2bc52015-10-07 18:49:10 -0700705 public:
Steven Moreland46e9da82018-07-27 15:45:29 -0700706 AidlParcelable(const AidlLocation& location, AidlQualifiedName* name,
Jiyong Parka6605ab2018-11-11 14:30:21 +0900707 const std::vector<std::string>& package, const std::string& comments,
Jeongik Chadf76dc72019-11-28 00:08:47 +0900708 const std::string& cpp_header = "",
709 std::vector<std::string>* type_params = nullptr);
Casey Dahlin1ae2bc52015-10-07 18:49:10 -0700710 virtual ~AidlParcelable() = default;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800711
Ningyuan Wangd17c58b2016-09-29 14:33:14 -0700712 // C++ uses "::" instead of "." to refer to a inner class.
713 std::string GetCppName() const { return name_->GetColonName(); }
Christopher Wiley8aa4d9f2015-11-16 19:10:45 -0800714 std::string GetCppHeader() const { return cpp_header_; }
Christopher Wiley8aa4d9f2015-11-16 19:10:45 -0800715
Jeongik Cha82317dd2019-02-27 20:26:11 +0900716 bool CheckValid(const AidlTypenames& typenames) const override;
Jeongik Cha047c5ee2019-08-07 23:16:49 +0900717 bool LanguageSpecificCheckValid(Options::Language lang) const override;
Steven Morelandc258abc2018-07-10 14:03:38 -0700718 const AidlParcelable* AsParcelable() const override { return this; }
Jeongik Chadf76dc72019-11-28 00:08:47 +0900719 const AidlParameterizable<std::string>* AsParameterizable() const override { return this; }
720 const AidlNode& AsAidlNode() const override { return *this; }
Steven Morelanded83a282018-07-17 13:27:29 -0700721 std::string GetPreprocessDeclarationName() const override { return "parcelable"; }
Steven Morelandc258abc2018-07-10 14:03:38 -0700722
Jeongik Cha997281d2020-01-16 15:23:59 +0900723 void Dump(CodeWriter* writer) const override;
Jiyong Park02da7422018-07-16 16:00:26 +0900724
Casey Dahlin1ae2bc52015-10-07 18:49:10 -0700725 private:
Christopher Wiley8aa4d9f2015-11-16 19:10:45 -0800726 std::unique_ptr<AidlQualifiedName> name_;
Christopher Wiley8aa4d9f2015-11-16 19:10:45 -0800727 std::string cpp_header_;
Casey Dahlin59401da2015-10-09 18:16:45 -0700728
Casey Dahlin1ae2bc52015-10-07 18:49:10 -0700729 DISALLOW_COPY_AND_ASSIGN(AidlParcelable);
Casey Dahlin0a2f8be2015-09-28 16:15:29 -0700730};
Adam Lesinskiffa16862014-01-23 18:17:42 -0800731
Steven Moreland5557f1c2018-07-02 13:50:23 -0700732class AidlStructuredParcelable : public AidlParcelable {
733 public:
Steven Moreland46e9da82018-07-27 15:45:29 -0700734 AidlStructuredParcelable(const AidlLocation& location, AidlQualifiedName* name,
Jiyong Parka6605ab2018-11-11 14:30:21 +0900735 const std::vector<std::string>& package, const std::string& comments,
Steven Moreland5557f1c2018-07-02 13:50:23 -0700736 std::vector<std::unique_ptr<AidlVariableDeclaration>>* variables);
737
738 const std::vector<std::unique_ptr<AidlVariableDeclaration>>& GetFields() const {
739 return variables_;
740 }
741
Steven Morelandc258abc2018-07-10 14:03:38 -0700742 const AidlStructuredParcelable* AsStructuredParcelable() const override { return this; }
Steven Morelanded83a282018-07-17 13:27:29 -0700743 std::string GetPreprocessDeclarationName() const override { return "structured_parcelable"; }
Steven Moreland5557f1c2018-07-02 13:50:23 -0700744
Jeongik Cha997281d2020-01-16 15:23:59 +0900745 void Dump(CodeWriter* writer) const override;
Jiyong Park02da7422018-07-16 16:00:26 +0900746
Jeongik Chadb0f59e2018-11-01 18:11:21 +0900747 bool CheckValid(const AidlTypenames& typenames) const override;
Jeongik Cha047c5ee2019-08-07 23:16:49 +0900748 bool LanguageSpecificCheckValid(Options::Language lang) const override;
Jeongik Chadb0f59e2018-11-01 18:11:21 +0900749
Steven Moreland5557f1c2018-07-02 13:50:23 -0700750 private:
751 const std::vector<std::unique_ptr<AidlVariableDeclaration>> variables_;
752
753 DISALLOW_COPY_AND_ASSIGN(AidlStructuredParcelable);
754};
755
Daniel Norman85aed542019-08-21 12:01:14 -0700756class AidlEnumerator : public AidlNode {
757 public:
Daniel Norman2e4112d2019-10-03 10:22:35 -0700758 AidlEnumerator(const AidlLocation& location, const std::string& name, AidlConstantValue* value,
759 const std::string& comments);
Daniel Norman85aed542019-08-21 12:01:14 -0700760 virtual ~AidlEnumerator() = default;
761
762 const std::string& GetName() const { return name_; }
Will McVickerd7d18df2019-09-12 13:40:50 -0700763 AidlConstantValue* GetValue() const { return value_.get(); }
Daniel Norman2e4112d2019-10-03 10:22:35 -0700764 const std::string& GetComments() const { return comments_; }
Daniel Norman85aed542019-08-21 12:01:14 -0700765 bool CheckValid(const AidlTypeSpecifier& enum_backing_type) const;
766
767 string ValueString(const AidlTypeSpecifier& backing_type,
768 const ConstantValueDecorator& decorator) const;
769
Daniel Normanb28684e2019-10-17 15:31:39 -0700770 void SetValue(std::unique_ptr<AidlConstantValue> value) { value_ = std::move(value); }
771
Daniel Norman85aed542019-08-21 12:01:14 -0700772 private:
773 const std::string name_;
Will McVickerd7d18df2019-09-12 13:40:50 -0700774 unique_ptr<AidlConstantValue> value_;
Daniel Norman2e4112d2019-10-03 10:22:35 -0700775 const std::string comments_;
Daniel Norman85aed542019-08-21 12:01:14 -0700776
777 DISALLOW_COPY_AND_ASSIGN(AidlEnumerator);
778};
779
780class AidlEnumDeclaration : public AidlDefinedType {
781 public:
Will McVickerd7d18df2019-09-12 13:40:50 -0700782 AidlEnumDeclaration(const AidlLocation& location, const string& name,
Daniel Norman85aed542019-08-21 12:01:14 -0700783 std::vector<std::unique_ptr<AidlEnumerator>>* enumerators,
Daniel Norman2e4112d2019-10-03 10:22:35 -0700784 const std::vector<std::string>& package, const std::string& comments);
Daniel Norman85aed542019-08-21 12:01:14 -0700785 virtual ~AidlEnumDeclaration() = default;
786
787 void SetBackingType(std::unique_ptr<const AidlTypeSpecifier> type);
788 const AidlTypeSpecifier& GetBackingType() const { return *backing_type_; }
789 const std::vector<std::unique_ptr<AidlEnumerator>>& GetEnumerators() const {
790 return enumerators_;
791 }
Steven Moreland59e53e42019-11-26 20:38:08 -0800792 bool Autofill();
Daniel Norman85aed542019-08-21 12:01:14 -0700793 bool CheckValid(const AidlTypenames& typenames) const override;
794 bool LanguageSpecificCheckValid(Options::Language) const override { return true; }
795 std::string GetPreprocessDeclarationName() const override { return "enum"; }
Jeongik Cha997281d2020-01-16 15:23:59 +0900796 void Dump(CodeWriter* writer) const override;
Daniel Norman85aed542019-08-21 12:01:14 -0700797
798 const AidlEnumDeclaration* AsEnumDeclaration() const override { return this; }
799
800 private:
801 const std::string name_;
802 const std::vector<std::unique_ptr<AidlEnumerator>> enumerators_;
803 std::unique_ptr<const AidlTypeSpecifier> backing_type_;
804
805 DISALLOW_COPY_AND_ASSIGN(AidlEnumDeclaration);
806};
807
Jiyong Park1deecc32018-07-17 01:14:41 +0900808class AidlInterface final : public AidlDefinedType {
Casey Dahlin1ae2bc52015-10-07 18:49:10 -0700809 public:
Steven Moreland46e9da82018-07-27 15:45:29 -0700810 AidlInterface(const AidlLocation& location, const std::string& name, const std::string& comments,
811 bool oneway_, std::vector<std::unique_ptr<AidlMember>>* members,
Christopher Wileyd76067c2015-10-19 17:00:13 -0700812 const std::vector<std::string>& package);
Casey Dahlin1ae2bc52015-10-07 18:49:10 -0700813 virtual ~AidlInterface() = default;
814
Casey Dahlinfb7da2e2015-10-08 17:26:09 -0700815 const std::vector<std::unique_ptr<AidlMethod>>& GetMethods() const
816 { return methods_; }
Jiyong Park309668e2018-07-28 16:55:44 +0900817 std::vector<std::unique_ptr<AidlMethod>>& GetMutableMethods() { return methods_; }
Steven Moreland693640b2018-07-19 13:46:27 -0700818 const std::vector<std::unique_ptr<AidlConstantDeclaration>>& GetConstantDeclarations() const {
819 return constants_;
820 }
Casey Dahlina2f77c42015-12-01 18:26:02 -0800821
Steven Morelandc258abc2018-07-10 14:03:38 -0700822 const AidlInterface* AsInterface() const override { return this; }
Steven Morelanded83a282018-07-17 13:27:29 -0700823 std::string GetPreprocessDeclarationName() const override { return "interface"; }
Steven Moreland5557f1c2018-07-02 13:50:23 -0700824
Jeongik Cha997281d2020-01-16 15:23:59 +0900825 void Dump(CodeWriter* writer) const override;
Jiyong Park02da7422018-07-16 16:00:26 +0900826
Jeongik Chadb0f59e2018-11-01 18:11:21 +0900827 bool CheckValid(const AidlTypenames& typenames) const override;
Jeongik Cha047c5ee2019-08-07 23:16:49 +0900828 bool LanguageSpecificCheckValid(Options::Language lang) const override;
Jeongik Chadb0f59e2018-11-01 18:11:21 +0900829
Casey Dahlin1ae2bc52015-10-07 18:49:10 -0700830 private:
Casey Dahlinfb7da2e2015-10-08 17:26:09 -0700831 std::vector<std::unique_ptr<AidlMethod>> methods_;
Steven Moreland693640b2018-07-19 13:46:27 -0700832 std::vector<std::unique_ptr<AidlConstantDeclaration>> constants_;
Casey Dahlinfb7da2e2015-10-08 17:26:09 -0700833
Casey Dahlin1ae2bc52015-10-07 18:49:10 -0700834 DISALLOW_COPY_AND_ASSIGN(AidlInterface);
Casey Dahlin0a2f8be2015-09-28 16:15:29 -0700835};
Adam Lesinskiffa16862014-01-23 18:17:42 -0800836
Casey Dahlin0edf3422015-10-07 12:34:59 -0700837class AidlImport : public AidlNode {
838 public:
Steven Moreland46e9da82018-07-27 15:45:29 -0700839 AidlImport(const AidlLocation& location, const std::string& needed_class);
Casey Dahlin0edf3422015-10-07 12:34:59 -0700840 virtual ~AidlImport() = default;
841
Casey Dahlin0edf3422015-10-07 12:34:59 -0700842 const std::string& GetFilename() const { return filename_; }
843 const std::string& GetNeededClass() const { return needed_class_; }
Casey Dahlin0edf3422015-10-07 12:34:59 -0700844
845 private:
Casey Dahlin0edf3422015-10-07 12:34:59 -0700846 std::string filename_;
847 std::string needed_class_;
Casey Dahlin0edf3422015-10-07 12:34:59 -0700848
849 DISALLOW_COPY_AND_ASSIGN(AidlImport);
Casey Dahline2507492015-09-14 17:11:20 -0700850};
851
852class Parser {
Casey Dahlindd691812015-09-09 17:59:06 -0700853 public:
Casey Dahline2507492015-09-14 17:11:20 -0700854 ~Parser();
Casey Dahlindd691812015-09-09 17:59:06 -0700855
Steven Moreland64e29be2018-08-08 18:52:19 -0700856 // Parse contents of file |filename|. Should only be called once.
857 static std::unique_ptr<Parser> Parse(const std::string& filename,
858 const android::aidl::IoDelegate& io_delegate,
859 AidlTypenames& typenames);
Casey Dahlin89d44842015-09-24 18:45:54 -0700860
Steven Moreland2ca4fcb2018-06-27 16:01:01 -0700861 void AddError() { error_++; }
Steven Moreland64e29be2018-08-08 18:52:19 -0700862 bool HasError() { return error_ != 0; }
Casey Dahlindd691812015-09-09 17:59:06 -0700863
Casey Dahlin3c6df362015-10-06 15:48:35 -0700864 const std::string& FileName() const { return filename_; }
Casey Dahlin42727f82015-10-12 19:23:40 -0700865 void* Scanner() const { return scanner_; }
Casey Dahlindd691812015-09-09 17:59:06 -0700866
Steven Morelandd1039a92020-01-23 09:49:43 -0800867 void AddImport(std::unique_ptr<AidlImport>&& import);
Christopher Wileyd76067c2015-10-19 17:00:13 -0700868 const std::vector<std::unique_ptr<AidlImport>>& GetImports() {
869 return imports_;
870 }
Casey Dahlindd691812015-09-09 17:59:06 -0700871
Jiyong Parkb034bf02018-07-30 17:44:33 +0900872 void SetPackage(unique_ptr<AidlQualifiedName> name) { package_ = std::move(name); }
873 std::vector<std::string> Package() const;
Jiyong Park1deecc32018-07-17 01:14:41 +0900874
875 void DeferResolution(AidlTypeSpecifier* typespec) {
876 unresolved_typespecs_.emplace_back(typespec);
877 }
878
Jiyong Parke59c3682018-09-11 23:10:25 +0900879 const vector<AidlTypeSpecifier*>& GetUnresolvedTypespecs() const { return unresolved_typespecs_; }
880
Jiyong Park1deecc32018-07-17 01:14:41 +0900881 bool Resolve();
882
Jiyong Parkb034bf02018-07-30 17:44:33 +0900883 void AddDefinedType(unique_ptr<AidlDefinedType> type) {
884 // Parser does NOT own AidlDefinedType, it just has references to the types
885 // that it encountered while parsing the input file.
886 defined_types_.emplace_back(type.get());
887
888 // AidlDefinedType IS owned by AidlTypenames
889 if (!typenames_.AddDefinedType(std::move(type))) {
890 AddError();
891 }
892 }
893
894 vector<AidlDefinedType*>& GetDefinedTypes() { return defined_types_; }
895
Casey Dahlindd691812015-09-09 17:59:06 -0700896 private:
Steven Moreland64e29be2018-08-08 18:52:19 -0700897 explicit Parser(const std::string& filename, std::string& raw_buffer,
898 android::aidl::AidlTypenames& typenames);
899
Casey Dahlindd691812015-09-09 17:59:06 -0700900 std::string filename_;
Christopher Wileyd76067c2015-10-19 17:00:13 -0700901 std::unique_ptr<AidlQualifiedName> package_;
Jiyong Parkb034bf02018-07-30 17:44:33 +0900902 AidlTypenames& typenames_;
Steven Moreland64e29be2018-08-08 18:52:19 -0700903
904 void* scanner_ = nullptr;
905 YY_BUFFER_STATE buffer_;
906 int error_ = 0;
907
908 std::vector<std::unique_ptr<AidlImport>> imports_;
Jiyong Parkb034bf02018-07-30 17:44:33 +0900909 vector<AidlDefinedType*> defined_types_;
Jiyong Park1deecc32018-07-17 01:14:41 +0900910 vector<AidlTypeSpecifier*> unresolved_typespecs_;
Casey Dahlindd691812015-09-09 17:59:06 -0700911
Casey Dahline2507492015-09-14 17:11:20 -0700912 DISALLOW_COPY_AND_ASSIGN(Parser);
Casey Dahlindd691812015-09-09 17:59:06 -0700913};