blob: 5f08504a0c2a0ba19827c67f436213e0b6c4e9a5 [file] [log] [blame]
Will McVickerd7d18df2019-09-12 13:40:50 -07001/*
2 * Copyright (C) 2019, The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Steven Moreland9fccf582018-08-27 20:36:27 -070017#pragma once
Christopher Wileyec31a052016-01-25 07:28:51 -080018
Jiyong Park1deecc32018-07-17 01:14:41 +090019#include "aidl_typenames.h"
Jiyong Park02da7422018-07-16 16:00:26 +090020#include "code_writer.h"
Jiyong Park1deecc32018-07-17 01:14:41 +090021#include "io_delegate.h"
Jeongik Cha047c5ee2019-08-07 23:16:49 +090022#include "options.h"
Jiyong Park1deecc32018-07-17 01:14:41 +090023
Casey Dahlinbc7a50a2015-09-28 19:20:50 -070024#include <memory>
Jeongik Cha997281d2020-01-16 15:23:59 +090025#include <regex>
Casey Dahlindd691812015-09-09 17:59:06 -070026#include <string>
Jeongik Chadf76dc72019-11-28 00:08:47 +090027#include <unordered_set>
Casey Dahlinbc7a50a2015-09-28 19:20:50 -070028#include <vector>
Casey Dahlindd691812015-09-09 17:59:06 -070029
Elliott Hughes0a620672015-12-04 13:53:18 -080030#include <android-base/macros.h>
31#include <android-base/strings.h>
Casey Dahlin73d46b02015-09-11 02:47:54 +000032
Casey Dahlin89d44842015-09-24 18:45:54 -070033struct yy_buffer_state;
34typedef yy_buffer_state* YY_BUFFER_STATE;
35
Jiyong Parkb034bf02018-07-30 17:44:33 +090036using android::aidl::AidlTypenames;
Jiyong Park02da7422018-07-16 16:00:26 +090037using android::aidl::CodeWriter;
Jeongik Cha047c5ee2019-08-07 23:16:49 +090038using android::aidl::Options;
Steven Moreland3f658cf2018-08-20 13:40:54 -070039using std::shared_ptr;
Jiyong Park1deecc32018-07-17 01:14:41 +090040using std::string;
41using std::unique_ptr;
42using std::vector;
Andrei Onea8714b022019-02-01 18:55:54 +000043class AidlNode;
44
45namespace android {
46namespace aidl {
47namespace mappings {
48std::string dump_location(const AidlNode& method);
49} // namespace mappings
Mathew Inwoodadb74672019-11-29 14:01:53 +000050namespace java {
51std::string dump_location(const AidlNode& method);
52} // namespace java
Andrei Onea8714b022019-02-01 18:55:54 +000053} // namespace aidl
54} // namespace android
55
Casey Dahlincdbbc8c2015-10-14 15:31:04 -070056class AidlToken {
57 public:
58 AidlToken(const std::string& text, const std::string& comments);
Adam Lesinskiffa16862014-01-23 18:17:42 -080059
Casey Dahlincdbbc8c2015-10-14 15:31:04 -070060 const std::string& GetText() const { return text_; }
61 const std::string& GetComments() const { return comments_; }
Adam Lesinskiffa16862014-01-23 18:17:42 -080062
Casey Dahlincdbbc8c2015-10-14 15:31:04 -070063 private:
64 std::string text_;
65 std::string comments_;
Casey Dahlin082f1d12015-09-21 14:06:25 -070066
Casey Dahlincdbbc8c2015-10-14 15:31:04 -070067 DISALLOW_COPY_AND_ASSIGN(AidlToken);
Casey Dahlin0a2f8be2015-09-28 16:15:29 -070068};
Adam Lesinskiffa16862014-01-23 18:17:42 -080069
Steven Moreland46e9da82018-07-27 15:45:29 -070070class AidlLocation {
Casey Dahlinbc7a50a2015-09-28 19:20:50 -070071 public:
Steven Moreland46e9da82018-07-27 15:45:29 -070072 struct Point {
Dan Willemsen609ba6d2019-12-30 10:44:00 -080073 int line;
74 int column;
Steven Moreland46e9da82018-07-27 15:45:29 -070075 };
76
Devin Mooredf93ebb2020-03-25 14:03:35 -070077 enum class Source {
78 // From internal aidl source code
79 INTERNAL = 0,
80 // From a parsed file
81 EXTERNAL = 1
82 };
83
84 AidlLocation(const std::string& file, Point begin, Point end, Source source);
Devin Moore5de18ed2020-04-02 13:52:29 -070085 AidlLocation(const std::string& file, Source source)
86 : AidlLocation(file, {0, 0}, {0, 0}, source) {}
Devin Mooredf93ebb2020-03-25 14:03:35 -070087
88 bool IsInternal() const { return source_ == Source::INTERNAL; }
Steven Moreland46e9da82018-07-27 15:45:29 -070089
Devin Moore5de18ed2020-04-02 13:52:29 -070090 // The first line of a file is line 1.
91 bool LocationKnown() const { return begin_.line != 0; }
92
Steven Moreland46e9da82018-07-27 15:45:29 -070093 friend std::ostream& operator<<(std::ostream& os, const AidlLocation& l);
Andrei Onea8714b022019-02-01 18:55:54 +000094 friend class AidlNode;
Casey Dahlinbc7a50a2015-09-28 19:20:50 -070095
96 private:
Steven Moreland46e9da82018-07-27 15:45:29 -070097 const std::string file_;
98 Point begin_;
99 Point end_;
Devin Mooredf93ebb2020-03-25 14:03:35 -0700100 Source source_;
Steven Moreland46e9da82018-07-27 15:45:29 -0700101};
102
Devin Mooredf93ebb2020-03-25 14:03:35 -0700103#define AIDL_LOCATION_HERE \
104 AidlLocation { __FILE__, {__LINE__, 0}, {__LINE__, 0}, AidlLocation::Source::INTERNAL }
Steven Moreland02e012e2018-08-02 14:58:10 -0700105
Steven Moreland46e9da82018-07-27 15:45:29 -0700106std::ostream& operator<<(std::ostream& os, const AidlLocation& l);
107
108// Anything that is locatable in a .aidl file.
109class AidlNode {
110 public:
111 AidlNode(const AidlLocation& location);
Steven Moreland3f658cf2018-08-20 13:40:54 -0700112
113 AidlNode(const AidlNode&) = default;
Steven Moreland3be75772018-08-20 13:27:43 -0700114 AidlNode(AidlNode&&) = default;
Steven Moreland46e9da82018-07-27 15:45:29 -0700115 virtual ~AidlNode() = default;
116
Devin Mooredf93ebb2020-03-25 14:03:35 -0700117 // To be able to print AidlLocation
Steven Morelandb0d15a52020-03-31 14:03:47 -0700118 friend class AidlErrorLog;
Andrei Onea8714b022019-02-01 18:55:54 +0000119 friend std::string android::aidl::mappings::dump_location(const AidlNode&);
Mathew Inwoodadb74672019-11-29 14:01:53 +0000120 friend std::string android::aidl::java::dump_location(const AidlNode&);
Steven Moreland46e9da82018-07-27 15:45:29 -0700121
Devin Mooredf93ebb2020-03-25 14:03:35 -0700122 protected:
123 // This should only be used to construct implicit nodes related to existing nodes
124 const AidlLocation& GetLocation() const { return location_; }
125
Steven Moreland46e9da82018-07-27 15:45:29 -0700126 private:
Mathew Inwoodadb74672019-11-29 14:01:53 +0000127 std::string PrintLine() const;
Andrei Onea8714b022019-02-01 18:55:54 +0000128 std::string PrintLocation() const;
Steven Moreland46e9da82018-07-27 15:45:29 -0700129 const AidlLocation location_;
Casey Dahlinbc7a50a2015-09-28 19:20:50 -0700130};
131
Steven Moreland46e9da82018-07-27 15:45:29 -0700132// Generic point for printing any error in the AIDL compiler.
Steven Morelandb0d15a52020-03-31 14:03:47 -0700133class AidlErrorLog {
Steven Moreland46e9da82018-07-27 15:45:29 -0700134 public:
Devin Moore5de18ed2020-04-02 13:52:29 -0700135 AidlErrorLog(bool fatal, const std::string& filename)
136 : AidlErrorLog(fatal, AidlLocation(filename, AidlLocation::Source::EXTERNAL)) {}
Steven Morelandb0d15a52020-03-31 14:03:47 -0700137 AidlErrorLog(bool fatal, const AidlLocation& location);
138 AidlErrorLog(bool fatal, const AidlNode& node) : AidlErrorLog(fatal, node.location_) {}
139 AidlErrorLog(bool fatal, const AidlNode* node) : AidlErrorLog(fatal, *node) {}
Steven Moreland92c55f12018-07-31 14:08:37 -0700140
141 template <typename T>
Steven Morelandb0d15a52020-03-31 14:03:47 -0700142 AidlErrorLog(bool fatal, const std::unique_ptr<T>& node) : AidlErrorLog(fatal, *node) {}
143 ~AidlErrorLog() {
Steven Moreland46e9da82018-07-27 15:45:29 -0700144 os_ << std::endl;
145 if (fatal_) abort();
Devin Moore5de18ed2020-04-02 13:52:29 -0700146 if (location_.IsInternal()) {
147 os_ << "Logging an internal location should not happen. Offending location: " << location_
148 << std::endl;
149 abort();
150 }
Steven Moreland46e9da82018-07-27 15:45:29 -0700151 }
152
153 std::ostream& os_;
154
Steven Morelandfdb57cd2020-01-08 20:03:30 -0800155 static bool hadError() { return sHadError; }
156
Steven Moreland46e9da82018-07-27 15:45:29 -0700157 private:
Steven Moreland46e9da82018-07-27 15:45:29 -0700158
159 bool fatal_;
160
Devin Moore5de18ed2020-04-02 13:52:29 -0700161 const AidlLocation location_;
162
Steven Morelandfdb57cd2020-01-08 20:03:30 -0800163 static bool sHadError;
164
Steven Morelandb0d15a52020-03-31 14:03:47 -0700165 DISALLOW_COPY_AND_ASSIGN(AidlErrorLog);
Steven Moreland46e9da82018-07-27 15:45:29 -0700166};
167
Steven Morelandb0d15a52020-03-31 14:03:47 -0700168#define AIDL_ERROR(CONTEXT) ::AidlErrorLog(false /*fatal*/, (CONTEXT)).os_
169#define AIDL_FATAL(CONTEXT) ::AidlErrorLog(true /*fatal*/, (CONTEXT)).os_
Steven Moreland3f658cf2018-08-20 13:40:54 -0700170#define AIDL_FATAL_IF(CONDITION, CONTEXT) \
171 if (CONDITION) AIDL_FATAL(CONTEXT) << "Bad internal state: " << #CONDITION << ": "
Steven Moreland46e9da82018-07-27 15:45:29 -0700172
Casey Dahlina2f77c42015-12-01 18:26:02 -0800173namespace android {
174namespace aidl {
175
Jiyong Park1deecc32018-07-17 01:14:41 +0900176class AidlTypenames;
Casey Dahlina2f77c42015-12-01 18:26:02 -0800177
178} // namespace aidl
179} // namespace android
180
Jeongik Chadf76dc72019-11-28 00:08:47 +0900181// unique_ptr<AidlTypeSpecifier> for type arugment,
182// std::string for type parameter(T, U, and so on).
183template <typename T>
184class AidlParameterizable {
185 public:
186 AidlParameterizable(std::vector<T>* type_params) : type_params_(type_params) {}
187 virtual ~AidlParameterizable() = default;
188 bool IsGeneric() const { return type_params_ != nullptr; }
189 const std::vector<T>& GetTypeParameters() const { return *type_params_; }
190 bool CheckValid() const;
191
192 virtual const AidlNode& AsAidlNode() const = 0;
193
194 protected:
195 AidlParameterizable(const AidlParameterizable&);
196
197 private:
198 const unique_ptr<std::vector<T>> type_params_;
199 static_assert(std::is_same<T, unique_ptr<AidlTypeSpecifier>>::value ||
200 std::is_same<T, std::string>::value);
201};
202template <>
203bool AidlParameterizable<std::string>::CheckValid() const;
204
Andrei Onea9445fc62019-06-27 18:11:59 +0100205class AidlConstantValue;
206class AidlConstantDeclaration;
207
208// Transforms a value string into a language specific form. Raw value as produced by
209// AidlConstantValue.
210using ConstantValueDecorator =
211 std::function<std::string(const AidlTypeSpecifier& type, const std::string& raw_value)>;
212
Jiyong Park68bc77a2018-07-19 19:00:45 +0900213class AidlAnnotation : public AidlNode {
214 public:
Andrei Onea9445fc62019-06-27 18:11:59 +0100215 static AidlAnnotation* Parse(
216 const AidlLocation& location, const string& name,
217 std::map<std::string, std::shared_ptr<AidlConstantValue>>* parameter_list);
Steven Moreland46e9da82018-07-27 15:45:29 -0700218
Steven Moreland3f658cf2018-08-20 13:40:54 -0700219 AidlAnnotation(const AidlAnnotation&) = default;
Steven Moreland3be75772018-08-20 13:27:43 -0700220 AidlAnnotation(AidlAnnotation&&) = default;
Jiyong Park68bc77a2018-07-19 19:00:45 +0900221 virtual ~AidlAnnotation() = default;
Andrei Onea9445fc62019-06-27 18:11:59 +0100222 bool CheckValid() const;
Steven Moreland3be75772018-08-20 13:27:43 -0700223
Jiyong Park68bc77a2018-07-19 19:00:45 +0900224 const string& GetName() const { return name_; }
Daniel Norman37d43dd2019-09-09 17:22:34 -0700225 string ToString(const ConstantValueDecorator& decorator) const;
Andrei Onea9445fc62019-06-27 18:11:59 +0100226 std::map<std::string, std::string> AnnotationParams(
227 const ConstantValueDecorator& decorator) const;
Jiyong Parka6605ab2018-11-11 14:30:21 +0900228 const string& GetComments() const { return comments_; }
229 void SetComments(const string& comments) { comments_ = comments; }
Jiyong Park68bc77a2018-07-19 19:00:45 +0900230
231 private:
Steven Moreland46e9da82018-07-27 15:45:29 -0700232 AidlAnnotation(const AidlLocation& location, const string& name);
Andrei Onea9445fc62019-06-27 18:11:59 +0100233 AidlAnnotation(const AidlLocation& location, const string& name,
234 std::map<std::string, std::shared_ptr<AidlConstantValue>>&& parameters);
Jiyong Park68bc77a2018-07-19 19:00:45 +0900235 const string name_;
Jiyong Parka6605ab2018-11-11 14:30:21 +0900236 string comments_;
Andrei Onea9445fc62019-06-27 18:11:59 +0100237 std::map<std::string, std::shared_ptr<AidlConstantValue>> parameters_;
Jiyong Park68bc77a2018-07-19 19:00:45 +0900238};
239
Steven Moreland3be75772018-08-20 13:27:43 -0700240static inline bool operator<(const AidlAnnotation& lhs, const AidlAnnotation& rhs) {
241 return lhs.GetName() < rhs.GetName();
242}
243static inline bool operator==(const AidlAnnotation& lhs, const AidlAnnotation& rhs) {
244 return lhs.GetName() == rhs.GetName();
245}
Jiyong Park3656c3c2018-08-01 20:02:01 +0900246
Casey Dahline7922932016-02-29 17:23:01 -0800247class AidlAnnotatable : public AidlNode {
Casey Dahlin0ee37582015-09-30 16:31:55 -0700248 public:
Steven Moreland46e9da82018-07-27 15:45:29 -0700249 AidlAnnotatable(const AidlLocation& location);
Steven Moreland3f658cf2018-08-20 13:40:54 -0700250
251 AidlAnnotatable(const AidlAnnotatable&) = default;
252 AidlAnnotatable(AidlAnnotatable&&) = default;
Casey Dahline7922932016-02-29 17:23:01 -0800253 virtual ~AidlAnnotatable() = default;
254
Artur Satayev91fe8712019-07-29 13:06:01 +0100255 void Annotate(vector<AidlAnnotation>&& annotations) {
256 for (auto& annotation : annotations) {
257 annotations_.emplace_back(std::move(annotation));
258 }
259 }
Jiyong Park68bc77a2018-07-19 19:00:45 +0900260 bool IsNullable() const;
Jiyong Park68bc77a2018-07-19 19:00:45 +0900261 bool IsUtf8InCpp() const;
Steven Morelanda57d0a62019-07-30 09:41:14 -0700262 bool IsVintfStability() const;
Jeongik Cha88f95a82020-01-15 13:02:16 +0900263 bool IsStableApiParcelable(Options::Language lang) const;
Makoto Onuki78a1c1c2020-03-04 16:57:23 -0800264 bool IsHide() const;
Andrei Onea9445fc62019-06-27 18:11:59 +0100265
Steven Moreland7e4b9502020-02-20 18:10:42 -0800266 void DumpAnnotations(CodeWriter* writer) const;
267
Andrei Onea9445fc62019-06-27 18:11:59 +0100268 const AidlAnnotation* UnsupportedAppUsage() const;
Daniel Norman716d3112019-09-10 13:11:56 -0700269 const AidlTypeSpecifier* BackingType(const AidlTypenames& typenames) const;
Jiyong Park68bc77a2018-07-19 19:00:45 +0900270 std::string ToString() const;
Casey Dahline7922932016-02-29 17:23:01 -0800271
Jiyong Parka6605ab2018-11-11 14:30:21 +0900272 const vector<AidlAnnotation>& GetAnnotations() const { return annotations_; }
Devin Moore24f68572020-02-26 13:20:59 -0800273 virtual std::set<string> GetSupportedAnnotations() const = 0;
274 virtual bool CheckValid(const AidlTypenames&) const;
Jiyong Park3656c3c2018-08-01 20:02:01 +0900275
Casey Dahline7922932016-02-29 17:23:01 -0800276 private:
Jiyong Parka6605ab2018-11-11 14:30:21 +0900277 vector<AidlAnnotation> annotations_;
Casey Dahline7922932016-02-29 17:23:01 -0800278};
279
Jiyong Park1deecc32018-07-17 01:14:41 +0900280class AidlQualifiedName;
281
282// AidlTypeSpecifier represents a reference to either a built-in type,
283// a defined type, or a variant (e.g., array of generic) of a type.
Jeongik Chadf76dc72019-11-28 00:08:47 +0900284class AidlTypeSpecifier final : public AidlAnnotatable,
285 public AidlParameterizable<unique_ptr<AidlTypeSpecifier>> {
Casey Dahline7922932016-02-29 17:23:01 -0800286 public:
Steven Moreland46e9da82018-07-27 15:45:29 -0700287 AidlTypeSpecifier(const AidlLocation& location, const string& unresolved_name, bool is_array,
288 vector<unique_ptr<AidlTypeSpecifier>>* type_params, const string& comments);
Jiyong Parkd59a10d2018-07-18 11:12:55 +0900289 virtual ~AidlTypeSpecifier() = default;
Casey Dahlin0ee37582015-09-30 16:31:55 -0700290
Steven Moreland3f658cf2018-08-20 13:40:54 -0700291 // Copy of this type which is not an array.
292 AidlTypeSpecifier ArrayBase() const;
293
Jiyong Park1deecc32018-07-17 01:14:41 +0900294 // Returns the full-qualified name of the base type.
295 // int -> int
296 // int[] -> int
297 // List<String> -> List
298 // IFoo -> foo.bar.IFoo (if IFoo is in package foo.bar)
299 const string& GetName() const {
300 if (IsResolved()) {
301 return fully_qualified_name_;
302 } else {
303 return GetUnresolvedName();
304 }
305 }
Casey Dahlin0ee37582015-09-30 16:31:55 -0700306
Jiyong Park1deecc32018-07-17 01:14:41 +0900307 // Returns string representation of this type specifier.
Artur Satayev91fe8712019-07-29 13:06:01 +0100308 // This is GetBaseTypeName() + array modifier or generic type parameters
Jiyong Park1deecc32018-07-17 01:14:41 +0900309 string ToString() const;
310
Jiyong Park02da7422018-07-16 16:00:26 +0900311 std::string Signature() const;
312
Jiyong Park1deecc32018-07-17 01:14:41 +0900313 const string& GetUnresolvedName() const { return unresolved_name_; }
314
Jeongik Cha997281d2020-01-16 15:23:59 +0900315 bool IsHidden() const;
316
Jiyong Park1deecc32018-07-17 01:14:41 +0900317 const string& GetComments() const { return comments_; }
318
Jeongik Cha1a7ab642019-07-29 17:31:02 +0900319 const std::vector<std::string> GetSplitName() const { return split_name_; }
320
Jiyong Parka6605ab2018-11-11 14:30:21 +0900321 void SetComments(const string& comment) { comments_ = comment; }
322
Jiyong Park1deecc32018-07-17 01:14:41 +0900323 bool IsResolved() const { return fully_qualified_name_ != ""; }
324
325 bool IsArray() const { return is_array_; }
326
Jiyong Park1deecc32018-07-17 01:14:41 +0900327 // Resolve the base type name to a fully-qualified name. Return false if the
328 // resolution fails.
Daniel Norman716d3112019-09-10 13:11:56 -0700329 bool Resolve(const AidlTypenames& typenames);
Casey Dahlin0ee37582015-09-30 16:31:55 -0700330
Devin Moore24f68572020-02-26 13:20:59 -0800331 std::set<string> GetSupportedAnnotations() const override;
332 bool CheckValid(const AidlTypenames& typenames) const override;
Jeongik Cha047c5ee2019-08-07 23:16:49 +0900333 bool LanguageSpecificCheckValid(Options::Language lang) const;
Jeongik Chadf76dc72019-11-28 00:08:47 +0900334 const AidlNode& AsAidlNode() const override { return *this; }
Jiyong Park1d2df7d2018-07-23 15:22:50 +0900335
Casey Dahlin0ee37582015-09-30 16:31:55 -0700336 private:
Steven Moreland3f658cf2018-08-20 13:40:54 -0700337 AidlTypeSpecifier(const AidlTypeSpecifier&) = default;
338
Jiyong Park1deecc32018-07-17 01:14:41 +0900339 const string unresolved_name_;
340 string fully_qualified_name_;
Steven Moreland3f658cf2018-08-20 13:40:54 -0700341 bool is_array_;
Jiyong Parka6605ab2018-11-11 14:30:21 +0900342 string comments_;
Jeongik Cha1a7ab642019-07-29 17:31:02 +0900343 vector<string> split_name_;
Casey Dahlin0ee37582015-09-30 16:31:55 -0700344};
345
Steven Moreland860b1942018-08-16 14:59:28 -0700346// Returns the universal value unaltered.
347std::string AidlConstantValueDecorator(const AidlTypeSpecifier& type, const std::string& raw_value);
348
Steven Moreland9ea10e32018-07-19 15:26:09 -0700349class AidlConstantValue;
Steven Moreland5557f1c2018-07-02 13:50:23 -0700350class AidlVariableDeclaration : public AidlNode {
351 public:
Steven Moreland46e9da82018-07-27 15:45:29 -0700352 AidlVariableDeclaration(const AidlLocation& location, AidlTypeSpecifier* type,
353 const std::string& name);
354 AidlVariableDeclaration(const AidlLocation& location, AidlTypeSpecifier* type,
355 const std::string& name, AidlConstantValue* default_value);
Steven Moreland5557f1c2018-07-02 13:50:23 -0700356 virtual ~AidlVariableDeclaration() = default;
357
358 std::string GetName() const { return name_; }
Jiyong Parkd59a10d2018-07-18 11:12:55 +0900359 const AidlTypeSpecifier& GetType() const { return *type_; }
Steven Moreland9ea10e32018-07-19 15:26:09 -0700360 const AidlConstantValue* GetDefaultValue() const { return default_value_.get(); }
361
Jiyong Parkd59a10d2018-07-18 11:12:55 +0900362 AidlTypeSpecifier* GetMutableType() { return type_.get(); }
Steven Moreland5557f1c2018-07-02 13:50:23 -0700363
Jeongik Chadb0f59e2018-11-01 18:11:21 +0900364 bool CheckValid(const AidlTypenames& typenames) const;
Steven Moreland5557f1c2018-07-02 13:50:23 -0700365 std::string ToString() const;
Jiyong Park02da7422018-07-16 16:00:26 +0900366 std::string Signature() const;
Steven Moreland5557f1c2018-07-02 13:50:23 -0700367
Steven Moreland860b1942018-08-16 14:59:28 -0700368 std::string ValueString(const ConstantValueDecorator& decorator) const;
Steven Moreland25294322018-08-07 18:13:55 -0700369
Steven Moreland5557f1c2018-07-02 13:50:23 -0700370 private:
Jiyong Parkd59a10d2018-07-18 11:12:55 +0900371 std::unique_ptr<AidlTypeSpecifier> type_;
Steven Moreland5557f1c2018-07-02 13:50:23 -0700372 std::string name_;
Steven Moreland9ea10e32018-07-19 15:26:09 -0700373 std::unique_ptr<AidlConstantValue> default_value_;
Steven Moreland5557f1c2018-07-02 13:50:23 -0700374
375 DISALLOW_COPY_AND_ASSIGN(AidlVariableDeclaration);
376};
377
378class AidlArgument : public AidlVariableDeclaration {
Casey Dahlinbc7a50a2015-09-28 19:20:50 -0700379 public:
Casey Dahlinc378c992015-09-29 16:50:40 -0700380 enum Direction { IN_DIR = 1, OUT_DIR = 2, INOUT_DIR = 3 };
381
Steven Moreland46e9da82018-07-27 15:45:29 -0700382 AidlArgument(const AidlLocation& location, AidlArgument::Direction direction,
383 AidlTypeSpecifier* type, const std::string& name);
384 AidlArgument(const AidlLocation& location, AidlTypeSpecifier* type, const std::string& name);
Casey Dahlinbc7a50a2015-09-28 19:20:50 -0700385 virtual ~AidlArgument() = default;
386
Casey Dahlinc378c992015-09-29 16:50:40 -0700387 Direction GetDirection() const { return direction_; }
Christopher Wileyad339272015-10-05 19:11:58 -0700388 bool IsOut() const { return direction_ & OUT_DIR; }
389 bool IsIn() const { return direction_ & IN_DIR; }
Casey Dahlinc378c992015-09-29 16:50:40 -0700390 bool DirectionWasSpecified() const { return direction_specified_; }
Jiyong Park3656c3c2018-08-01 20:02:01 +0900391 string GetDirectionSpecifier() const;
Christopher Wileyad339272015-10-05 19:11:58 -0700392
Casey Dahlinc378c992015-09-29 16:50:40 -0700393 std::string ToString() const;
Jiyong Park02da7422018-07-16 16:00:26 +0900394 std::string Signature() const;
Casey Dahlinc378c992015-09-29 16:50:40 -0700395
Casey Dahlinbc7a50a2015-09-28 19:20:50 -0700396 private:
Casey Dahlinc378c992015-09-29 16:50:40 -0700397 Direction direction_;
398 bool direction_specified_;
399
Casey Dahlinbc7a50a2015-09-28 19:20:50 -0700400 DISALLOW_COPY_AND_ASSIGN(AidlArgument);
Casey Dahlina834dd42015-09-23 11:52:15 -0700401};
Adam Lesinskiffa16862014-01-23 18:17:42 -0800402
Casey Dahlind40e2fe2015-11-24 14:06:52 -0800403class AidlMethod;
Steven Moreland693640b2018-07-19 13:46:27 -0700404class AidlConstantDeclaration;
Daniel Norman85aed542019-08-21 12:01:14 -0700405class AidlEnumDeclaration;
Casey Dahlind40e2fe2015-11-24 14:06:52 -0800406class AidlMember : public AidlNode {
407 public:
Steven Moreland46e9da82018-07-27 15:45:29 -0700408 AidlMember(const AidlLocation& location);
Casey Dahlind40e2fe2015-11-24 14:06:52 -0800409 virtual ~AidlMember() = default;
410
411 virtual AidlMethod* AsMethod() { return nullptr; }
Steven Moreland693640b2018-07-19 13:46:27 -0700412 virtual AidlConstantDeclaration* AsConstantDeclaration() { return nullptr; }
Casey Dahlind40e2fe2015-11-24 14:06:52 -0800413
414 private:
415 DISALLOW_COPY_AND_ASSIGN(AidlMember);
416};
417
Will McVickerd7d18df2019-09-12 13:40:50 -0700418class AidlUnaryConstExpression;
419class AidlBinaryConstExpression;
420
Steven Moreland693640b2018-07-19 13:46:27 -0700421class AidlConstantValue : public AidlNode {
Casey Dahlind40e2fe2015-11-24 14:06:52 -0800422 public:
Will McVickerd7d18df2019-09-12 13:40:50 -0700423 enum class Type {
424 // WARNING: Don't change this order! The order is used to determine type
425 // promotion during a binary expression.
426 BOOLEAN,
427 INT8,
428 INT32,
429 INT64,
430 ARRAY,
431 CHARACTER,
432 STRING,
433 FLOATING,
434 UNARY,
435 BINARY,
436 ERROR,
437 };
438
439 /*
440 * Return the value casted to the given type.
441 */
442 template <typename T>
443 T cast() const;
Casey Dahlind40e2fe2015-11-24 14:06:52 -0800444
Steven Moreland693640b2018-07-19 13:46:27 -0700445 virtual ~AidlConstantValue() = default;
Casey Dahlind40e2fe2015-11-24 14:06:52 -0800446
Steven Moreland25294322018-08-07 18:13:55 -0700447 static AidlConstantValue* Boolean(const AidlLocation& location, bool value);
448 static AidlConstantValue* Character(const AidlLocation& location, char value);
Steven Moreland25294322018-08-07 18:13:55 -0700449 // example: 123, -5498, maybe any size
Will McVickerd7d18df2019-09-12 13:40:50 -0700450 static AidlConstantValue* Integral(const AidlLocation& location, const string& value);
451 static AidlConstantValue* Floating(const AidlLocation& location, const std::string& value);
Steven Moreland860b1942018-08-16 14:59:28 -0700452 static AidlConstantValue* Array(const AidlLocation& location,
Will McVickerd7d18df2019-09-12 13:40:50 -0700453 std::unique_ptr<vector<unique_ptr<AidlConstantValue>>> values);
Steven Moreland693640b2018-07-19 13:46:27 -0700454 // example: "\"asdf\""
Will McVickerd7d18df2019-09-12 13:40:50 -0700455 static AidlConstantValue* String(const AidlLocation& location, const string& value);
Steven Moreland693640b2018-07-19 13:46:27 -0700456
Daniel Normanf0ca44f2019-10-25 09:59:44 -0700457 // Construct an AidlConstantValue by evaluating the other integral constant's
458 // value string. This does not preserve the structure of the copied constant.
Steven Moreland59e53e42019-11-26 20:38:08 -0800459 // Returns nullptr and logs if value cannot be copied.
Daniel Normanf0ca44f2019-10-25 09:59:44 -0700460 static AidlConstantValue* ShallowIntegralCopy(const AidlConstantValue& other);
Daniel Normanb28684e2019-10-17 15:31:39 -0700461
Will McVickerd7d18df2019-09-12 13:40:50 -0700462 Type GetType() const { return final_type_; }
Steven Moreland25294322018-08-07 18:13:55 -0700463
Will McVickerd7d18df2019-09-12 13:40:50 -0700464 virtual bool CheckValid() const;
Steven Moreland860b1942018-08-16 14:59:28 -0700465
466 // Raw value of type (currently valid in C++ and Java). Empty string on error.
Steven Moreland4bcb05c2019-11-27 18:57:47 -0800467 string ValueString(const AidlTypeSpecifier& type, const ConstantValueDecorator& decorator) const;
Casey Dahlind40e2fe2015-11-24 14:06:52 -0800468
469 private:
Will McVickerd7d18df2019-09-12 13:40:50 -0700470 AidlConstantValue(const AidlLocation& location, Type parsed_type, int64_t parsed_value,
471 const string& checked_value);
472 AidlConstantValue(const AidlLocation& location, Type type, const string& checked_value);
Steven Moreland860b1942018-08-16 14:59:28 -0700473 AidlConstantValue(const AidlLocation& location, Type type,
Will McVickerd7d18df2019-09-12 13:40:50 -0700474 std::unique_ptr<vector<unique_ptr<AidlConstantValue>>> values);
Steven Moreland25294322018-08-07 18:13:55 -0700475 static string ToString(Type type);
Will McVickerd7d18df2019-09-12 13:40:50 -0700476 static bool ParseIntegral(const string& value, int64_t* parsed_value, Type* parsed_type);
477 static bool IsHex(const string& value);
Steven Moreland4bcb05c2019-11-27 18:57:47 -0800478
Will McVickerd7d18df2019-09-12 13:40:50 -0700479 virtual bool evaluate(const AidlTypeSpecifier& type) const;
Casey Dahlind40e2fe2015-11-24 14:06:52 -0800480
Steven Moreland693640b2018-07-19 13:46:27 -0700481 const Type type_ = Type::ERROR;
Will McVickerd7d18df2019-09-12 13:40:50 -0700482 const vector<unique_ptr<AidlConstantValue>> values_; // if type_ == ARRAY
483 const string value_; // otherwise
484
485 // State for tracking evaluation of expressions
Steven Moreland4bcb05c2019-11-27 18:57:47 -0800486 mutable bool is_valid_ = false; // cache of CheckValid, but may be marked false in evaluate
487 mutable bool is_evaluated_ = false; // whether evaluate has been called
Will McVickerd7d18df2019-09-12 13:40:50 -0700488 mutable Type final_type_;
489 mutable int64_t final_value_;
490 mutable string final_string_value_ = "";
Steven Moreland693640b2018-07-19 13:46:27 -0700491
492 DISALLOW_COPY_AND_ASSIGN(AidlConstantValue);
Will McVickerd7d18df2019-09-12 13:40:50 -0700493
494 friend AidlUnaryConstExpression;
495 friend AidlBinaryConstExpression;
496};
497
498class AidlUnaryConstExpression : public AidlConstantValue {
499 public:
500 AidlUnaryConstExpression(const AidlLocation& location, const string& op,
501 std::unique_ptr<AidlConstantValue> rval);
502
503 static bool IsCompatibleType(Type type, const string& op);
504 bool CheckValid() const override;
Will McVickerd7d18df2019-09-12 13:40:50 -0700505 private:
506 bool evaluate(const AidlTypeSpecifier& type) const override;
507
508 std::unique_ptr<AidlConstantValue> unary_;
509 const string op_;
510};
511
512class AidlBinaryConstExpression : public AidlConstantValue {
513 public:
514 AidlBinaryConstExpression(const AidlLocation& location, std::unique_ptr<AidlConstantValue> lval,
515 const string& op, std::unique_ptr<AidlConstantValue> rval);
516
517 bool CheckValid() const override;
Will McVickerd7d18df2019-09-12 13:40:50 -0700518
519 static bool AreCompatibleTypes(Type t1, Type t2);
520 // Returns the promoted kind for both operands
521 static Type UsualArithmeticConversion(Type left, Type right);
522 // Returns the promoted integral type where INT32 is the smallest type
523 static Type IntegralPromotion(Type in);
524
525 private:
526 bool evaluate(const AidlTypeSpecifier& type) const override;
527
528 std::unique_ptr<AidlConstantValue> left_val_;
529 std::unique_ptr<AidlConstantValue> right_val_;
530 const string op_;
Christopher Wileyd6bdd8d2016-05-03 11:23:13 -0700531};
532
Andrei Onea9445fc62019-06-27 18:11:59 +0100533struct AidlAnnotationParameter {
534 std::string name;
535 std::unique_ptr<AidlConstantValue> value;
536};
537
Steven Moreland693640b2018-07-19 13:46:27 -0700538class AidlConstantDeclaration : public AidlMember {
Christopher Wileyd6bdd8d2016-05-03 11:23:13 -0700539 public:
Steven Moreland46e9da82018-07-27 15:45:29 -0700540 AidlConstantDeclaration(const AidlLocation& location, AidlTypeSpecifier* specifier,
Will McVickerd7d18df2019-09-12 13:40:50 -0700541 const string& name, AidlConstantValue* value);
Steven Moreland693640b2018-07-19 13:46:27 -0700542 virtual ~AidlConstantDeclaration() = default;
Christopher Wileyd6bdd8d2016-05-03 11:23:13 -0700543
Steven Moreland693640b2018-07-19 13:46:27 -0700544 const AidlTypeSpecifier& GetType() const { return *type_; }
Steven Moreland4d12f9a2018-10-31 14:30:55 -0700545 AidlTypeSpecifier* GetMutableType() { return type_.get(); }
Will McVickerd7d18df2019-09-12 13:40:50 -0700546 const string& GetName() const { return name_; }
Steven Moreland693640b2018-07-19 13:46:27 -0700547 const AidlConstantValue& GetValue() const { return *value_; }
Jeongik Chadb0f59e2018-11-01 18:11:21 +0900548 bool CheckValid(const AidlTypenames& typenames) const;
Christopher Wileyd6bdd8d2016-05-03 11:23:13 -0700549
Will McVickerd7d18df2019-09-12 13:40:50 -0700550 string ToString() const;
551 string Signature() const;
Steven Moreland860b1942018-08-16 14:59:28 -0700552 string ValueString(const ConstantValueDecorator& decorator) const {
Will McVickerd7d18df2019-09-12 13:40:50 -0700553 return value_->ValueString(GetType(), decorator);
Steven Moreland860b1942018-08-16 14:59:28 -0700554 }
Steven Moreland25294322018-08-07 18:13:55 -0700555
Steven Moreland693640b2018-07-19 13:46:27 -0700556 AidlConstantDeclaration* AsConstantDeclaration() override { return this; }
Christopher Wileyd6bdd8d2016-05-03 11:23:13 -0700557
558 private:
Steven Moreland693640b2018-07-19 13:46:27 -0700559 const unique_ptr<AidlTypeSpecifier> type_;
Will McVickerd7d18df2019-09-12 13:40:50 -0700560 const string name_;
561 unique_ptr<AidlConstantValue> value_;
Christopher Wileyd6bdd8d2016-05-03 11:23:13 -0700562
Steven Moreland693640b2018-07-19 13:46:27 -0700563 DISALLOW_COPY_AND_ASSIGN(AidlConstantDeclaration);
Casey Dahlind40e2fe2015-11-24 14:06:52 -0800564};
565
566class AidlMethod : public AidlMember {
Casey Dahlin5c69deb2015-10-01 14:44:12 -0700567 public:
Will McVickerd7d18df2019-09-12 13:40:50 -0700568 AidlMethod(const AidlLocation& location, bool oneway, AidlTypeSpecifier* type, const string& name,
569 vector<unique_ptr<AidlArgument>>* args, const string& comments);
570 AidlMethod(const AidlLocation& location, bool oneway, AidlTypeSpecifier* type, const string& name,
571 vector<unique_ptr<AidlArgument>>* args, const string& comments, int id,
572 bool is_user_defined = true);
Casey Dahlin5c69deb2015-10-01 14:44:12 -0700573 virtual ~AidlMethod() = default;
574
Casey Dahlind40e2fe2015-11-24 14:06:52 -0800575 AidlMethod* AsMethod() override { return this; }
Jeongik Cha997281d2020-01-16 15:23:59 +0900576 bool IsHidden() const;
Will McVickerd7d18df2019-09-12 13:40:50 -0700577 const string& GetComments() const { return comments_; }
Jiyong Parkd59a10d2018-07-18 11:12:55 +0900578 const AidlTypeSpecifier& GetType() const { return *type_; }
579 AidlTypeSpecifier* GetMutableType() { return type_.get(); }
Steven Morelandacd53472018-12-14 10:17:26 -0800580
Steven Moreland8c70ba92018-12-17 10:20:31 -0800581 // set if this method is part of an interface that is marked oneway
582 void ApplyInterfaceOneway(bool oneway) { oneway_ = oneway_ || oneway; }
Casey Dahlinf4a93112015-10-05 16:58:09 -0700583 bool IsOneway() const { return oneway_; }
Steven Morelandacd53472018-12-14 10:17:26 -0800584
Casey Dahlinf4a93112015-10-05 16:58:09 -0700585 const std::string& GetName() const { return name_; }
Casey Dahlinf4a93112015-10-05 16:58:09 -0700586 bool HasId() const { return has_id_; }
Jiyong Parked65bf42018-08-28 15:43:27 +0900587 int GetId() const { return id_; }
Casey Dahlinf4a93112015-10-05 16:58:09 -0700588 void SetId(unsigned id) { id_ = id; }
Casey Dahlinf2d23f72015-10-02 16:19:19 -0700589
Jiyong Park309668e2018-07-28 16:55:44 +0900590 bool IsUserDefined() const { return is_user_defined_; }
591
Casey Dahlinf4a93112015-10-05 16:58:09 -0700592 const std::vector<std::unique_ptr<AidlArgument>>& GetArguments() const {
Christopher Wileyad339272015-10-05 19:11:58 -0700593 return arguments_;
594 }
595 // An inout parameter will appear in both GetInArguments()
596 // and GetOutArguments(). AidlMethod retains ownership of the argument
597 // pointers returned in this way.
598 const std::vector<const AidlArgument*>& GetInArguments() const {
599 return in_arguments_;
600 }
601 const std::vector<const AidlArgument*>& GetOutArguments() const {
602 return out_arguments_;
Casey Dahlinf4a93112015-10-05 16:58:09 -0700603 }
Casey Dahlin5c69deb2015-10-01 14:44:12 -0700604
Jiyong Park309668e2018-07-28 16:55:44 +0900605 // name + type parameter types
606 // i.e, foo(int, String)
Jiyong Park02da7422018-07-16 16:00:26 +0900607 std::string Signature() const;
608
Jiyong Park309668e2018-07-28 16:55:44 +0900609 // return type + name + type parameter types + annotations
610 // i.e, boolean foo(int, @Nullable String)
611 std::string ToString() const;
612
Casey Dahlin5c69deb2015-10-01 14:44:12 -0700613 private:
Casey Dahlinf4a93112015-10-05 16:58:09 -0700614 bool oneway_;
Casey Dahlinf2d23f72015-10-02 16:19:19 -0700615 std::string comments_;
Jiyong Parkd59a10d2018-07-18 11:12:55 +0900616 std::unique_ptr<AidlTypeSpecifier> type_;
Casey Dahlinf4a93112015-10-05 16:58:09 -0700617 std::string name_;
Christopher Wileyad339272015-10-05 19:11:58 -0700618 const std::vector<std::unique_ptr<AidlArgument>> arguments_;
619 std::vector<const AidlArgument*> in_arguments_;
620 std::vector<const AidlArgument*> out_arguments_;
Casey Dahlinf4a93112015-10-05 16:58:09 -0700621 bool has_id_;
622 int id_;
Jiyong Park309668e2018-07-28 16:55:44 +0900623 bool is_user_defined_ = true;
Casey Dahlinf2d23f72015-10-02 16:19:19 -0700624
Casey Dahlin5c69deb2015-10-01 14:44:12 -0700625 DISALLOW_COPY_AND_ASSIGN(AidlMethod);
Casey Dahlin0a2f8be2015-09-28 16:15:29 -0700626};
Adam Lesinskiffa16862014-01-23 18:17:42 -0800627
Steven Morelandc258abc2018-07-10 14:03:38 -0700628class AidlDefinedType;
Jiyong Parkb034bf02018-07-30 17:44:33 +0900629class AidlInterface;
630class AidlParcelable;
631class AidlStructuredParcelable;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800632
Casey Dahlin2b2879b2015-10-13 16:59:44 -0700633class AidlQualifiedName : public AidlNode {
634 public:
Steven Moreland46e9da82018-07-27 15:45:29 -0700635 AidlQualifiedName(const AidlLocation& location, const std::string& term,
636 const std::string& comments);
Casey Dahlin2b2879b2015-10-13 16:59:44 -0700637 virtual ~AidlQualifiedName() = default;
638
639 const std::vector<std::string>& GetTerms() const { return terms_; }
640 const std::string& GetComments() const { return comments_; }
641 std::string GetDotName() const { return android::base::Join(terms_, '.'); }
Ningyuan Wangd17c58b2016-09-29 14:33:14 -0700642 std::string GetColonName() const { return android::base::Join(terms_, "::"); }
Casey Dahlin2b2879b2015-10-13 16:59:44 -0700643
Chih-Hung Hsiehf05cc262016-07-27 11:42:51 -0700644 void AddTerm(const std::string& term);
Casey Dahlin2b2879b2015-10-13 16:59:44 -0700645
646 private:
647 std::vector<std::string> terms_;
648 std::string comments_;
649
650 DISALLOW_COPY_AND_ASSIGN(AidlQualifiedName);
651};
652
Steven Moreland46e9da82018-07-27 15:45:29 -0700653class AidlInterface;
654class AidlParcelable;
655class AidlStructuredParcelable;
Daniel Norman85aed542019-08-21 12:01:14 -0700656// AidlDefinedType represents either an interface, parcelable, or enum that is
Jiyong Park1deecc32018-07-17 01:14:41 +0900657// defined in the source file.
658class AidlDefinedType : public AidlAnnotatable {
Steven Moreland787b0432018-07-03 09:00:58 -0700659 public:
Steven Moreland46e9da82018-07-27 15:45:29 -0700660 AidlDefinedType(const AidlLocation& location, const std::string& name,
661 const std::string& comments, const std::vector<std::string>& package);
Steven Moreland787b0432018-07-03 09:00:58 -0700662 virtual ~AidlDefinedType() = default;
663
Jiyong Park1deecc32018-07-17 01:14:41 +0900664 const std::string& GetName() const { return name_; };
Jeongik Cha997281d2020-01-16 15:23:59 +0900665 bool IsHidden() const;
Jiyong Park1deecc32018-07-17 01:14:41 +0900666 const std::string& GetComments() const { return comments_; }
Jiyong Parka6605ab2018-11-11 14:30:21 +0900667 void SetComments(const std::string comments) { comments_ = comments; }
Jiyong Park1deecc32018-07-17 01:14:41 +0900668
Steven Moreland787b0432018-07-03 09:00:58 -0700669 /* dot joined package, example: "android.package.foo" */
670 std::string GetPackage() const;
671 /* dot joined package and name, example: "android.package.foo.IBar" */
672 std::string GetCanonicalName() const;
673 const std::vector<std::string>& GetSplitPackage() const { return package_; }
674
Steven Morelanded83a282018-07-17 13:27:29 -0700675 virtual std::string GetPreprocessDeclarationName() const = 0;
Steven Morelandc258abc2018-07-10 14:03:38 -0700676
Steven Moreland5557f1c2018-07-02 13:50:23 -0700677 virtual const AidlStructuredParcelable* AsStructuredParcelable() const { return nullptr; }
Steven Morelandc258abc2018-07-10 14:03:38 -0700678 virtual const AidlParcelable* AsParcelable() const { return nullptr; }
Daniel Norman85aed542019-08-21 12:01:14 -0700679 virtual const AidlEnumDeclaration* AsEnumDeclaration() const { return nullptr; }
Steven Moreland5557f1c2018-07-02 13:50:23 -0700680 virtual const AidlInterface* AsInterface() const { return nullptr; }
Jeongik Chadf76dc72019-11-28 00:08:47 +0900681 virtual const AidlParameterizable<std::string>* AsParameterizable() const { return nullptr; }
Devin Moore24f68572020-02-26 13:20:59 -0800682 bool CheckValid(const AidlTypenames& typenames) const override;
Jeongik Cha047c5ee2019-08-07 23:16:49 +0900683 virtual bool LanguageSpecificCheckValid(Options::Language lang) const = 0;
Steven Morelandc258abc2018-07-10 14:03:38 -0700684 AidlStructuredParcelable* AsStructuredParcelable() {
685 return const_cast<AidlStructuredParcelable*>(
686 const_cast<const AidlDefinedType*>(this)->AsStructuredParcelable());
687 }
688 AidlParcelable* AsParcelable() {
689 return const_cast<AidlParcelable*>(const_cast<const AidlDefinedType*>(this)->AsParcelable());
690 }
Daniel Norman85aed542019-08-21 12:01:14 -0700691 AidlEnumDeclaration* AsEnumDeclaration() {
692 return const_cast<AidlEnumDeclaration*>(
693 const_cast<const AidlDefinedType*>(this)->AsEnumDeclaration());
694 }
Steven Morelandc258abc2018-07-10 14:03:38 -0700695 AidlInterface* AsInterface() {
696 return const_cast<AidlInterface*>(const_cast<const AidlDefinedType*>(this)->AsInterface());
697 }
698
Jeongik Chadf76dc72019-11-28 00:08:47 +0900699 AidlParameterizable<std::string>* AsParameterizable() {
700 return const_cast<AidlParameterizable<std::string>*>(
701 const_cast<const AidlDefinedType*>(this)->AsParameterizable());
702 }
703
Steven Moreland6cee3482018-07-18 14:39:58 -0700704 const AidlParcelable* AsUnstructuredParcelable() const {
705 if (this->AsStructuredParcelable() != nullptr) return nullptr;
706 return this->AsParcelable();
707 }
708 AidlParcelable* AsUnstructuredParcelable() {
709 return const_cast<AidlParcelable*>(
710 const_cast<const AidlDefinedType*>(this)->AsUnstructuredParcelable());
711 }
712
Jeongik Cha997281d2020-01-16 15:23:59 +0900713 virtual void Dump(CodeWriter* writer) const = 0;
Steven Morelanda5d9c5c2020-02-21 16:01:09 -0800714 void DumpHeader(CodeWriter* writer) const;
Jiyong Park02da7422018-07-16 16:00:26 +0900715
Steven Moreland787b0432018-07-03 09:00:58 -0700716 private:
Jiyong Park1deecc32018-07-17 01:14:41 +0900717 std::string name_;
Jiyong Park1deecc32018-07-17 01:14:41 +0900718 std::string comments_;
Steven Moreland787b0432018-07-03 09:00:58 -0700719 const std::vector<std::string> package_;
Steven Moreland5557f1c2018-07-02 13:50:23 -0700720
721 DISALLOW_COPY_AND_ASSIGN(AidlDefinedType);
Steven Moreland787b0432018-07-03 09:00:58 -0700722};
723
Jeongik Chadf76dc72019-11-28 00:08:47 +0900724class AidlParcelable : public AidlDefinedType, public AidlParameterizable<std::string> {
Casey Dahlin1ae2bc52015-10-07 18:49:10 -0700725 public:
Steven Moreland46e9da82018-07-27 15:45:29 -0700726 AidlParcelable(const AidlLocation& location, AidlQualifiedName* name,
Jiyong Parka6605ab2018-11-11 14:30:21 +0900727 const std::vector<std::string>& package, const std::string& comments,
Jeongik Chadf76dc72019-11-28 00:08:47 +0900728 const std::string& cpp_header = "",
729 std::vector<std::string>* type_params = nullptr);
Casey Dahlin1ae2bc52015-10-07 18:49:10 -0700730 virtual ~AidlParcelable() = default;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800731
Ningyuan Wangd17c58b2016-09-29 14:33:14 -0700732 // C++ uses "::" instead of "." to refer to a inner class.
733 std::string GetCppName() const { return name_->GetColonName(); }
Christopher Wiley8aa4d9f2015-11-16 19:10:45 -0800734 std::string GetCppHeader() const { return cpp_header_; }
Christopher Wiley8aa4d9f2015-11-16 19:10:45 -0800735
Devin Moore24f68572020-02-26 13:20:59 -0800736 std::set<string> GetSupportedAnnotations() const override;
Jeongik Cha82317dd2019-02-27 20:26:11 +0900737 bool CheckValid(const AidlTypenames& typenames) const override;
Jeongik Cha047c5ee2019-08-07 23:16:49 +0900738 bool LanguageSpecificCheckValid(Options::Language lang) const override;
Steven Morelandc258abc2018-07-10 14:03:38 -0700739 const AidlParcelable* AsParcelable() const override { return this; }
Jeongik Chadf76dc72019-11-28 00:08:47 +0900740 const AidlParameterizable<std::string>* AsParameterizable() const override { return this; }
741 const AidlNode& AsAidlNode() const override { return *this; }
Steven Morelanded83a282018-07-17 13:27:29 -0700742 std::string GetPreprocessDeclarationName() const override { return "parcelable"; }
Steven Morelandc258abc2018-07-10 14:03:38 -0700743
Jeongik Cha997281d2020-01-16 15:23:59 +0900744 void Dump(CodeWriter* writer) const override;
Jiyong Park02da7422018-07-16 16:00:26 +0900745
Casey Dahlin1ae2bc52015-10-07 18:49:10 -0700746 private:
Christopher Wiley8aa4d9f2015-11-16 19:10:45 -0800747 std::unique_ptr<AidlQualifiedName> name_;
Christopher Wiley8aa4d9f2015-11-16 19:10:45 -0800748 std::string cpp_header_;
Casey Dahlin59401da2015-10-09 18:16:45 -0700749
Casey Dahlin1ae2bc52015-10-07 18:49:10 -0700750 DISALLOW_COPY_AND_ASSIGN(AidlParcelable);
Casey Dahlin0a2f8be2015-09-28 16:15:29 -0700751};
Adam Lesinskiffa16862014-01-23 18:17:42 -0800752
Steven Moreland5557f1c2018-07-02 13:50:23 -0700753class AidlStructuredParcelable : public AidlParcelable {
754 public:
Steven Moreland46e9da82018-07-27 15:45:29 -0700755 AidlStructuredParcelable(const AidlLocation& location, AidlQualifiedName* name,
Jiyong Parka6605ab2018-11-11 14:30:21 +0900756 const std::vector<std::string>& package, const std::string& comments,
Steven Moreland5557f1c2018-07-02 13:50:23 -0700757 std::vector<std::unique_ptr<AidlVariableDeclaration>>* variables);
758
759 const std::vector<std::unique_ptr<AidlVariableDeclaration>>& GetFields() const {
760 return variables_;
761 }
762
Steven Morelandc258abc2018-07-10 14:03:38 -0700763 const AidlStructuredParcelable* AsStructuredParcelable() const override { return this; }
Steven Morelanded83a282018-07-17 13:27:29 -0700764 std::string GetPreprocessDeclarationName() const override { return "structured_parcelable"; }
Steven Moreland5557f1c2018-07-02 13:50:23 -0700765
Jeongik Cha997281d2020-01-16 15:23:59 +0900766 void Dump(CodeWriter* writer) const override;
Jiyong Park02da7422018-07-16 16:00:26 +0900767
Devin Moore24f68572020-02-26 13:20:59 -0800768 std::set<string> GetSupportedAnnotations() const override;
Jeongik Chadb0f59e2018-11-01 18:11:21 +0900769 bool CheckValid(const AidlTypenames& typenames) const override;
Jeongik Cha047c5ee2019-08-07 23:16:49 +0900770 bool LanguageSpecificCheckValid(Options::Language lang) const override;
Jeongik Chadb0f59e2018-11-01 18:11:21 +0900771
Steven Moreland5557f1c2018-07-02 13:50:23 -0700772 private:
773 const std::vector<std::unique_ptr<AidlVariableDeclaration>> variables_;
774
775 DISALLOW_COPY_AND_ASSIGN(AidlStructuredParcelable);
776};
777
Daniel Norman85aed542019-08-21 12:01:14 -0700778class AidlEnumerator : public AidlNode {
779 public:
Daniel Norman2e4112d2019-10-03 10:22:35 -0700780 AidlEnumerator(const AidlLocation& location, const std::string& name, AidlConstantValue* value,
781 const std::string& comments);
Daniel Norman85aed542019-08-21 12:01:14 -0700782 virtual ~AidlEnumerator() = default;
783
784 const std::string& GetName() const { return name_; }
Will McVickerd7d18df2019-09-12 13:40:50 -0700785 AidlConstantValue* GetValue() const { return value_.get(); }
Daniel Norman2e4112d2019-10-03 10:22:35 -0700786 const std::string& GetComments() const { return comments_; }
Daniel Norman85aed542019-08-21 12:01:14 -0700787 bool CheckValid(const AidlTypeSpecifier& enum_backing_type) const;
788
789 string ValueString(const AidlTypeSpecifier& backing_type,
790 const ConstantValueDecorator& decorator) const;
791
Daniel Normanb28684e2019-10-17 15:31:39 -0700792 void SetValue(std::unique_ptr<AidlConstantValue> value) { value_ = std::move(value); }
793
Daniel Norman85aed542019-08-21 12:01:14 -0700794 private:
795 const std::string name_;
Will McVickerd7d18df2019-09-12 13:40:50 -0700796 unique_ptr<AidlConstantValue> value_;
Daniel Norman2e4112d2019-10-03 10:22:35 -0700797 const std::string comments_;
Daniel Norman85aed542019-08-21 12:01:14 -0700798
799 DISALLOW_COPY_AND_ASSIGN(AidlEnumerator);
800};
801
802class AidlEnumDeclaration : public AidlDefinedType {
803 public:
Will McVickerd7d18df2019-09-12 13:40:50 -0700804 AidlEnumDeclaration(const AidlLocation& location, const string& name,
Daniel Norman85aed542019-08-21 12:01:14 -0700805 std::vector<std::unique_ptr<AidlEnumerator>>* enumerators,
Daniel Norman2e4112d2019-10-03 10:22:35 -0700806 const std::vector<std::string>& package, const std::string& comments);
Daniel Norman85aed542019-08-21 12:01:14 -0700807 virtual ~AidlEnumDeclaration() = default;
808
809 void SetBackingType(std::unique_ptr<const AidlTypeSpecifier> type);
810 const AidlTypeSpecifier& GetBackingType() const { return *backing_type_; }
811 const std::vector<std::unique_ptr<AidlEnumerator>>& GetEnumerators() const {
812 return enumerators_;
813 }
Steven Moreland59e53e42019-11-26 20:38:08 -0800814 bool Autofill();
Devin Moore24f68572020-02-26 13:20:59 -0800815 std::set<string> GetSupportedAnnotations() const override;
Daniel Norman85aed542019-08-21 12:01:14 -0700816 bool CheckValid(const AidlTypenames& typenames) const override;
817 bool LanguageSpecificCheckValid(Options::Language) const override { return true; }
818 std::string GetPreprocessDeclarationName() const override { return "enum"; }
Jeongik Cha997281d2020-01-16 15:23:59 +0900819 void Dump(CodeWriter* writer) const override;
Daniel Norman85aed542019-08-21 12:01:14 -0700820
821 const AidlEnumDeclaration* AsEnumDeclaration() const override { return this; }
822
823 private:
824 const std::string name_;
825 const std::vector<std::unique_ptr<AidlEnumerator>> enumerators_;
826 std::unique_ptr<const AidlTypeSpecifier> backing_type_;
827
828 DISALLOW_COPY_AND_ASSIGN(AidlEnumDeclaration);
829};
830
Jiyong Park1deecc32018-07-17 01:14:41 +0900831class AidlInterface final : public AidlDefinedType {
Casey Dahlin1ae2bc52015-10-07 18:49:10 -0700832 public:
Steven Moreland46e9da82018-07-27 15:45:29 -0700833 AidlInterface(const AidlLocation& location, const std::string& name, const std::string& comments,
834 bool oneway_, std::vector<std::unique_ptr<AidlMember>>* members,
Christopher Wileyd76067c2015-10-19 17:00:13 -0700835 const std::vector<std::string>& package);
Casey Dahlin1ae2bc52015-10-07 18:49:10 -0700836 virtual ~AidlInterface() = default;
837
Casey Dahlinfb7da2e2015-10-08 17:26:09 -0700838 const std::vector<std::unique_ptr<AidlMethod>>& GetMethods() const
839 { return methods_; }
Jiyong Park309668e2018-07-28 16:55:44 +0900840 std::vector<std::unique_ptr<AidlMethod>>& GetMutableMethods() { return methods_; }
Steven Moreland693640b2018-07-19 13:46:27 -0700841 const std::vector<std::unique_ptr<AidlConstantDeclaration>>& GetConstantDeclarations() const {
842 return constants_;
843 }
Casey Dahlina2f77c42015-12-01 18:26:02 -0800844
Steven Morelandc258abc2018-07-10 14:03:38 -0700845 const AidlInterface* AsInterface() const override { return this; }
Steven Morelanded83a282018-07-17 13:27:29 -0700846 std::string GetPreprocessDeclarationName() const override { return "interface"; }
Steven Moreland5557f1c2018-07-02 13:50:23 -0700847
Jeongik Cha997281d2020-01-16 15:23:59 +0900848 void Dump(CodeWriter* writer) const override;
Jiyong Park02da7422018-07-16 16:00:26 +0900849
Devin Moore24f68572020-02-26 13:20:59 -0800850 std::set<string> GetSupportedAnnotations() const override;
Jeongik Chadb0f59e2018-11-01 18:11:21 +0900851 bool CheckValid(const AidlTypenames& typenames) const override;
Jeongik Cha047c5ee2019-08-07 23:16:49 +0900852 bool LanguageSpecificCheckValid(Options::Language lang) const override;
Jeongik Chadb0f59e2018-11-01 18:11:21 +0900853
Casey Dahlin1ae2bc52015-10-07 18:49:10 -0700854 private:
Casey Dahlinfb7da2e2015-10-08 17:26:09 -0700855 std::vector<std::unique_ptr<AidlMethod>> methods_;
Steven Moreland693640b2018-07-19 13:46:27 -0700856 std::vector<std::unique_ptr<AidlConstantDeclaration>> constants_;
Casey Dahlinfb7da2e2015-10-08 17:26:09 -0700857
Casey Dahlin1ae2bc52015-10-07 18:49:10 -0700858 DISALLOW_COPY_AND_ASSIGN(AidlInterface);
Casey Dahlin0a2f8be2015-09-28 16:15:29 -0700859};
Adam Lesinskiffa16862014-01-23 18:17:42 -0800860
Casey Dahlin0edf3422015-10-07 12:34:59 -0700861class AidlImport : public AidlNode {
862 public:
Steven Moreland46e9da82018-07-27 15:45:29 -0700863 AidlImport(const AidlLocation& location, const std::string& needed_class);
Casey Dahlin0edf3422015-10-07 12:34:59 -0700864 virtual ~AidlImport() = default;
865
Casey Dahlin0edf3422015-10-07 12:34:59 -0700866 const std::string& GetFilename() const { return filename_; }
867 const std::string& GetNeededClass() const { return needed_class_; }
Casey Dahlin0edf3422015-10-07 12:34:59 -0700868
869 private:
Casey Dahlin0edf3422015-10-07 12:34:59 -0700870 std::string filename_;
871 std::string needed_class_;
Casey Dahlin0edf3422015-10-07 12:34:59 -0700872
873 DISALLOW_COPY_AND_ASSIGN(AidlImport);
Casey Dahline2507492015-09-14 17:11:20 -0700874};
875
876class Parser {
Casey Dahlindd691812015-09-09 17:59:06 -0700877 public:
Casey Dahline2507492015-09-14 17:11:20 -0700878 ~Parser();
Casey Dahlindd691812015-09-09 17:59:06 -0700879
Steven Moreland64e29be2018-08-08 18:52:19 -0700880 // Parse contents of file |filename|. Should only be called once.
881 static std::unique_ptr<Parser> Parse(const std::string& filename,
882 const android::aidl::IoDelegate& io_delegate,
883 AidlTypenames& typenames);
Casey Dahlin89d44842015-09-24 18:45:54 -0700884
Steven Moreland2ca4fcb2018-06-27 16:01:01 -0700885 void AddError() { error_++; }
Steven Moreland64e29be2018-08-08 18:52:19 -0700886 bool HasError() { return error_ != 0; }
Casey Dahlindd691812015-09-09 17:59:06 -0700887
Casey Dahlin3c6df362015-10-06 15:48:35 -0700888 const std::string& FileName() const { return filename_; }
Casey Dahlin42727f82015-10-12 19:23:40 -0700889 void* Scanner() const { return scanner_; }
Casey Dahlindd691812015-09-09 17:59:06 -0700890
Steven Morelandd1039a92020-01-23 09:49:43 -0800891 void AddImport(std::unique_ptr<AidlImport>&& import);
Christopher Wileyd76067c2015-10-19 17:00:13 -0700892 const std::vector<std::unique_ptr<AidlImport>>& GetImports() {
893 return imports_;
894 }
Casey Dahlindd691812015-09-09 17:59:06 -0700895
Jiyong Parkb034bf02018-07-30 17:44:33 +0900896 void SetPackage(unique_ptr<AidlQualifiedName> name) { package_ = std::move(name); }
897 std::vector<std::string> Package() const;
Jiyong Park1deecc32018-07-17 01:14:41 +0900898
899 void DeferResolution(AidlTypeSpecifier* typespec) {
900 unresolved_typespecs_.emplace_back(typespec);
901 }
902
Jiyong Parke59c3682018-09-11 23:10:25 +0900903 const vector<AidlTypeSpecifier*>& GetUnresolvedTypespecs() const { return unresolved_typespecs_; }
904
Jiyong Park1deecc32018-07-17 01:14:41 +0900905 bool Resolve();
906
Jiyong Parkb034bf02018-07-30 17:44:33 +0900907 void AddDefinedType(unique_ptr<AidlDefinedType> type) {
908 // Parser does NOT own AidlDefinedType, it just has references to the types
909 // that it encountered while parsing the input file.
910 defined_types_.emplace_back(type.get());
911
912 // AidlDefinedType IS owned by AidlTypenames
913 if (!typenames_.AddDefinedType(std::move(type))) {
914 AddError();
915 }
916 }
917
918 vector<AidlDefinedType*>& GetDefinedTypes() { return defined_types_; }
919
Casey Dahlindd691812015-09-09 17:59:06 -0700920 private:
Steven Moreland64e29be2018-08-08 18:52:19 -0700921 explicit Parser(const std::string& filename, std::string& raw_buffer,
922 android::aidl::AidlTypenames& typenames);
923
Casey Dahlindd691812015-09-09 17:59:06 -0700924 std::string filename_;
Christopher Wileyd76067c2015-10-19 17:00:13 -0700925 std::unique_ptr<AidlQualifiedName> package_;
Jiyong Parkb034bf02018-07-30 17:44:33 +0900926 AidlTypenames& typenames_;
Steven Moreland64e29be2018-08-08 18:52:19 -0700927
928 void* scanner_ = nullptr;
929 YY_BUFFER_STATE buffer_;
930 int error_ = 0;
931
932 std::vector<std::unique_ptr<AidlImport>> imports_;
Jiyong Parkb034bf02018-07-30 17:44:33 +0900933 vector<AidlDefinedType*> defined_types_;
Jiyong Park1deecc32018-07-17 01:14:41 +0900934 vector<AidlTypeSpecifier*> unresolved_typespecs_;
Casey Dahlindd691812015-09-09 17:59:06 -0700935
Casey Dahline2507492015-09-14 17:11:20 -0700936 DISALLOW_COPY_AND_ASSIGN(Parser);
Casey Dahlindd691812015-09-09 17:59:06 -0700937};