blob: b8ee857fdbbfca056ebb671c3deda85331e69596 [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 Cha698e6af2018-12-12 14:39:55 +0900245 bool IsSystemApi() const;
Jeongik Cha88f95a82020-01-15 13:02:16 +0900246 bool IsStableApiParcelable(Options::Language lang) const;
Andrei Onea9445fc62019-06-27 18:11:59 +0100247
248 const AidlAnnotation* UnsupportedAppUsage() const;
Daniel Norman716d3112019-09-10 13:11:56 -0700249 const AidlTypeSpecifier* BackingType(const AidlTypenames& typenames) const;
Jiyong Park68bc77a2018-07-19 19:00:45 +0900250 std::string ToString() const;
Casey Dahline7922932016-02-29 17:23:01 -0800251
Jiyong Parka6605ab2018-11-11 14:30:21 +0900252 const vector<AidlAnnotation>& GetAnnotations() const { return annotations_; }
Andrei Onea9445fc62019-06-27 18:11:59 +0100253 bool CheckValidAnnotations() const;
Jiyong Park3656c3c2018-08-01 20:02:01 +0900254
Casey Dahline7922932016-02-29 17:23:01 -0800255 private:
Jiyong Parka6605ab2018-11-11 14:30:21 +0900256 vector<AidlAnnotation> annotations_;
Casey Dahline7922932016-02-29 17:23:01 -0800257};
258
Jiyong Park1deecc32018-07-17 01:14:41 +0900259class AidlQualifiedName;
260
261// AidlTypeSpecifier represents a reference to either a built-in type,
262// a defined type, or a variant (e.g., array of generic) of a type.
Jeongik Chadf76dc72019-11-28 00:08:47 +0900263class AidlTypeSpecifier final : public AidlAnnotatable,
264 public AidlParameterizable<unique_ptr<AidlTypeSpecifier>> {
Casey Dahline7922932016-02-29 17:23:01 -0800265 public:
Steven Moreland46e9da82018-07-27 15:45:29 -0700266 AidlTypeSpecifier(const AidlLocation& location, const string& unresolved_name, bool is_array,
267 vector<unique_ptr<AidlTypeSpecifier>>* type_params, const string& comments);
Jiyong Parkd59a10d2018-07-18 11:12:55 +0900268 virtual ~AidlTypeSpecifier() = default;
Casey Dahlin0ee37582015-09-30 16:31:55 -0700269
Steven Moreland3f658cf2018-08-20 13:40:54 -0700270 // Copy of this type which is not an array.
271 AidlTypeSpecifier ArrayBase() const;
272
Jiyong Park1deecc32018-07-17 01:14:41 +0900273 // Returns the full-qualified name of the base type.
274 // int -> int
275 // int[] -> int
276 // List<String> -> List
277 // IFoo -> foo.bar.IFoo (if IFoo is in package foo.bar)
278 const string& GetName() const {
279 if (IsResolved()) {
280 return fully_qualified_name_;
281 } else {
282 return GetUnresolvedName();
283 }
284 }
Casey Dahlin0ee37582015-09-30 16:31:55 -0700285
Jiyong Park1deecc32018-07-17 01:14:41 +0900286 // Returns string representation of this type specifier.
Artur Satayev91fe8712019-07-29 13:06:01 +0100287 // This is GetBaseTypeName() + array modifier or generic type parameters
Jiyong Park1deecc32018-07-17 01:14:41 +0900288 string ToString() const;
289
Jiyong Park02da7422018-07-16 16:00:26 +0900290 std::string Signature() const;
291
Jiyong Park1deecc32018-07-17 01:14:41 +0900292 const string& GetUnresolvedName() const { return unresolved_name_; }
293
Jeongik Cha997281d2020-01-16 15:23:59 +0900294 bool IsHidden() const;
295
Jiyong Park1deecc32018-07-17 01:14:41 +0900296 const string& GetComments() const { return comments_; }
297
Jeongik Cha1a7ab642019-07-29 17:31:02 +0900298 const std::vector<std::string> GetSplitName() const { return split_name_; }
299
Jiyong Parka6605ab2018-11-11 14:30:21 +0900300 void SetComments(const string& comment) { comments_ = comment; }
301
Jiyong Park1deecc32018-07-17 01:14:41 +0900302 bool IsResolved() const { return fully_qualified_name_ != ""; }
303
304 bool IsArray() const { return is_array_; }
305
Jiyong Park1deecc32018-07-17 01:14:41 +0900306 // Resolve the base type name to a fully-qualified name. Return false if the
307 // resolution fails.
Daniel Norman716d3112019-09-10 13:11:56 -0700308 bool Resolve(const AidlTypenames& typenames);
Casey Dahlin0ee37582015-09-30 16:31:55 -0700309
Jeongik Chadb0f59e2018-11-01 18:11:21 +0900310 bool CheckValid(const AidlTypenames& typenames) const;
Jeongik Cha047c5ee2019-08-07 23:16:49 +0900311 bool LanguageSpecificCheckValid(Options::Language lang) const;
Jeongik Chadf76dc72019-11-28 00:08:47 +0900312 const AidlNode& AsAidlNode() const override { return *this; }
Jiyong Park1d2df7d2018-07-23 15:22:50 +0900313
Casey Dahlin0ee37582015-09-30 16:31:55 -0700314 private:
Steven Moreland3f658cf2018-08-20 13:40:54 -0700315 AidlTypeSpecifier(const AidlTypeSpecifier&) = default;
316
Jiyong Park1deecc32018-07-17 01:14:41 +0900317 const string unresolved_name_;
318 string fully_qualified_name_;
Steven Moreland3f658cf2018-08-20 13:40:54 -0700319 bool is_array_;
Jiyong Parka6605ab2018-11-11 14:30:21 +0900320 string comments_;
Jeongik Cha1a7ab642019-07-29 17:31:02 +0900321 vector<string> split_name_;
Casey Dahlin0ee37582015-09-30 16:31:55 -0700322};
323
Steven Moreland860b1942018-08-16 14:59:28 -0700324// Returns the universal value unaltered.
325std::string AidlConstantValueDecorator(const AidlTypeSpecifier& type, const std::string& raw_value);
326
Steven Moreland9ea10e32018-07-19 15:26:09 -0700327class AidlConstantValue;
Steven Moreland5557f1c2018-07-02 13:50:23 -0700328class AidlVariableDeclaration : public AidlNode {
329 public:
Steven Moreland46e9da82018-07-27 15:45:29 -0700330 AidlVariableDeclaration(const AidlLocation& location, AidlTypeSpecifier* type,
331 const std::string& name);
332 AidlVariableDeclaration(const AidlLocation& location, AidlTypeSpecifier* type,
333 const std::string& name, AidlConstantValue* default_value);
Steven Moreland5557f1c2018-07-02 13:50:23 -0700334 virtual ~AidlVariableDeclaration() = default;
335
336 std::string GetName() const { return name_; }
Jiyong Parkd59a10d2018-07-18 11:12:55 +0900337 const AidlTypeSpecifier& GetType() const { return *type_; }
Steven Moreland9ea10e32018-07-19 15:26:09 -0700338 const AidlConstantValue* GetDefaultValue() const { return default_value_.get(); }
339
Jiyong Parkd59a10d2018-07-18 11:12:55 +0900340 AidlTypeSpecifier* GetMutableType() { return type_.get(); }
Steven Moreland5557f1c2018-07-02 13:50:23 -0700341
Jeongik Chadb0f59e2018-11-01 18:11:21 +0900342 bool CheckValid(const AidlTypenames& typenames) const;
Steven Moreland5557f1c2018-07-02 13:50:23 -0700343 std::string ToString() const;
Jiyong Park02da7422018-07-16 16:00:26 +0900344 std::string Signature() const;
Steven Moreland5557f1c2018-07-02 13:50:23 -0700345
Steven Moreland860b1942018-08-16 14:59:28 -0700346 std::string ValueString(const ConstantValueDecorator& decorator) const;
Steven Moreland25294322018-08-07 18:13:55 -0700347
Steven Moreland5557f1c2018-07-02 13:50:23 -0700348 private:
Jiyong Parkd59a10d2018-07-18 11:12:55 +0900349 std::unique_ptr<AidlTypeSpecifier> type_;
Steven Moreland5557f1c2018-07-02 13:50:23 -0700350 std::string name_;
Steven Moreland9ea10e32018-07-19 15:26:09 -0700351 std::unique_ptr<AidlConstantValue> default_value_;
Steven Moreland5557f1c2018-07-02 13:50:23 -0700352
353 DISALLOW_COPY_AND_ASSIGN(AidlVariableDeclaration);
354};
355
356class AidlArgument : public AidlVariableDeclaration {
Casey Dahlinbc7a50a2015-09-28 19:20:50 -0700357 public:
Casey Dahlinc378c992015-09-29 16:50:40 -0700358 enum Direction { IN_DIR = 1, OUT_DIR = 2, INOUT_DIR = 3 };
359
Steven Moreland46e9da82018-07-27 15:45:29 -0700360 AidlArgument(const AidlLocation& location, AidlArgument::Direction direction,
361 AidlTypeSpecifier* type, const std::string& name);
362 AidlArgument(const AidlLocation& location, AidlTypeSpecifier* type, const std::string& name);
Casey Dahlinbc7a50a2015-09-28 19:20:50 -0700363 virtual ~AidlArgument() = default;
364
Casey Dahlinc378c992015-09-29 16:50:40 -0700365 Direction GetDirection() const { return direction_; }
Christopher Wileyad339272015-10-05 19:11:58 -0700366 bool IsOut() const { return direction_ & OUT_DIR; }
367 bool IsIn() const { return direction_ & IN_DIR; }
Casey Dahlinc378c992015-09-29 16:50:40 -0700368 bool DirectionWasSpecified() const { return direction_specified_; }
Jiyong Park3656c3c2018-08-01 20:02:01 +0900369 string GetDirectionSpecifier() const;
Christopher Wileyad339272015-10-05 19:11:58 -0700370
Casey Dahlinc378c992015-09-29 16:50:40 -0700371 std::string ToString() const;
Jiyong Park02da7422018-07-16 16:00:26 +0900372 std::string Signature() const;
Casey Dahlinc378c992015-09-29 16:50:40 -0700373
Casey Dahlinbc7a50a2015-09-28 19:20:50 -0700374 private:
Casey Dahlinc378c992015-09-29 16:50:40 -0700375 Direction direction_;
376 bool direction_specified_;
377
Casey Dahlinbc7a50a2015-09-28 19:20:50 -0700378 DISALLOW_COPY_AND_ASSIGN(AidlArgument);
Casey Dahlina834dd42015-09-23 11:52:15 -0700379};
Adam Lesinskiffa16862014-01-23 18:17:42 -0800380
Casey Dahlind40e2fe2015-11-24 14:06:52 -0800381class AidlMethod;
Steven Moreland693640b2018-07-19 13:46:27 -0700382class AidlConstantDeclaration;
Daniel Norman85aed542019-08-21 12:01:14 -0700383class AidlEnumDeclaration;
Casey Dahlind40e2fe2015-11-24 14:06:52 -0800384class AidlMember : public AidlNode {
385 public:
Steven Moreland46e9da82018-07-27 15:45:29 -0700386 AidlMember(const AidlLocation& location);
Casey Dahlind40e2fe2015-11-24 14:06:52 -0800387 virtual ~AidlMember() = default;
388
389 virtual AidlMethod* AsMethod() { return nullptr; }
Steven Moreland693640b2018-07-19 13:46:27 -0700390 virtual AidlConstantDeclaration* AsConstantDeclaration() { return nullptr; }
Casey Dahlind40e2fe2015-11-24 14:06:52 -0800391
392 private:
393 DISALLOW_COPY_AND_ASSIGN(AidlMember);
394};
395
Will McVickerd7d18df2019-09-12 13:40:50 -0700396class AidlUnaryConstExpression;
397class AidlBinaryConstExpression;
398
Steven Moreland693640b2018-07-19 13:46:27 -0700399class AidlConstantValue : public AidlNode {
Casey Dahlind40e2fe2015-11-24 14:06:52 -0800400 public:
Will McVickerd7d18df2019-09-12 13:40:50 -0700401 enum class Type {
402 // WARNING: Don't change this order! The order is used to determine type
403 // promotion during a binary expression.
404 BOOLEAN,
405 INT8,
406 INT32,
407 INT64,
408 ARRAY,
409 CHARACTER,
410 STRING,
411 FLOATING,
412 UNARY,
413 BINARY,
414 ERROR,
415 };
416
417 /*
418 * Return the value casted to the given type.
419 */
420 template <typename T>
421 T cast() const;
Casey Dahlind40e2fe2015-11-24 14:06:52 -0800422
Steven Moreland693640b2018-07-19 13:46:27 -0700423 virtual ~AidlConstantValue() = default;
Casey Dahlind40e2fe2015-11-24 14:06:52 -0800424
Steven Moreland25294322018-08-07 18:13:55 -0700425 static AidlConstantValue* Boolean(const AidlLocation& location, bool value);
426 static AidlConstantValue* Character(const AidlLocation& location, char value);
Steven Moreland25294322018-08-07 18:13:55 -0700427 // example: 123, -5498, maybe any size
Will McVickerd7d18df2019-09-12 13:40:50 -0700428 static AidlConstantValue* Integral(const AidlLocation& location, const string& value);
429 static AidlConstantValue* Floating(const AidlLocation& location, const std::string& value);
Steven Moreland860b1942018-08-16 14:59:28 -0700430 static AidlConstantValue* Array(const AidlLocation& location,
Will McVickerd7d18df2019-09-12 13:40:50 -0700431 std::unique_ptr<vector<unique_ptr<AidlConstantValue>>> values);
Steven Moreland693640b2018-07-19 13:46:27 -0700432 // example: "\"asdf\""
Will McVickerd7d18df2019-09-12 13:40:50 -0700433 static AidlConstantValue* String(const AidlLocation& location, const string& value);
Steven Moreland693640b2018-07-19 13:46:27 -0700434
Daniel Normanf0ca44f2019-10-25 09:59:44 -0700435 // Construct an AidlConstantValue by evaluating the other integral constant's
436 // value string. This does not preserve the structure of the copied constant.
Steven Moreland59e53e42019-11-26 20:38:08 -0800437 // Returns nullptr and logs if value cannot be copied.
Daniel Normanf0ca44f2019-10-25 09:59:44 -0700438 static AidlConstantValue* ShallowIntegralCopy(const AidlConstantValue& other);
Daniel Normanb28684e2019-10-17 15:31:39 -0700439
Will McVickerd7d18df2019-09-12 13:40:50 -0700440 Type GetType() const { return final_type_; }
Steven Moreland25294322018-08-07 18:13:55 -0700441
Will McVickerd7d18df2019-09-12 13:40:50 -0700442 virtual bool CheckValid() const;
Steven Moreland860b1942018-08-16 14:59:28 -0700443
444 // Raw value of type (currently valid in C++ and Java). Empty string on error.
Steven Moreland4bcb05c2019-11-27 18:57:47 -0800445 string ValueString(const AidlTypeSpecifier& type, const ConstantValueDecorator& decorator) const;
Casey Dahlind40e2fe2015-11-24 14:06:52 -0800446
447 private:
Will McVickerd7d18df2019-09-12 13:40:50 -0700448 AidlConstantValue(const AidlLocation& location, Type parsed_type, int64_t parsed_value,
449 const string& checked_value);
450 AidlConstantValue(const AidlLocation& location, Type type, const string& checked_value);
Steven Moreland860b1942018-08-16 14:59:28 -0700451 AidlConstantValue(const AidlLocation& location, Type type,
Will McVickerd7d18df2019-09-12 13:40:50 -0700452 std::unique_ptr<vector<unique_ptr<AidlConstantValue>>> values);
Steven Moreland25294322018-08-07 18:13:55 -0700453 static string ToString(Type type);
Will McVickerd7d18df2019-09-12 13:40:50 -0700454 static bool ParseIntegral(const string& value, int64_t* parsed_value, Type* parsed_type);
455 static bool IsHex(const string& value);
Steven Moreland4bcb05c2019-11-27 18:57:47 -0800456
Will McVickerd7d18df2019-09-12 13:40:50 -0700457 virtual bool evaluate(const AidlTypeSpecifier& type) const;
Casey Dahlind40e2fe2015-11-24 14:06:52 -0800458
Steven Moreland693640b2018-07-19 13:46:27 -0700459 const Type type_ = Type::ERROR;
Will McVickerd7d18df2019-09-12 13:40:50 -0700460 const vector<unique_ptr<AidlConstantValue>> values_; // if type_ == ARRAY
461 const string value_; // otherwise
462
463 // State for tracking evaluation of expressions
Steven Moreland4bcb05c2019-11-27 18:57:47 -0800464 mutable bool is_valid_ = false; // cache of CheckValid, but may be marked false in evaluate
465 mutable bool is_evaluated_ = false; // whether evaluate has been called
Will McVickerd7d18df2019-09-12 13:40:50 -0700466 mutable Type final_type_;
467 mutable int64_t final_value_;
468 mutable string final_string_value_ = "";
Steven Moreland693640b2018-07-19 13:46:27 -0700469
470 DISALLOW_COPY_AND_ASSIGN(AidlConstantValue);
Will McVickerd7d18df2019-09-12 13:40:50 -0700471
472 friend AidlUnaryConstExpression;
473 friend AidlBinaryConstExpression;
474};
475
476class AidlUnaryConstExpression : public AidlConstantValue {
477 public:
478 AidlUnaryConstExpression(const AidlLocation& location, const string& op,
479 std::unique_ptr<AidlConstantValue> rval);
480
481 static bool IsCompatibleType(Type type, const string& op);
482 bool CheckValid() const override;
Will McVickerd7d18df2019-09-12 13:40:50 -0700483 private:
484 bool evaluate(const AidlTypeSpecifier& type) const override;
485
486 std::unique_ptr<AidlConstantValue> unary_;
487 const string op_;
488};
489
490class AidlBinaryConstExpression : public AidlConstantValue {
491 public:
492 AidlBinaryConstExpression(const AidlLocation& location, std::unique_ptr<AidlConstantValue> lval,
493 const string& op, std::unique_ptr<AidlConstantValue> rval);
494
495 bool CheckValid() const override;
Will McVickerd7d18df2019-09-12 13:40:50 -0700496
497 static bool AreCompatibleTypes(Type t1, Type t2);
498 // Returns the promoted kind for both operands
499 static Type UsualArithmeticConversion(Type left, Type right);
500 // Returns the promoted integral type where INT32 is the smallest type
501 static Type IntegralPromotion(Type in);
502
503 private:
504 bool evaluate(const AidlTypeSpecifier& type) const override;
505
506 std::unique_ptr<AidlConstantValue> left_val_;
507 std::unique_ptr<AidlConstantValue> right_val_;
508 const string op_;
Christopher Wileyd6bdd8d2016-05-03 11:23:13 -0700509};
510
Andrei Onea9445fc62019-06-27 18:11:59 +0100511struct AidlAnnotationParameter {
512 std::string name;
513 std::unique_ptr<AidlConstantValue> value;
514};
515
Steven Moreland693640b2018-07-19 13:46:27 -0700516class AidlConstantDeclaration : public AidlMember {
Christopher Wileyd6bdd8d2016-05-03 11:23:13 -0700517 public:
Steven Moreland46e9da82018-07-27 15:45:29 -0700518 AidlConstantDeclaration(const AidlLocation& location, AidlTypeSpecifier* specifier,
Will McVickerd7d18df2019-09-12 13:40:50 -0700519 const string& name, AidlConstantValue* value);
Steven Moreland693640b2018-07-19 13:46:27 -0700520 virtual ~AidlConstantDeclaration() = default;
Christopher Wileyd6bdd8d2016-05-03 11:23:13 -0700521
Steven Moreland693640b2018-07-19 13:46:27 -0700522 const AidlTypeSpecifier& GetType() const { return *type_; }
Steven Moreland4d12f9a2018-10-31 14:30:55 -0700523 AidlTypeSpecifier* GetMutableType() { return type_.get(); }
Will McVickerd7d18df2019-09-12 13:40:50 -0700524 const string& GetName() const { return name_; }
Steven Moreland693640b2018-07-19 13:46:27 -0700525 const AidlConstantValue& GetValue() const { return *value_; }
Jeongik Chadb0f59e2018-11-01 18:11:21 +0900526 bool CheckValid(const AidlTypenames& typenames) const;
Christopher Wileyd6bdd8d2016-05-03 11:23:13 -0700527
Will McVickerd7d18df2019-09-12 13:40:50 -0700528 string ToString() const;
529 string Signature() const;
Steven Moreland860b1942018-08-16 14:59:28 -0700530 string ValueString(const ConstantValueDecorator& decorator) const {
Will McVickerd7d18df2019-09-12 13:40:50 -0700531 return value_->ValueString(GetType(), decorator);
Steven Moreland860b1942018-08-16 14:59:28 -0700532 }
Steven Moreland25294322018-08-07 18:13:55 -0700533
Steven Moreland693640b2018-07-19 13:46:27 -0700534 AidlConstantDeclaration* AsConstantDeclaration() override { return this; }
Christopher Wileyd6bdd8d2016-05-03 11:23:13 -0700535
536 private:
Steven Moreland693640b2018-07-19 13:46:27 -0700537 const unique_ptr<AidlTypeSpecifier> type_;
Will McVickerd7d18df2019-09-12 13:40:50 -0700538 const string name_;
539 unique_ptr<AidlConstantValue> value_;
Christopher Wileyd6bdd8d2016-05-03 11:23:13 -0700540
Steven Moreland693640b2018-07-19 13:46:27 -0700541 DISALLOW_COPY_AND_ASSIGN(AidlConstantDeclaration);
Casey Dahlind40e2fe2015-11-24 14:06:52 -0800542};
543
544class AidlMethod : public AidlMember {
Casey Dahlin5c69deb2015-10-01 14:44:12 -0700545 public:
Will McVickerd7d18df2019-09-12 13:40:50 -0700546 AidlMethod(const AidlLocation& location, bool oneway, AidlTypeSpecifier* type, const string& name,
547 vector<unique_ptr<AidlArgument>>* args, const string& comments);
548 AidlMethod(const AidlLocation& location, bool oneway, AidlTypeSpecifier* type, const string& name,
549 vector<unique_ptr<AidlArgument>>* args, const string& comments, int id,
550 bool is_user_defined = true);
Casey Dahlin5c69deb2015-10-01 14:44:12 -0700551 virtual ~AidlMethod() = default;
552
Casey Dahlind40e2fe2015-11-24 14:06:52 -0800553 AidlMethod* AsMethod() override { return this; }
Jeongik Cha997281d2020-01-16 15:23:59 +0900554 bool IsHidden() const;
Will McVickerd7d18df2019-09-12 13:40:50 -0700555 const string& GetComments() const { return comments_; }
Jiyong Parkd59a10d2018-07-18 11:12:55 +0900556 const AidlTypeSpecifier& GetType() const { return *type_; }
557 AidlTypeSpecifier* GetMutableType() { return type_.get(); }
Steven Morelandacd53472018-12-14 10:17:26 -0800558
Steven Moreland8c70ba92018-12-17 10:20:31 -0800559 // set if this method is part of an interface that is marked oneway
560 void ApplyInterfaceOneway(bool oneway) { oneway_ = oneway_ || oneway; }
Casey Dahlinf4a93112015-10-05 16:58:09 -0700561 bool IsOneway() const { return oneway_; }
Steven Morelandacd53472018-12-14 10:17:26 -0800562
Casey Dahlinf4a93112015-10-05 16:58:09 -0700563 const std::string& GetName() const { return name_; }
Casey Dahlinf4a93112015-10-05 16:58:09 -0700564 bool HasId() const { return has_id_; }
Jiyong Parked65bf42018-08-28 15:43:27 +0900565 int GetId() const { return id_; }
Casey Dahlinf4a93112015-10-05 16:58:09 -0700566 void SetId(unsigned id) { id_ = id; }
Casey Dahlinf2d23f72015-10-02 16:19:19 -0700567
Jiyong Park309668e2018-07-28 16:55:44 +0900568 bool IsUserDefined() const { return is_user_defined_; }
569
Casey Dahlinf4a93112015-10-05 16:58:09 -0700570 const std::vector<std::unique_ptr<AidlArgument>>& GetArguments() const {
Christopher Wileyad339272015-10-05 19:11:58 -0700571 return arguments_;
572 }
573 // An inout parameter will appear in both GetInArguments()
574 // and GetOutArguments(). AidlMethod retains ownership of the argument
575 // pointers returned in this way.
576 const std::vector<const AidlArgument*>& GetInArguments() const {
577 return in_arguments_;
578 }
579 const std::vector<const AidlArgument*>& GetOutArguments() const {
580 return out_arguments_;
Casey Dahlinf4a93112015-10-05 16:58:09 -0700581 }
Casey Dahlin5c69deb2015-10-01 14:44:12 -0700582
Jiyong Park309668e2018-07-28 16:55:44 +0900583 // name + type parameter types
584 // i.e, foo(int, String)
Jiyong Park02da7422018-07-16 16:00:26 +0900585 std::string Signature() const;
586
Jiyong Park309668e2018-07-28 16:55:44 +0900587 // return type + name + type parameter types + annotations
588 // i.e, boolean foo(int, @Nullable String)
589 std::string ToString() const;
590
Casey Dahlin5c69deb2015-10-01 14:44:12 -0700591 private:
Casey Dahlinf4a93112015-10-05 16:58:09 -0700592 bool oneway_;
Casey Dahlinf2d23f72015-10-02 16:19:19 -0700593 std::string comments_;
Jiyong Parkd59a10d2018-07-18 11:12:55 +0900594 std::unique_ptr<AidlTypeSpecifier> type_;
Casey Dahlinf4a93112015-10-05 16:58:09 -0700595 std::string name_;
Christopher Wileyad339272015-10-05 19:11:58 -0700596 const std::vector<std::unique_ptr<AidlArgument>> arguments_;
597 std::vector<const AidlArgument*> in_arguments_;
598 std::vector<const AidlArgument*> out_arguments_;
Casey Dahlinf4a93112015-10-05 16:58:09 -0700599 bool has_id_;
600 int id_;
Jiyong Park309668e2018-07-28 16:55:44 +0900601 bool is_user_defined_ = true;
Casey Dahlinf2d23f72015-10-02 16:19:19 -0700602
Casey Dahlin5c69deb2015-10-01 14:44:12 -0700603 DISALLOW_COPY_AND_ASSIGN(AidlMethod);
Casey Dahlin0a2f8be2015-09-28 16:15:29 -0700604};
Adam Lesinskiffa16862014-01-23 18:17:42 -0800605
Steven Morelandc258abc2018-07-10 14:03:38 -0700606class AidlDefinedType;
Jiyong Parkb034bf02018-07-30 17:44:33 +0900607class AidlInterface;
608class AidlParcelable;
609class AidlStructuredParcelable;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800610
Casey Dahlin2b2879b2015-10-13 16:59:44 -0700611class AidlQualifiedName : public AidlNode {
612 public:
Steven Moreland46e9da82018-07-27 15:45:29 -0700613 AidlQualifiedName(const AidlLocation& location, const std::string& term,
614 const std::string& comments);
Casey Dahlin2b2879b2015-10-13 16:59:44 -0700615 virtual ~AidlQualifiedName() = default;
616
617 const std::vector<std::string>& GetTerms() const { return terms_; }
618 const std::string& GetComments() const { return comments_; }
619 std::string GetDotName() const { return android::base::Join(terms_, '.'); }
Ningyuan Wangd17c58b2016-09-29 14:33:14 -0700620 std::string GetColonName() const { return android::base::Join(terms_, "::"); }
Casey Dahlin2b2879b2015-10-13 16:59:44 -0700621
Chih-Hung Hsiehf05cc262016-07-27 11:42:51 -0700622 void AddTerm(const std::string& term);
Casey Dahlin2b2879b2015-10-13 16:59:44 -0700623
624 private:
625 std::vector<std::string> terms_;
626 std::string comments_;
627
628 DISALLOW_COPY_AND_ASSIGN(AidlQualifiedName);
629};
630
Steven Moreland46e9da82018-07-27 15:45:29 -0700631class AidlInterface;
632class AidlParcelable;
633class AidlStructuredParcelable;
Daniel Norman85aed542019-08-21 12:01:14 -0700634// AidlDefinedType represents either an interface, parcelable, or enum that is
Jiyong Park1deecc32018-07-17 01:14:41 +0900635// defined in the source file.
636class AidlDefinedType : public AidlAnnotatable {
Steven Moreland787b0432018-07-03 09:00:58 -0700637 public:
Steven Moreland46e9da82018-07-27 15:45:29 -0700638 AidlDefinedType(const AidlLocation& location, const std::string& name,
639 const std::string& comments, const std::vector<std::string>& package);
Steven Moreland787b0432018-07-03 09:00:58 -0700640 virtual ~AidlDefinedType() = default;
641
Jiyong Park1deecc32018-07-17 01:14:41 +0900642 const std::string& GetName() const { return name_; };
Jeongik Cha997281d2020-01-16 15:23:59 +0900643 bool IsHidden() const;
Jiyong Park1deecc32018-07-17 01:14:41 +0900644 const std::string& GetComments() const { return comments_; }
Jiyong Parka6605ab2018-11-11 14:30:21 +0900645 void SetComments(const std::string comments) { comments_ = comments; }
Jiyong Park1deecc32018-07-17 01:14:41 +0900646
Steven Moreland787b0432018-07-03 09:00:58 -0700647 /* dot joined package, example: "android.package.foo" */
648 std::string GetPackage() const;
649 /* dot joined package and name, example: "android.package.foo.IBar" */
650 std::string GetCanonicalName() const;
651 const std::vector<std::string>& GetSplitPackage() const { return package_; }
652
Steven Morelanded83a282018-07-17 13:27:29 -0700653 virtual std::string GetPreprocessDeclarationName() const = 0;
Steven Morelandc258abc2018-07-10 14:03:38 -0700654
Steven Moreland5557f1c2018-07-02 13:50:23 -0700655 virtual const AidlStructuredParcelable* AsStructuredParcelable() const { return nullptr; }
Steven Morelandc258abc2018-07-10 14:03:38 -0700656 virtual const AidlParcelable* AsParcelable() const { return nullptr; }
Daniel Norman85aed542019-08-21 12:01:14 -0700657 virtual const AidlEnumDeclaration* AsEnumDeclaration() const { return nullptr; }
Steven Moreland5557f1c2018-07-02 13:50:23 -0700658 virtual const AidlInterface* AsInterface() const { return nullptr; }
Jeongik Chadf76dc72019-11-28 00:08:47 +0900659 virtual const AidlParameterizable<std::string>* AsParameterizable() const { return nullptr; }
Andrei Onea9445fc62019-06-27 18:11:59 +0100660 virtual bool CheckValid(const AidlTypenames&) const { return CheckValidAnnotations(); }
Jeongik Cha047c5ee2019-08-07 23:16:49 +0900661 virtual bool LanguageSpecificCheckValid(Options::Language lang) const = 0;
Steven Morelandc258abc2018-07-10 14:03:38 -0700662 AidlStructuredParcelable* AsStructuredParcelable() {
663 return const_cast<AidlStructuredParcelable*>(
664 const_cast<const AidlDefinedType*>(this)->AsStructuredParcelable());
665 }
666 AidlParcelable* AsParcelable() {
667 return const_cast<AidlParcelable*>(const_cast<const AidlDefinedType*>(this)->AsParcelable());
668 }
Daniel Norman85aed542019-08-21 12:01:14 -0700669 AidlEnumDeclaration* AsEnumDeclaration() {
670 return const_cast<AidlEnumDeclaration*>(
671 const_cast<const AidlDefinedType*>(this)->AsEnumDeclaration());
672 }
Steven Morelandc258abc2018-07-10 14:03:38 -0700673 AidlInterface* AsInterface() {
674 return const_cast<AidlInterface*>(const_cast<const AidlDefinedType*>(this)->AsInterface());
675 }
676
Jeongik Chadf76dc72019-11-28 00:08:47 +0900677 AidlParameterizable<std::string>* AsParameterizable() {
678 return const_cast<AidlParameterizable<std::string>*>(
679 const_cast<const AidlDefinedType*>(this)->AsParameterizable());
680 }
681
Steven Moreland6cee3482018-07-18 14:39:58 -0700682 const AidlParcelable* AsUnstructuredParcelable() const {
683 if (this->AsStructuredParcelable() != nullptr) return nullptr;
684 return this->AsParcelable();
685 }
686 AidlParcelable* AsUnstructuredParcelable() {
687 return const_cast<AidlParcelable*>(
688 const_cast<const AidlDefinedType*>(this)->AsUnstructuredParcelable());
689 }
690
Jeongik Cha997281d2020-01-16 15:23:59 +0900691 virtual void Dump(CodeWriter* writer) const = 0;
Jiyong Park02da7422018-07-16 16:00:26 +0900692
Steven Moreland787b0432018-07-03 09:00:58 -0700693 private:
Jiyong Park1deecc32018-07-17 01:14:41 +0900694 std::string name_;
Jiyong Park1deecc32018-07-17 01:14:41 +0900695 std::string comments_;
Steven Moreland787b0432018-07-03 09:00:58 -0700696 const std::vector<std::string> package_;
Steven Moreland5557f1c2018-07-02 13:50:23 -0700697
698 DISALLOW_COPY_AND_ASSIGN(AidlDefinedType);
Steven Moreland787b0432018-07-03 09:00:58 -0700699};
700
Jeongik Chadf76dc72019-11-28 00:08:47 +0900701class AidlParcelable : public AidlDefinedType, public AidlParameterizable<std::string> {
Casey Dahlin1ae2bc52015-10-07 18:49:10 -0700702 public:
Steven Moreland46e9da82018-07-27 15:45:29 -0700703 AidlParcelable(const AidlLocation& location, AidlQualifiedName* name,
Jiyong Parka6605ab2018-11-11 14:30:21 +0900704 const std::vector<std::string>& package, const std::string& comments,
Jeongik Chadf76dc72019-11-28 00:08:47 +0900705 const std::string& cpp_header = "",
706 std::vector<std::string>* type_params = nullptr);
Casey Dahlin1ae2bc52015-10-07 18:49:10 -0700707 virtual ~AidlParcelable() = default;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800708
Ningyuan Wangd17c58b2016-09-29 14:33:14 -0700709 // C++ uses "::" instead of "." to refer to a inner class.
710 std::string GetCppName() const { return name_->GetColonName(); }
Christopher Wiley8aa4d9f2015-11-16 19:10:45 -0800711 std::string GetCppHeader() const { return cpp_header_; }
Christopher Wiley8aa4d9f2015-11-16 19:10:45 -0800712
Jeongik Cha82317dd2019-02-27 20:26:11 +0900713 bool CheckValid(const AidlTypenames& typenames) const override;
Jeongik Cha047c5ee2019-08-07 23:16:49 +0900714 bool LanguageSpecificCheckValid(Options::Language lang) const override;
Steven Morelandc258abc2018-07-10 14:03:38 -0700715 const AidlParcelable* AsParcelable() const override { return this; }
Jeongik Chadf76dc72019-11-28 00:08:47 +0900716 const AidlParameterizable<std::string>* AsParameterizable() const override { return this; }
717 const AidlNode& AsAidlNode() const override { return *this; }
Steven Morelanded83a282018-07-17 13:27:29 -0700718 std::string GetPreprocessDeclarationName() const override { return "parcelable"; }
Steven Morelandc258abc2018-07-10 14:03:38 -0700719
Jeongik Cha997281d2020-01-16 15:23:59 +0900720 void Dump(CodeWriter* writer) const override;
Jiyong Park02da7422018-07-16 16:00:26 +0900721
Casey Dahlin1ae2bc52015-10-07 18:49:10 -0700722 private:
Christopher Wiley8aa4d9f2015-11-16 19:10:45 -0800723 std::unique_ptr<AidlQualifiedName> name_;
Christopher Wiley8aa4d9f2015-11-16 19:10:45 -0800724 std::string cpp_header_;
Casey Dahlin59401da2015-10-09 18:16:45 -0700725
Casey Dahlin1ae2bc52015-10-07 18:49:10 -0700726 DISALLOW_COPY_AND_ASSIGN(AidlParcelable);
Casey Dahlin0a2f8be2015-09-28 16:15:29 -0700727};
Adam Lesinskiffa16862014-01-23 18:17:42 -0800728
Steven Moreland5557f1c2018-07-02 13:50:23 -0700729class AidlStructuredParcelable : public AidlParcelable {
730 public:
Steven Moreland46e9da82018-07-27 15:45:29 -0700731 AidlStructuredParcelable(const AidlLocation& location, AidlQualifiedName* name,
Jiyong Parka6605ab2018-11-11 14:30:21 +0900732 const std::vector<std::string>& package, const std::string& comments,
Steven Moreland5557f1c2018-07-02 13:50:23 -0700733 std::vector<std::unique_ptr<AidlVariableDeclaration>>* variables);
734
735 const std::vector<std::unique_ptr<AidlVariableDeclaration>>& GetFields() const {
736 return variables_;
737 }
738
Steven Morelandc258abc2018-07-10 14:03:38 -0700739 const AidlStructuredParcelable* AsStructuredParcelable() const override { return this; }
Steven Morelanded83a282018-07-17 13:27:29 -0700740 std::string GetPreprocessDeclarationName() const override { return "structured_parcelable"; }
Steven Moreland5557f1c2018-07-02 13:50:23 -0700741
Jeongik Cha997281d2020-01-16 15:23:59 +0900742 void Dump(CodeWriter* writer) const override;
Jiyong Park02da7422018-07-16 16:00:26 +0900743
Jeongik Chadb0f59e2018-11-01 18:11:21 +0900744 bool CheckValid(const AidlTypenames& typenames) const override;
Jeongik Cha047c5ee2019-08-07 23:16:49 +0900745 bool LanguageSpecificCheckValid(Options::Language lang) const override;
Jeongik Chadb0f59e2018-11-01 18:11:21 +0900746
Steven Moreland5557f1c2018-07-02 13:50:23 -0700747 private:
748 const std::vector<std::unique_ptr<AidlVariableDeclaration>> variables_;
749
750 DISALLOW_COPY_AND_ASSIGN(AidlStructuredParcelable);
751};
752
Daniel Norman85aed542019-08-21 12:01:14 -0700753class AidlEnumerator : public AidlNode {
754 public:
Daniel Norman2e4112d2019-10-03 10:22:35 -0700755 AidlEnumerator(const AidlLocation& location, const std::string& name, AidlConstantValue* value,
756 const std::string& comments);
Daniel Norman85aed542019-08-21 12:01:14 -0700757 virtual ~AidlEnumerator() = default;
758
759 const std::string& GetName() const { return name_; }
Will McVickerd7d18df2019-09-12 13:40:50 -0700760 AidlConstantValue* GetValue() const { return value_.get(); }
Daniel Norman2e4112d2019-10-03 10:22:35 -0700761 const std::string& GetComments() const { return comments_; }
Daniel Norman85aed542019-08-21 12:01:14 -0700762 bool CheckValid(const AidlTypeSpecifier& enum_backing_type) const;
763
764 string ValueString(const AidlTypeSpecifier& backing_type,
765 const ConstantValueDecorator& decorator) const;
766
Daniel Normanb28684e2019-10-17 15:31:39 -0700767 void SetValue(std::unique_ptr<AidlConstantValue> value) { value_ = std::move(value); }
768
Daniel Norman85aed542019-08-21 12:01:14 -0700769 private:
770 const std::string name_;
Will McVickerd7d18df2019-09-12 13:40:50 -0700771 unique_ptr<AidlConstantValue> value_;
Daniel Norman2e4112d2019-10-03 10:22:35 -0700772 const std::string comments_;
Daniel Norman85aed542019-08-21 12:01:14 -0700773
774 DISALLOW_COPY_AND_ASSIGN(AidlEnumerator);
775};
776
777class AidlEnumDeclaration : public AidlDefinedType {
778 public:
Will McVickerd7d18df2019-09-12 13:40:50 -0700779 AidlEnumDeclaration(const AidlLocation& location, const string& name,
Daniel Norman85aed542019-08-21 12:01:14 -0700780 std::vector<std::unique_ptr<AidlEnumerator>>* enumerators,
Daniel Norman2e4112d2019-10-03 10:22:35 -0700781 const std::vector<std::string>& package, const std::string& comments);
Daniel Norman85aed542019-08-21 12:01:14 -0700782 virtual ~AidlEnumDeclaration() = default;
783
784 void SetBackingType(std::unique_ptr<const AidlTypeSpecifier> type);
785 const AidlTypeSpecifier& GetBackingType() const { return *backing_type_; }
786 const std::vector<std::unique_ptr<AidlEnumerator>>& GetEnumerators() const {
787 return enumerators_;
788 }
Steven Moreland59e53e42019-11-26 20:38:08 -0800789 bool Autofill();
Daniel Norman85aed542019-08-21 12:01:14 -0700790 bool CheckValid(const AidlTypenames& typenames) const override;
791 bool LanguageSpecificCheckValid(Options::Language) const override { return true; }
792 std::string GetPreprocessDeclarationName() const override { return "enum"; }
Jeongik Cha997281d2020-01-16 15:23:59 +0900793 void Dump(CodeWriter* writer) const override;
Daniel Norman85aed542019-08-21 12:01:14 -0700794
795 const AidlEnumDeclaration* AsEnumDeclaration() const override { return this; }
796
797 private:
798 const std::string name_;
799 const std::vector<std::unique_ptr<AidlEnumerator>> enumerators_;
800 std::unique_ptr<const AidlTypeSpecifier> backing_type_;
801
802 DISALLOW_COPY_AND_ASSIGN(AidlEnumDeclaration);
803};
804
Jiyong Park1deecc32018-07-17 01:14:41 +0900805class AidlInterface final : public AidlDefinedType {
Casey Dahlin1ae2bc52015-10-07 18:49:10 -0700806 public:
Steven Moreland46e9da82018-07-27 15:45:29 -0700807 AidlInterface(const AidlLocation& location, const std::string& name, const std::string& comments,
808 bool oneway_, std::vector<std::unique_ptr<AidlMember>>* members,
Christopher Wileyd76067c2015-10-19 17:00:13 -0700809 const std::vector<std::string>& package);
Casey Dahlin1ae2bc52015-10-07 18:49:10 -0700810 virtual ~AidlInterface() = default;
811
Casey Dahlinfb7da2e2015-10-08 17:26:09 -0700812 const std::vector<std::unique_ptr<AidlMethod>>& GetMethods() const
813 { return methods_; }
Jiyong Park309668e2018-07-28 16:55:44 +0900814 std::vector<std::unique_ptr<AidlMethod>>& GetMutableMethods() { return methods_; }
Steven Moreland693640b2018-07-19 13:46:27 -0700815 const std::vector<std::unique_ptr<AidlConstantDeclaration>>& GetConstantDeclarations() const {
816 return constants_;
817 }
Casey Dahlina2f77c42015-12-01 18:26:02 -0800818
Steven Morelandc258abc2018-07-10 14:03:38 -0700819 const AidlInterface* AsInterface() const override { return this; }
Steven Morelanded83a282018-07-17 13:27:29 -0700820 std::string GetPreprocessDeclarationName() const override { return "interface"; }
Steven Moreland5557f1c2018-07-02 13:50:23 -0700821
Jeongik Cha997281d2020-01-16 15:23:59 +0900822 void Dump(CodeWriter* writer) const override;
Jiyong Park02da7422018-07-16 16:00:26 +0900823
Jeongik Chadb0f59e2018-11-01 18:11:21 +0900824 bool CheckValid(const AidlTypenames& typenames) const override;
Jeongik Cha047c5ee2019-08-07 23:16:49 +0900825 bool LanguageSpecificCheckValid(Options::Language lang) const override;
Jeongik Chadb0f59e2018-11-01 18:11:21 +0900826
Casey Dahlin1ae2bc52015-10-07 18:49:10 -0700827 private:
Casey Dahlinfb7da2e2015-10-08 17:26:09 -0700828 std::vector<std::unique_ptr<AidlMethod>> methods_;
Steven Moreland693640b2018-07-19 13:46:27 -0700829 std::vector<std::unique_ptr<AidlConstantDeclaration>> constants_;
Casey Dahlinfb7da2e2015-10-08 17:26:09 -0700830
Casey Dahlin1ae2bc52015-10-07 18:49:10 -0700831 DISALLOW_COPY_AND_ASSIGN(AidlInterface);
Casey Dahlin0a2f8be2015-09-28 16:15:29 -0700832};
Adam Lesinskiffa16862014-01-23 18:17:42 -0800833
Casey Dahlin0edf3422015-10-07 12:34:59 -0700834class AidlImport : public AidlNode {
835 public:
Steven Moreland46e9da82018-07-27 15:45:29 -0700836 AidlImport(const AidlLocation& location, const std::string& needed_class);
Casey Dahlin0edf3422015-10-07 12:34:59 -0700837 virtual ~AidlImport() = default;
838
Casey Dahlin0edf3422015-10-07 12:34:59 -0700839 const std::string& GetFilename() const { return filename_; }
840 const std::string& GetNeededClass() const { return needed_class_; }
Casey Dahlin0edf3422015-10-07 12:34:59 -0700841
842 private:
Casey Dahlin0edf3422015-10-07 12:34:59 -0700843 std::string filename_;
844 std::string needed_class_;
Casey Dahlin0edf3422015-10-07 12:34:59 -0700845
846 DISALLOW_COPY_AND_ASSIGN(AidlImport);
Casey Dahline2507492015-09-14 17:11:20 -0700847};
848
849class Parser {
Casey Dahlindd691812015-09-09 17:59:06 -0700850 public:
Casey Dahline2507492015-09-14 17:11:20 -0700851 ~Parser();
Casey Dahlindd691812015-09-09 17:59:06 -0700852
Steven Moreland64e29be2018-08-08 18:52:19 -0700853 // Parse contents of file |filename|. Should only be called once.
854 static std::unique_ptr<Parser> Parse(const std::string& filename,
855 const android::aidl::IoDelegate& io_delegate,
856 AidlTypenames& typenames);
Casey Dahlin89d44842015-09-24 18:45:54 -0700857
Steven Moreland2ca4fcb2018-06-27 16:01:01 -0700858 void AddError() { error_++; }
Steven Moreland64e29be2018-08-08 18:52:19 -0700859 bool HasError() { return error_ != 0; }
Casey Dahlindd691812015-09-09 17:59:06 -0700860
Casey Dahlin3c6df362015-10-06 15:48:35 -0700861 const std::string& FileName() const { return filename_; }
Casey Dahlin42727f82015-10-12 19:23:40 -0700862 void* Scanner() const { return scanner_; }
Casey Dahlindd691812015-09-09 17:59:06 -0700863
Steven Morelandd1039a92020-01-23 09:49:43 -0800864 void AddImport(std::unique_ptr<AidlImport>&& import);
Christopher Wileyd76067c2015-10-19 17:00:13 -0700865 const std::vector<std::unique_ptr<AidlImport>>& GetImports() {
866 return imports_;
867 }
Casey Dahlindd691812015-09-09 17:59:06 -0700868
Jiyong Parkb034bf02018-07-30 17:44:33 +0900869 void SetPackage(unique_ptr<AidlQualifiedName> name) { package_ = std::move(name); }
870 std::vector<std::string> Package() const;
Jiyong Park1deecc32018-07-17 01:14:41 +0900871
872 void DeferResolution(AidlTypeSpecifier* typespec) {
873 unresolved_typespecs_.emplace_back(typespec);
874 }
875
Jiyong Parke59c3682018-09-11 23:10:25 +0900876 const vector<AidlTypeSpecifier*>& GetUnresolvedTypespecs() const { return unresolved_typespecs_; }
877
Jiyong Park1deecc32018-07-17 01:14:41 +0900878 bool Resolve();
879
Jiyong Parkb034bf02018-07-30 17:44:33 +0900880 void AddDefinedType(unique_ptr<AidlDefinedType> type) {
881 // Parser does NOT own AidlDefinedType, it just has references to the types
882 // that it encountered while parsing the input file.
883 defined_types_.emplace_back(type.get());
884
885 // AidlDefinedType IS owned by AidlTypenames
886 if (!typenames_.AddDefinedType(std::move(type))) {
887 AddError();
888 }
889 }
890
891 vector<AidlDefinedType*>& GetDefinedTypes() { return defined_types_; }
892
Casey Dahlindd691812015-09-09 17:59:06 -0700893 private:
Steven Moreland64e29be2018-08-08 18:52:19 -0700894 explicit Parser(const std::string& filename, std::string& raw_buffer,
895 android::aidl::AidlTypenames& typenames);
896
Casey Dahlindd691812015-09-09 17:59:06 -0700897 std::string filename_;
Christopher Wileyd76067c2015-10-19 17:00:13 -0700898 std::unique_ptr<AidlQualifiedName> package_;
Jiyong Parkb034bf02018-07-30 17:44:33 +0900899 AidlTypenames& typenames_;
Steven Moreland64e29be2018-08-08 18:52:19 -0700900
901 void* scanner_ = nullptr;
902 YY_BUFFER_STATE buffer_;
903 int error_ = 0;
904
905 std::vector<std::unique_ptr<AidlImport>> imports_;
Jiyong Parkb034bf02018-07-30 17:44:33 +0900906 vector<AidlDefinedType*> defined_types_;
Jiyong Park1deecc32018-07-17 01:14:41 +0900907 vector<AidlTypeSpecifier*> unresolved_typespecs_;
Casey Dahlindd691812015-09-09 17:59:06 -0700908
Casey Dahline2507492015-09-14 17:11:20 -0700909 DISALLOW_COPY_AND_ASSIGN(Parser);
Casey Dahlindd691812015-09-09 17:59:06 -0700910};