blob: 86640e917b2e41278f00eeb3299f08de604e163b [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);
85
86 bool IsInternal() const { return source_ == Source::INTERNAL; }
Steven Moreland46e9da82018-07-27 15:45:29 -070087
Steven Moreland46e9da82018-07-27 15:45:29 -070088 friend std::ostream& operator<<(std::ostream& os, const AidlLocation& l);
Andrei Onea8714b022019-02-01 18:55:54 +000089 friend class AidlNode;
Casey Dahlinbc7a50a2015-09-28 19:20:50 -070090
91 private:
Steven Moreland46e9da82018-07-27 15:45:29 -070092 const std::string file_;
93 Point begin_;
94 Point end_;
Devin Mooredf93ebb2020-03-25 14:03:35 -070095 Source source_;
Steven Moreland46e9da82018-07-27 15:45:29 -070096};
97
Devin Mooredf93ebb2020-03-25 14:03:35 -070098#define AIDL_LOCATION_HERE \
99 AidlLocation { __FILE__, {__LINE__, 0}, {__LINE__, 0}, AidlLocation::Source::INTERNAL }
Steven Moreland02e012e2018-08-02 14:58:10 -0700100
Steven Moreland46e9da82018-07-27 15:45:29 -0700101std::ostream& operator<<(std::ostream& os, const AidlLocation& l);
102
103// Anything that is locatable in a .aidl file.
104class AidlNode {
105 public:
106 AidlNode(const AidlLocation& location);
Steven Moreland3f658cf2018-08-20 13:40:54 -0700107
108 AidlNode(const AidlNode&) = default;
Steven Moreland3be75772018-08-20 13:27:43 -0700109 AidlNode(AidlNode&&) = default;
Steven Moreland46e9da82018-07-27 15:45:29 -0700110 virtual ~AidlNode() = default;
111
Devin Mooredf93ebb2020-03-25 14:03:35 -0700112 // To be able to print AidlLocation
Steven Morelandb0d15a52020-03-31 14:03:47 -0700113 friend class AidlErrorLog;
Andrei Onea8714b022019-02-01 18:55:54 +0000114 friend std::string android::aidl::mappings::dump_location(const AidlNode&);
Mathew Inwoodadb74672019-11-29 14:01:53 +0000115 friend std::string android::aidl::java::dump_location(const AidlNode&);
Steven Moreland46e9da82018-07-27 15:45:29 -0700116
Devin Mooredf93ebb2020-03-25 14:03:35 -0700117 protected:
118 // This should only be used to construct implicit nodes related to existing nodes
119 const AidlLocation& GetLocation() const { return location_; }
120
Steven Moreland46e9da82018-07-27 15:45:29 -0700121 private:
Mathew Inwoodadb74672019-11-29 14:01:53 +0000122 std::string PrintLine() const;
Andrei Onea8714b022019-02-01 18:55:54 +0000123 std::string PrintLocation() const;
Steven Moreland46e9da82018-07-27 15:45:29 -0700124 const AidlLocation location_;
Casey Dahlinbc7a50a2015-09-28 19:20:50 -0700125};
126
Steven Moreland46e9da82018-07-27 15:45:29 -0700127// Generic point for printing any error in the AIDL compiler.
Steven Morelandb0d15a52020-03-31 14:03:47 -0700128class AidlErrorLog {
Steven Moreland46e9da82018-07-27 15:45:29 -0700129 public:
Steven Morelandb0d15a52020-03-31 14:03:47 -0700130 AidlErrorLog(bool fatal, const std::string& filename) : AidlErrorLog(fatal) {
131 os_ << filename << ": ";
132 }
133 AidlErrorLog(bool fatal, const AidlLocation& location);
134 AidlErrorLog(bool fatal, const AidlNode& node) : AidlErrorLog(fatal, node.location_) {}
135 AidlErrorLog(bool fatal, const AidlNode* node) : AidlErrorLog(fatal, *node) {}
Steven Moreland92c55f12018-07-31 14:08:37 -0700136
137 template <typename T>
Steven Morelandb0d15a52020-03-31 14:03:47 -0700138 AidlErrorLog(bool fatal, const std::unique_ptr<T>& node) : AidlErrorLog(fatal, *node) {}
139 ~AidlErrorLog() {
Steven Moreland46e9da82018-07-27 15:45:29 -0700140 os_ << std::endl;
141 if (fatal_) abort();
142 }
143
144 std::ostream& os_;
145
Steven Morelandfdb57cd2020-01-08 20:03:30 -0800146 static bool hadError() { return sHadError; }
147
Steven Moreland46e9da82018-07-27 15:45:29 -0700148 private:
Steven Morelandb0d15a52020-03-31 14:03:47 -0700149 AidlErrorLog(bool fatal);
Steven Moreland46e9da82018-07-27 15:45:29 -0700150
151 bool fatal_;
152
Steven Morelandfdb57cd2020-01-08 20:03:30 -0800153 static bool sHadError;
154
Steven Morelandb0d15a52020-03-31 14:03:47 -0700155 DISALLOW_COPY_AND_ASSIGN(AidlErrorLog);
Steven Moreland46e9da82018-07-27 15:45:29 -0700156};
157
Steven Morelandb0d15a52020-03-31 14:03:47 -0700158#define AIDL_ERROR(CONTEXT) ::AidlErrorLog(false /*fatal*/, (CONTEXT)).os_
159#define AIDL_FATAL(CONTEXT) ::AidlErrorLog(true /*fatal*/, (CONTEXT)).os_
Steven Moreland3f658cf2018-08-20 13:40:54 -0700160#define AIDL_FATAL_IF(CONDITION, CONTEXT) \
161 if (CONDITION) AIDL_FATAL(CONTEXT) << "Bad internal state: " << #CONDITION << ": "
Steven Moreland46e9da82018-07-27 15:45:29 -0700162
Casey Dahlina2f77c42015-12-01 18:26:02 -0800163namespace android {
164namespace aidl {
165
Jiyong Park1deecc32018-07-17 01:14:41 +0900166class AidlTypenames;
Casey Dahlina2f77c42015-12-01 18:26:02 -0800167
168} // namespace aidl
169} // namespace android
170
Jeongik Chadf76dc72019-11-28 00:08:47 +0900171// unique_ptr<AidlTypeSpecifier> for type arugment,
172// std::string for type parameter(T, U, and so on).
173template <typename T>
174class AidlParameterizable {
175 public:
176 AidlParameterizable(std::vector<T>* type_params) : type_params_(type_params) {}
177 virtual ~AidlParameterizable() = default;
178 bool IsGeneric() const { return type_params_ != nullptr; }
179 const std::vector<T>& GetTypeParameters() const { return *type_params_; }
180 bool CheckValid() const;
181
182 virtual const AidlNode& AsAidlNode() const = 0;
183
184 protected:
185 AidlParameterizable(const AidlParameterizable&);
186
187 private:
188 const unique_ptr<std::vector<T>> type_params_;
189 static_assert(std::is_same<T, unique_ptr<AidlTypeSpecifier>>::value ||
190 std::is_same<T, std::string>::value);
191};
192template <>
193bool AidlParameterizable<std::string>::CheckValid() const;
194
Andrei Onea9445fc62019-06-27 18:11:59 +0100195class AidlConstantValue;
196class AidlConstantDeclaration;
197
198// Transforms a value string into a language specific form. Raw value as produced by
199// AidlConstantValue.
200using ConstantValueDecorator =
201 std::function<std::string(const AidlTypeSpecifier& type, const std::string& raw_value)>;
202
Jiyong Park68bc77a2018-07-19 19:00:45 +0900203class AidlAnnotation : public AidlNode {
204 public:
Andrei Onea9445fc62019-06-27 18:11:59 +0100205 static AidlAnnotation* Parse(
206 const AidlLocation& location, const string& name,
207 std::map<std::string, std::shared_ptr<AidlConstantValue>>* parameter_list);
Steven Moreland46e9da82018-07-27 15:45:29 -0700208
Steven Moreland3f658cf2018-08-20 13:40:54 -0700209 AidlAnnotation(const AidlAnnotation&) = default;
Steven Moreland3be75772018-08-20 13:27:43 -0700210 AidlAnnotation(AidlAnnotation&&) = default;
Jiyong Park68bc77a2018-07-19 19:00:45 +0900211 virtual ~AidlAnnotation() = default;
Andrei Onea9445fc62019-06-27 18:11:59 +0100212 bool CheckValid() const;
Steven Moreland3be75772018-08-20 13:27:43 -0700213
Jiyong Park68bc77a2018-07-19 19:00:45 +0900214 const string& GetName() const { return name_; }
Daniel Norman37d43dd2019-09-09 17:22:34 -0700215 string ToString(const ConstantValueDecorator& decorator) const;
Andrei Onea9445fc62019-06-27 18:11:59 +0100216 std::map<std::string, std::string> AnnotationParams(
217 const ConstantValueDecorator& decorator) const;
Jiyong Parka6605ab2018-11-11 14:30:21 +0900218 const string& GetComments() const { return comments_; }
219 void SetComments(const string& comments) { comments_ = comments; }
Jiyong Park68bc77a2018-07-19 19:00:45 +0900220
221 private:
Steven Moreland46e9da82018-07-27 15:45:29 -0700222 AidlAnnotation(const AidlLocation& location, const string& name);
Andrei Onea9445fc62019-06-27 18:11:59 +0100223 AidlAnnotation(const AidlLocation& location, const string& name,
224 std::map<std::string, std::shared_ptr<AidlConstantValue>>&& parameters);
Jiyong Park68bc77a2018-07-19 19:00:45 +0900225 const string name_;
Jiyong Parka6605ab2018-11-11 14:30:21 +0900226 string comments_;
Andrei Onea9445fc62019-06-27 18:11:59 +0100227 std::map<std::string, std::shared_ptr<AidlConstantValue>> parameters_;
Jiyong Park68bc77a2018-07-19 19:00:45 +0900228};
229
Steven Moreland3be75772018-08-20 13:27:43 -0700230static inline bool operator<(const AidlAnnotation& lhs, const AidlAnnotation& rhs) {
231 return lhs.GetName() < rhs.GetName();
232}
233static inline bool operator==(const AidlAnnotation& lhs, const AidlAnnotation& rhs) {
234 return lhs.GetName() == rhs.GetName();
235}
Jiyong Park3656c3c2018-08-01 20:02:01 +0900236
Casey Dahline7922932016-02-29 17:23:01 -0800237class AidlAnnotatable : public AidlNode {
Casey Dahlin0ee37582015-09-30 16:31:55 -0700238 public:
Steven Moreland46e9da82018-07-27 15:45:29 -0700239 AidlAnnotatable(const AidlLocation& location);
Steven Moreland3f658cf2018-08-20 13:40:54 -0700240
241 AidlAnnotatable(const AidlAnnotatable&) = default;
242 AidlAnnotatable(AidlAnnotatable&&) = default;
Casey Dahline7922932016-02-29 17:23:01 -0800243 virtual ~AidlAnnotatable() = default;
244
Artur Satayev91fe8712019-07-29 13:06:01 +0100245 void Annotate(vector<AidlAnnotation>&& annotations) {
246 for (auto& annotation : annotations) {
247 annotations_.emplace_back(std::move(annotation));
248 }
249 }
Jiyong Park68bc77a2018-07-19 19:00:45 +0900250 bool IsNullable() const;
Jiyong Park68bc77a2018-07-19 19:00:45 +0900251 bool IsUtf8InCpp() const;
Steven Morelanda57d0a62019-07-30 09:41:14 -0700252 bool IsVintfStability() const;
Jeongik Cha88f95a82020-01-15 13:02:16 +0900253 bool IsStableApiParcelable(Options::Language lang) const;
Makoto Onuki78a1c1c2020-03-04 16:57:23 -0800254 bool IsHide() const;
Andrei Onea9445fc62019-06-27 18:11:59 +0100255
Steven Moreland7e4b9502020-02-20 18:10:42 -0800256 void DumpAnnotations(CodeWriter* writer) const;
257
Andrei Onea9445fc62019-06-27 18:11:59 +0100258 const AidlAnnotation* UnsupportedAppUsage() const;
Daniel Norman716d3112019-09-10 13:11:56 -0700259 const AidlTypeSpecifier* BackingType(const AidlTypenames& typenames) const;
Jiyong Park68bc77a2018-07-19 19:00:45 +0900260 std::string ToString() const;
Casey Dahline7922932016-02-29 17:23:01 -0800261
Jiyong Parka6605ab2018-11-11 14:30:21 +0900262 const vector<AidlAnnotation>& GetAnnotations() const { return annotations_; }
Devin Moore24f68572020-02-26 13:20:59 -0800263 virtual std::set<string> GetSupportedAnnotations() const = 0;
264 virtual bool CheckValid(const AidlTypenames&) const;
Jiyong Park3656c3c2018-08-01 20:02:01 +0900265
Casey Dahline7922932016-02-29 17:23:01 -0800266 private:
Jiyong Parka6605ab2018-11-11 14:30:21 +0900267 vector<AidlAnnotation> annotations_;
Casey Dahline7922932016-02-29 17:23:01 -0800268};
269
Jiyong Park1deecc32018-07-17 01:14:41 +0900270class AidlQualifiedName;
271
272// AidlTypeSpecifier represents a reference to either a built-in type,
273// a defined type, or a variant (e.g., array of generic) of a type.
Jeongik Chadf76dc72019-11-28 00:08:47 +0900274class AidlTypeSpecifier final : public AidlAnnotatable,
275 public AidlParameterizable<unique_ptr<AidlTypeSpecifier>> {
Casey Dahline7922932016-02-29 17:23:01 -0800276 public:
Steven Moreland46e9da82018-07-27 15:45:29 -0700277 AidlTypeSpecifier(const AidlLocation& location, const string& unresolved_name, bool is_array,
278 vector<unique_ptr<AidlTypeSpecifier>>* type_params, const string& comments);
Jiyong Parkd59a10d2018-07-18 11:12:55 +0900279 virtual ~AidlTypeSpecifier() = default;
Casey Dahlin0ee37582015-09-30 16:31:55 -0700280
Steven Moreland3f658cf2018-08-20 13:40:54 -0700281 // Copy of this type which is not an array.
282 AidlTypeSpecifier ArrayBase() const;
283
Jiyong Park1deecc32018-07-17 01:14:41 +0900284 // Returns the full-qualified name of the base type.
285 // int -> int
286 // int[] -> int
287 // List<String> -> List
288 // IFoo -> foo.bar.IFoo (if IFoo is in package foo.bar)
289 const string& GetName() const {
290 if (IsResolved()) {
291 return fully_qualified_name_;
292 } else {
293 return GetUnresolvedName();
294 }
295 }
Casey Dahlin0ee37582015-09-30 16:31:55 -0700296
Jiyong Park1deecc32018-07-17 01:14:41 +0900297 // Returns string representation of this type specifier.
Artur Satayev91fe8712019-07-29 13:06:01 +0100298 // This is GetBaseTypeName() + array modifier or generic type parameters
Jiyong Park1deecc32018-07-17 01:14:41 +0900299 string ToString() const;
300
Jiyong Park02da7422018-07-16 16:00:26 +0900301 std::string Signature() const;
302
Jiyong Park1deecc32018-07-17 01:14:41 +0900303 const string& GetUnresolvedName() const { return unresolved_name_; }
304
Jeongik Cha997281d2020-01-16 15:23:59 +0900305 bool IsHidden() const;
306
Jiyong Park1deecc32018-07-17 01:14:41 +0900307 const string& GetComments() const { return comments_; }
308
Jeongik Cha1a7ab642019-07-29 17:31:02 +0900309 const std::vector<std::string> GetSplitName() const { return split_name_; }
310
Jiyong Parka6605ab2018-11-11 14:30:21 +0900311 void SetComments(const string& comment) { comments_ = comment; }
312
Jiyong Park1deecc32018-07-17 01:14:41 +0900313 bool IsResolved() const { return fully_qualified_name_ != ""; }
314
315 bool IsArray() const { return is_array_; }
316
Jiyong Park1deecc32018-07-17 01:14:41 +0900317 // Resolve the base type name to a fully-qualified name. Return false if the
318 // resolution fails.
Daniel Norman716d3112019-09-10 13:11:56 -0700319 bool Resolve(const AidlTypenames& typenames);
Casey Dahlin0ee37582015-09-30 16:31:55 -0700320
Devin Moore24f68572020-02-26 13:20:59 -0800321 std::set<string> GetSupportedAnnotations() const override;
322 bool CheckValid(const AidlTypenames& typenames) const override;
Jeongik Cha047c5ee2019-08-07 23:16:49 +0900323 bool LanguageSpecificCheckValid(Options::Language lang) const;
Jeongik Chadf76dc72019-11-28 00:08:47 +0900324 const AidlNode& AsAidlNode() const override { return *this; }
Jiyong Park1d2df7d2018-07-23 15:22:50 +0900325
Casey Dahlin0ee37582015-09-30 16:31:55 -0700326 private:
Steven Moreland3f658cf2018-08-20 13:40:54 -0700327 AidlTypeSpecifier(const AidlTypeSpecifier&) = default;
328
Jiyong Park1deecc32018-07-17 01:14:41 +0900329 const string unresolved_name_;
330 string fully_qualified_name_;
Steven Moreland3f658cf2018-08-20 13:40:54 -0700331 bool is_array_;
Jiyong Parka6605ab2018-11-11 14:30:21 +0900332 string comments_;
Jeongik Cha1a7ab642019-07-29 17:31:02 +0900333 vector<string> split_name_;
Casey Dahlin0ee37582015-09-30 16:31:55 -0700334};
335
Steven Moreland860b1942018-08-16 14:59:28 -0700336// Returns the universal value unaltered.
337std::string AidlConstantValueDecorator(const AidlTypeSpecifier& type, const std::string& raw_value);
338
Steven Moreland9ea10e32018-07-19 15:26:09 -0700339class AidlConstantValue;
Steven Moreland5557f1c2018-07-02 13:50:23 -0700340class AidlVariableDeclaration : public AidlNode {
341 public:
Steven Moreland46e9da82018-07-27 15:45:29 -0700342 AidlVariableDeclaration(const AidlLocation& location, AidlTypeSpecifier* type,
343 const std::string& name);
344 AidlVariableDeclaration(const AidlLocation& location, AidlTypeSpecifier* type,
345 const std::string& name, AidlConstantValue* default_value);
Steven Moreland5557f1c2018-07-02 13:50:23 -0700346 virtual ~AidlVariableDeclaration() = default;
347
348 std::string GetName() const { return name_; }
Jiyong Parkd59a10d2018-07-18 11:12:55 +0900349 const AidlTypeSpecifier& GetType() const { return *type_; }
Steven Moreland9ea10e32018-07-19 15:26:09 -0700350 const AidlConstantValue* GetDefaultValue() const { return default_value_.get(); }
351
Jiyong Parkd59a10d2018-07-18 11:12:55 +0900352 AidlTypeSpecifier* GetMutableType() { return type_.get(); }
Steven Moreland5557f1c2018-07-02 13:50:23 -0700353
Jeongik Chadb0f59e2018-11-01 18:11:21 +0900354 bool CheckValid(const AidlTypenames& typenames) const;
Steven Moreland5557f1c2018-07-02 13:50:23 -0700355 std::string ToString() const;
Jiyong Park02da7422018-07-16 16:00:26 +0900356 std::string Signature() const;
Steven Moreland5557f1c2018-07-02 13:50:23 -0700357
Steven Moreland860b1942018-08-16 14:59:28 -0700358 std::string ValueString(const ConstantValueDecorator& decorator) const;
Steven Moreland25294322018-08-07 18:13:55 -0700359
Steven Moreland5557f1c2018-07-02 13:50:23 -0700360 private:
Jiyong Parkd59a10d2018-07-18 11:12:55 +0900361 std::unique_ptr<AidlTypeSpecifier> type_;
Steven Moreland5557f1c2018-07-02 13:50:23 -0700362 std::string name_;
Steven Moreland9ea10e32018-07-19 15:26:09 -0700363 std::unique_ptr<AidlConstantValue> default_value_;
Steven Moreland5557f1c2018-07-02 13:50:23 -0700364
365 DISALLOW_COPY_AND_ASSIGN(AidlVariableDeclaration);
366};
367
368class AidlArgument : public AidlVariableDeclaration {
Casey Dahlinbc7a50a2015-09-28 19:20:50 -0700369 public:
Casey Dahlinc378c992015-09-29 16:50:40 -0700370 enum Direction { IN_DIR = 1, OUT_DIR = 2, INOUT_DIR = 3 };
371
Steven Moreland46e9da82018-07-27 15:45:29 -0700372 AidlArgument(const AidlLocation& location, AidlArgument::Direction direction,
373 AidlTypeSpecifier* type, const std::string& name);
374 AidlArgument(const AidlLocation& location, AidlTypeSpecifier* type, const std::string& name);
Casey Dahlinbc7a50a2015-09-28 19:20:50 -0700375 virtual ~AidlArgument() = default;
376
Casey Dahlinc378c992015-09-29 16:50:40 -0700377 Direction GetDirection() const { return direction_; }
Christopher Wileyad339272015-10-05 19:11:58 -0700378 bool IsOut() const { return direction_ & OUT_DIR; }
379 bool IsIn() const { return direction_ & IN_DIR; }
Casey Dahlinc378c992015-09-29 16:50:40 -0700380 bool DirectionWasSpecified() const { return direction_specified_; }
Jiyong Park3656c3c2018-08-01 20:02:01 +0900381 string GetDirectionSpecifier() const;
Christopher Wileyad339272015-10-05 19:11:58 -0700382
Casey Dahlinc378c992015-09-29 16:50:40 -0700383 std::string ToString() const;
Jiyong Park02da7422018-07-16 16:00:26 +0900384 std::string Signature() const;
Casey Dahlinc378c992015-09-29 16:50:40 -0700385
Casey Dahlinbc7a50a2015-09-28 19:20:50 -0700386 private:
Casey Dahlinc378c992015-09-29 16:50:40 -0700387 Direction direction_;
388 bool direction_specified_;
389
Casey Dahlinbc7a50a2015-09-28 19:20:50 -0700390 DISALLOW_COPY_AND_ASSIGN(AidlArgument);
Casey Dahlina834dd42015-09-23 11:52:15 -0700391};
Adam Lesinskiffa16862014-01-23 18:17:42 -0800392
Casey Dahlind40e2fe2015-11-24 14:06:52 -0800393class AidlMethod;
Steven Moreland693640b2018-07-19 13:46:27 -0700394class AidlConstantDeclaration;
Daniel Norman85aed542019-08-21 12:01:14 -0700395class AidlEnumDeclaration;
Casey Dahlind40e2fe2015-11-24 14:06:52 -0800396class AidlMember : public AidlNode {
397 public:
Steven Moreland46e9da82018-07-27 15:45:29 -0700398 AidlMember(const AidlLocation& location);
Casey Dahlind40e2fe2015-11-24 14:06:52 -0800399 virtual ~AidlMember() = default;
400
401 virtual AidlMethod* AsMethod() { return nullptr; }
Steven Moreland693640b2018-07-19 13:46:27 -0700402 virtual AidlConstantDeclaration* AsConstantDeclaration() { return nullptr; }
Casey Dahlind40e2fe2015-11-24 14:06:52 -0800403
404 private:
405 DISALLOW_COPY_AND_ASSIGN(AidlMember);
406};
407
Will McVickerd7d18df2019-09-12 13:40:50 -0700408class AidlUnaryConstExpression;
409class AidlBinaryConstExpression;
410
Steven Moreland693640b2018-07-19 13:46:27 -0700411class AidlConstantValue : public AidlNode {
Casey Dahlind40e2fe2015-11-24 14:06:52 -0800412 public:
Will McVickerd7d18df2019-09-12 13:40:50 -0700413 enum class Type {
414 // WARNING: Don't change this order! The order is used to determine type
415 // promotion during a binary expression.
416 BOOLEAN,
417 INT8,
418 INT32,
419 INT64,
420 ARRAY,
421 CHARACTER,
422 STRING,
423 FLOATING,
424 UNARY,
425 BINARY,
426 ERROR,
427 };
428
429 /*
430 * Return the value casted to the given type.
431 */
432 template <typename T>
433 T cast() const;
Casey Dahlind40e2fe2015-11-24 14:06:52 -0800434
Steven Moreland693640b2018-07-19 13:46:27 -0700435 virtual ~AidlConstantValue() = default;
Casey Dahlind40e2fe2015-11-24 14:06:52 -0800436
Steven Moreland25294322018-08-07 18:13:55 -0700437 static AidlConstantValue* Boolean(const AidlLocation& location, bool value);
438 static AidlConstantValue* Character(const AidlLocation& location, char value);
Steven Moreland25294322018-08-07 18:13:55 -0700439 // example: 123, -5498, maybe any size
Will McVickerd7d18df2019-09-12 13:40:50 -0700440 static AidlConstantValue* Integral(const AidlLocation& location, const string& value);
441 static AidlConstantValue* Floating(const AidlLocation& location, const std::string& value);
Steven Moreland860b1942018-08-16 14:59:28 -0700442 static AidlConstantValue* Array(const AidlLocation& location,
Will McVickerd7d18df2019-09-12 13:40:50 -0700443 std::unique_ptr<vector<unique_ptr<AidlConstantValue>>> values);
Steven Moreland693640b2018-07-19 13:46:27 -0700444 // example: "\"asdf\""
Will McVickerd7d18df2019-09-12 13:40:50 -0700445 static AidlConstantValue* String(const AidlLocation& location, const string& value);
Steven Moreland693640b2018-07-19 13:46:27 -0700446
Daniel Normanf0ca44f2019-10-25 09:59:44 -0700447 // Construct an AidlConstantValue by evaluating the other integral constant's
448 // value string. This does not preserve the structure of the copied constant.
Steven Moreland59e53e42019-11-26 20:38:08 -0800449 // Returns nullptr and logs if value cannot be copied.
Daniel Normanf0ca44f2019-10-25 09:59:44 -0700450 static AidlConstantValue* ShallowIntegralCopy(const AidlConstantValue& other);
Daniel Normanb28684e2019-10-17 15:31:39 -0700451
Will McVickerd7d18df2019-09-12 13:40:50 -0700452 Type GetType() const { return final_type_; }
Steven Moreland25294322018-08-07 18:13:55 -0700453
Will McVickerd7d18df2019-09-12 13:40:50 -0700454 virtual bool CheckValid() const;
Steven Moreland860b1942018-08-16 14:59:28 -0700455
456 // Raw value of type (currently valid in C++ and Java). Empty string on error.
Steven Moreland4bcb05c2019-11-27 18:57:47 -0800457 string ValueString(const AidlTypeSpecifier& type, const ConstantValueDecorator& decorator) const;
Casey Dahlind40e2fe2015-11-24 14:06:52 -0800458
459 private:
Will McVickerd7d18df2019-09-12 13:40:50 -0700460 AidlConstantValue(const AidlLocation& location, Type parsed_type, int64_t parsed_value,
461 const string& checked_value);
462 AidlConstantValue(const AidlLocation& location, Type type, const string& checked_value);
Steven Moreland860b1942018-08-16 14:59:28 -0700463 AidlConstantValue(const AidlLocation& location, Type type,
Will McVickerd7d18df2019-09-12 13:40:50 -0700464 std::unique_ptr<vector<unique_ptr<AidlConstantValue>>> values);
Steven Moreland25294322018-08-07 18:13:55 -0700465 static string ToString(Type type);
Will McVickerd7d18df2019-09-12 13:40:50 -0700466 static bool ParseIntegral(const string& value, int64_t* parsed_value, Type* parsed_type);
467 static bool IsHex(const string& value);
Steven Moreland4bcb05c2019-11-27 18:57:47 -0800468
Will McVickerd7d18df2019-09-12 13:40:50 -0700469 virtual bool evaluate(const AidlTypeSpecifier& type) const;
Casey Dahlind40e2fe2015-11-24 14:06:52 -0800470
Steven Moreland693640b2018-07-19 13:46:27 -0700471 const Type type_ = Type::ERROR;
Will McVickerd7d18df2019-09-12 13:40:50 -0700472 const vector<unique_ptr<AidlConstantValue>> values_; // if type_ == ARRAY
473 const string value_; // otherwise
474
475 // State for tracking evaluation of expressions
Steven Moreland4bcb05c2019-11-27 18:57:47 -0800476 mutable bool is_valid_ = false; // cache of CheckValid, but may be marked false in evaluate
477 mutable bool is_evaluated_ = false; // whether evaluate has been called
Will McVickerd7d18df2019-09-12 13:40:50 -0700478 mutable Type final_type_;
479 mutable int64_t final_value_;
480 mutable string final_string_value_ = "";
Steven Moreland693640b2018-07-19 13:46:27 -0700481
482 DISALLOW_COPY_AND_ASSIGN(AidlConstantValue);
Will McVickerd7d18df2019-09-12 13:40:50 -0700483
484 friend AidlUnaryConstExpression;
485 friend AidlBinaryConstExpression;
486};
487
488class AidlUnaryConstExpression : public AidlConstantValue {
489 public:
490 AidlUnaryConstExpression(const AidlLocation& location, const string& op,
491 std::unique_ptr<AidlConstantValue> rval);
492
493 static bool IsCompatibleType(Type type, const string& op);
494 bool CheckValid() const override;
Will McVickerd7d18df2019-09-12 13:40:50 -0700495 private:
496 bool evaluate(const AidlTypeSpecifier& type) const override;
497
498 std::unique_ptr<AidlConstantValue> unary_;
499 const string op_;
500};
501
502class AidlBinaryConstExpression : public AidlConstantValue {
503 public:
504 AidlBinaryConstExpression(const AidlLocation& location, std::unique_ptr<AidlConstantValue> lval,
505 const string& op, std::unique_ptr<AidlConstantValue> rval);
506
507 bool CheckValid() const override;
Will McVickerd7d18df2019-09-12 13:40:50 -0700508
509 static bool AreCompatibleTypes(Type t1, Type t2);
510 // Returns the promoted kind for both operands
511 static Type UsualArithmeticConversion(Type left, Type right);
512 // Returns the promoted integral type where INT32 is the smallest type
513 static Type IntegralPromotion(Type in);
514
515 private:
516 bool evaluate(const AidlTypeSpecifier& type) const override;
517
518 std::unique_ptr<AidlConstantValue> left_val_;
519 std::unique_ptr<AidlConstantValue> right_val_;
520 const string op_;
Christopher Wileyd6bdd8d2016-05-03 11:23:13 -0700521};
522
Andrei Onea9445fc62019-06-27 18:11:59 +0100523struct AidlAnnotationParameter {
524 std::string name;
525 std::unique_ptr<AidlConstantValue> value;
526};
527
Steven Moreland693640b2018-07-19 13:46:27 -0700528class AidlConstantDeclaration : public AidlMember {
Christopher Wileyd6bdd8d2016-05-03 11:23:13 -0700529 public:
Steven Moreland46e9da82018-07-27 15:45:29 -0700530 AidlConstantDeclaration(const AidlLocation& location, AidlTypeSpecifier* specifier,
Will McVickerd7d18df2019-09-12 13:40:50 -0700531 const string& name, AidlConstantValue* value);
Steven Moreland693640b2018-07-19 13:46:27 -0700532 virtual ~AidlConstantDeclaration() = default;
Christopher Wileyd6bdd8d2016-05-03 11:23:13 -0700533
Steven Moreland693640b2018-07-19 13:46:27 -0700534 const AidlTypeSpecifier& GetType() const { return *type_; }
Steven Moreland4d12f9a2018-10-31 14:30:55 -0700535 AidlTypeSpecifier* GetMutableType() { return type_.get(); }
Will McVickerd7d18df2019-09-12 13:40:50 -0700536 const string& GetName() const { return name_; }
Steven Moreland693640b2018-07-19 13:46:27 -0700537 const AidlConstantValue& GetValue() const { return *value_; }
Jeongik Chadb0f59e2018-11-01 18:11:21 +0900538 bool CheckValid(const AidlTypenames& typenames) const;
Christopher Wileyd6bdd8d2016-05-03 11:23:13 -0700539
Will McVickerd7d18df2019-09-12 13:40:50 -0700540 string ToString() const;
541 string Signature() const;
Steven Moreland860b1942018-08-16 14:59:28 -0700542 string ValueString(const ConstantValueDecorator& decorator) const {
Will McVickerd7d18df2019-09-12 13:40:50 -0700543 return value_->ValueString(GetType(), decorator);
Steven Moreland860b1942018-08-16 14:59:28 -0700544 }
Steven Moreland25294322018-08-07 18:13:55 -0700545
Steven Moreland693640b2018-07-19 13:46:27 -0700546 AidlConstantDeclaration* AsConstantDeclaration() override { return this; }
Christopher Wileyd6bdd8d2016-05-03 11:23:13 -0700547
548 private:
Steven Moreland693640b2018-07-19 13:46:27 -0700549 const unique_ptr<AidlTypeSpecifier> type_;
Will McVickerd7d18df2019-09-12 13:40:50 -0700550 const string name_;
551 unique_ptr<AidlConstantValue> value_;
Christopher Wileyd6bdd8d2016-05-03 11:23:13 -0700552
Steven Moreland693640b2018-07-19 13:46:27 -0700553 DISALLOW_COPY_AND_ASSIGN(AidlConstantDeclaration);
Casey Dahlind40e2fe2015-11-24 14:06:52 -0800554};
555
556class AidlMethod : public AidlMember {
Casey Dahlin5c69deb2015-10-01 14:44:12 -0700557 public:
Will McVickerd7d18df2019-09-12 13:40:50 -0700558 AidlMethod(const AidlLocation& location, bool oneway, AidlTypeSpecifier* type, const string& name,
559 vector<unique_ptr<AidlArgument>>* args, const string& comments);
560 AidlMethod(const AidlLocation& location, bool oneway, AidlTypeSpecifier* type, const string& name,
561 vector<unique_ptr<AidlArgument>>* args, const string& comments, int id,
562 bool is_user_defined = true);
Casey Dahlin5c69deb2015-10-01 14:44:12 -0700563 virtual ~AidlMethod() = default;
564
Casey Dahlind40e2fe2015-11-24 14:06:52 -0800565 AidlMethod* AsMethod() override { return this; }
Jeongik Cha997281d2020-01-16 15:23:59 +0900566 bool IsHidden() const;
Will McVickerd7d18df2019-09-12 13:40:50 -0700567 const string& GetComments() const { return comments_; }
Jiyong Parkd59a10d2018-07-18 11:12:55 +0900568 const AidlTypeSpecifier& GetType() const { return *type_; }
569 AidlTypeSpecifier* GetMutableType() { return type_.get(); }
Steven Morelandacd53472018-12-14 10:17:26 -0800570
Steven Moreland8c70ba92018-12-17 10:20:31 -0800571 // set if this method is part of an interface that is marked oneway
572 void ApplyInterfaceOneway(bool oneway) { oneway_ = oneway_ || oneway; }
Casey Dahlinf4a93112015-10-05 16:58:09 -0700573 bool IsOneway() const { return oneway_; }
Steven Morelandacd53472018-12-14 10:17:26 -0800574
Casey Dahlinf4a93112015-10-05 16:58:09 -0700575 const std::string& GetName() const { return name_; }
Casey Dahlinf4a93112015-10-05 16:58:09 -0700576 bool HasId() const { return has_id_; }
Jiyong Parked65bf42018-08-28 15:43:27 +0900577 int GetId() const { return id_; }
Casey Dahlinf4a93112015-10-05 16:58:09 -0700578 void SetId(unsigned id) { id_ = id; }
Casey Dahlinf2d23f72015-10-02 16:19:19 -0700579
Jiyong Park309668e2018-07-28 16:55:44 +0900580 bool IsUserDefined() const { return is_user_defined_; }
581
Casey Dahlinf4a93112015-10-05 16:58:09 -0700582 const std::vector<std::unique_ptr<AidlArgument>>& GetArguments() const {
Christopher Wileyad339272015-10-05 19:11:58 -0700583 return arguments_;
584 }
585 // An inout parameter will appear in both GetInArguments()
586 // and GetOutArguments(). AidlMethod retains ownership of the argument
587 // pointers returned in this way.
588 const std::vector<const AidlArgument*>& GetInArguments() const {
589 return in_arguments_;
590 }
591 const std::vector<const AidlArgument*>& GetOutArguments() const {
592 return out_arguments_;
Casey Dahlinf4a93112015-10-05 16:58:09 -0700593 }
Casey Dahlin5c69deb2015-10-01 14:44:12 -0700594
Jiyong Park309668e2018-07-28 16:55:44 +0900595 // name + type parameter types
596 // i.e, foo(int, String)
Jiyong Park02da7422018-07-16 16:00:26 +0900597 std::string Signature() const;
598
Jiyong Park309668e2018-07-28 16:55:44 +0900599 // return type + name + type parameter types + annotations
600 // i.e, boolean foo(int, @Nullable String)
601 std::string ToString() const;
602
Casey Dahlin5c69deb2015-10-01 14:44:12 -0700603 private:
Casey Dahlinf4a93112015-10-05 16:58:09 -0700604 bool oneway_;
Casey Dahlinf2d23f72015-10-02 16:19:19 -0700605 std::string comments_;
Jiyong Parkd59a10d2018-07-18 11:12:55 +0900606 std::unique_ptr<AidlTypeSpecifier> type_;
Casey Dahlinf4a93112015-10-05 16:58:09 -0700607 std::string name_;
Christopher Wileyad339272015-10-05 19:11:58 -0700608 const std::vector<std::unique_ptr<AidlArgument>> arguments_;
609 std::vector<const AidlArgument*> in_arguments_;
610 std::vector<const AidlArgument*> out_arguments_;
Casey Dahlinf4a93112015-10-05 16:58:09 -0700611 bool has_id_;
612 int id_;
Jiyong Park309668e2018-07-28 16:55:44 +0900613 bool is_user_defined_ = true;
Casey Dahlinf2d23f72015-10-02 16:19:19 -0700614
Casey Dahlin5c69deb2015-10-01 14:44:12 -0700615 DISALLOW_COPY_AND_ASSIGN(AidlMethod);
Casey Dahlin0a2f8be2015-09-28 16:15:29 -0700616};
Adam Lesinskiffa16862014-01-23 18:17:42 -0800617
Steven Morelandc258abc2018-07-10 14:03:38 -0700618class AidlDefinedType;
Jiyong Parkb034bf02018-07-30 17:44:33 +0900619class AidlInterface;
620class AidlParcelable;
621class AidlStructuredParcelable;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800622
Casey Dahlin2b2879b2015-10-13 16:59:44 -0700623class AidlQualifiedName : public AidlNode {
624 public:
Steven Moreland46e9da82018-07-27 15:45:29 -0700625 AidlQualifiedName(const AidlLocation& location, const std::string& term,
626 const std::string& comments);
Casey Dahlin2b2879b2015-10-13 16:59:44 -0700627 virtual ~AidlQualifiedName() = default;
628
629 const std::vector<std::string>& GetTerms() const { return terms_; }
630 const std::string& GetComments() const { return comments_; }
631 std::string GetDotName() const { return android::base::Join(terms_, '.'); }
Ningyuan Wangd17c58b2016-09-29 14:33:14 -0700632 std::string GetColonName() const { return android::base::Join(terms_, "::"); }
Casey Dahlin2b2879b2015-10-13 16:59:44 -0700633
Chih-Hung Hsiehf05cc262016-07-27 11:42:51 -0700634 void AddTerm(const std::string& term);
Casey Dahlin2b2879b2015-10-13 16:59:44 -0700635
636 private:
637 std::vector<std::string> terms_;
638 std::string comments_;
639
640 DISALLOW_COPY_AND_ASSIGN(AidlQualifiedName);
641};
642
Steven Moreland46e9da82018-07-27 15:45:29 -0700643class AidlInterface;
644class AidlParcelable;
645class AidlStructuredParcelable;
Daniel Norman85aed542019-08-21 12:01:14 -0700646// AidlDefinedType represents either an interface, parcelable, or enum that is
Jiyong Park1deecc32018-07-17 01:14:41 +0900647// defined in the source file.
648class AidlDefinedType : public AidlAnnotatable {
Steven Moreland787b0432018-07-03 09:00:58 -0700649 public:
Steven Moreland46e9da82018-07-27 15:45:29 -0700650 AidlDefinedType(const AidlLocation& location, const std::string& name,
651 const std::string& comments, const std::vector<std::string>& package);
Steven Moreland787b0432018-07-03 09:00:58 -0700652 virtual ~AidlDefinedType() = default;
653
Jiyong Park1deecc32018-07-17 01:14:41 +0900654 const std::string& GetName() const { return name_; };
Jeongik Cha997281d2020-01-16 15:23:59 +0900655 bool IsHidden() const;
Jiyong Park1deecc32018-07-17 01:14:41 +0900656 const std::string& GetComments() const { return comments_; }
Jiyong Parka6605ab2018-11-11 14:30:21 +0900657 void SetComments(const std::string comments) { comments_ = comments; }
Jiyong Park1deecc32018-07-17 01:14:41 +0900658
Steven Moreland787b0432018-07-03 09:00:58 -0700659 /* dot joined package, example: "android.package.foo" */
660 std::string GetPackage() const;
661 /* dot joined package and name, example: "android.package.foo.IBar" */
662 std::string GetCanonicalName() const;
663 const std::vector<std::string>& GetSplitPackage() const { return package_; }
664
Steven Morelanded83a282018-07-17 13:27:29 -0700665 virtual std::string GetPreprocessDeclarationName() const = 0;
Steven Morelandc258abc2018-07-10 14:03:38 -0700666
Steven Moreland5557f1c2018-07-02 13:50:23 -0700667 virtual const AidlStructuredParcelable* AsStructuredParcelable() const { return nullptr; }
Steven Morelandc258abc2018-07-10 14:03:38 -0700668 virtual const AidlParcelable* AsParcelable() const { return nullptr; }
Daniel Norman85aed542019-08-21 12:01:14 -0700669 virtual const AidlEnumDeclaration* AsEnumDeclaration() const { return nullptr; }
Steven Moreland5557f1c2018-07-02 13:50:23 -0700670 virtual const AidlInterface* AsInterface() const { return nullptr; }
Jeongik Chadf76dc72019-11-28 00:08:47 +0900671 virtual const AidlParameterizable<std::string>* AsParameterizable() const { return nullptr; }
Devin Moore24f68572020-02-26 13:20:59 -0800672 bool CheckValid(const AidlTypenames& typenames) const override;
Jeongik Cha047c5ee2019-08-07 23:16:49 +0900673 virtual bool LanguageSpecificCheckValid(Options::Language lang) const = 0;
Steven Morelandc258abc2018-07-10 14:03:38 -0700674 AidlStructuredParcelable* AsStructuredParcelable() {
675 return const_cast<AidlStructuredParcelable*>(
676 const_cast<const AidlDefinedType*>(this)->AsStructuredParcelable());
677 }
678 AidlParcelable* AsParcelable() {
679 return const_cast<AidlParcelable*>(const_cast<const AidlDefinedType*>(this)->AsParcelable());
680 }
Daniel Norman85aed542019-08-21 12:01:14 -0700681 AidlEnumDeclaration* AsEnumDeclaration() {
682 return const_cast<AidlEnumDeclaration*>(
683 const_cast<const AidlDefinedType*>(this)->AsEnumDeclaration());
684 }
Steven Morelandc258abc2018-07-10 14:03:38 -0700685 AidlInterface* AsInterface() {
686 return const_cast<AidlInterface*>(const_cast<const AidlDefinedType*>(this)->AsInterface());
687 }
688
Jeongik Chadf76dc72019-11-28 00:08:47 +0900689 AidlParameterizable<std::string>* AsParameterizable() {
690 return const_cast<AidlParameterizable<std::string>*>(
691 const_cast<const AidlDefinedType*>(this)->AsParameterizable());
692 }
693
Steven Moreland6cee3482018-07-18 14:39:58 -0700694 const AidlParcelable* AsUnstructuredParcelable() const {
695 if (this->AsStructuredParcelable() != nullptr) return nullptr;
696 return this->AsParcelable();
697 }
698 AidlParcelable* AsUnstructuredParcelable() {
699 return const_cast<AidlParcelable*>(
700 const_cast<const AidlDefinedType*>(this)->AsUnstructuredParcelable());
701 }
702
Jeongik Cha997281d2020-01-16 15:23:59 +0900703 virtual void Dump(CodeWriter* writer) const = 0;
Steven Morelanda5d9c5c2020-02-21 16:01:09 -0800704 void DumpHeader(CodeWriter* writer) const;
Jiyong Park02da7422018-07-16 16:00:26 +0900705
Steven Moreland787b0432018-07-03 09:00:58 -0700706 private:
Jiyong Park1deecc32018-07-17 01:14:41 +0900707 std::string name_;
Jiyong Park1deecc32018-07-17 01:14:41 +0900708 std::string comments_;
Steven Moreland787b0432018-07-03 09:00:58 -0700709 const std::vector<std::string> package_;
Steven Moreland5557f1c2018-07-02 13:50:23 -0700710
711 DISALLOW_COPY_AND_ASSIGN(AidlDefinedType);
Steven Moreland787b0432018-07-03 09:00:58 -0700712};
713
Jeongik Chadf76dc72019-11-28 00:08:47 +0900714class AidlParcelable : public AidlDefinedType, public AidlParameterizable<std::string> {
Casey Dahlin1ae2bc52015-10-07 18:49:10 -0700715 public:
Steven Moreland46e9da82018-07-27 15:45:29 -0700716 AidlParcelable(const AidlLocation& location, AidlQualifiedName* name,
Jiyong Parka6605ab2018-11-11 14:30:21 +0900717 const std::vector<std::string>& package, const std::string& comments,
Jeongik Chadf76dc72019-11-28 00:08:47 +0900718 const std::string& cpp_header = "",
719 std::vector<std::string>* type_params = nullptr);
Casey Dahlin1ae2bc52015-10-07 18:49:10 -0700720 virtual ~AidlParcelable() = default;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800721
Ningyuan Wangd17c58b2016-09-29 14:33:14 -0700722 // C++ uses "::" instead of "." to refer to a inner class.
723 std::string GetCppName() const { return name_->GetColonName(); }
Christopher Wiley8aa4d9f2015-11-16 19:10:45 -0800724 std::string GetCppHeader() const { return cpp_header_; }
Christopher Wiley8aa4d9f2015-11-16 19:10:45 -0800725
Devin Moore24f68572020-02-26 13:20:59 -0800726 std::set<string> GetSupportedAnnotations() const override;
Jeongik Cha82317dd2019-02-27 20:26:11 +0900727 bool CheckValid(const AidlTypenames& typenames) const override;
Jeongik Cha047c5ee2019-08-07 23:16:49 +0900728 bool LanguageSpecificCheckValid(Options::Language lang) const override;
Steven Morelandc258abc2018-07-10 14:03:38 -0700729 const AidlParcelable* AsParcelable() const override { return this; }
Jeongik Chadf76dc72019-11-28 00:08:47 +0900730 const AidlParameterizable<std::string>* AsParameterizable() const override { return this; }
731 const AidlNode& AsAidlNode() const override { return *this; }
Steven Morelanded83a282018-07-17 13:27:29 -0700732 std::string GetPreprocessDeclarationName() const override { return "parcelable"; }
Steven Morelandc258abc2018-07-10 14:03:38 -0700733
Jeongik Cha997281d2020-01-16 15:23:59 +0900734 void Dump(CodeWriter* writer) const override;
Jiyong Park02da7422018-07-16 16:00:26 +0900735
Casey Dahlin1ae2bc52015-10-07 18:49:10 -0700736 private:
Christopher Wiley8aa4d9f2015-11-16 19:10:45 -0800737 std::unique_ptr<AidlQualifiedName> name_;
Christopher Wiley8aa4d9f2015-11-16 19:10:45 -0800738 std::string cpp_header_;
Casey Dahlin59401da2015-10-09 18:16:45 -0700739
Casey Dahlin1ae2bc52015-10-07 18:49:10 -0700740 DISALLOW_COPY_AND_ASSIGN(AidlParcelable);
Casey Dahlin0a2f8be2015-09-28 16:15:29 -0700741};
Adam Lesinskiffa16862014-01-23 18:17:42 -0800742
Steven Moreland5557f1c2018-07-02 13:50:23 -0700743class AidlStructuredParcelable : public AidlParcelable {
744 public:
Steven Moreland46e9da82018-07-27 15:45:29 -0700745 AidlStructuredParcelable(const AidlLocation& location, AidlQualifiedName* name,
Jiyong Parka6605ab2018-11-11 14:30:21 +0900746 const std::vector<std::string>& package, const std::string& comments,
Steven Moreland5557f1c2018-07-02 13:50:23 -0700747 std::vector<std::unique_ptr<AidlVariableDeclaration>>* variables);
748
749 const std::vector<std::unique_ptr<AidlVariableDeclaration>>& GetFields() const {
750 return variables_;
751 }
752
Steven Morelandc258abc2018-07-10 14:03:38 -0700753 const AidlStructuredParcelable* AsStructuredParcelable() const override { return this; }
Steven Morelanded83a282018-07-17 13:27:29 -0700754 std::string GetPreprocessDeclarationName() const override { return "structured_parcelable"; }
Steven Moreland5557f1c2018-07-02 13:50:23 -0700755
Jeongik Cha997281d2020-01-16 15:23:59 +0900756 void Dump(CodeWriter* writer) const override;
Jiyong Park02da7422018-07-16 16:00:26 +0900757
Devin Moore24f68572020-02-26 13:20:59 -0800758 std::set<string> GetSupportedAnnotations() const override;
Jeongik Chadb0f59e2018-11-01 18:11:21 +0900759 bool CheckValid(const AidlTypenames& typenames) const override;
Jeongik Cha047c5ee2019-08-07 23:16:49 +0900760 bool LanguageSpecificCheckValid(Options::Language lang) const override;
Jeongik Chadb0f59e2018-11-01 18:11:21 +0900761
Steven Moreland5557f1c2018-07-02 13:50:23 -0700762 private:
763 const std::vector<std::unique_ptr<AidlVariableDeclaration>> variables_;
764
765 DISALLOW_COPY_AND_ASSIGN(AidlStructuredParcelable);
766};
767
Daniel Norman85aed542019-08-21 12:01:14 -0700768class AidlEnumerator : public AidlNode {
769 public:
Daniel Norman2e4112d2019-10-03 10:22:35 -0700770 AidlEnumerator(const AidlLocation& location, const std::string& name, AidlConstantValue* value,
771 const std::string& comments);
Daniel Norman85aed542019-08-21 12:01:14 -0700772 virtual ~AidlEnumerator() = default;
773
774 const std::string& GetName() const { return name_; }
Will McVickerd7d18df2019-09-12 13:40:50 -0700775 AidlConstantValue* GetValue() const { return value_.get(); }
Daniel Norman2e4112d2019-10-03 10:22:35 -0700776 const std::string& GetComments() const { return comments_; }
Daniel Norman85aed542019-08-21 12:01:14 -0700777 bool CheckValid(const AidlTypeSpecifier& enum_backing_type) const;
778
779 string ValueString(const AidlTypeSpecifier& backing_type,
780 const ConstantValueDecorator& decorator) const;
781
Daniel Normanb28684e2019-10-17 15:31:39 -0700782 void SetValue(std::unique_ptr<AidlConstantValue> value) { value_ = std::move(value); }
783
Daniel Norman85aed542019-08-21 12:01:14 -0700784 private:
785 const std::string name_;
Will McVickerd7d18df2019-09-12 13:40:50 -0700786 unique_ptr<AidlConstantValue> value_;
Daniel Norman2e4112d2019-10-03 10:22:35 -0700787 const std::string comments_;
Daniel Norman85aed542019-08-21 12:01:14 -0700788
789 DISALLOW_COPY_AND_ASSIGN(AidlEnumerator);
790};
791
792class AidlEnumDeclaration : public AidlDefinedType {
793 public:
Will McVickerd7d18df2019-09-12 13:40:50 -0700794 AidlEnumDeclaration(const AidlLocation& location, const string& name,
Daniel Norman85aed542019-08-21 12:01:14 -0700795 std::vector<std::unique_ptr<AidlEnumerator>>* enumerators,
Daniel Norman2e4112d2019-10-03 10:22:35 -0700796 const std::vector<std::string>& package, const std::string& comments);
Daniel Norman85aed542019-08-21 12:01:14 -0700797 virtual ~AidlEnumDeclaration() = default;
798
799 void SetBackingType(std::unique_ptr<const AidlTypeSpecifier> type);
800 const AidlTypeSpecifier& GetBackingType() const { return *backing_type_; }
801 const std::vector<std::unique_ptr<AidlEnumerator>>& GetEnumerators() const {
802 return enumerators_;
803 }
Steven Moreland59e53e42019-11-26 20:38:08 -0800804 bool Autofill();
Devin Moore24f68572020-02-26 13:20:59 -0800805 std::set<string> GetSupportedAnnotations() const override;
Daniel Norman85aed542019-08-21 12:01:14 -0700806 bool CheckValid(const AidlTypenames& typenames) const override;
807 bool LanguageSpecificCheckValid(Options::Language) const override { return true; }
808 std::string GetPreprocessDeclarationName() const override { return "enum"; }
Jeongik Cha997281d2020-01-16 15:23:59 +0900809 void Dump(CodeWriter* writer) const override;
Daniel Norman85aed542019-08-21 12:01:14 -0700810
811 const AidlEnumDeclaration* AsEnumDeclaration() const override { return this; }
812
813 private:
814 const std::string name_;
815 const std::vector<std::unique_ptr<AidlEnumerator>> enumerators_;
816 std::unique_ptr<const AidlTypeSpecifier> backing_type_;
817
818 DISALLOW_COPY_AND_ASSIGN(AidlEnumDeclaration);
819};
820
Jiyong Park1deecc32018-07-17 01:14:41 +0900821class AidlInterface final : public AidlDefinedType {
Casey Dahlin1ae2bc52015-10-07 18:49:10 -0700822 public:
Steven Moreland46e9da82018-07-27 15:45:29 -0700823 AidlInterface(const AidlLocation& location, const std::string& name, const std::string& comments,
824 bool oneway_, std::vector<std::unique_ptr<AidlMember>>* members,
Christopher Wileyd76067c2015-10-19 17:00:13 -0700825 const std::vector<std::string>& package);
Casey Dahlin1ae2bc52015-10-07 18:49:10 -0700826 virtual ~AidlInterface() = default;
827
Casey Dahlinfb7da2e2015-10-08 17:26:09 -0700828 const std::vector<std::unique_ptr<AidlMethod>>& GetMethods() const
829 { return methods_; }
Jiyong Park309668e2018-07-28 16:55:44 +0900830 std::vector<std::unique_ptr<AidlMethod>>& GetMutableMethods() { return methods_; }
Steven Moreland693640b2018-07-19 13:46:27 -0700831 const std::vector<std::unique_ptr<AidlConstantDeclaration>>& GetConstantDeclarations() const {
832 return constants_;
833 }
Casey Dahlina2f77c42015-12-01 18:26:02 -0800834
Steven Morelandc258abc2018-07-10 14:03:38 -0700835 const AidlInterface* AsInterface() const override { return this; }
Steven Morelanded83a282018-07-17 13:27:29 -0700836 std::string GetPreprocessDeclarationName() const override { return "interface"; }
Steven Moreland5557f1c2018-07-02 13:50:23 -0700837
Jeongik Cha997281d2020-01-16 15:23:59 +0900838 void Dump(CodeWriter* writer) const override;
Jiyong Park02da7422018-07-16 16:00:26 +0900839
Devin Moore24f68572020-02-26 13:20:59 -0800840 std::set<string> GetSupportedAnnotations() const override;
Jeongik Chadb0f59e2018-11-01 18:11:21 +0900841 bool CheckValid(const AidlTypenames& typenames) const override;
Jeongik Cha047c5ee2019-08-07 23:16:49 +0900842 bool LanguageSpecificCheckValid(Options::Language lang) const override;
Jeongik Chadb0f59e2018-11-01 18:11:21 +0900843
Casey Dahlin1ae2bc52015-10-07 18:49:10 -0700844 private:
Casey Dahlinfb7da2e2015-10-08 17:26:09 -0700845 std::vector<std::unique_ptr<AidlMethod>> methods_;
Steven Moreland693640b2018-07-19 13:46:27 -0700846 std::vector<std::unique_ptr<AidlConstantDeclaration>> constants_;
Casey Dahlinfb7da2e2015-10-08 17:26:09 -0700847
Casey Dahlin1ae2bc52015-10-07 18:49:10 -0700848 DISALLOW_COPY_AND_ASSIGN(AidlInterface);
Casey Dahlin0a2f8be2015-09-28 16:15:29 -0700849};
Adam Lesinskiffa16862014-01-23 18:17:42 -0800850
Casey Dahlin0edf3422015-10-07 12:34:59 -0700851class AidlImport : public AidlNode {
852 public:
Steven Moreland46e9da82018-07-27 15:45:29 -0700853 AidlImport(const AidlLocation& location, const std::string& needed_class);
Casey Dahlin0edf3422015-10-07 12:34:59 -0700854 virtual ~AidlImport() = default;
855
Casey Dahlin0edf3422015-10-07 12:34:59 -0700856 const std::string& GetFilename() const { return filename_; }
857 const std::string& GetNeededClass() const { return needed_class_; }
Casey Dahlin0edf3422015-10-07 12:34:59 -0700858
859 private:
Casey Dahlin0edf3422015-10-07 12:34:59 -0700860 std::string filename_;
861 std::string needed_class_;
Casey Dahlin0edf3422015-10-07 12:34:59 -0700862
863 DISALLOW_COPY_AND_ASSIGN(AidlImport);
Casey Dahline2507492015-09-14 17:11:20 -0700864};
865
866class Parser {
Casey Dahlindd691812015-09-09 17:59:06 -0700867 public:
Casey Dahline2507492015-09-14 17:11:20 -0700868 ~Parser();
Casey Dahlindd691812015-09-09 17:59:06 -0700869
Steven Moreland64e29be2018-08-08 18:52:19 -0700870 // Parse contents of file |filename|. Should only be called once.
871 static std::unique_ptr<Parser> Parse(const std::string& filename,
872 const android::aidl::IoDelegate& io_delegate,
873 AidlTypenames& typenames);
Casey Dahlin89d44842015-09-24 18:45:54 -0700874
Steven Moreland2ca4fcb2018-06-27 16:01:01 -0700875 void AddError() { error_++; }
Steven Moreland64e29be2018-08-08 18:52:19 -0700876 bool HasError() { return error_ != 0; }
Casey Dahlindd691812015-09-09 17:59:06 -0700877
Casey Dahlin3c6df362015-10-06 15:48:35 -0700878 const std::string& FileName() const { return filename_; }
Casey Dahlin42727f82015-10-12 19:23:40 -0700879 void* Scanner() const { return scanner_; }
Casey Dahlindd691812015-09-09 17:59:06 -0700880
Steven Morelandd1039a92020-01-23 09:49:43 -0800881 void AddImport(std::unique_ptr<AidlImport>&& import);
Christopher Wileyd76067c2015-10-19 17:00:13 -0700882 const std::vector<std::unique_ptr<AidlImport>>& GetImports() {
883 return imports_;
884 }
Casey Dahlindd691812015-09-09 17:59:06 -0700885
Jiyong Parkb034bf02018-07-30 17:44:33 +0900886 void SetPackage(unique_ptr<AidlQualifiedName> name) { package_ = std::move(name); }
887 std::vector<std::string> Package() const;
Jiyong Park1deecc32018-07-17 01:14:41 +0900888
889 void DeferResolution(AidlTypeSpecifier* typespec) {
890 unresolved_typespecs_.emplace_back(typespec);
891 }
892
Jiyong Parke59c3682018-09-11 23:10:25 +0900893 const vector<AidlTypeSpecifier*>& GetUnresolvedTypespecs() const { return unresolved_typespecs_; }
894
Jiyong Park1deecc32018-07-17 01:14:41 +0900895 bool Resolve();
896
Jiyong Parkb034bf02018-07-30 17:44:33 +0900897 void AddDefinedType(unique_ptr<AidlDefinedType> type) {
898 // Parser does NOT own AidlDefinedType, it just has references to the types
899 // that it encountered while parsing the input file.
900 defined_types_.emplace_back(type.get());
901
902 // AidlDefinedType IS owned by AidlTypenames
903 if (!typenames_.AddDefinedType(std::move(type))) {
904 AddError();
905 }
906 }
907
908 vector<AidlDefinedType*>& GetDefinedTypes() { return defined_types_; }
909
Casey Dahlindd691812015-09-09 17:59:06 -0700910 private:
Steven Moreland64e29be2018-08-08 18:52:19 -0700911 explicit Parser(const std::string& filename, std::string& raw_buffer,
912 android::aidl::AidlTypenames& typenames);
913
Casey Dahlindd691812015-09-09 17:59:06 -0700914 std::string filename_;
Christopher Wileyd76067c2015-10-19 17:00:13 -0700915 std::unique_ptr<AidlQualifiedName> package_;
Jiyong Parkb034bf02018-07-30 17:44:33 +0900916 AidlTypenames& typenames_;
Steven Moreland64e29be2018-08-08 18:52:19 -0700917
918 void* scanner_ = nullptr;
919 YY_BUFFER_STATE buffer_;
920 int error_ = 0;
921
922 std::vector<std::unique_ptr<AidlImport>> imports_;
Jiyong Parkb034bf02018-07-30 17:44:33 +0900923 vector<AidlDefinedType*> defined_types_;
Jiyong Park1deecc32018-07-17 01:14:41 +0900924 vector<AidlTypeSpecifier*> unresolved_typespecs_;
Casey Dahlindd691812015-09-09 17:59:06 -0700925
Casey Dahline2507492015-09-14 17:11:20 -0700926 DISALLOW_COPY_AND_ASSIGN(Parser);
Casey Dahlindd691812015-09-09 17:59:06 -0700927};