blob: 23638b012c7cadb55049c3be97bc44dae85e4638 [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;
Andrei Onea9445fc62019-06-27 18:11:59 +0100246
Steven Moreland7e4b9502020-02-20 18:10:42 -0800247 void DumpAnnotations(CodeWriter* writer) const;
248
Andrei Onea9445fc62019-06-27 18:11:59 +0100249 const AidlAnnotation* UnsupportedAppUsage() const;
Daniel Norman716d3112019-09-10 13:11:56 -0700250 const AidlTypeSpecifier* BackingType(const AidlTypenames& typenames) const;
Jiyong Park68bc77a2018-07-19 19:00:45 +0900251 std::string ToString() const;
Casey Dahline7922932016-02-29 17:23:01 -0800252
Jiyong Parka6605ab2018-11-11 14:30:21 +0900253 const vector<AidlAnnotation>& GetAnnotations() const { return annotations_; }
Andrei Onea9445fc62019-06-27 18:11:59 +0100254 bool CheckValidAnnotations() const;
Jiyong Park3656c3c2018-08-01 20:02:01 +0900255
Casey Dahline7922932016-02-29 17:23:01 -0800256 private:
Jiyong Parka6605ab2018-11-11 14:30:21 +0900257 vector<AidlAnnotation> annotations_;
Casey Dahline7922932016-02-29 17:23:01 -0800258};
259
Jiyong Park1deecc32018-07-17 01:14:41 +0900260class AidlQualifiedName;
261
262// AidlTypeSpecifier represents a reference to either a built-in type,
263// a defined type, or a variant (e.g., array of generic) of a type.
Jeongik Chadf76dc72019-11-28 00:08:47 +0900264class AidlTypeSpecifier final : public AidlAnnotatable,
265 public AidlParameterizable<unique_ptr<AidlTypeSpecifier>> {
Casey Dahline7922932016-02-29 17:23:01 -0800266 public:
Steven Moreland46e9da82018-07-27 15:45:29 -0700267 AidlTypeSpecifier(const AidlLocation& location, const string& unresolved_name, bool is_array,
268 vector<unique_ptr<AidlTypeSpecifier>>* type_params, const string& comments);
Jiyong Parkd59a10d2018-07-18 11:12:55 +0900269 virtual ~AidlTypeSpecifier() = default;
Casey Dahlin0ee37582015-09-30 16:31:55 -0700270
Steven Moreland3f658cf2018-08-20 13:40:54 -0700271 // Copy of this type which is not an array.
272 AidlTypeSpecifier ArrayBase() const;
273
Jiyong Park1deecc32018-07-17 01:14:41 +0900274 // Returns the full-qualified name of the base type.
275 // int -> int
276 // int[] -> int
277 // List<String> -> List
278 // IFoo -> foo.bar.IFoo (if IFoo is in package foo.bar)
279 const string& GetName() const {
280 if (IsResolved()) {
281 return fully_qualified_name_;
282 } else {
283 return GetUnresolvedName();
284 }
285 }
Casey Dahlin0ee37582015-09-30 16:31:55 -0700286
Jiyong Park1deecc32018-07-17 01:14:41 +0900287 // Returns string representation of this type specifier.
Artur Satayev91fe8712019-07-29 13:06:01 +0100288 // This is GetBaseTypeName() + array modifier or generic type parameters
Jiyong Park1deecc32018-07-17 01:14:41 +0900289 string ToString() const;
290
Jiyong Park02da7422018-07-16 16:00:26 +0900291 std::string Signature() const;
292
Jiyong Park1deecc32018-07-17 01:14:41 +0900293 const string& GetUnresolvedName() const { return unresolved_name_; }
294
Jeongik Cha997281d2020-01-16 15:23:59 +0900295 bool IsHidden() const;
296
Jiyong Park1deecc32018-07-17 01:14:41 +0900297 const string& GetComments() const { return comments_; }
298
Jeongik Cha1a7ab642019-07-29 17:31:02 +0900299 const std::vector<std::string> GetSplitName() const { return split_name_; }
300
Jiyong Parka6605ab2018-11-11 14:30:21 +0900301 void SetComments(const string& comment) { comments_ = comment; }
302
Jiyong Park1deecc32018-07-17 01:14:41 +0900303 bool IsResolved() const { return fully_qualified_name_ != ""; }
304
305 bool IsArray() const { return is_array_; }
306
Jiyong Park1deecc32018-07-17 01:14:41 +0900307 // Resolve the base type name to a fully-qualified name. Return false if the
308 // resolution fails.
Daniel Norman716d3112019-09-10 13:11:56 -0700309 bool Resolve(const AidlTypenames& typenames);
Casey Dahlin0ee37582015-09-30 16:31:55 -0700310
Jeongik Chadb0f59e2018-11-01 18:11:21 +0900311 bool CheckValid(const AidlTypenames& typenames) const;
Jeongik Cha047c5ee2019-08-07 23:16:49 +0900312 bool LanguageSpecificCheckValid(Options::Language lang) const;
Jeongik Chadf76dc72019-11-28 00:08:47 +0900313 const AidlNode& AsAidlNode() const override { return *this; }
Jiyong Park1d2df7d2018-07-23 15:22:50 +0900314
Casey Dahlin0ee37582015-09-30 16:31:55 -0700315 private:
Steven Moreland3f658cf2018-08-20 13:40:54 -0700316 AidlTypeSpecifier(const AidlTypeSpecifier&) = default;
317
Jiyong Park1deecc32018-07-17 01:14:41 +0900318 const string unresolved_name_;
319 string fully_qualified_name_;
Steven Moreland3f658cf2018-08-20 13:40:54 -0700320 bool is_array_;
Jiyong Parka6605ab2018-11-11 14:30:21 +0900321 string comments_;
Jeongik Cha1a7ab642019-07-29 17:31:02 +0900322 vector<string> split_name_;
Casey Dahlin0ee37582015-09-30 16:31:55 -0700323};
324
Steven Moreland860b1942018-08-16 14:59:28 -0700325// Returns the universal value unaltered.
326std::string AidlConstantValueDecorator(const AidlTypeSpecifier& type, const std::string& raw_value);
327
Steven Moreland9ea10e32018-07-19 15:26:09 -0700328class AidlConstantValue;
Steven Moreland5557f1c2018-07-02 13:50:23 -0700329class AidlVariableDeclaration : public AidlNode {
330 public:
Steven Moreland46e9da82018-07-27 15:45:29 -0700331 AidlVariableDeclaration(const AidlLocation& location, AidlTypeSpecifier* type,
332 const std::string& name);
333 AidlVariableDeclaration(const AidlLocation& location, AidlTypeSpecifier* type,
334 const std::string& name, AidlConstantValue* default_value);
Steven Moreland5557f1c2018-07-02 13:50:23 -0700335 virtual ~AidlVariableDeclaration() = default;
336
337 std::string GetName() const { return name_; }
Jiyong Parkd59a10d2018-07-18 11:12:55 +0900338 const AidlTypeSpecifier& GetType() const { return *type_; }
Steven Moreland9ea10e32018-07-19 15:26:09 -0700339 const AidlConstantValue* GetDefaultValue() const { return default_value_.get(); }
340
Jiyong Parkd59a10d2018-07-18 11:12:55 +0900341 AidlTypeSpecifier* GetMutableType() { return type_.get(); }
Steven Moreland5557f1c2018-07-02 13:50:23 -0700342
Jeongik Chadb0f59e2018-11-01 18:11:21 +0900343 bool CheckValid(const AidlTypenames& typenames) const;
Steven Moreland5557f1c2018-07-02 13:50:23 -0700344 std::string ToString() const;
Jiyong Park02da7422018-07-16 16:00:26 +0900345 std::string Signature() const;
Steven Moreland5557f1c2018-07-02 13:50:23 -0700346
Steven Moreland860b1942018-08-16 14:59:28 -0700347 std::string ValueString(const ConstantValueDecorator& decorator) const;
Steven Moreland25294322018-08-07 18:13:55 -0700348
Steven Moreland5557f1c2018-07-02 13:50:23 -0700349 private:
Jiyong Parkd59a10d2018-07-18 11:12:55 +0900350 std::unique_ptr<AidlTypeSpecifier> type_;
Steven Moreland5557f1c2018-07-02 13:50:23 -0700351 std::string name_;
Steven Moreland9ea10e32018-07-19 15:26:09 -0700352 std::unique_ptr<AidlConstantValue> default_value_;
Steven Moreland5557f1c2018-07-02 13:50:23 -0700353
354 DISALLOW_COPY_AND_ASSIGN(AidlVariableDeclaration);
355};
356
357class AidlArgument : public AidlVariableDeclaration {
Casey Dahlinbc7a50a2015-09-28 19:20:50 -0700358 public:
Casey Dahlinc378c992015-09-29 16:50:40 -0700359 enum Direction { IN_DIR = 1, OUT_DIR = 2, INOUT_DIR = 3 };
360
Steven Moreland46e9da82018-07-27 15:45:29 -0700361 AidlArgument(const AidlLocation& location, AidlArgument::Direction direction,
362 AidlTypeSpecifier* type, const std::string& name);
363 AidlArgument(const AidlLocation& location, AidlTypeSpecifier* type, const std::string& name);
Casey Dahlinbc7a50a2015-09-28 19:20:50 -0700364 virtual ~AidlArgument() = default;
365
Casey Dahlinc378c992015-09-29 16:50:40 -0700366 Direction GetDirection() const { return direction_; }
Christopher Wileyad339272015-10-05 19:11:58 -0700367 bool IsOut() const { return direction_ & OUT_DIR; }
368 bool IsIn() const { return direction_ & IN_DIR; }
Casey Dahlinc378c992015-09-29 16:50:40 -0700369 bool DirectionWasSpecified() const { return direction_specified_; }
Jiyong Park3656c3c2018-08-01 20:02:01 +0900370 string GetDirectionSpecifier() const;
Christopher Wileyad339272015-10-05 19:11:58 -0700371
Casey Dahlinc378c992015-09-29 16:50:40 -0700372 std::string ToString() const;
Jiyong Park02da7422018-07-16 16:00:26 +0900373 std::string Signature() const;
Casey Dahlinc378c992015-09-29 16:50:40 -0700374
Casey Dahlinbc7a50a2015-09-28 19:20:50 -0700375 private:
Casey Dahlinc378c992015-09-29 16:50:40 -0700376 Direction direction_;
377 bool direction_specified_;
378
Casey Dahlinbc7a50a2015-09-28 19:20:50 -0700379 DISALLOW_COPY_AND_ASSIGN(AidlArgument);
Casey Dahlina834dd42015-09-23 11:52:15 -0700380};
Adam Lesinskiffa16862014-01-23 18:17:42 -0800381
Casey Dahlind40e2fe2015-11-24 14:06:52 -0800382class AidlMethod;
Steven Moreland693640b2018-07-19 13:46:27 -0700383class AidlConstantDeclaration;
Daniel Norman85aed542019-08-21 12:01:14 -0700384class AidlEnumDeclaration;
Casey Dahlind40e2fe2015-11-24 14:06:52 -0800385class AidlMember : public AidlNode {
386 public:
Steven Moreland46e9da82018-07-27 15:45:29 -0700387 AidlMember(const AidlLocation& location);
Casey Dahlind40e2fe2015-11-24 14:06:52 -0800388 virtual ~AidlMember() = default;
389
390 virtual AidlMethod* AsMethod() { return nullptr; }
Steven Moreland693640b2018-07-19 13:46:27 -0700391 virtual AidlConstantDeclaration* AsConstantDeclaration() { return nullptr; }
Casey Dahlind40e2fe2015-11-24 14:06:52 -0800392
393 private:
394 DISALLOW_COPY_AND_ASSIGN(AidlMember);
395};
396
Will McVickerd7d18df2019-09-12 13:40:50 -0700397class AidlUnaryConstExpression;
398class AidlBinaryConstExpression;
399
Steven Moreland693640b2018-07-19 13:46:27 -0700400class AidlConstantValue : public AidlNode {
Casey Dahlind40e2fe2015-11-24 14:06:52 -0800401 public:
Will McVickerd7d18df2019-09-12 13:40:50 -0700402 enum class Type {
403 // WARNING: Don't change this order! The order is used to determine type
404 // promotion during a binary expression.
405 BOOLEAN,
406 INT8,
407 INT32,
408 INT64,
409 ARRAY,
410 CHARACTER,
411 STRING,
412 FLOATING,
413 UNARY,
414 BINARY,
415 ERROR,
416 };
417
418 /*
419 * Return the value casted to the given type.
420 */
421 template <typename T>
422 T cast() const;
Casey Dahlind40e2fe2015-11-24 14:06:52 -0800423
Steven Moreland693640b2018-07-19 13:46:27 -0700424 virtual ~AidlConstantValue() = default;
Casey Dahlind40e2fe2015-11-24 14:06:52 -0800425
Steven Moreland25294322018-08-07 18:13:55 -0700426 static AidlConstantValue* Boolean(const AidlLocation& location, bool value);
427 static AidlConstantValue* Character(const AidlLocation& location, char value);
Steven Moreland25294322018-08-07 18:13:55 -0700428 // example: 123, -5498, maybe any size
Will McVickerd7d18df2019-09-12 13:40:50 -0700429 static AidlConstantValue* Integral(const AidlLocation& location, const string& value);
430 static AidlConstantValue* Floating(const AidlLocation& location, const std::string& value);
Steven Moreland860b1942018-08-16 14:59:28 -0700431 static AidlConstantValue* Array(const AidlLocation& location,
Will McVickerd7d18df2019-09-12 13:40:50 -0700432 std::unique_ptr<vector<unique_ptr<AidlConstantValue>>> values);
Steven Moreland693640b2018-07-19 13:46:27 -0700433 // example: "\"asdf\""
Will McVickerd7d18df2019-09-12 13:40:50 -0700434 static AidlConstantValue* String(const AidlLocation& location, const string& value);
Steven Moreland693640b2018-07-19 13:46:27 -0700435
Daniel Normanf0ca44f2019-10-25 09:59:44 -0700436 // Construct an AidlConstantValue by evaluating the other integral constant's
437 // value string. This does not preserve the structure of the copied constant.
Steven Moreland59e53e42019-11-26 20:38:08 -0800438 // Returns nullptr and logs if value cannot be copied.
Daniel Normanf0ca44f2019-10-25 09:59:44 -0700439 static AidlConstantValue* ShallowIntegralCopy(const AidlConstantValue& other);
Daniel Normanb28684e2019-10-17 15:31:39 -0700440
Will McVickerd7d18df2019-09-12 13:40:50 -0700441 Type GetType() const { return final_type_; }
Steven Moreland25294322018-08-07 18:13:55 -0700442
Will McVickerd7d18df2019-09-12 13:40:50 -0700443 virtual bool CheckValid() const;
Steven Moreland860b1942018-08-16 14:59:28 -0700444
445 // Raw value of type (currently valid in C++ and Java). Empty string on error.
Steven Moreland4bcb05c2019-11-27 18:57:47 -0800446 string ValueString(const AidlTypeSpecifier& type, const ConstantValueDecorator& decorator) const;
Casey Dahlind40e2fe2015-11-24 14:06:52 -0800447
448 private:
Will McVickerd7d18df2019-09-12 13:40:50 -0700449 AidlConstantValue(const AidlLocation& location, Type parsed_type, int64_t parsed_value,
450 const string& checked_value);
451 AidlConstantValue(const AidlLocation& location, Type type, const string& checked_value);
Steven Moreland860b1942018-08-16 14:59:28 -0700452 AidlConstantValue(const AidlLocation& location, Type type,
Will McVickerd7d18df2019-09-12 13:40:50 -0700453 std::unique_ptr<vector<unique_ptr<AidlConstantValue>>> values);
Steven Moreland25294322018-08-07 18:13:55 -0700454 static string ToString(Type type);
Will McVickerd7d18df2019-09-12 13:40:50 -0700455 static bool ParseIntegral(const string& value, int64_t* parsed_value, Type* parsed_type);
456 static bool IsHex(const string& value);
Steven Moreland4bcb05c2019-11-27 18:57:47 -0800457
Will McVickerd7d18df2019-09-12 13:40:50 -0700458 virtual bool evaluate(const AidlTypeSpecifier& type) const;
Casey Dahlind40e2fe2015-11-24 14:06:52 -0800459
Steven Moreland693640b2018-07-19 13:46:27 -0700460 const Type type_ = Type::ERROR;
Will McVickerd7d18df2019-09-12 13:40:50 -0700461 const vector<unique_ptr<AidlConstantValue>> values_; // if type_ == ARRAY
462 const string value_; // otherwise
463
464 // State for tracking evaluation of expressions
Steven Moreland4bcb05c2019-11-27 18:57:47 -0800465 mutable bool is_valid_ = false; // cache of CheckValid, but may be marked false in evaluate
466 mutable bool is_evaluated_ = false; // whether evaluate has been called
Will McVickerd7d18df2019-09-12 13:40:50 -0700467 mutable Type final_type_;
468 mutable int64_t final_value_;
469 mutable string final_string_value_ = "";
Steven Moreland693640b2018-07-19 13:46:27 -0700470
471 DISALLOW_COPY_AND_ASSIGN(AidlConstantValue);
Will McVickerd7d18df2019-09-12 13:40:50 -0700472
473 friend AidlUnaryConstExpression;
474 friend AidlBinaryConstExpression;
475};
476
477class AidlUnaryConstExpression : public AidlConstantValue {
478 public:
479 AidlUnaryConstExpression(const AidlLocation& location, const string& op,
480 std::unique_ptr<AidlConstantValue> rval);
481
482 static bool IsCompatibleType(Type type, const string& op);
483 bool CheckValid() const override;
Will McVickerd7d18df2019-09-12 13:40:50 -0700484 private:
485 bool evaluate(const AidlTypeSpecifier& type) const override;
486
487 std::unique_ptr<AidlConstantValue> unary_;
488 const string op_;
489};
490
491class AidlBinaryConstExpression : public AidlConstantValue {
492 public:
493 AidlBinaryConstExpression(const AidlLocation& location, std::unique_ptr<AidlConstantValue> lval,
494 const string& op, std::unique_ptr<AidlConstantValue> rval);
495
496 bool CheckValid() const override;
Will McVickerd7d18df2019-09-12 13:40:50 -0700497
498 static bool AreCompatibleTypes(Type t1, Type t2);
499 // Returns the promoted kind for both operands
500 static Type UsualArithmeticConversion(Type left, Type right);
501 // Returns the promoted integral type where INT32 is the smallest type
502 static Type IntegralPromotion(Type in);
503
504 private:
505 bool evaluate(const AidlTypeSpecifier& type) const override;
506
507 std::unique_ptr<AidlConstantValue> left_val_;
508 std::unique_ptr<AidlConstantValue> right_val_;
509 const string op_;
Christopher Wileyd6bdd8d2016-05-03 11:23:13 -0700510};
511
Andrei Onea9445fc62019-06-27 18:11:59 +0100512struct AidlAnnotationParameter {
513 std::string name;
514 std::unique_ptr<AidlConstantValue> value;
515};
516
Steven Moreland693640b2018-07-19 13:46:27 -0700517class AidlConstantDeclaration : public AidlMember {
Christopher Wileyd6bdd8d2016-05-03 11:23:13 -0700518 public:
Steven Moreland46e9da82018-07-27 15:45:29 -0700519 AidlConstantDeclaration(const AidlLocation& location, AidlTypeSpecifier* specifier,
Will McVickerd7d18df2019-09-12 13:40:50 -0700520 const string& name, AidlConstantValue* value);
Steven Moreland693640b2018-07-19 13:46:27 -0700521 virtual ~AidlConstantDeclaration() = default;
Christopher Wileyd6bdd8d2016-05-03 11:23:13 -0700522
Steven Moreland693640b2018-07-19 13:46:27 -0700523 const AidlTypeSpecifier& GetType() const { return *type_; }
Steven Moreland4d12f9a2018-10-31 14:30:55 -0700524 AidlTypeSpecifier* GetMutableType() { return type_.get(); }
Will McVickerd7d18df2019-09-12 13:40:50 -0700525 const string& GetName() const { return name_; }
Steven Moreland693640b2018-07-19 13:46:27 -0700526 const AidlConstantValue& GetValue() const { return *value_; }
Jeongik Chadb0f59e2018-11-01 18:11:21 +0900527 bool CheckValid(const AidlTypenames& typenames) const;
Christopher Wileyd6bdd8d2016-05-03 11:23:13 -0700528
Will McVickerd7d18df2019-09-12 13:40:50 -0700529 string ToString() const;
530 string Signature() const;
Steven Moreland860b1942018-08-16 14:59:28 -0700531 string ValueString(const ConstantValueDecorator& decorator) const {
Will McVickerd7d18df2019-09-12 13:40:50 -0700532 return value_->ValueString(GetType(), decorator);
Steven Moreland860b1942018-08-16 14:59:28 -0700533 }
Steven Moreland25294322018-08-07 18:13:55 -0700534
Steven Moreland693640b2018-07-19 13:46:27 -0700535 AidlConstantDeclaration* AsConstantDeclaration() override { return this; }
Christopher Wileyd6bdd8d2016-05-03 11:23:13 -0700536
537 private:
Steven Moreland693640b2018-07-19 13:46:27 -0700538 const unique_ptr<AidlTypeSpecifier> type_;
Will McVickerd7d18df2019-09-12 13:40:50 -0700539 const string name_;
540 unique_ptr<AidlConstantValue> value_;
Christopher Wileyd6bdd8d2016-05-03 11:23:13 -0700541
Steven Moreland693640b2018-07-19 13:46:27 -0700542 DISALLOW_COPY_AND_ASSIGN(AidlConstantDeclaration);
Casey Dahlind40e2fe2015-11-24 14:06:52 -0800543};
544
545class AidlMethod : public AidlMember {
Casey Dahlin5c69deb2015-10-01 14:44:12 -0700546 public:
Will McVickerd7d18df2019-09-12 13:40:50 -0700547 AidlMethod(const AidlLocation& location, bool oneway, AidlTypeSpecifier* type, const string& name,
548 vector<unique_ptr<AidlArgument>>* args, const string& comments);
549 AidlMethod(const AidlLocation& location, bool oneway, AidlTypeSpecifier* type, const string& name,
550 vector<unique_ptr<AidlArgument>>* args, const string& comments, int id,
551 bool is_user_defined = true);
Casey Dahlin5c69deb2015-10-01 14:44:12 -0700552 virtual ~AidlMethod() = default;
553
Casey Dahlind40e2fe2015-11-24 14:06:52 -0800554 AidlMethod* AsMethod() override { return this; }
Jeongik Cha997281d2020-01-16 15:23:59 +0900555 bool IsHidden() const;
Will McVickerd7d18df2019-09-12 13:40:50 -0700556 const string& GetComments() const { return comments_; }
Jiyong Parkd59a10d2018-07-18 11:12:55 +0900557 const AidlTypeSpecifier& GetType() const { return *type_; }
558 AidlTypeSpecifier* GetMutableType() { return type_.get(); }
Steven Morelandacd53472018-12-14 10:17:26 -0800559
Steven Moreland8c70ba92018-12-17 10:20:31 -0800560 // set if this method is part of an interface that is marked oneway
561 void ApplyInterfaceOneway(bool oneway) { oneway_ = oneway_ || oneway; }
Casey Dahlinf4a93112015-10-05 16:58:09 -0700562 bool IsOneway() const { return oneway_; }
Steven Morelandacd53472018-12-14 10:17:26 -0800563
Casey Dahlinf4a93112015-10-05 16:58:09 -0700564 const std::string& GetName() const { return name_; }
Casey Dahlinf4a93112015-10-05 16:58:09 -0700565 bool HasId() const { return has_id_; }
Jiyong Parked65bf42018-08-28 15:43:27 +0900566 int GetId() const { return id_; }
Casey Dahlinf4a93112015-10-05 16:58:09 -0700567 void SetId(unsigned id) { id_ = id; }
Casey Dahlinf2d23f72015-10-02 16:19:19 -0700568
Jiyong Park309668e2018-07-28 16:55:44 +0900569 bool IsUserDefined() const { return is_user_defined_; }
570
Casey Dahlinf4a93112015-10-05 16:58:09 -0700571 const std::vector<std::unique_ptr<AidlArgument>>& GetArguments() const {
Christopher Wileyad339272015-10-05 19:11:58 -0700572 return arguments_;
573 }
574 // An inout parameter will appear in both GetInArguments()
575 // and GetOutArguments(). AidlMethod retains ownership of the argument
576 // pointers returned in this way.
577 const std::vector<const AidlArgument*>& GetInArguments() const {
578 return in_arguments_;
579 }
580 const std::vector<const AidlArgument*>& GetOutArguments() const {
581 return out_arguments_;
Casey Dahlinf4a93112015-10-05 16:58:09 -0700582 }
Casey Dahlin5c69deb2015-10-01 14:44:12 -0700583
Jiyong Park309668e2018-07-28 16:55:44 +0900584 // name + type parameter types
585 // i.e, foo(int, String)
Jiyong Park02da7422018-07-16 16:00:26 +0900586 std::string Signature() const;
587
Jiyong Park309668e2018-07-28 16:55:44 +0900588 // return type + name + type parameter types + annotations
589 // i.e, boolean foo(int, @Nullable String)
590 std::string ToString() const;
591
Casey Dahlin5c69deb2015-10-01 14:44:12 -0700592 private:
Casey Dahlinf4a93112015-10-05 16:58:09 -0700593 bool oneway_;
Casey Dahlinf2d23f72015-10-02 16:19:19 -0700594 std::string comments_;
Jiyong Parkd59a10d2018-07-18 11:12:55 +0900595 std::unique_ptr<AidlTypeSpecifier> type_;
Casey Dahlinf4a93112015-10-05 16:58:09 -0700596 std::string name_;
Christopher Wileyad339272015-10-05 19:11:58 -0700597 const std::vector<std::unique_ptr<AidlArgument>> arguments_;
598 std::vector<const AidlArgument*> in_arguments_;
599 std::vector<const AidlArgument*> out_arguments_;
Casey Dahlinf4a93112015-10-05 16:58:09 -0700600 bool has_id_;
601 int id_;
Jiyong Park309668e2018-07-28 16:55:44 +0900602 bool is_user_defined_ = true;
Casey Dahlinf2d23f72015-10-02 16:19:19 -0700603
Casey Dahlin5c69deb2015-10-01 14:44:12 -0700604 DISALLOW_COPY_AND_ASSIGN(AidlMethod);
Casey Dahlin0a2f8be2015-09-28 16:15:29 -0700605};
Adam Lesinskiffa16862014-01-23 18:17:42 -0800606
Steven Morelandc258abc2018-07-10 14:03:38 -0700607class AidlDefinedType;
Jiyong Parkb034bf02018-07-30 17:44:33 +0900608class AidlInterface;
609class AidlParcelable;
610class AidlStructuredParcelable;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800611
Casey Dahlin2b2879b2015-10-13 16:59:44 -0700612class AidlQualifiedName : public AidlNode {
613 public:
Steven Moreland46e9da82018-07-27 15:45:29 -0700614 AidlQualifiedName(const AidlLocation& location, const std::string& term,
615 const std::string& comments);
Casey Dahlin2b2879b2015-10-13 16:59:44 -0700616 virtual ~AidlQualifiedName() = default;
617
618 const std::vector<std::string>& GetTerms() const { return terms_; }
619 const std::string& GetComments() const { return comments_; }
620 std::string GetDotName() const { return android::base::Join(terms_, '.'); }
Ningyuan Wangd17c58b2016-09-29 14:33:14 -0700621 std::string GetColonName() const { return android::base::Join(terms_, "::"); }
Casey Dahlin2b2879b2015-10-13 16:59:44 -0700622
Chih-Hung Hsiehf05cc262016-07-27 11:42:51 -0700623 void AddTerm(const std::string& term);
Casey Dahlin2b2879b2015-10-13 16:59:44 -0700624
625 private:
626 std::vector<std::string> terms_;
627 std::string comments_;
628
629 DISALLOW_COPY_AND_ASSIGN(AidlQualifiedName);
630};
631
Steven Moreland46e9da82018-07-27 15:45:29 -0700632class AidlInterface;
633class AidlParcelable;
634class AidlStructuredParcelable;
Daniel Norman85aed542019-08-21 12:01:14 -0700635// AidlDefinedType represents either an interface, parcelable, or enum that is
Jiyong Park1deecc32018-07-17 01:14:41 +0900636// defined in the source file.
637class AidlDefinedType : public AidlAnnotatable {
Steven Moreland787b0432018-07-03 09:00:58 -0700638 public:
Steven Moreland46e9da82018-07-27 15:45:29 -0700639 AidlDefinedType(const AidlLocation& location, const std::string& name,
640 const std::string& comments, const std::vector<std::string>& package);
Steven Moreland787b0432018-07-03 09:00:58 -0700641 virtual ~AidlDefinedType() = default;
642
Jiyong Park1deecc32018-07-17 01:14:41 +0900643 const std::string& GetName() const { return name_; };
Jeongik Cha997281d2020-01-16 15:23:59 +0900644 bool IsHidden() const;
Jiyong Park1deecc32018-07-17 01:14:41 +0900645 const std::string& GetComments() const { return comments_; }
Jiyong Parka6605ab2018-11-11 14:30:21 +0900646 void SetComments(const std::string comments) { comments_ = comments; }
Jiyong Park1deecc32018-07-17 01:14:41 +0900647
Steven Moreland787b0432018-07-03 09:00:58 -0700648 /* dot joined package, example: "android.package.foo" */
649 std::string GetPackage() const;
650 /* dot joined package and name, example: "android.package.foo.IBar" */
651 std::string GetCanonicalName() const;
652 const std::vector<std::string>& GetSplitPackage() const { return package_; }
653
Steven Morelanded83a282018-07-17 13:27:29 -0700654 virtual std::string GetPreprocessDeclarationName() const = 0;
Steven Morelandc258abc2018-07-10 14:03:38 -0700655
Steven Moreland5557f1c2018-07-02 13:50:23 -0700656 virtual const AidlStructuredParcelable* AsStructuredParcelable() const { return nullptr; }
Steven Morelandc258abc2018-07-10 14:03:38 -0700657 virtual const AidlParcelable* AsParcelable() const { return nullptr; }
Daniel Norman85aed542019-08-21 12:01:14 -0700658 virtual const AidlEnumDeclaration* AsEnumDeclaration() const { return nullptr; }
Steven Moreland5557f1c2018-07-02 13:50:23 -0700659 virtual const AidlInterface* AsInterface() const { return nullptr; }
Jeongik Chadf76dc72019-11-28 00:08:47 +0900660 virtual const AidlParameterizable<std::string>* AsParameterizable() const { return nullptr; }
Andrei Onea9445fc62019-06-27 18:11:59 +0100661 virtual bool CheckValid(const AidlTypenames&) const { return CheckValidAnnotations(); }
Jeongik Cha047c5ee2019-08-07 23:16:49 +0900662 virtual bool LanguageSpecificCheckValid(Options::Language lang) const = 0;
Steven Morelandc258abc2018-07-10 14:03:38 -0700663 AidlStructuredParcelable* AsStructuredParcelable() {
664 return const_cast<AidlStructuredParcelable*>(
665 const_cast<const AidlDefinedType*>(this)->AsStructuredParcelable());
666 }
667 AidlParcelable* AsParcelable() {
668 return const_cast<AidlParcelable*>(const_cast<const AidlDefinedType*>(this)->AsParcelable());
669 }
Daniel Norman85aed542019-08-21 12:01:14 -0700670 AidlEnumDeclaration* AsEnumDeclaration() {
671 return const_cast<AidlEnumDeclaration*>(
672 const_cast<const AidlDefinedType*>(this)->AsEnumDeclaration());
673 }
Steven Morelandc258abc2018-07-10 14:03:38 -0700674 AidlInterface* AsInterface() {
675 return const_cast<AidlInterface*>(const_cast<const AidlDefinedType*>(this)->AsInterface());
676 }
677
Jeongik Chadf76dc72019-11-28 00:08:47 +0900678 AidlParameterizable<std::string>* AsParameterizable() {
679 return const_cast<AidlParameterizable<std::string>*>(
680 const_cast<const AidlDefinedType*>(this)->AsParameterizable());
681 }
682
Steven Moreland6cee3482018-07-18 14:39:58 -0700683 const AidlParcelable* AsUnstructuredParcelable() const {
684 if (this->AsStructuredParcelable() != nullptr) return nullptr;
685 return this->AsParcelable();
686 }
687 AidlParcelable* AsUnstructuredParcelable() {
688 return const_cast<AidlParcelable*>(
689 const_cast<const AidlDefinedType*>(this)->AsUnstructuredParcelable());
690 }
691
Jeongik Cha997281d2020-01-16 15:23:59 +0900692 virtual void Dump(CodeWriter* writer) const = 0;
Steven Morelanda5d9c5c2020-02-21 16:01:09 -0800693 void DumpHeader(CodeWriter* writer) const;
Jiyong Park02da7422018-07-16 16:00:26 +0900694
Steven Moreland787b0432018-07-03 09:00:58 -0700695 private:
Jiyong Park1deecc32018-07-17 01:14:41 +0900696 std::string name_;
Jiyong Park1deecc32018-07-17 01:14:41 +0900697 std::string comments_;
Steven Moreland787b0432018-07-03 09:00:58 -0700698 const std::vector<std::string> package_;
Steven Moreland5557f1c2018-07-02 13:50:23 -0700699
700 DISALLOW_COPY_AND_ASSIGN(AidlDefinedType);
Steven Moreland787b0432018-07-03 09:00:58 -0700701};
702
Jeongik Chadf76dc72019-11-28 00:08:47 +0900703class AidlParcelable : public AidlDefinedType, public AidlParameterizable<std::string> {
Casey Dahlin1ae2bc52015-10-07 18:49:10 -0700704 public:
Steven Moreland46e9da82018-07-27 15:45:29 -0700705 AidlParcelable(const AidlLocation& location, AidlQualifiedName* name,
Jiyong Parka6605ab2018-11-11 14:30:21 +0900706 const std::vector<std::string>& package, const std::string& comments,
Jeongik Chadf76dc72019-11-28 00:08:47 +0900707 const std::string& cpp_header = "",
708 std::vector<std::string>* type_params = nullptr);
Casey Dahlin1ae2bc52015-10-07 18:49:10 -0700709 virtual ~AidlParcelable() = default;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800710
Ningyuan Wangd17c58b2016-09-29 14:33:14 -0700711 // C++ uses "::" instead of "." to refer to a inner class.
712 std::string GetCppName() const { return name_->GetColonName(); }
Christopher Wiley8aa4d9f2015-11-16 19:10:45 -0800713 std::string GetCppHeader() const { return cpp_header_; }
Christopher Wiley8aa4d9f2015-11-16 19:10:45 -0800714
Jeongik Cha82317dd2019-02-27 20:26:11 +0900715 bool CheckValid(const AidlTypenames& typenames) const override;
Jeongik Cha047c5ee2019-08-07 23:16:49 +0900716 bool LanguageSpecificCheckValid(Options::Language lang) const override;
Steven Morelandc258abc2018-07-10 14:03:38 -0700717 const AidlParcelable* AsParcelable() const override { return this; }
Jeongik Chadf76dc72019-11-28 00:08:47 +0900718 const AidlParameterizable<std::string>* AsParameterizable() const override { return this; }
719 const AidlNode& AsAidlNode() const override { return *this; }
Steven Morelanded83a282018-07-17 13:27:29 -0700720 std::string GetPreprocessDeclarationName() const override { return "parcelable"; }
Steven Morelandc258abc2018-07-10 14:03:38 -0700721
Jeongik Cha997281d2020-01-16 15:23:59 +0900722 void Dump(CodeWriter* writer) const override;
Jiyong Park02da7422018-07-16 16:00:26 +0900723
Casey Dahlin1ae2bc52015-10-07 18:49:10 -0700724 private:
Christopher Wiley8aa4d9f2015-11-16 19:10:45 -0800725 std::unique_ptr<AidlQualifiedName> name_;
Christopher Wiley8aa4d9f2015-11-16 19:10:45 -0800726 std::string cpp_header_;
Casey Dahlin59401da2015-10-09 18:16:45 -0700727
Casey Dahlin1ae2bc52015-10-07 18:49:10 -0700728 DISALLOW_COPY_AND_ASSIGN(AidlParcelable);
Casey Dahlin0a2f8be2015-09-28 16:15:29 -0700729};
Adam Lesinskiffa16862014-01-23 18:17:42 -0800730
Steven Moreland5557f1c2018-07-02 13:50:23 -0700731class AidlStructuredParcelable : public AidlParcelable {
732 public:
Steven Moreland46e9da82018-07-27 15:45:29 -0700733 AidlStructuredParcelable(const AidlLocation& location, AidlQualifiedName* name,
Jiyong Parka6605ab2018-11-11 14:30:21 +0900734 const std::vector<std::string>& package, const std::string& comments,
Steven Moreland5557f1c2018-07-02 13:50:23 -0700735 std::vector<std::unique_ptr<AidlVariableDeclaration>>* variables);
736
737 const std::vector<std::unique_ptr<AidlVariableDeclaration>>& GetFields() const {
738 return variables_;
739 }
740
Steven Morelandc258abc2018-07-10 14:03:38 -0700741 const AidlStructuredParcelable* AsStructuredParcelable() const override { return this; }
Steven Morelanded83a282018-07-17 13:27:29 -0700742 std::string GetPreprocessDeclarationName() const override { return "structured_parcelable"; }
Steven Moreland5557f1c2018-07-02 13:50:23 -0700743
Jeongik Cha997281d2020-01-16 15:23:59 +0900744 void Dump(CodeWriter* writer) const override;
Jiyong Park02da7422018-07-16 16:00:26 +0900745
Jeongik Chadb0f59e2018-11-01 18:11:21 +0900746 bool CheckValid(const AidlTypenames& typenames) const override;
Jeongik Cha047c5ee2019-08-07 23:16:49 +0900747 bool LanguageSpecificCheckValid(Options::Language lang) const override;
Jeongik Chadb0f59e2018-11-01 18:11:21 +0900748
Steven Moreland5557f1c2018-07-02 13:50:23 -0700749 private:
750 const std::vector<std::unique_ptr<AidlVariableDeclaration>> variables_;
751
752 DISALLOW_COPY_AND_ASSIGN(AidlStructuredParcelable);
753};
754
Daniel Norman85aed542019-08-21 12:01:14 -0700755class AidlEnumerator : public AidlNode {
756 public:
Daniel Norman2e4112d2019-10-03 10:22:35 -0700757 AidlEnumerator(const AidlLocation& location, const std::string& name, AidlConstantValue* value,
758 const std::string& comments);
Daniel Norman85aed542019-08-21 12:01:14 -0700759 virtual ~AidlEnumerator() = default;
760
761 const std::string& GetName() const { return name_; }
Will McVickerd7d18df2019-09-12 13:40:50 -0700762 AidlConstantValue* GetValue() const { return value_.get(); }
Daniel Norman2e4112d2019-10-03 10:22:35 -0700763 const std::string& GetComments() const { return comments_; }
Daniel Norman85aed542019-08-21 12:01:14 -0700764 bool CheckValid(const AidlTypeSpecifier& enum_backing_type) const;
765
766 string ValueString(const AidlTypeSpecifier& backing_type,
767 const ConstantValueDecorator& decorator) const;
768
Daniel Normanb28684e2019-10-17 15:31:39 -0700769 void SetValue(std::unique_ptr<AidlConstantValue> value) { value_ = std::move(value); }
770
Daniel Norman85aed542019-08-21 12:01:14 -0700771 private:
772 const std::string name_;
Will McVickerd7d18df2019-09-12 13:40:50 -0700773 unique_ptr<AidlConstantValue> value_;
Daniel Norman2e4112d2019-10-03 10:22:35 -0700774 const std::string comments_;
Daniel Norman85aed542019-08-21 12:01:14 -0700775
776 DISALLOW_COPY_AND_ASSIGN(AidlEnumerator);
777};
778
779class AidlEnumDeclaration : public AidlDefinedType {
780 public:
Will McVickerd7d18df2019-09-12 13:40:50 -0700781 AidlEnumDeclaration(const AidlLocation& location, const string& name,
Daniel Norman85aed542019-08-21 12:01:14 -0700782 std::vector<std::unique_ptr<AidlEnumerator>>* enumerators,
Daniel Norman2e4112d2019-10-03 10:22:35 -0700783 const std::vector<std::string>& package, const std::string& comments);
Daniel Norman85aed542019-08-21 12:01:14 -0700784 virtual ~AidlEnumDeclaration() = default;
785
786 void SetBackingType(std::unique_ptr<const AidlTypeSpecifier> type);
787 const AidlTypeSpecifier& GetBackingType() const { return *backing_type_; }
788 const std::vector<std::unique_ptr<AidlEnumerator>>& GetEnumerators() const {
789 return enumerators_;
790 }
Steven Moreland59e53e42019-11-26 20:38:08 -0800791 bool Autofill();
Daniel Norman85aed542019-08-21 12:01:14 -0700792 bool CheckValid(const AidlTypenames& typenames) const override;
793 bool LanguageSpecificCheckValid(Options::Language) const override { return true; }
794 std::string GetPreprocessDeclarationName() const override { return "enum"; }
Jeongik Cha997281d2020-01-16 15:23:59 +0900795 void Dump(CodeWriter* writer) const override;
Daniel Norman85aed542019-08-21 12:01:14 -0700796
797 const AidlEnumDeclaration* AsEnumDeclaration() const override { return this; }
798
799 private:
800 const std::string name_;
801 const std::vector<std::unique_ptr<AidlEnumerator>> enumerators_;
802 std::unique_ptr<const AidlTypeSpecifier> backing_type_;
803
804 DISALLOW_COPY_AND_ASSIGN(AidlEnumDeclaration);
805};
806
Jiyong Park1deecc32018-07-17 01:14:41 +0900807class AidlInterface final : public AidlDefinedType {
Casey Dahlin1ae2bc52015-10-07 18:49:10 -0700808 public:
Steven Moreland46e9da82018-07-27 15:45:29 -0700809 AidlInterface(const AidlLocation& location, const std::string& name, const std::string& comments,
810 bool oneway_, std::vector<std::unique_ptr<AidlMember>>* members,
Christopher Wileyd76067c2015-10-19 17:00:13 -0700811 const std::vector<std::string>& package);
Casey Dahlin1ae2bc52015-10-07 18:49:10 -0700812 virtual ~AidlInterface() = default;
813
Casey Dahlinfb7da2e2015-10-08 17:26:09 -0700814 const std::vector<std::unique_ptr<AidlMethod>>& GetMethods() const
815 { return methods_; }
Jiyong Park309668e2018-07-28 16:55:44 +0900816 std::vector<std::unique_ptr<AidlMethod>>& GetMutableMethods() { return methods_; }
Steven Moreland693640b2018-07-19 13:46:27 -0700817 const std::vector<std::unique_ptr<AidlConstantDeclaration>>& GetConstantDeclarations() const {
818 return constants_;
819 }
Casey Dahlina2f77c42015-12-01 18:26:02 -0800820
Steven Morelandc258abc2018-07-10 14:03:38 -0700821 const AidlInterface* AsInterface() const override { return this; }
Steven Morelanded83a282018-07-17 13:27:29 -0700822 std::string GetPreprocessDeclarationName() const override { return "interface"; }
Steven Moreland5557f1c2018-07-02 13:50:23 -0700823
Jeongik Cha997281d2020-01-16 15:23:59 +0900824 void Dump(CodeWriter* writer) const override;
Jiyong Park02da7422018-07-16 16:00:26 +0900825
Jeongik Chadb0f59e2018-11-01 18:11:21 +0900826 bool CheckValid(const AidlTypenames& typenames) const override;
Jeongik Cha047c5ee2019-08-07 23:16:49 +0900827 bool LanguageSpecificCheckValid(Options::Language lang) const override;
Jeongik Chadb0f59e2018-11-01 18:11:21 +0900828
Casey Dahlin1ae2bc52015-10-07 18:49:10 -0700829 private:
Casey Dahlinfb7da2e2015-10-08 17:26:09 -0700830 std::vector<std::unique_ptr<AidlMethod>> methods_;
Steven Moreland693640b2018-07-19 13:46:27 -0700831 std::vector<std::unique_ptr<AidlConstantDeclaration>> constants_;
Casey Dahlinfb7da2e2015-10-08 17:26:09 -0700832
Casey Dahlin1ae2bc52015-10-07 18:49:10 -0700833 DISALLOW_COPY_AND_ASSIGN(AidlInterface);
Casey Dahlin0a2f8be2015-09-28 16:15:29 -0700834};
Adam Lesinskiffa16862014-01-23 18:17:42 -0800835
Casey Dahlin0edf3422015-10-07 12:34:59 -0700836class AidlImport : public AidlNode {
837 public:
Steven Moreland46e9da82018-07-27 15:45:29 -0700838 AidlImport(const AidlLocation& location, const std::string& needed_class);
Casey Dahlin0edf3422015-10-07 12:34:59 -0700839 virtual ~AidlImport() = default;
840
Casey Dahlin0edf3422015-10-07 12:34:59 -0700841 const std::string& GetFilename() const { return filename_; }
842 const std::string& GetNeededClass() const { return needed_class_; }
Casey Dahlin0edf3422015-10-07 12:34:59 -0700843
844 private:
Casey Dahlin0edf3422015-10-07 12:34:59 -0700845 std::string filename_;
846 std::string needed_class_;
Casey Dahlin0edf3422015-10-07 12:34:59 -0700847
848 DISALLOW_COPY_AND_ASSIGN(AidlImport);
Casey Dahline2507492015-09-14 17:11:20 -0700849};
850
851class Parser {
Casey Dahlindd691812015-09-09 17:59:06 -0700852 public:
Casey Dahline2507492015-09-14 17:11:20 -0700853 ~Parser();
Casey Dahlindd691812015-09-09 17:59:06 -0700854
Steven Moreland64e29be2018-08-08 18:52:19 -0700855 // Parse contents of file |filename|. Should only be called once.
856 static std::unique_ptr<Parser> Parse(const std::string& filename,
857 const android::aidl::IoDelegate& io_delegate,
858 AidlTypenames& typenames);
Casey Dahlin89d44842015-09-24 18:45:54 -0700859
Steven Moreland2ca4fcb2018-06-27 16:01:01 -0700860 void AddError() { error_++; }
Steven Moreland64e29be2018-08-08 18:52:19 -0700861 bool HasError() { return error_ != 0; }
Casey Dahlindd691812015-09-09 17:59:06 -0700862
Casey Dahlin3c6df362015-10-06 15:48:35 -0700863 const std::string& FileName() const { return filename_; }
Casey Dahlin42727f82015-10-12 19:23:40 -0700864 void* Scanner() const { return scanner_; }
Casey Dahlindd691812015-09-09 17:59:06 -0700865
Steven Morelandd1039a92020-01-23 09:49:43 -0800866 void AddImport(std::unique_ptr<AidlImport>&& import);
Christopher Wileyd76067c2015-10-19 17:00:13 -0700867 const std::vector<std::unique_ptr<AidlImport>>& GetImports() {
868 return imports_;
869 }
Casey Dahlindd691812015-09-09 17:59:06 -0700870
Jiyong Parkb034bf02018-07-30 17:44:33 +0900871 void SetPackage(unique_ptr<AidlQualifiedName> name) { package_ = std::move(name); }
872 std::vector<std::string> Package() const;
Jiyong Park1deecc32018-07-17 01:14:41 +0900873
874 void DeferResolution(AidlTypeSpecifier* typespec) {
875 unresolved_typespecs_.emplace_back(typespec);
876 }
877
Jiyong Parke59c3682018-09-11 23:10:25 +0900878 const vector<AidlTypeSpecifier*>& GetUnresolvedTypespecs() const { return unresolved_typespecs_; }
879
Jiyong Park1deecc32018-07-17 01:14:41 +0900880 bool Resolve();
881
Jiyong Parkb034bf02018-07-30 17:44:33 +0900882 void AddDefinedType(unique_ptr<AidlDefinedType> type) {
883 // Parser does NOT own AidlDefinedType, it just has references to the types
884 // that it encountered while parsing the input file.
885 defined_types_.emplace_back(type.get());
886
887 // AidlDefinedType IS owned by AidlTypenames
888 if (!typenames_.AddDefinedType(std::move(type))) {
889 AddError();
890 }
891 }
892
893 vector<AidlDefinedType*>& GetDefinedTypes() { return defined_types_; }
894
Casey Dahlindd691812015-09-09 17:59:06 -0700895 private:
Steven Moreland64e29be2018-08-08 18:52:19 -0700896 explicit Parser(const std::string& filename, std::string& raw_buffer,
897 android::aidl::AidlTypenames& typenames);
898
Casey Dahlindd691812015-09-09 17:59:06 -0700899 std::string filename_;
Christopher Wileyd76067c2015-10-19 17:00:13 -0700900 std::unique_ptr<AidlQualifiedName> package_;
Jiyong Parkb034bf02018-07-30 17:44:33 +0900901 AidlTypenames& typenames_;
Steven Moreland64e29be2018-08-08 18:52:19 -0700902
903 void* scanner_ = nullptr;
904 YY_BUFFER_STATE buffer_;
905 int error_ = 0;
906
907 std::vector<std::unique_ptr<AidlImport>> imports_;
Jiyong Parkb034bf02018-07-30 17:44:33 +0900908 vector<AidlDefinedType*> defined_types_;
Jiyong Park1deecc32018-07-17 01:14:41 +0900909 vector<AidlTypeSpecifier*> unresolved_typespecs_;
Casey Dahlindd691812015-09-09 17:59:06 -0700910
Casey Dahline2507492015-09-14 17:11:20 -0700911 DISALLOW_COPY_AND_ASSIGN(Parser);
Casey Dahlindd691812015-09-09 17:59:06 -0700912};