blob: 55bed950110b96e640b228ab1084ebc8c84802c4 [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 Onuki78a1c1c2020-03-04 16:57:23 -0800246 bool IsHide() const;
Andrei Onea9445fc62019-06-27 18:11:59 +0100247
Steven Moreland7e4b9502020-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_; }
Devin Moore24f68572020-02-26 13:20:59 -0800255 virtual std::set<string> GetSupportedAnnotations() const = 0;
256 virtual bool CheckValid(const AidlTypenames&) const;
Jiyong Park3656c3c2018-08-01 20:02:01 +0900257
Casey Dahline7922932016-02-29 17:23:01 -0800258 private:
Jiyong Parka6605ab2018-11-11 14:30:21 +0900259 vector<AidlAnnotation> annotations_;
Casey Dahline7922932016-02-29 17:23:01 -0800260};
261
Jiyong Park1deecc32018-07-17 01:14:41 +0900262class AidlQualifiedName;
263
264// AidlTypeSpecifier represents a reference to either a built-in type,
265// a defined type, or a variant (e.g., array of generic) of a type.
Jeongik Chadf76dc72019-11-28 00:08:47 +0900266class AidlTypeSpecifier final : public AidlAnnotatable,
267 public AidlParameterizable<unique_ptr<AidlTypeSpecifier>> {
Casey Dahline7922932016-02-29 17:23:01 -0800268 public:
Steven Moreland46e9da82018-07-27 15:45:29 -0700269 AidlTypeSpecifier(const AidlLocation& location, const string& unresolved_name, bool is_array,
270 vector<unique_ptr<AidlTypeSpecifier>>* type_params, const string& comments);
Jiyong Parkd59a10d2018-07-18 11:12:55 +0900271 virtual ~AidlTypeSpecifier() = default;
Casey Dahlin0ee37582015-09-30 16:31:55 -0700272
Steven Moreland3f658cf2018-08-20 13:40:54 -0700273 // Copy of this type which is not an array.
274 AidlTypeSpecifier ArrayBase() const;
275
Jiyong Park1deecc32018-07-17 01:14:41 +0900276 // Returns the full-qualified name of the base type.
277 // int -> int
278 // int[] -> int
279 // List<String> -> List
280 // IFoo -> foo.bar.IFoo (if IFoo is in package foo.bar)
281 const string& GetName() const {
282 if (IsResolved()) {
283 return fully_qualified_name_;
284 } else {
285 return GetUnresolvedName();
286 }
287 }
Casey Dahlin0ee37582015-09-30 16:31:55 -0700288
Jiyong Park1deecc32018-07-17 01:14:41 +0900289 // Returns string representation of this type specifier.
Artur Satayev91fe8712019-07-29 13:06:01 +0100290 // This is GetBaseTypeName() + array modifier or generic type parameters
Jiyong Park1deecc32018-07-17 01:14:41 +0900291 string ToString() const;
292
Jiyong Park02da7422018-07-16 16:00:26 +0900293 std::string Signature() const;
294
Jiyong Park1deecc32018-07-17 01:14:41 +0900295 const string& GetUnresolvedName() const { return unresolved_name_; }
296
Jeongik Cha997281d2020-01-16 15:23:59 +0900297 bool IsHidden() const;
298
Jiyong Park1deecc32018-07-17 01:14:41 +0900299 const string& GetComments() const { return comments_; }
300
Jeongik Cha1a7ab642019-07-29 17:31:02 +0900301 const std::vector<std::string> GetSplitName() const { return split_name_; }
302
Jiyong Parka6605ab2018-11-11 14:30:21 +0900303 void SetComments(const string& comment) { comments_ = comment; }
304
Jiyong Park1deecc32018-07-17 01:14:41 +0900305 bool IsResolved() const { return fully_qualified_name_ != ""; }
306
307 bool IsArray() const { return is_array_; }
308
Jiyong Park1deecc32018-07-17 01:14:41 +0900309 // Resolve the base type name to a fully-qualified name. Return false if the
310 // resolution fails.
Daniel Norman716d3112019-09-10 13:11:56 -0700311 bool Resolve(const AidlTypenames& typenames);
Casey Dahlin0ee37582015-09-30 16:31:55 -0700312
Devin Moore24f68572020-02-26 13:20:59 -0800313 std::set<string> GetSupportedAnnotations() const override;
314 bool CheckValid(const AidlTypenames& typenames) const override;
Jeongik Cha047c5ee2019-08-07 23:16:49 +0900315 bool LanguageSpecificCheckValid(Options::Language lang) const;
Jeongik Chadf76dc72019-11-28 00:08:47 +0900316 const AidlNode& AsAidlNode() const override { return *this; }
Jiyong Park1d2df7d2018-07-23 15:22:50 +0900317
Casey Dahlin0ee37582015-09-30 16:31:55 -0700318 private:
Steven Moreland3f658cf2018-08-20 13:40:54 -0700319 AidlTypeSpecifier(const AidlTypeSpecifier&) = default;
320
Jiyong Park1deecc32018-07-17 01:14:41 +0900321 const string unresolved_name_;
322 string fully_qualified_name_;
Steven Moreland3f658cf2018-08-20 13:40:54 -0700323 bool is_array_;
Jiyong Parka6605ab2018-11-11 14:30:21 +0900324 string comments_;
Jeongik Cha1a7ab642019-07-29 17:31:02 +0900325 vector<string> split_name_;
Casey Dahlin0ee37582015-09-30 16:31:55 -0700326};
327
Steven Moreland860b1942018-08-16 14:59:28 -0700328// Returns the universal value unaltered.
329std::string AidlConstantValueDecorator(const AidlTypeSpecifier& type, const std::string& raw_value);
330
Steven Moreland9ea10e32018-07-19 15:26:09 -0700331class AidlConstantValue;
Steven Moreland5557f1c2018-07-02 13:50:23 -0700332class AidlVariableDeclaration : public AidlNode {
333 public:
Steven Moreland46e9da82018-07-27 15:45:29 -0700334 AidlVariableDeclaration(const AidlLocation& location, AidlTypeSpecifier* type,
335 const std::string& name);
336 AidlVariableDeclaration(const AidlLocation& location, AidlTypeSpecifier* type,
337 const std::string& name, AidlConstantValue* default_value);
Steven Moreland5557f1c2018-07-02 13:50:23 -0700338 virtual ~AidlVariableDeclaration() = default;
339
340 std::string GetName() const { return name_; }
Jiyong Parkd59a10d2018-07-18 11:12:55 +0900341 const AidlTypeSpecifier& GetType() const { return *type_; }
Steven Moreland9ea10e32018-07-19 15:26:09 -0700342 const AidlConstantValue* GetDefaultValue() const { return default_value_.get(); }
343
Jiyong Parkd59a10d2018-07-18 11:12:55 +0900344 AidlTypeSpecifier* GetMutableType() { return type_.get(); }
Steven Moreland5557f1c2018-07-02 13:50:23 -0700345
Jeongik Chadb0f59e2018-11-01 18:11:21 +0900346 bool CheckValid(const AidlTypenames& typenames) const;
Steven Moreland5557f1c2018-07-02 13:50:23 -0700347 std::string ToString() const;
Jiyong Park02da7422018-07-16 16:00:26 +0900348 std::string Signature() const;
Steven Moreland5557f1c2018-07-02 13:50:23 -0700349
Steven Moreland860b1942018-08-16 14:59:28 -0700350 std::string ValueString(const ConstantValueDecorator& decorator) const;
Steven Moreland25294322018-08-07 18:13:55 -0700351
Steven Moreland5557f1c2018-07-02 13:50:23 -0700352 private:
Jiyong Parkd59a10d2018-07-18 11:12:55 +0900353 std::unique_ptr<AidlTypeSpecifier> type_;
Steven Moreland5557f1c2018-07-02 13:50:23 -0700354 std::string name_;
Steven Moreland9ea10e32018-07-19 15:26:09 -0700355 std::unique_ptr<AidlConstantValue> default_value_;
Steven Moreland5557f1c2018-07-02 13:50:23 -0700356
357 DISALLOW_COPY_AND_ASSIGN(AidlVariableDeclaration);
358};
359
360class AidlArgument : public AidlVariableDeclaration {
Casey Dahlinbc7a50a2015-09-28 19:20:50 -0700361 public:
Casey Dahlinc378c992015-09-29 16:50:40 -0700362 enum Direction { IN_DIR = 1, OUT_DIR = 2, INOUT_DIR = 3 };
363
Steven Moreland46e9da82018-07-27 15:45:29 -0700364 AidlArgument(const AidlLocation& location, AidlArgument::Direction direction,
365 AidlTypeSpecifier* type, const std::string& name);
366 AidlArgument(const AidlLocation& location, AidlTypeSpecifier* type, const std::string& name);
Casey Dahlinbc7a50a2015-09-28 19:20:50 -0700367 virtual ~AidlArgument() = default;
368
Casey Dahlinc378c992015-09-29 16:50:40 -0700369 Direction GetDirection() const { return direction_; }
Christopher Wileyad339272015-10-05 19:11:58 -0700370 bool IsOut() const { return direction_ & OUT_DIR; }
371 bool IsIn() const { return direction_ & IN_DIR; }
Casey Dahlinc378c992015-09-29 16:50:40 -0700372 bool DirectionWasSpecified() const { return direction_specified_; }
Jiyong Park3656c3c2018-08-01 20:02:01 +0900373 string GetDirectionSpecifier() const;
Christopher Wileyad339272015-10-05 19:11:58 -0700374
Casey Dahlinc378c992015-09-29 16:50:40 -0700375 std::string ToString() const;
Jiyong Park02da7422018-07-16 16:00:26 +0900376 std::string Signature() const;
Casey Dahlinc378c992015-09-29 16:50:40 -0700377
Casey Dahlinbc7a50a2015-09-28 19:20:50 -0700378 private:
Casey Dahlinc378c992015-09-29 16:50:40 -0700379 Direction direction_;
380 bool direction_specified_;
381
Casey Dahlinbc7a50a2015-09-28 19:20:50 -0700382 DISALLOW_COPY_AND_ASSIGN(AidlArgument);
Casey Dahlina834dd42015-09-23 11:52:15 -0700383};
Adam Lesinskiffa16862014-01-23 18:17:42 -0800384
Casey Dahlind40e2fe2015-11-24 14:06:52 -0800385class AidlMethod;
Steven Moreland693640b2018-07-19 13:46:27 -0700386class AidlConstantDeclaration;
Daniel Norman85aed542019-08-21 12:01:14 -0700387class AidlEnumDeclaration;
Casey Dahlind40e2fe2015-11-24 14:06:52 -0800388class AidlMember : public AidlNode {
389 public:
Steven Moreland46e9da82018-07-27 15:45:29 -0700390 AidlMember(const AidlLocation& location);
Casey Dahlind40e2fe2015-11-24 14:06:52 -0800391 virtual ~AidlMember() = default;
392
393 virtual AidlMethod* AsMethod() { return nullptr; }
Steven Moreland693640b2018-07-19 13:46:27 -0700394 virtual AidlConstantDeclaration* AsConstantDeclaration() { return nullptr; }
Casey Dahlind40e2fe2015-11-24 14:06:52 -0800395
396 private:
397 DISALLOW_COPY_AND_ASSIGN(AidlMember);
398};
399
Will McVickerd7d18df2019-09-12 13:40:50 -0700400class AidlUnaryConstExpression;
401class AidlBinaryConstExpression;
402
Steven Moreland693640b2018-07-19 13:46:27 -0700403class AidlConstantValue : public AidlNode {
Casey Dahlind40e2fe2015-11-24 14:06:52 -0800404 public:
Will McVickerd7d18df2019-09-12 13:40:50 -0700405 enum class Type {
406 // WARNING: Don't change this order! The order is used to determine type
407 // promotion during a binary expression.
408 BOOLEAN,
409 INT8,
410 INT32,
411 INT64,
412 ARRAY,
413 CHARACTER,
414 STRING,
415 FLOATING,
416 UNARY,
417 BINARY,
418 ERROR,
419 };
420
421 /*
422 * Return the value casted to the given type.
423 */
424 template <typename T>
425 T cast() const;
Casey Dahlind40e2fe2015-11-24 14:06:52 -0800426
Steven Moreland693640b2018-07-19 13:46:27 -0700427 virtual ~AidlConstantValue() = default;
Casey Dahlind40e2fe2015-11-24 14:06:52 -0800428
Steven Moreland25294322018-08-07 18:13:55 -0700429 static AidlConstantValue* Boolean(const AidlLocation& location, bool value);
430 static AidlConstantValue* Character(const AidlLocation& location, char value);
Steven Moreland25294322018-08-07 18:13:55 -0700431 // example: 123, -5498, maybe any size
Will McVickerd7d18df2019-09-12 13:40:50 -0700432 static AidlConstantValue* Integral(const AidlLocation& location, const string& value);
433 static AidlConstantValue* Floating(const AidlLocation& location, const std::string& value);
Steven Moreland860b1942018-08-16 14:59:28 -0700434 static AidlConstantValue* Array(const AidlLocation& location,
Will McVickerd7d18df2019-09-12 13:40:50 -0700435 std::unique_ptr<vector<unique_ptr<AidlConstantValue>>> values);
Steven Moreland693640b2018-07-19 13:46:27 -0700436 // example: "\"asdf\""
Will McVickerd7d18df2019-09-12 13:40:50 -0700437 static AidlConstantValue* String(const AidlLocation& location, const string& value);
Steven Moreland693640b2018-07-19 13:46:27 -0700438
Daniel Normanf0ca44f2019-10-25 09:59:44 -0700439 // Construct an AidlConstantValue by evaluating the other integral constant's
440 // value string. This does not preserve the structure of the copied constant.
Steven Moreland59e53e42019-11-26 20:38:08 -0800441 // Returns nullptr and logs if value cannot be copied.
Daniel Normanf0ca44f2019-10-25 09:59:44 -0700442 static AidlConstantValue* ShallowIntegralCopy(const AidlConstantValue& other);
Daniel Normanb28684e2019-10-17 15:31:39 -0700443
Will McVickerd7d18df2019-09-12 13:40:50 -0700444 Type GetType() const { return final_type_; }
Steven Moreland25294322018-08-07 18:13:55 -0700445
Will McVickerd7d18df2019-09-12 13:40:50 -0700446 virtual bool CheckValid() const;
Steven Moreland860b1942018-08-16 14:59:28 -0700447
448 // Raw value of type (currently valid in C++ and Java). Empty string on error.
Steven Moreland4bcb05c2019-11-27 18:57:47 -0800449 string ValueString(const AidlTypeSpecifier& type, const ConstantValueDecorator& decorator) const;
Casey Dahlind40e2fe2015-11-24 14:06:52 -0800450
451 private:
Will McVickerd7d18df2019-09-12 13:40:50 -0700452 AidlConstantValue(const AidlLocation& location, Type parsed_type, int64_t parsed_value,
453 const string& checked_value);
454 AidlConstantValue(const AidlLocation& location, Type type, const string& checked_value);
Steven Moreland860b1942018-08-16 14:59:28 -0700455 AidlConstantValue(const AidlLocation& location, Type type,
Will McVickerd7d18df2019-09-12 13:40:50 -0700456 std::unique_ptr<vector<unique_ptr<AidlConstantValue>>> values);
Steven Moreland25294322018-08-07 18:13:55 -0700457 static string ToString(Type type);
Will McVickerd7d18df2019-09-12 13:40:50 -0700458 static bool ParseIntegral(const string& value, int64_t* parsed_value, Type* parsed_type);
459 static bool IsHex(const string& value);
Steven Moreland4bcb05c2019-11-27 18:57:47 -0800460
Will McVickerd7d18df2019-09-12 13:40:50 -0700461 virtual bool evaluate(const AidlTypeSpecifier& type) const;
Casey Dahlind40e2fe2015-11-24 14:06:52 -0800462
Steven Moreland693640b2018-07-19 13:46:27 -0700463 const Type type_ = Type::ERROR;
Will McVickerd7d18df2019-09-12 13:40:50 -0700464 const vector<unique_ptr<AidlConstantValue>> values_; // if type_ == ARRAY
465 const string value_; // otherwise
466
467 // State for tracking evaluation of expressions
Steven Moreland4bcb05c2019-11-27 18:57:47 -0800468 mutable bool is_valid_ = false; // cache of CheckValid, but may be marked false in evaluate
469 mutable bool is_evaluated_ = false; // whether evaluate has been called
Will McVickerd7d18df2019-09-12 13:40:50 -0700470 mutable Type final_type_;
471 mutable int64_t final_value_;
472 mutable string final_string_value_ = "";
Steven Moreland693640b2018-07-19 13:46:27 -0700473
474 DISALLOW_COPY_AND_ASSIGN(AidlConstantValue);
Will McVickerd7d18df2019-09-12 13:40:50 -0700475
476 friend AidlUnaryConstExpression;
477 friend AidlBinaryConstExpression;
478};
479
480class AidlUnaryConstExpression : public AidlConstantValue {
481 public:
482 AidlUnaryConstExpression(const AidlLocation& location, const string& op,
483 std::unique_ptr<AidlConstantValue> rval);
484
485 static bool IsCompatibleType(Type type, const string& op);
486 bool CheckValid() const override;
Will McVickerd7d18df2019-09-12 13:40:50 -0700487 private:
488 bool evaluate(const AidlTypeSpecifier& type) const override;
489
490 std::unique_ptr<AidlConstantValue> unary_;
491 const string op_;
492};
493
494class AidlBinaryConstExpression : public AidlConstantValue {
495 public:
496 AidlBinaryConstExpression(const AidlLocation& location, std::unique_ptr<AidlConstantValue> lval,
497 const string& op, std::unique_ptr<AidlConstantValue> rval);
498
499 bool CheckValid() const override;
Will McVickerd7d18df2019-09-12 13:40:50 -0700500
501 static bool AreCompatibleTypes(Type t1, Type t2);
502 // Returns the promoted kind for both operands
503 static Type UsualArithmeticConversion(Type left, Type right);
504 // Returns the promoted integral type where INT32 is the smallest type
505 static Type IntegralPromotion(Type in);
506
507 private:
508 bool evaluate(const AidlTypeSpecifier& type) const override;
509
510 std::unique_ptr<AidlConstantValue> left_val_;
511 std::unique_ptr<AidlConstantValue> right_val_;
512 const string op_;
Christopher Wileyd6bdd8d2016-05-03 11:23:13 -0700513};
514
Andrei Onea9445fc62019-06-27 18:11:59 +0100515struct AidlAnnotationParameter {
516 std::string name;
517 std::unique_ptr<AidlConstantValue> value;
518};
519
Steven Moreland693640b2018-07-19 13:46:27 -0700520class AidlConstantDeclaration : public AidlMember {
Christopher Wileyd6bdd8d2016-05-03 11:23:13 -0700521 public:
Steven Moreland46e9da82018-07-27 15:45:29 -0700522 AidlConstantDeclaration(const AidlLocation& location, AidlTypeSpecifier* specifier,
Will McVickerd7d18df2019-09-12 13:40:50 -0700523 const string& name, AidlConstantValue* value);
Steven Moreland693640b2018-07-19 13:46:27 -0700524 virtual ~AidlConstantDeclaration() = default;
Christopher Wileyd6bdd8d2016-05-03 11:23:13 -0700525
Steven Moreland693640b2018-07-19 13:46:27 -0700526 const AidlTypeSpecifier& GetType() const { return *type_; }
Steven Moreland4d12f9a2018-10-31 14:30:55 -0700527 AidlTypeSpecifier* GetMutableType() { return type_.get(); }
Will McVickerd7d18df2019-09-12 13:40:50 -0700528 const string& GetName() const { return name_; }
Steven Moreland693640b2018-07-19 13:46:27 -0700529 const AidlConstantValue& GetValue() const { return *value_; }
Jeongik Chadb0f59e2018-11-01 18:11:21 +0900530 bool CheckValid(const AidlTypenames& typenames) const;
Christopher Wileyd6bdd8d2016-05-03 11:23:13 -0700531
Will McVickerd7d18df2019-09-12 13:40:50 -0700532 string ToString() const;
533 string Signature() const;
Steven Moreland860b1942018-08-16 14:59:28 -0700534 string ValueString(const ConstantValueDecorator& decorator) const {
Will McVickerd7d18df2019-09-12 13:40:50 -0700535 return value_->ValueString(GetType(), decorator);
Steven Moreland860b1942018-08-16 14:59:28 -0700536 }
Steven Moreland25294322018-08-07 18:13:55 -0700537
Steven Moreland693640b2018-07-19 13:46:27 -0700538 AidlConstantDeclaration* AsConstantDeclaration() override { return this; }
Christopher Wileyd6bdd8d2016-05-03 11:23:13 -0700539
540 private:
Steven Moreland693640b2018-07-19 13:46:27 -0700541 const unique_ptr<AidlTypeSpecifier> type_;
Will McVickerd7d18df2019-09-12 13:40:50 -0700542 const string name_;
543 unique_ptr<AidlConstantValue> value_;
Christopher Wileyd6bdd8d2016-05-03 11:23:13 -0700544
Steven Moreland693640b2018-07-19 13:46:27 -0700545 DISALLOW_COPY_AND_ASSIGN(AidlConstantDeclaration);
Casey Dahlind40e2fe2015-11-24 14:06:52 -0800546};
547
548class AidlMethod : public AidlMember {
Casey Dahlin5c69deb2015-10-01 14:44:12 -0700549 public:
Will McVickerd7d18df2019-09-12 13:40:50 -0700550 AidlMethod(const AidlLocation& location, bool oneway, AidlTypeSpecifier* type, const string& name,
551 vector<unique_ptr<AidlArgument>>* args, const string& comments);
552 AidlMethod(const AidlLocation& location, bool oneway, AidlTypeSpecifier* type, const string& name,
553 vector<unique_ptr<AidlArgument>>* args, const string& comments, int id,
554 bool is_user_defined = true);
Casey Dahlin5c69deb2015-10-01 14:44:12 -0700555 virtual ~AidlMethod() = default;
556
Casey Dahlind40e2fe2015-11-24 14:06:52 -0800557 AidlMethod* AsMethod() override { return this; }
Jeongik Cha997281d2020-01-16 15:23:59 +0900558 bool IsHidden() const;
Will McVickerd7d18df2019-09-12 13:40:50 -0700559 const string& GetComments() const { return comments_; }
Jiyong Parkd59a10d2018-07-18 11:12:55 +0900560 const AidlTypeSpecifier& GetType() const { return *type_; }
561 AidlTypeSpecifier* GetMutableType() { return type_.get(); }
Steven Morelandacd53472018-12-14 10:17:26 -0800562
Steven Moreland8c70ba92018-12-17 10:20:31 -0800563 // set if this method is part of an interface that is marked oneway
564 void ApplyInterfaceOneway(bool oneway) { oneway_ = oneway_ || oneway; }
Casey Dahlinf4a93112015-10-05 16:58:09 -0700565 bool IsOneway() const { return oneway_; }
Steven Morelandacd53472018-12-14 10:17:26 -0800566
Casey Dahlinf4a93112015-10-05 16:58:09 -0700567 const std::string& GetName() const { return name_; }
Casey Dahlinf4a93112015-10-05 16:58:09 -0700568 bool HasId() const { return has_id_; }
Jiyong Parked65bf42018-08-28 15:43:27 +0900569 int GetId() const { return id_; }
Casey Dahlinf4a93112015-10-05 16:58:09 -0700570 void SetId(unsigned id) { id_ = id; }
Casey Dahlinf2d23f72015-10-02 16:19:19 -0700571
Jiyong Park309668e2018-07-28 16:55:44 +0900572 bool IsUserDefined() const { return is_user_defined_; }
573
Casey Dahlinf4a93112015-10-05 16:58:09 -0700574 const std::vector<std::unique_ptr<AidlArgument>>& GetArguments() const {
Christopher Wileyad339272015-10-05 19:11:58 -0700575 return arguments_;
576 }
577 // An inout parameter will appear in both GetInArguments()
578 // and GetOutArguments(). AidlMethod retains ownership of the argument
579 // pointers returned in this way.
580 const std::vector<const AidlArgument*>& GetInArguments() const {
581 return in_arguments_;
582 }
583 const std::vector<const AidlArgument*>& GetOutArguments() const {
584 return out_arguments_;
Casey Dahlinf4a93112015-10-05 16:58:09 -0700585 }
Casey Dahlin5c69deb2015-10-01 14:44:12 -0700586
Jiyong Park309668e2018-07-28 16:55:44 +0900587 // name + type parameter types
588 // i.e, foo(int, String)
Jiyong Park02da7422018-07-16 16:00:26 +0900589 std::string Signature() const;
590
Jiyong Park309668e2018-07-28 16:55:44 +0900591 // return type + name + type parameter types + annotations
592 // i.e, boolean foo(int, @Nullable String)
593 std::string ToString() const;
594
Casey Dahlin5c69deb2015-10-01 14:44:12 -0700595 private:
Casey Dahlinf4a93112015-10-05 16:58:09 -0700596 bool oneway_;
Casey Dahlinf2d23f72015-10-02 16:19:19 -0700597 std::string comments_;
Jiyong Parkd59a10d2018-07-18 11:12:55 +0900598 std::unique_ptr<AidlTypeSpecifier> type_;
Casey Dahlinf4a93112015-10-05 16:58:09 -0700599 std::string name_;
Christopher Wileyad339272015-10-05 19:11:58 -0700600 const std::vector<std::unique_ptr<AidlArgument>> arguments_;
601 std::vector<const AidlArgument*> in_arguments_;
602 std::vector<const AidlArgument*> out_arguments_;
Casey Dahlinf4a93112015-10-05 16:58:09 -0700603 bool has_id_;
604 int id_;
Jiyong Park309668e2018-07-28 16:55:44 +0900605 bool is_user_defined_ = true;
Casey Dahlinf2d23f72015-10-02 16:19:19 -0700606
Casey Dahlin5c69deb2015-10-01 14:44:12 -0700607 DISALLOW_COPY_AND_ASSIGN(AidlMethod);
Casey Dahlin0a2f8be2015-09-28 16:15:29 -0700608};
Adam Lesinskiffa16862014-01-23 18:17:42 -0800609
Steven Morelandc258abc2018-07-10 14:03:38 -0700610class AidlDefinedType;
Jiyong Parkb034bf02018-07-30 17:44:33 +0900611class AidlInterface;
612class AidlParcelable;
613class AidlStructuredParcelable;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800614
Casey Dahlin2b2879b2015-10-13 16:59:44 -0700615class AidlQualifiedName : public AidlNode {
616 public:
Steven Moreland46e9da82018-07-27 15:45:29 -0700617 AidlQualifiedName(const AidlLocation& location, const std::string& term,
618 const std::string& comments);
Casey Dahlin2b2879b2015-10-13 16:59:44 -0700619 virtual ~AidlQualifiedName() = default;
620
621 const std::vector<std::string>& GetTerms() const { return terms_; }
622 const std::string& GetComments() const { return comments_; }
623 std::string GetDotName() const { return android::base::Join(terms_, '.'); }
Ningyuan Wangd17c58b2016-09-29 14:33:14 -0700624 std::string GetColonName() const { return android::base::Join(terms_, "::"); }
Casey Dahlin2b2879b2015-10-13 16:59:44 -0700625
Chih-Hung Hsiehf05cc262016-07-27 11:42:51 -0700626 void AddTerm(const std::string& term);
Casey Dahlin2b2879b2015-10-13 16:59:44 -0700627
628 private:
629 std::vector<std::string> terms_;
630 std::string comments_;
631
632 DISALLOW_COPY_AND_ASSIGN(AidlQualifiedName);
633};
634
Steven Moreland46e9da82018-07-27 15:45:29 -0700635class AidlInterface;
636class AidlParcelable;
637class AidlStructuredParcelable;
Daniel Norman85aed542019-08-21 12:01:14 -0700638// AidlDefinedType represents either an interface, parcelable, or enum that is
Jiyong Park1deecc32018-07-17 01:14:41 +0900639// defined in the source file.
640class AidlDefinedType : public AidlAnnotatable {
Steven Moreland787b0432018-07-03 09:00:58 -0700641 public:
Steven Moreland46e9da82018-07-27 15:45:29 -0700642 AidlDefinedType(const AidlLocation& location, const std::string& name,
643 const std::string& comments, const std::vector<std::string>& package);
Steven Moreland787b0432018-07-03 09:00:58 -0700644 virtual ~AidlDefinedType() = default;
645
Jiyong Park1deecc32018-07-17 01:14:41 +0900646 const std::string& GetName() const { return name_; };
Jeongik Cha997281d2020-01-16 15:23:59 +0900647 bool IsHidden() const;
Jiyong Park1deecc32018-07-17 01:14:41 +0900648 const std::string& GetComments() const { return comments_; }
Jiyong Parka6605ab2018-11-11 14:30:21 +0900649 void SetComments(const std::string comments) { comments_ = comments; }
Jiyong Park1deecc32018-07-17 01:14:41 +0900650
Steven Moreland787b0432018-07-03 09:00:58 -0700651 /* dot joined package, example: "android.package.foo" */
652 std::string GetPackage() const;
653 /* dot joined package and name, example: "android.package.foo.IBar" */
654 std::string GetCanonicalName() const;
655 const std::vector<std::string>& GetSplitPackage() const { return package_; }
656
Steven Morelanded83a282018-07-17 13:27:29 -0700657 virtual std::string GetPreprocessDeclarationName() const = 0;
Steven Morelandc258abc2018-07-10 14:03:38 -0700658
Steven Moreland5557f1c2018-07-02 13:50:23 -0700659 virtual const AidlStructuredParcelable* AsStructuredParcelable() const { return nullptr; }
Steven Morelandc258abc2018-07-10 14:03:38 -0700660 virtual const AidlParcelable* AsParcelable() const { return nullptr; }
Daniel Norman85aed542019-08-21 12:01:14 -0700661 virtual const AidlEnumDeclaration* AsEnumDeclaration() const { return nullptr; }
Steven Moreland5557f1c2018-07-02 13:50:23 -0700662 virtual const AidlInterface* AsInterface() const { return nullptr; }
Jeongik Chadf76dc72019-11-28 00:08:47 +0900663 virtual const AidlParameterizable<std::string>* AsParameterizable() const { return nullptr; }
Devin Moore24f68572020-02-26 13:20:59 -0800664 bool CheckValid(const AidlTypenames& typenames) const override;
Jeongik Cha047c5ee2019-08-07 23:16:49 +0900665 virtual bool LanguageSpecificCheckValid(Options::Language lang) const = 0;
Steven Morelandc258abc2018-07-10 14:03:38 -0700666 AidlStructuredParcelable* AsStructuredParcelable() {
667 return const_cast<AidlStructuredParcelable*>(
668 const_cast<const AidlDefinedType*>(this)->AsStructuredParcelable());
669 }
670 AidlParcelable* AsParcelable() {
671 return const_cast<AidlParcelable*>(const_cast<const AidlDefinedType*>(this)->AsParcelable());
672 }
Daniel Norman85aed542019-08-21 12:01:14 -0700673 AidlEnumDeclaration* AsEnumDeclaration() {
674 return const_cast<AidlEnumDeclaration*>(
675 const_cast<const AidlDefinedType*>(this)->AsEnumDeclaration());
676 }
Steven Morelandc258abc2018-07-10 14:03:38 -0700677 AidlInterface* AsInterface() {
678 return const_cast<AidlInterface*>(const_cast<const AidlDefinedType*>(this)->AsInterface());
679 }
680
Jeongik Chadf76dc72019-11-28 00:08:47 +0900681 AidlParameterizable<std::string>* AsParameterizable() {
682 return const_cast<AidlParameterizable<std::string>*>(
683 const_cast<const AidlDefinedType*>(this)->AsParameterizable());
684 }
685
Steven Moreland6cee3482018-07-18 14:39:58 -0700686 const AidlParcelable* AsUnstructuredParcelable() const {
687 if (this->AsStructuredParcelable() != nullptr) return nullptr;
688 return this->AsParcelable();
689 }
690 AidlParcelable* AsUnstructuredParcelable() {
691 return const_cast<AidlParcelable*>(
692 const_cast<const AidlDefinedType*>(this)->AsUnstructuredParcelable());
693 }
694
Jeongik Cha997281d2020-01-16 15:23:59 +0900695 virtual void Dump(CodeWriter* writer) const = 0;
Steven Morelanda5d9c5c2020-02-21 16:01:09 -0800696 void DumpHeader(CodeWriter* writer) const;
Jiyong Park02da7422018-07-16 16:00:26 +0900697
Steven Moreland787b0432018-07-03 09:00:58 -0700698 private:
Jiyong Park1deecc32018-07-17 01:14:41 +0900699 std::string name_;
Jiyong Park1deecc32018-07-17 01:14:41 +0900700 std::string comments_;
Steven Moreland787b0432018-07-03 09:00:58 -0700701 const std::vector<std::string> package_;
Steven Moreland5557f1c2018-07-02 13:50:23 -0700702
703 DISALLOW_COPY_AND_ASSIGN(AidlDefinedType);
Steven Moreland787b0432018-07-03 09:00:58 -0700704};
705
Jeongik Chadf76dc72019-11-28 00:08:47 +0900706class AidlParcelable : public AidlDefinedType, public AidlParameterizable<std::string> {
Casey Dahlin1ae2bc52015-10-07 18:49:10 -0700707 public:
Steven Moreland46e9da82018-07-27 15:45:29 -0700708 AidlParcelable(const AidlLocation& location, AidlQualifiedName* name,
Jiyong Parka6605ab2018-11-11 14:30:21 +0900709 const std::vector<std::string>& package, const std::string& comments,
Jeongik Chadf76dc72019-11-28 00:08:47 +0900710 const std::string& cpp_header = "",
711 std::vector<std::string>* type_params = nullptr);
Casey Dahlin1ae2bc52015-10-07 18:49:10 -0700712 virtual ~AidlParcelable() = default;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800713
Ningyuan Wangd17c58b2016-09-29 14:33:14 -0700714 // C++ uses "::" instead of "." to refer to a inner class.
715 std::string GetCppName() const { return name_->GetColonName(); }
Christopher Wiley8aa4d9f2015-11-16 19:10:45 -0800716 std::string GetCppHeader() const { return cpp_header_; }
Christopher Wiley8aa4d9f2015-11-16 19:10:45 -0800717
Devin Moore24f68572020-02-26 13:20:59 -0800718 std::set<string> GetSupportedAnnotations() const override;
Jeongik Cha82317dd2019-02-27 20:26:11 +0900719 bool CheckValid(const AidlTypenames& typenames) const override;
Jeongik Cha047c5ee2019-08-07 23:16:49 +0900720 bool LanguageSpecificCheckValid(Options::Language lang) const override;
Steven Morelandc258abc2018-07-10 14:03:38 -0700721 const AidlParcelable* AsParcelable() const override { return this; }
Jeongik Chadf76dc72019-11-28 00:08:47 +0900722 const AidlParameterizable<std::string>* AsParameterizable() const override { return this; }
723 const AidlNode& AsAidlNode() const override { return *this; }
Steven Morelanded83a282018-07-17 13:27:29 -0700724 std::string GetPreprocessDeclarationName() const override { return "parcelable"; }
Steven Morelandc258abc2018-07-10 14:03:38 -0700725
Jeongik Cha997281d2020-01-16 15:23:59 +0900726 void Dump(CodeWriter* writer) const override;
Jiyong Park02da7422018-07-16 16:00:26 +0900727
Casey Dahlin1ae2bc52015-10-07 18:49:10 -0700728 private:
Christopher Wiley8aa4d9f2015-11-16 19:10:45 -0800729 std::unique_ptr<AidlQualifiedName> name_;
Christopher Wiley8aa4d9f2015-11-16 19:10:45 -0800730 std::string cpp_header_;
Casey Dahlin59401da2015-10-09 18:16:45 -0700731
Casey Dahlin1ae2bc52015-10-07 18:49:10 -0700732 DISALLOW_COPY_AND_ASSIGN(AidlParcelable);
Casey Dahlin0a2f8be2015-09-28 16:15:29 -0700733};
Adam Lesinskiffa16862014-01-23 18:17:42 -0800734
Steven Moreland5557f1c2018-07-02 13:50:23 -0700735class AidlStructuredParcelable : public AidlParcelable {
736 public:
Steven Moreland46e9da82018-07-27 15:45:29 -0700737 AidlStructuredParcelable(const AidlLocation& location, AidlQualifiedName* name,
Jiyong Parka6605ab2018-11-11 14:30:21 +0900738 const std::vector<std::string>& package, const std::string& comments,
Steven Moreland5557f1c2018-07-02 13:50:23 -0700739 std::vector<std::unique_ptr<AidlVariableDeclaration>>* variables);
740
741 const std::vector<std::unique_ptr<AidlVariableDeclaration>>& GetFields() const {
742 return variables_;
743 }
744
Steven Morelandc258abc2018-07-10 14:03:38 -0700745 const AidlStructuredParcelable* AsStructuredParcelable() const override { return this; }
Steven Morelanded83a282018-07-17 13:27:29 -0700746 std::string GetPreprocessDeclarationName() const override { return "structured_parcelable"; }
Steven Moreland5557f1c2018-07-02 13:50:23 -0700747
Jeongik Cha997281d2020-01-16 15:23:59 +0900748 void Dump(CodeWriter* writer) const override;
Jiyong Park02da7422018-07-16 16:00:26 +0900749
Devin Moore24f68572020-02-26 13:20:59 -0800750 std::set<string> GetSupportedAnnotations() const override;
Jeongik Chadb0f59e2018-11-01 18:11:21 +0900751 bool CheckValid(const AidlTypenames& typenames) const override;
Jeongik Cha047c5ee2019-08-07 23:16:49 +0900752 bool LanguageSpecificCheckValid(Options::Language lang) const override;
Jeongik Chadb0f59e2018-11-01 18:11:21 +0900753
Steven Moreland5557f1c2018-07-02 13:50:23 -0700754 private:
755 const std::vector<std::unique_ptr<AidlVariableDeclaration>> variables_;
756
757 DISALLOW_COPY_AND_ASSIGN(AidlStructuredParcelable);
758};
759
Daniel Norman85aed542019-08-21 12:01:14 -0700760class AidlEnumerator : public AidlNode {
761 public:
Daniel Norman2e4112d2019-10-03 10:22:35 -0700762 AidlEnumerator(const AidlLocation& location, const std::string& name, AidlConstantValue* value,
763 const std::string& comments);
Daniel Norman85aed542019-08-21 12:01:14 -0700764 virtual ~AidlEnumerator() = default;
765
766 const std::string& GetName() const { return name_; }
Will McVickerd7d18df2019-09-12 13:40:50 -0700767 AidlConstantValue* GetValue() const { return value_.get(); }
Daniel Norman2e4112d2019-10-03 10:22:35 -0700768 const std::string& GetComments() const { return comments_; }
Daniel Norman85aed542019-08-21 12:01:14 -0700769 bool CheckValid(const AidlTypeSpecifier& enum_backing_type) const;
770
771 string ValueString(const AidlTypeSpecifier& backing_type,
772 const ConstantValueDecorator& decorator) const;
773
Daniel Normanb28684e2019-10-17 15:31:39 -0700774 void SetValue(std::unique_ptr<AidlConstantValue> value) { value_ = std::move(value); }
775
Daniel Norman85aed542019-08-21 12:01:14 -0700776 private:
777 const std::string name_;
Will McVickerd7d18df2019-09-12 13:40:50 -0700778 unique_ptr<AidlConstantValue> value_;
Daniel Norman2e4112d2019-10-03 10:22:35 -0700779 const std::string comments_;
Daniel Norman85aed542019-08-21 12:01:14 -0700780
781 DISALLOW_COPY_AND_ASSIGN(AidlEnumerator);
782};
783
784class AidlEnumDeclaration : public AidlDefinedType {
785 public:
Will McVickerd7d18df2019-09-12 13:40:50 -0700786 AidlEnumDeclaration(const AidlLocation& location, const string& name,
Daniel Norman85aed542019-08-21 12:01:14 -0700787 std::vector<std::unique_ptr<AidlEnumerator>>* enumerators,
Daniel Norman2e4112d2019-10-03 10:22:35 -0700788 const std::vector<std::string>& package, const std::string& comments);
Daniel Norman85aed542019-08-21 12:01:14 -0700789 virtual ~AidlEnumDeclaration() = default;
790
791 void SetBackingType(std::unique_ptr<const AidlTypeSpecifier> type);
792 const AidlTypeSpecifier& GetBackingType() const { return *backing_type_; }
793 const std::vector<std::unique_ptr<AidlEnumerator>>& GetEnumerators() const {
794 return enumerators_;
795 }
Steven Moreland59e53e42019-11-26 20:38:08 -0800796 bool Autofill();
Devin Moore24f68572020-02-26 13:20:59 -0800797 std::set<string> GetSupportedAnnotations() const override;
Daniel Norman85aed542019-08-21 12:01:14 -0700798 bool CheckValid(const AidlTypenames& typenames) const override;
799 bool LanguageSpecificCheckValid(Options::Language) const override { return true; }
800 std::string GetPreprocessDeclarationName() const override { return "enum"; }
Jeongik Cha997281d2020-01-16 15:23:59 +0900801 void Dump(CodeWriter* writer) const override;
Daniel Norman85aed542019-08-21 12:01:14 -0700802
803 const AidlEnumDeclaration* AsEnumDeclaration() const override { return this; }
804
805 private:
806 const std::string name_;
807 const std::vector<std::unique_ptr<AidlEnumerator>> enumerators_;
808 std::unique_ptr<const AidlTypeSpecifier> backing_type_;
809
810 DISALLOW_COPY_AND_ASSIGN(AidlEnumDeclaration);
811};
812
Jiyong Park1deecc32018-07-17 01:14:41 +0900813class AidlInterface final : public AidlDefinedType {
Casey Dahlin1ae2bc52015-10-07 18:49:10 -0700814 public:
Steven Moreland46e9da82018-07-27 15:45:29 -0700815 AidlInterface(const AidlLocation& location, const std::string& name, const std::string& comments,
816 bool oneway_, std::vector<std::unique_ptr<AidlMember>>* members,
Christopher Wileyd76067c2015-10-19 17:00:13 -0700817 const std::vector<std::string>& package);
Casey Dahlin1ae2bc52015-10-07 18:49:10 -0700818 virtual ~AidlInterface() = default;
819
Casey Dahlinfb7da2e2015-10-08 17:26:09 -0700820 const std::vector<std::unique_ptr<AidlMethod>>& GetMethods() const
821 { return methods_; }
Jiyong Park309668e2018-07-28 16:55:44 +0900822 std::vector<std::unique_ptr<AidlMethod>>& GetMutableMethods() { return methods_; }
Steven Moreland693640b2018-07-19 13:46:27 -0700823 const std::vector<std::unique_ptr<AidlConstantDeclaration>>& GetConstantDeclarations() const {
824 return constants_;
825 }
Casey Dahlina2f77c42015-12-01 18:26:02 -0800826
Steven Morelandc258abc2018-07-10 14:03:38 -0700827 const AidlInterface* AsInterface() const override { return this; }
Steven Morelanded83a282018-07-17 13:27:29 -0700828 std::string GetPreprocessDeclarationName() const override { return "interface"; }
Steven Moreland5557f1c2018-07-02 13:50:23 -0700829
Jeongik Cha997281d2020-01-16 15:23:59 +0900830 void Dump(CodeWriter* writer) const override;
Jiyong Park02da7422018-07-16 16:00:26 +0900831
Devin Moore24f68572020-02-26 13:20:59 -0800832 std::set<string> GetSupportedAnnotations() const override;
Jeongik Chadb0f59e2018-11-01 18:11:21 +0900833 bool CheckValid(const AidlTypenames& typenames) const override;
Jeongik Cha047c5ee2019-08-07 23:16:49 +0900834 bool LanguageSpecificCheckValid(Options::Language lang) const override;
Jeongik Chadb0f59e2018-11-01 18:11:21 +0900835
Casey Dahlin1ae2bc52015-10-07 18:49:10 -0700836 private:
Casey Dahlinfb7da2e2015-10-08 17:26:09 -0700837 std::vector<std::unique_ptr<AidlMethod>> methods_;
Steven Moreland693640b2018-07-19 13:46:27 -0700838 std::vector<std::unique_ptr<AidlConstantDeclaration>> constants_;
Casey Dahlinfb7da2e2015-10-08 17:26:09 -0700839
Casey Dahlin1ae2bc52015-10-07 18:49:10 -0700840 DISALLOW_COPY_AND_ASSIGN(AidlInterface);
Casey Dahlin0a2f8be2015-09-28 16:15:29 -0700841};
Adam Lesinskiffa16862014-01-23 18:17:42 -0800842
Casey Dahlin0edf3422015-10-07 12:34:59 -0700843class AidlImport : public AidlNode {
844 public:
Steven Moreland46e9da82018-07-27 15:45:29 -0700845 AidlImport(const AidlLocation& location, const std::string& needed_class);
Casey Dahlin0edf3422015-10-07 12:34:59 -0700846 virtual ~AidlImport() = default;
847
Casey Dahlin0edf3422015-10-07 12:34:59 -0700848 const std::string& GetFilename() const { return filename_; }
849 const std::string& GetNeededClass() const { return needed_class_; }
Casey Dahlin0edf3422015-10-07 12:34:59 -0700850
851 private:
Casey Dahlin0edf3422015-10-07 12:34:59 -0700852 std::string filename_;
853 std::string needed_class_;
Casey Dahlin0edf3422015-10-07 12:34:59 -0700854
855 DISALLOW_COPY_AND_ASSIGN(AidlImport);
Casey Dahline2507492015-09-14 17:11:20 -0700856};
857
858class Parser {
Casey Dahlindd691812015-09-09 17:59:06 -0700859 public:
Casey Dahline2507492015-09-14 17:11:20 -0700860 ~Parser();
Casey Dahlindd691812015-09-09 17:59:06 -0700861
Steven Moreland64e29be2018-08-08 18:52:19 -0700862 // Parse contents of file |filename|. Should only be called once.
863 static std::unique_ptr<Parser> Parse(const std::string& filename,
864 const android::aidl::IoDelegate& io_delegate,
865 AidlTypenames& typenames);
Casey Dahlin89d44842015-09-24 18:45:54 -0700866
Steven Moreland2ca4fcb2018-06-27 16:01:01 -0700867 void AddError() { error_++; }
Steven Moreland64e29be2018-08-08 18:52:19 -0700868 bool HasError() { return error_ != 0; }
Casey Dahlindd691812015-09-09 17:59:06 -0700869
Casey Dahlin3c6df362015-10-06 15:48:35 -0700870 const std::string& FileName() const { return filename_; }
Casey Dahlin42727f82015-10-12 19:23:40 -0700871 void* Scanner() const { return scanner_; }
Casey Dahlindd691812015-09-09 17:59:06 -0700872
Steven Morelandd1039a92020-01-23 09:49:43 -0800873 void AddImport(std::unique_ptr<AidlImport>&& import);
Christopher Wileyd76067c2015-10-19 17:00:13 -0700874 const std::vector<std::unique_ptr<AidlImport>>& GetImports() {
875 return imports_;
876 }
Casey Dahlindd691812015-09-09 17:59:06 -0700877
Jiyong Parkb034bf02018-07-30 17:44:33 +0900878 void SetPackage(unique_ptr<AidlQualifiedName> name) { package_ = std::move(name); }
879 std::vector<std::string> Package() const;
Jiyong Park1deecc32018-07-17 01:14:41 +0900880
881 void DeferResolution(AidlTypeSpecifier* typespec) {
882 unresolved_typespecs_.emplace_back(typespec);
883 }
884
Jiyong Parke59c3682018-09-11 23:10:25 +0900885 const vector<AidlTypeSpecifier*>& GetUnresolvedTypespecs() const { return unresolved_typespecs_; }
886
Jiyong Park1deecc32018-07-17 01:14:41 +0900887 bool Resolve();
888
Jiyong Parkb034bf02018-07-30 17:44:33 +0900889 void AddDefinedType(unique_ptr<AidlDefinedType> type) {
890 // Parser does NOT own AidlDefinedType, it just has references to the types
891 // that it encountered while parsing the input file.
892 defined_types_.emplace_back(type.get());
893
894 // AidlDefinedType IS owned by AidlTypenames
895 if (!typenames_.AddDefinedType(std::move(type))) {
896 AddError();
897 }
898 }
899
900 vector<AidlDefinedType*>& GetDefinedTypes() { return defined_types_; }
901
Casey Dahlindd691812015-09-09 17:59:06 -0700902 private:
Steven Moreland64e29be2018-08-08 18:52:19 -0700903 explicit Parser(const std::string& filename, std::string& raw_buffer,
904 android::aidl::AidlTypenames& typenames);
905
Casey Dahlindd691812015-09-09 17:59:06 -0700906 std::string filename_;
Christopher Wileyd76067c2015-10-19 17:00:13 -0700907 std::unique_ptr<AidlQualifiedName> package_;
Jiyong Parkb034bf02018-07-30 17:44:33 +0900908 AidlTypenames& typenames_;
Steven Moreland64e29be2018-08-08 18:52:19 -0700909
910 void* scanner_ = nullptr;
911 YY_BUFFER_STATE buffer_;
912 int error_ = 0;
913
914 std::vector<std::unique_ptr<AidlImport>> imports_;
Jiyong Parkb034bf02018-07-30 17:44:33 +0900915 vector<AidlDefinedType*> defined_types_;
Jiyong Park1deecc32018-07-17 01:14:41 +0900916 vector<AidlTypeSpecifier*> unresolved_typespecs_;
Casey Dahlindd691812015-09-09 17:59:06 -0700917
Casey Dahline2507492015-09-14 17:11:20 -0700918 DISALLOW_COPY_AND_ASSIGN(Parser);
Casey Dahlindd691812015-09-09 17:59:06 -0700919};