blob: 97c8ba38be8ce63f6ce369dac6f510255e0bc8be [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 Moreland46e9da82018-07-27 15:45:29 -0700113 friend class AidlError;
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.
128class AidlError {
129 public:
Steven Moreland92c55f12018-07-31 14:08:37 -0700130 AidlError(bool fatal, const std::string& filename) : AidlError(fatal) { os_ << filename << ": "; }
Devin Mooredf93ebb2020-03-25 14:03:35 -0700131 AidlError(bool fatal, const AidlLocation& location);
Steven Moreland92c55f12018-07-31 14:08:37 -0700132 AidlError(bool fatal, const AidlNode& node) : AidlError(fatal, node.location_) {}
133 AidlError(bool fatal, const AidlNode* node) : AidlError(fatal, *node) {}
134
135 template <typename T>
136 AidlError(bool fatal, const std::unique_ptr<T>& node) : AidlError(fatal, *node) {}
Steven Moreland46e9da82018-07-27 15:45:29 -0700137 ~AidlError() {
138 os_ << std::endl;
139 if (fatal_) abort();
140 }
141
142 std::ostream& os_;
143
Steven Morelandfdb57cd2020-01-08 20:03:30 -0800144 static bool hadError() { return sHadError; }
145
Steven Moreland46e9da82018-07-27 15:45:29 -0700146 private:
Steven Moreland92c55f12018-07-31 14:08:37 -0700147 AidlError(bool fatal);
Steven Moreland46e9da82018-07-27 15:45:29 -0700148
149 bool fatal_;
150
Steven Morelandfdb57cd2020-01-08 20:03:30 -0800151 static bool sHadError;
152
Steven Moreland46e9da82018-07-27 15:45:29 -0700153 DISALLOW_COPY_AND_ASSIGN(AidlError);
154};
155
Steven Moreland92c55f12018-07-31 14:08:37 -0700156#define AIDL_ERROR(CONTEXT) ::AidlError(false /*fatal*/, (CONTEXT)).os_
157#define AIDL_FATAL(CONTEXT) ::AidlError(true /*fatal*/, (CONTEXT)).os_
Steven Moreland3f658cf2018-08-20 13:40:54 -0700158#define AIDL_FATAL_IF(CONDITION, CONTEXT) \
159 if (CONDITION) AIDL_FATAL(CONTEXT) << "Bad internal state: " << #CONDITION << ": "
Steven Moreland46e9da82018-07-27 15:45:29 -0700160
Casey Dahlina2f77c42015-12-01 18:26:02 -0800161namespace android {
162namespace aidl {
163
Jiyong Park1deecc32018-07-17 01:14:41 +0900164class AidlTypenames;
Casey Dahlina2f77c42015-12-01 18:26:02 -0800165
166} // namespace aidl
167} // namespace android
168
Jeongik Chadf76dc72019-11-28 00:08:47 +0900169// unique_ptr<AidlTypeSpecifier> for type arugment,
170// std::string for type parameter(T, U, and so on).
171template <typename T>
172class AidlParameterizable {
173 public:
174 AidlParameterizable(std::vector<T>* type_params) : type_params_(type_params) {}
175 virtual ~AidlParameterizable() = default;
176 bool IsGeneric() const { return type_params_ != nullptr; }
177 const std::vector<T>& GetTypeParameters() const { return *type_params_; }
178 bool CheckValid() const;
179
180 virtual const AidlNode& AsAidlNode() const = 0;
181
182 protected:
183 AidlParameterizable(const AidlParameterizable&);
184
185 private:
186 const unique_ptr<std::vector<T>> type_params_;
187 static_assert(std::is_same<T, unique_ptr<AidlTypeSpecifier>>::value ||
188 std::is_same<T, std::string>::value);
189};
190template <>
191bool AidlParameterizable<std::string>::CheckValid() const;
192
Andrei Onea9445fc62019-06-27 18:11:59 +0100193class AidlConstantValue;
194class AidlConstantDeclaration;
195
196// Transforms a value string into a language specific form. Raw value as produced by
197// AidlConstantValue.
198using ConstantValueDecorator =
199 std::function<std::string(const AidlTypeSpecifier& type, const std::string& raw_value)>;
200
Jiyong Park68bc77a2018-07-19 19:00:45 +0900201class AidlAnnotation : public AidlNode {
202 public:
Andrei Onea9445fc62019-06-27 18:11:59 +0100203 static AidlAnnotation* Parse(
204 const AidlLocation& location, const string& name,
205 std::map<std::string, std::shared_ptr<AidlConstantValue>>* parameter_list);
Steven Moreland46e9da82018-07-27 15:45:29 -0700206
Steven Moreland3f658cf2018-08-20 13:40:54 -0700207 AidlAnnotation(const AidlAnnotation&) = default;
Steven Moreland3be75772018-08-20 13:27:43 -0700208 AidlAnnotation(AidlAnnotation&&) = default;
Jiyong Park68bc77a2018-07-19 19:00:45 +0900209 virtual ~AidlAnnotation() = default;
Andrei Onea9445fc62019-06-27 18:11:59 +0100210 bool CheckValid() const;
Steven Moreland3be75772018-08-20 13:27:43 -0700211
Jiyong Park68bc77a2018-07-19 19:00:45 +0900212 const string& GetName() const { return name_; }
Daniel Norman37d43dd2019-09-09 17:22:34 -0700213 string ToString(const ConstantValueDecorator& decorator) const;
Andrei Onea9445fc62019-06-27 18:11:59 +0100214 std::map<std::string, std::string> AnnotationParams(
215 const ConstantValueDecorator& decorator) const;
Jiyong Parka6605ab2018-11-11 14:30:21 +0900216 const string& GetComments() const { return comments_; }
217 void SetComments(const string& comments) { comments_ = comments; }
Jiyong Park68bc77a2018-07-19 19:00:45 +0900218
219 private:
Steven Moreland46e9da82018-07-27 15:45:29 -0700220 AidlAnnotation(const AidlLocation& location, const string& name);
Andrei Onea9445fc62019-06-27 18:11:59 +0100221 AidlAnnotation(const AidlLocation& location, const string& name,
222 std::map<std::string, std::shared_ptr<AidlConstantValue>>&& parameters);
Jiyong Park68bc77a2018-07-19 19:00:45 +0900223 const string name_;
Jiyong Parka6605ab2018-11-11 14:30:21 +0900224 string comments_;
Andrei Onea9445fc62019-06-27 18:11:59 +0100225 std::map<std::string, std::shared_ptr<AidlConstantValue>> parameters_;
Jiyong Park68bc77a2018-07-19 19:00:45 +0900226};
227
Steven Moreland3be75772018-08-20 13:27:43 -0700228static inline bool operator<(const AidlAnnotation& lhs, const AidlAnnotation& rhs) {
229 return lhs.GetName() < rhs.GetName();
230}
231static inline bool operator==(const AidlAnnotation& lhs, const AidlAnnotation& rhs) {
232 return lhs.GetName() == rhs.GetName();
233}
Jiyong Park3656c3c2018-08-01 20:02:01 +0900234
Casey Dahline7922932016-02-29 17:23:01 -0800235class AidlAnnotatable : public AidlNode {
Casey Dahlin0ee37582015-09-30 16:31:55 -0700236 public:
Steven Moreland46e9da82018-07-27 15:45:29 -0700237 AidlAnnotatable(const AidlLocation& location);
Steven Moreland3f658cf2018-08-20 13:40:54 -0700238
239 AidlAnnotatable(const AidlAnnotatable&) = default;
240 AidlAnnotatable(AidlAnnotatable&&) = default;
Casey Dahline7922932016-02-29 17:23:01 -0800241 virtual ~AidlAnnotatable() = default;
242
Artur Satayev91fe8712019-07-29 13:06:01 +0100243 void Annotate(vector<AidlAnnotation>&& annotations) {
244 for (auto& annotation : annotations) {
245 annotations_.emplace_back(std::move(annotation));
246 }
247 }
Jiyong Park68bc77a2018-07-19 19:00:45 +0900248 bool IsNullable() const;
Jiyong Park68bc77a2018-07-19 19:00:45 +0900249 bool IsUtf8InCpp() const;
Steven Morelanda57d0a62019-07-30 09:41:14 -0700250 bool IsVintfStability() const;
Jeongik Cha88f95a82020-01-15 13:02:16 +0900251 bool IsStableApiParcelable(Options::Language lang) const;
Makoto Onuki78a1c1c2020-03-04 16:57:23 -0800252 bool IsHide() const;
Andrei Onea9445fc62019-06-27 18:11:59 +0100253
Steven Moreland7e4b9502020-02-20 18:10:42 -0800254 void DumpAnnotations(CodeWriter* writer) const;
255
Andrei Onea9445fc62019-06-27 18:11:59 +0100256 const AidlAnnotation* UnsupportedAppUsage() const;
Daniel Norman716d3112019-09-10 13:11:56 -0700257 const AidlTypeSpecifier* BackingType(const AidlTypenames& typenames) const;
Jiyong Park68bc77a2018-07-19 19:00:45 +0900258 std::string ToString() const;
Casey Dahline7922932016-02-29 17:23:01 -0800259
Jiyong Parka6605ab2018-11-11 14:30:21 +0900260 const vector<AidlAnnotation>& GetAnnotations() const { return annotations_; }
Devin Moore24f68572020-02-26 13:20:59 -0800261 virtual std::set<string> GetSupportedAnnotations() const = 0;
262 virtual bool CheckValid(const AidlTypenames&) const;
Jiyong Park3656c3c2018-08-01 20:02:01 +0900263
Casey Dahline7922932016-02-29 17:23:01 -0800264 private:
Jiyong Parka6605ab2018-11-11 14:30:21 +0900265 vector<AidlAnnotation> annotations_;
Casey Dahline7922932016-02-29 17:23:01 -0800266};
267
Jiyong Park1deecc32018-07-17 01:14:41 +0900268class AidlQualifiedName;
269
270// AidlTypeSpecifier represents a reference to either a built-in type,
271// a defined type, or a variant (e.g., array of generic) of a type.
Jeongik Chadf76dc72019-11-28 00:08:47 +0900272class AidlTypeSpecifier final : public AidlAnnotatable,
273 public AidlParameterizable<unique_ptr<AidlTypeSpecifier>> {
Casey Dahline7922932016-02-29 17:23:01 -0800274 public:
Steven Moreland46e9da82018-07-27 15:45:29 -0700275 AidlTypeSpecifier(const AidlLocation& location, const string& unresolved_name, bool is_array,
276 vector<unique_ptr<AidlTypeSpecifier>>* type_params, const string& comments);
Jiyong Parkd59a10d2018-07-18 11:12:55 +0900277 virtual ~AidlTypeSpecifier() = default;
Casey Dahlin0ee37582015-09-30 16:31:55 -0700278
Steven Moreland3f658cf2018-08-20 13:40:54 -0700279 // Copy of this type which is not an array.
280 AidlTypeSpecifier ArrayBase() const;
281
Jiyong Park1deecc32018-07-17 01:14:41 +0900282 // Returns the full-qualified name of the base type.
283 // int -> int
284 // int[] -> int
285 // List<String> -> List
286 // IFoo -> foo.bar.IFoo (if IFoo is in package foo.bar)
287 const string& GetName() const {
288 if (IsResolved()) {
289 return fully_qualified_name_;
290 } else {
291 return GetUnresolvedName();
292 }
293 }
Casey Dahlin0ee37582015-09-30 16:31:55 -0700294
Jiyong Park1deecc32018-07-17 01:14:41 +0900295 // Returns string representation of this type specifier.
Artur Satayev91fe8712019-07-29 13:06:01 +0100296 // This is GetBaseTypeName() + array modifier or generic type parameters
Jiyong Park1deecc32018-07-17 01:14:41 +0900297 string ToString() const;
298
Jiyong Park02da7422018-07-16 16:00:26 +0900299 std::string Signature() const;
300
Jiyong Park1deecc32018-07-17 01:14:41 +0900301 const string& GetUnresolvedName() const { return unresolved_name_; }
302
Jeongik Cha997281d2020-01-16 15:23:59 +0900303 bool IsHidden() const;
304
Jiyong Park1deecc32018-07-17 01:14:41 +0900305 const string& GetComments() const { return comments_; }
306
Jeongik Cha1a7ab642019-07-29 17:31:02 +0900307 const std::vector<std::string> GetSplitName() const { return split_name_; }
308
Jiyong Parka6605ab2018-11-11 14:30:21 +0900309 void SetComments(const string& comment) { comments_ = comment; }
310
Jiyong Park1deecc32018-07-17 01:14:41 +0900311 bool IsResolved() const { return fully_qualified_name_ != ""; }
312
313 bool IsArray() const { return is_array_; }
314
Jiyong Park1deecc32018-07-17 01:14:41 +0900315 // Resolve the base type name to a fully-qualified name. Return false if the
316 // resolution fails.
Daniel Norman716d3112019-09-10 13:11:56 -0700317 bool Resolve(const AidlTypenames& typenames);
Casey Dahlin0ee37582015-09-30 16:31:55 -0700318
Devin Moore24f68572020-02-26 13:20:59 -0800319 std::set<string> GetSupportedAnnotations() const override;
320 bool CheckValid(const AidlTypenames& typenames) const override;
Jeongik Cha047c5ee2019-08-07 23:16:49 +0900321 bool LanguageSpecificCheckValid(Options::Language lang) const;
Jeongik Chadf76dc72019-11-28 00:08:47 +0900322 const AidlNode& AsAidlNode() const override { return *this; }
Jiyong Park1d2df7d2018-07-23 15:22:50 +0900323
Casey Dahlin0ee37582015-09-30 16:31:55 -0700324 private:
Steven Moreland3f658cf2018-08-20 13:40:54 -0700325 AidlTypeSpecifier(const AidlTypeSpecifier&) = default;
326
Jiyong Park1deecc32018-07-17 01:14:41 +0900327 const string unresolved_name_;
328 string fully_qualified_name_;
Steven Moreland3f658cf2018-08-20 13:40:54 -0700329 bool is_array_;
Jiyong Parka6605ab2018-11-11 14:30:21 +0900330 string comments_;
Jeongik Cha1a7ab642019-07-29 17:31:02 +0900331 vector<string> split_name_;
Casey Dahlin0ee37582015-09-30 16:31:55 -0700332};
333
Steven Moreland860b1942018-08-16 14:59:28 -0700334// Returns the universal value unaltered.
335std::string AidlConstantValueDecorator(const AidlTypeSpecifier& type, const std::string& raw_value);
336
Steven Moreland9ea10e32018-07-19 15:26:09 -0700337class AidlConstantValue;
Steven Moreland5557f1c2018-07-02 13:50:23 -0700338class AidlVariableDeclaration : public AidlNode {
339 public:
Steven Moreland46e9da82018-07-27 15:45:29 -0700340 AidlVariableDeclaration(const AidlLocation& location, AidlTypeSpecifier* type,
341 const std::string& name);
342 AidlVariableDeclaration(const AidlLocation& location, AidlTypeSpecifier* type,
343 const std::string& name, AidlConstantValue* default_value);
Steven Moreland5557f1c2018-07-02 13:50:23 -0700344 virtual ~AidlVariableDeclaration() = default;
345
346 std::string GetName() const { return name_; }
Jiyong Parkd59a10d2018-07-18 11:12:55 +0900347 const AidlTypeSpecifier& GetType() const { return *type_; }
Steven Moreland9ea10e32018-07-19 15:26:09 -0700348 const AidlConstantValue* GetDefaultValue() const { return default_value_.get(); }
349
Jiyong Parkd59a10d2018-07-18 11:12:55 +0900350 AidlTypeSpecifier* GetMutableType() { return type_.get(); }
Steven Moreland5557f1c2018-07-02 13:50:23 -0700351
Jeongik Chadb0f59e2018-11-01 18:11:21 +0900352 bool CheckValid(const AidlTypenames& typenames) const;
Steven Moreland5557f1c2018-07-02 13:50:23 -0700353 std::string ToString() const;
Jiyong Park02da7422018-07-16 16:00:26 +0900354 std::string Signature() const;
Steven Moreland5557f1c2018-07-02 13:50:23 -0700355
Steven Moreland860b1942018-08-16 14:59:28 -0700356 std::string ValueString(const ConstantValueDecorator& decorator) const;
Steven Moreland25294322018-08-07 18:13:55 -0700357
Steven Moreland5557f1c2018-07-02 13:50:23 -0700358 private:
Jiyong Parkd59a10d2018-07-18 11:12:55 +0900359 std::unique_ptr<AidlTypeSpecifier> type_;
Steven Moreland5557f1c2018-07-02 13:50:23 -0700360 std::string name_;
Steven Moreland9ea10e32018-07-19 15:26:09 -0700361 std::unique_ptr<AidlConstantValue> default_value_;
Steven Moreland5557f1c2018-07-02 13:50:23 -0700362
363 DISALLOW_COPY_AND_ASSIGN(AidlVariableDeclaration);
364};
365
366class AidlArgument : public AidlVariableDeclaration {
Casey Dahlinbc7a50a2015-09-28 19:20:50 -0700367 public:
Casey Dahlinc378c992015-09-29 16:50:40 -0700368 enum Direction { IN_DIR = 1, OUT_DIR = 2, INOUT_DIR = 3 };
369
Steven Moreland46e9da82018-07-27 15:45:29 -0700370 AidlArgument(const AidlLocation& location, AidlArgument::Direction direction,
371 AidlTypeSpecifier* type, const std::string& name);
372 AidlArgument(const AidlLocation& location, AidlTypeSpecifier* type, const std::string& name);
Casey Dahlinbc7a50a2015-09-28 19:20:50 -0700373 virtual ~AidlArgument() = default;
374
Casey Dahlinc378c992015-09-29 16:50:40 -0700375 Direction GetDirection() const { return direction_; }
Christopher Wileyad339272015-10-05 19:11:58 -0700376 bool IsOut() const { return direction_ & OUT_DIR; }
377 bool IsIn() const { return direction_ & IN_DIR; }
Casey Dahlinc378c992015-09-29 16:50:40 -0700378 bool DirectionWasSpecified() const { return direction_specified_; }
Jiyong Park3656c3c2018-08-01 20:02:01 +0900379 string GetDirectionSpecifier() const;
Christopher Wileyad339272015-10-05 19:11:58 -0700380
Casey Dahlinc378c992015-09-29 16:50:40 -0700381 std::string ToString() const;
Jiyong Park02da7422018-07-16 16:00:26 +0900382 std::string Signature() const;
Casey Dahlinc378c992015-09-29 16:50:40 -0700383
Casey Dahlinbc7a50a2015-09-28 19:20:50 -0700384 private:
Casey Dahlinc378c992015-09-29 16:50:40 -0700385 Direction direction_;
386 bool direction_specified_;
387
Casey Dahlinbc7a50a2015-09-28 19:20:50 -0700388 DISALLOW_COPY_AND_ASSIGN(AidlArgument);
Casey Dahlina834dd42015-09-23 11:52:15 -0700389};
Adam Lesinskiffa16862014-01-23 18:17:42 -0800390
Casey Dahlind40e2fe2015-11-24 14:06:52 -0800391class AidlMethod;
Steven Moreland693640b2018-07-19 13:46:27 -0700392class AidlConstantDeclaration;
Daniel Norman85aed542019-08-21 12:01:14 -0700393class AidlEnumDeclaration;
Casey Dahlind40e2fe2015-11-24 14:06:52 -0800394class AidlMember : public AidlNode {
395 public:
Steven Moreland46e9da82018-07-27 15:45:29 -0700396 AidlMember(const AidlLocation& location);
Casey Dahlind40e2fe2015-11-24 14:06:52 -0800397 virtual ~AidlMember() = default;
398
399 virtual AidlMethod* AsMethod() { return nullptr; }
Steven Moreland693640b2018-07-19 13:46:27 -0700400 virtual AidlConstantDeclaration* AsConstantDeclaration() { return nullptr; }
Casey Dahlind40e2fe2015-11-24 14:06:52 -0800401
402 private:
403 DISALLOW_COPY_AND_ASSIGN(AidlMember);
404};
405
Will McVickerd7d18df2019-09-12 13:40:50 -0700406class AidlUnaryConstExpression;
407class AidlBinaryConstExpression;
408
Steven Moreland693640b2018-07-19 13:46:27 -0700409class AidlConstantValue : public AidlNode {
Casey Dahlind40e2fe2015-11-24 14:06:52 -0800410 public:
Will McVickerd7d18df2019-09-12 13:40:50 -0700411 enum class Type {
412 // WARNING: Don't change this order! The order is used to determine type
413 // promotion during a binary expression.
414 BOOLEAN,
415 INT8,
416 INT32,
417 INT64,
418 ARRAY,
419 CHARACTER,
420 STRING,
421 FLOATING,
422 UNARY,
423 BINARY,
424 ERROR,
425 };
426
427 /*
428 * Return the value casted to the given type.
429 */
430 template <typename T>
431 T cast() const;
Casey Dahlind40e2fe2015-11-24 14:06:52 -0800432
Steven Moreland693640b2018-07-19 13:46:27 -0700433 virtual ~AidlConstantValue() = default;
Casey Dahlind40e2fe2015-11-24 14:06:52 -0800434
Steven Moreland25294322018-08-07 18:13:55 -0700435 static AidlConstantValue* Boolean(const AidlLocation& location, bool value);
436 static AidlConstantValue* Character(const AidlLocation& location, char value);
Steven Moreland25294322018-08-07 18:13:55 -0700437 // example: 123, -5498, maybe any size
Will McVickerd7d18df2019-09-12 13:40:50 -0700438 static AidlConstantValue* Integral(const AidlLocation& location, const string& value);
439 static AidlConstantValue* Floating(const AidlLocation& location, const std::string& value);
Steven Moreland860b1942018-08-16 14:59:28 -0700440 static AidlConstantValue* Array(const AidlLocation& location,
Will McVickerd7d18df2019-09-12 13:40:50 -0700441 std::unique_ptr<vector<unique_ptr<AidlConstantValue>>> values);
Steven Moreland693640b2018-07-19 13:46:27 -0700442 // example: "\"asdf\""
Will McVickerd7d18df2019-09-12 13:40:50 -0700443 static AidlConstantValue* String(const AidlLocation& location, const string& value);
Steven Moreland693640b2018-07-19 13:46:27 -0700444
Daniel Normanf0ca44f2019-10-25 09:59:44 -0700445 // Construct an AidlConstantValue by evaluating the other integral constant's
446 // value string. This does not preserve the structure of the copied constant.
Steven Moreland59e53e42019-11-26 20:38:08 -0800447 // Returns nullptr and logs if value cannot be copied.
Daniel Normanf0ca44f2019-10-25 09:59:44 -0700448 static AidlConstantValue* ShallowIntegralCopy(const AidlConstantValue& other);
Daniel Normanb28684e2019-10-17 15:31:39 -0700449
Will McVickerd7d18df2019-09-12 13:40:50 -0700450 Type GetType() const { return final_type_; }
Steven Moreland25294322018-08-07 18:13:55 -0700451
Will McVickerd7d18df2019-09-12 13:40:50 -0700452 virtual bool CheckValid() const;
Steven Moreland860b1942018-08-16 14:59:28 -0700453
454 // Raw value of type (currently valid in C++ and Java). Empty string on error.
Steven Moreland4bcb05c2019-11-27 18:57:47 -0800455 string ValueString(const AidlTypeSpecifier& type, const ConstantValueDecorator& decorator) const;
Casey Dahlind40e2fe2015-11-24 14:06:52 -0800456
457 private:
Will McVickerd7d18df2019-09-12 13:40:50 -0700458 AidlConstantValue(const AidlLocation& location, Type parsed_type, int64_t parsed_value,
459 const string& checked_value);
460 AidlConstantValue(const AidlLocation& location, Type type, const string& checked_value);
Steven Moreland860b1942018-08-16 14:59:28 -0700461 AidlConstantValue(const AidlLocation& location, Type type,
Will McVickerd7d18df2019-09-12 13:40:50 -0700462 std::unique_ptr<vector<unique_ptr<AidlConstantValue>>> values);
Steven Moreland25294322018-08-07 18:13:55 -0700463 static string ToString(Type type);
Will McVickerd7d18df2019-09-12 13:40:50 -0700464 static bool ParseIntegral(const string& value, int64_t* parsed_value, Type* parsed_type);
465 static bool IsHex(const string& value);
Steven Moreland4bcb05c2019-11-27 18:57:47 -0800466
Will McVickerd7d18df2019-09-12 13:40:50 -0700467 virtual bool evaluate(const AidlTypeSpecifier& type) const;
Casey Dahlind40e2fe2015-11-24 14:06:52 -0800468
Steven Moreland693640b2018-07-19 13:46:27 -0700469 const Type type_ = Type::ERROR;
Will McVickerd7d18df2019-09-12 13:40:50 -0700470 const vector<unique_ptr<AidlConstantValue>> values_; // if type_ == ARRAY
471 const string value_; // otherwise
472
473 // State for tracking evaluation of expressions
Steven Moreland4bcb05c2019-11-27 18:57:47 -0800474 mutable bool is_valid_ = false; // cache of CheckValid, but may be marked false in evaluate
475 mutable bool is_evaluated_ = false; // whether evaluate has been called
Will McVickerd7d18df2019-09-12 13:40:50 -0700476 mutable Type final_type_;
477 mutable int64_t final_value_;
478 mutable string final_string_value_ = "";
Steven Moreland693640b2018-07-19 13:46:27 -0700479
480 DISALLOW_COPY_AND_ASSIGN(AidlConstantValue);
Will McVickerd7d18df2019-09-12 13:40:50 -0700481
482 friend AidlUnaryConstExpression;
483 friend AidlBinaryConstExpression;
484};
485
486class AidlUnaryConstExpression : public AidlConstantValue {
487 public:
488 AidlUnaryConstExpression(const AidlLocation& location, const string& op,
489 std::unique_ptr<AidlConstantValue> rval);
490
491 static bool IsCompatibleType(Type type, const string& op);
492 bool CheckValid() const override;
Will McVickerd7d18df2019-09-12 13:40:50 -0700493 private:
494 bool evaluate(const AidlTypeSpecifier& type) const override;
495
496 std::unique_ptr<AidlConstantValue> unary_;
497 const string op_;
498};
499
500class AidlBinaryConstExpression : public AidlConstantValue {
501 public:
502 AidlBinaryConstExpression(const AidlLocation& location, std::unique_ptr<AidlConstantValue> lval,
503 const string& op, std::unique_ptr<AidlConstantValue> rval);
504
505 bool CheckValid() const override;
Will McVickerd7d18df2019-09-12 13:40:50 -0700506
507 static bool AreCompatibleTypes(Type t1, Type t2);
508 // Returns the promoted kind for both operands
509 static Type UsualArithmeticConversion(Type left, Type right);
510 // Returns the promoted integral type where INT32 is the smallest type
511 static Type IntegralPromotion(Type in);
512
513 private:
514 bool evaluate(const AidlTypeSpecifier& type) const override;
515
516 std::unique_ptr<AidlConstantValue> left_val_;
517 std::unique_ptr<AidlConstantValue> right_val_;
518 const string op_;
Christopher Wileyd6bdd8d2016-05-03 11:23:13 -0700519};
520
Andrei Onea9445fc62019-06-27 18:11:59 +0100521struct AidlAnnotationParameter {
522 std::string name;
523 std::unique_ptr<AidlConstantValue> value;
524};
525
Steven Moreland693640b2018-07-19 13:46:27 -0700526class AidlConstantDeclaration : public AidlMember {
Christopher Wileyd6bdd8d2016-05-03 11:23:13 -0700527 public:
Steven Moreland46e9da82018-07-27 15:45:29 -0700528 AidlConstantDeclaration(const AidlLocation& location, AidlTypeSpecifier* specifier,
Will McVickerd7d18df2019-09-12 13:40:50 -0700529 const string& name, AidlConstantValue* value);
Steven Moreland693640b2018-07-19 13:46:27 -0700530 virtual ~AidlConstantDeclaration() = default;
Christopher Wileyd6bdd8d2016-05-03 11:23:13 -0700531
Steven Moreland693640b2018-07-19 13:46:27 -0700532 const AidlTypeSpecifier& GetType() const { return *type_; }
Steven Moreland4d12f9a2018-10-31 14:30:55 -0700533 AidlTypeSpecifier* GetMutableType() { return type_.get(); }
Will McVickerd7d18df2019-09-12 13:40:50 -0700534 const string& GetName() const { return name_; }
Steven Moreland693640b2018-07-19 13:46:27 -0700535 const AidlConstantValue& GetValue() const { return *value_; }
Jeongik Chadb0f59e2018-11-01 18:11:21 +0900536 bool CheckValid(const AidlTypenames& typenames) const;
Christopher Wileyd6bdd8d2016-05-03 11:23:13 -0700537
Will McVickerd7d18df2019-09-12 13:40:50 -0700538 string ToString() const;
539 string Signature() const;
Steven Moreland860b1942018-08-16 14:59:28 -0700540 string ValueString(const ConstantValueDecorator& decorator) const {
Will McVickerd7d18df2019-09-12 13:40:50 -0700541 return value_->ValueString(GetType(), decorator);
Steven Moreland860b1942018-08-16 14:59:28 -0700542 }
Steven Moreland25294322018-08-07 18:13:55 -0700543
Steven Moreland693640b2018-07-19 13:46:27 -0700544 AidlConstantDeclaration* AsConstantDeclaration() override { return this; }
Christopher Wileyd6bdd8d2016-05-03 11:23:13 -0700545
546 private:
Steven Moreland693640b2018-07-19 13:46:27 -0700547 const unique_ptr<AidlTypeSpecifier> type_;
Will McVickerd7d18df2019-09-12 13:40:50 -0700548 const string name_;
549 unique_ptr<AidlConstantValue> value_;
Christopher Wileyd6bdd8d2016-05-03 11:23:13 -0700550
Steven Moreland693640b2018-07-19 13:46:27 -0700551 DISALLOW_COPY_AND_ASSIGN(AidlConstantDeclaration);
Casey Dahlind40e2fe2015-11-24 14:06:52 -0800552};
553
554class AidlMethod : public AidlMember {
Casey Dahlin5c69deb2015-10-01 14:44:12 -0700555 public:
Will McVickerd7d18df2019-09-12 13:40:50 -0700556 AidlMethod(const AidlLocation& location, bool oneway, AidlTypeSpecifier* type, const string& name,
557 vector<unique_ptr<AidlArgument>>* args, const string& comments);
558 AidlMethod(const AidlLocation& location, bool oneway, AidlTypeSpecifier* type, const string& name,
559 vector<unique_ptr<AidlArgument>>* args, const string& comments, int id,
560 bool is_user_defined = true);
Casey Dahlin5c69deb2015-10-01 14:44:12 -0700561 virtual ~AidlMethod() = default;
562
Casey Dahlind40e2fe2015-11-24 14:06:52 -0800563 AidlMethod* AsMethod() override { return this; }
Jeongik Cha997281d2020-01-16 15:23:59 +0900564 bool IsHidden() const;
Will McVickerd7d18df2019-09-12 13:40:50 -0700565 const string& GetComments() const { return comments_; }
Jiyong Parkd59a10d2018-07-18 11:12:55 +0900566 const AidlTypeSpecifier& GetType() const { return *type_; }
567 AidlTypeSpecifier* GetMutableType() { return type_.get(); }
Steven Morelandacd53472018-12-14 10:17:26 -0800568
Steven Moreland8c70ba92018-12-17 10:20:31 -0800569 // set if this method is part of an interface that is marked oneway
570 void ApplyInterfaceOneway(bool oneway) { oneway_ = oneway_ || oneway; }
Casey Dahlinf4a93112015-10-05 16:58:09 -0700571 bool IsOneway() const { return oneway_; }
Steven Morelandacd53472018-12-14 10:17:26 -0800572
Casey Dahlinf4a93112015-10-05 16:58:09 -0700573 const std::string& GetName() const { return name_; }
Casey Dahlinf4a93112015-10-05 16:58:09 -0700574 bool HasId() const { return has_id_; }
Jiyong Parked65bf42018-08-28 15:43:27 +0900575 int GetId() const { return id_; }
Casey Dahlinf4a93112015-10-05 16:58:09 -0700576 void SetId(unsigned id) { id_ = id; }
Casey Dahlinf2d23f72015-10-02 16:19:19 -0700577
Jiyong Park309668e2018-07-28 16:55:44 +0900578 bool IsUserDefined() const { return is_user_defined_; }
579
Casey Dahlinf4a93112015-10-05 16:58:09 -0700580 const std::vector<std::unique_ptr<AidlArgument>>& GetArguments() const {
Christopher Wileyad339272015-10-05 19:11:58 -0700581 return arguments_;
582 }
583 // An inout parameter will appear in both GetInArguments()
584 // and GetOutArguments(). AidlMethod retains ownership of the argument
585 // pointers returned in this way.
586 const std::vector<const AidlArgument*>& GetInArguments() const {
587 return in_arguments_;
588 }
589 const std::vector<const AidlArgument*>& GetOutArguments() const {
590 return out_arguments_;
Casey Dahlinf4a93112015-10-05 16:58:09 -0700591 }
Casey Dahlin5c69deb2015-10-01 14:44:12 -0700592
Jiyong Park309668e2018-07-28 16:55:44 +0900593 // name + type parameter types
594 // i.e, foo(int, String)
Jiyong Park02da7422018-07-16 16:00:26 +0900595 std::string Signature() const;
596
Jiyong Park309668e2018-07-28 16:55:44 +0900597 // return type + name + type parameter types + annotations
598 // i.e, boolean foo(int, @Nullable String)
599 std::string ToString() const;
600
Casey Dahlin5c69deb2015-10-01 14:44:12 -0700601 private:
Casey Dahlinf4a93112015-10-05 16:58:09 -0700602 bool oneway_;
Casey Dahlinf2d23f72015-10-02 16:19:19 -0700603 std::string comments_;
Jiyong Parkd59a10d2018-07-18 11:12:55 +0900604 std::unique_ptr<AidlTypeSpecifier> type_;
Casey Dahlinf4a93112015-10-05 16:58:09 -0700605 std::string name_;
Christopher Wileyad339272015-10-05 19:11:58 -0700606 const std::vector<std::unique_ptr<AidlArgument>> arguments_;
607 std::vector<const AidlArgument*> in_arguments_;
608 std::vector<const AidlArgument*> out_arguments_;
Casey Dahlinf4a93112015-10-05 16:58:09 -0700609 bool has_id_;
610 int id_;
Jiyong Park309668e2018-07-28 16:55:44 +0900611 bool is_user_defined_ = true;
Casey Dahlinf2d23f72015-10-02 16:19:19 -0700612
Casey Dahlin5c69deb2015-10-01 14:44:12 -0700613 DISALLOW_COPY_AND_ASSIGN(AidlMethod);
Casey Dahlin0a2f8be2015-09-28 16:15:29 -0700614};
Adam Lesinskiffa16862014-01-23 18:17:42 -0800615
Steven Morelandc258abc2018-07-10 14:03:38 -0700616class AidlDefinedType;
Jiyong Parkb034bf02018-07-30 17:44:33 +0900617class AidlInterface;
618class AidlParcelable;
619class AidlStructuredParcelable;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800620
Casey Dahlin2b2879b2015-10-13 16:59:44 -0700621class AidlQualifiedName : public AidlNode {
622 public:
Steven Moreland46e9da82018-07-27 15:45:29 -0700623 AidlQualifiedName(const AidlLocation& location, const std::string& term,
624 const std::string& comments);
Casey Dahlin2b2879b2015-10-13 16:59:44 -0700625 virtual ~AidlQualifiedName() = default;
626
627 const std::vector<std::string>& GetTerms() const { return terms_; }
628 const std::string& GetComments() const { return comments_; }
629 std::string GetDotName() const { return android::base::Join(terms_, '.'); }
Ningyuan Wangd17c58b2016-09-29 14:33:14 -0700630 std::string GetColonName() const { return android::base::Join(terms_, "::"); }
Casey Dahlin2b2879b2015-10-13 16:59:44 -0700631
Chih-Hung Hsiehf05cc262016-07-27 11:42:51 -0700632 void AddTerm(const std::string& term);
Casey Dahlin2b2879b2015-10-13 16:59:44 -0700633
634 private:
635 std::vector<std::string> terms_;
636 std::string comments_;
637
638 DISALLOW_COPY_AND_ASSIGN(AidlQualifiedName);
639};
640
Steven Moreland46e9da82018-07-27 15:45:29 -0700641class AidlInterface;
642class AidlParcelable;
643class AidlStructuredParcelable;
Daniel Norman85aed542019-08-21 12:01:14 -0700644// AidlDefinedType represents either an interface, parcelable, or enum that is
Jiyong Park1deecc32018-07-17 01:14:41 +0900645// defined in the source file.
646class AidlDefinedType : public AidlAnnotatable {
Steven Moreland787b0432018-07-03 09:00:58 -0700647 public:
Steven Moreland46e9da82018-07-27 15:45:29 -0700648 AidlDefinedType(const AidlLocation& location, const std::string& name,
649 const std::string& comments, const std::vector<std::string>& package);
Steven Moreland787b0432018-07-03 09:00:58 -0700650 virtual ~AidlDefinedType() = default;
651
Jiyong Park1deecc32018-07-17 01:14:41 +0900652 const std::string& GetName() const { return name_; };
Jeongik Cha997281d2020-01-16 15:23:59 +0900653 bool IsHidden() const;
Jiyong Park1deecc32018-07-17 01:14:41 +0900654 const std::string& GetComments() const { return comments_; }
Jiyong Parka6605ab2018-11-11 14:30:21 +0900655 void SetComments(const std::string comments) { comments_ = comments; }
Jiyong Park1deecc32018-07-17 01:14:41 +0900656
Steven Moreland787b0432018-07-03 09:00:58 -0700657 /* dot joined package, example: "android.package.foo" */
658 std::string GetPackage() const;
659 /* dot joined package and name, example: "android.package.foo.IBar" */
660 std::string GetCanonicalName() const;
661 const std::vector<std::string>& GetSplitPackage() const { return package_; }
662
Steven Morelanded83a282018-07-17 13:27:29 -0700663 virtual std::string GetPreprocessDeclarationName() const = 0;
Steven Morelandc258abc2018-07-10 14:03:38 -0700664
Steven Moreland5557f1c2018-07-02 13:50:23 -0700665 virtual const AidlStructuredParcelable* AsStructuredParcelable() const { return nullptr; }
Steven Morelandc258abc2018-07-10 14:03:38 -0700666 virtual const AidlParcelable* AsParcelable() const { return nullptr; }
Daniel Norman85aed542019-08-21 12:01:14 -0700667 virtual const AidlEnumDeclaration* AsEnumDeclaration() const { return nullptr; }
Steven Moreland5557f1c2018-07-02 13:50:23 -0700668 virtual const AidlInterface* AsInterface() const { return nullptr; }
Jeongik Chadf76dc72019-11-28 00:08:47 +0900669 virtual const AidlParameterizable<std::string>* AsParameterizable() const { return nullptr; }
Devin Moore24f68572020-02-26 13:20:59 -0800670 bool CheckValid(const AidlTypenames& typenames) const override;
Jeongik Cha047c5ee2019-08-07 23:16:49 +0900671 virtual bool LanguageSpecificCheckValid(Options::Language lang) const = 0;
Steven Morelandc258abc2018-07-10 14:03:38 -0700672 AidlStructuredParcelable* AsStructuredParcelable() {
673 return const_cast<AidlStructuredParcelable*>(
674 const_cast<const AidlDefinedType*>(this)->AsStructuredParcelable());
675 }
676 AidlParcelable* AsParcelable() {
677 return const_cast<AidlParcelable*>(const_cast<const AidlDefinedType*>(this)->AsParcelable());
678 }
Daniel Norman85aed542019-08-21 12:01:14 -0700679 AidlEnumDeclaration* AsEnumDeclaration() {
680 return const_cast<AidlEnumDeclaration*>(
681 const_cast<const AidlDefinedType*>(this)->AsEnumDeclaration());
682 }
Steven Morelandc258abc2018-07-10 14:03:38 -0700683 AidlInterface* AsInterface() {
684 return const_cast<AidlInterface*>(const_cast<const AidlDefinedType*>(this)->AsInterface());
685 }
686
Jeongik Chadf76dc72019-11-28 00:08:47 +0900687 AidlParameterizable<std::string>* AsParameterizable() {
688 return const_cast<AidlParameterizable<std::string>*>(
689 const_cast<const AidlDefinedType*>(this)->AsParameterizable());
690 }
691
Steven Moreland6cee3482018-07-18 14:39:58 -0700692 const AidlParcelable* AsUnstructuredParcelable() const {
693 if (this->AsStructuredParcelable() != nullptr) return nullptr;
694 return this->AsParcelable();
695 }
696 AidlParcelable* AsUnstructuredParcelable() {
697 return const_cast<AidlParcelable*>(
698 const_cast<const AidlDefinedType*>(this)->AsUnstructuredParcelable());
699 }
700
Jeongik Cha997281d2020-01-16 15:23:59 +0900701 virtual void Dump(CodeWriter* writer) const = 0;
Steven Morelanda5d9c5c2020-02-21 16:01:09 -0800702 void DumpHeader(CodeWriter* writer) const;
Jiyong Park02da7422018-07-16 16:00:26 +0900703
Steven Moreland787b0432018-07-03 09:00:58 -0700704 private:
Jiyong Park1deecc32018-07-17 01:14:41 +0900705 std::string name_;
Jiyong Park1deecc32018-07-17 01:14:41 +0900706 std::string comments_;
Steven Moreland787b0432018-07-03 09:00:58 -0700707 const std::vector<std::string> package_;
Steven Moreland5557f1c2018-07-02 13:50:23 -0700708
709 DISALLOW_COPY_AND_ASSIGN(AidlDefinedType);
Steven Moreland787b0432018-07-03 09:00:58 -0700710};
711
Jeongik Chadf76dc72019-11-28 00:08:47 +0900712class AidlParcelable : public AidlDefinedType, public AidlParameterizable<std::string> {
Casey Dahlin1ae2bc52015-10-07 18:49:10 -0700713 public:
Steven Moreland46e9da82018-07-27 15:45:29 -0700714 AidlParcelable(const AidlLocation& location, AidlQualifiedName* name,
Jiyong Parka6605ab2018-11-11 14:30:21 +0900715 const std::vector<std::string>& package, const std::string& comments,
Jeongik Chadf76dc72019-11-28 00:08:47 +0900716 const std::string& cpp_header = "",
717 std::vector<std::string>* type_params = nullptr);
Casey Dahlin1ae2bc52015-10-07 18:49:10 -0700718 virtual ~AidlParcelable() = default;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800719
Ningyuan Wangd17c58b2016-09-29 14:33:14 -0700720 // C++ uses "::" instead of "." to refer to a inner class.
721 std::string GetCppName() const { return name_->GetColonName(); }
Christopher Wiley8aa4d9f2015-11-16 19:10:45 -0800722 std::string GetCppHeader() const { return cpp_header_; }
Christopher Wiley8aa4d9f2015-11-16 19:10:45 -0800723
Devin Moore24f68572020-02-26 13:20:59 -0800724 std::set<string> GetSupportedAnnotations() const override;
Jeongik Cha82317dd2019-02-27 20:26:11 +0900725 bool CheckValid(const AidlTypenames& typenames) const override;
Jeongik Cha047c5ee2019-08-07 23:16:49 +0900726 bool LanguageSpecificCheckValid(Options::Language lang) const override;
Steven Morelandc258abc2018-07-10 14:03:38 -0700727 const AidlParcelable* AsParcelable() const override { return this; }
Jeongik Chadf76dc72019-11-28 00:08:47 +0900728 const AidlParameterizable<std::string>* AsParameterizable() const override { return this; }
729 const AidlNode& AsAidlNode() const override { return *this; }
Steven Morelanded83a282018-07-17 13:27:29 -0700730 std::string GetPreprocessDeclarationName() const override { return "parcelable"; }
Steven Morelandc258abc2018-07-10 14:03:38 -0700731
Jeongik Cha997281d2020-01-16 15:23:59 +0900732 void Dump(CodeWriter* writer) const override;
Jiyong Park02da7422018-07-16 16:00:26 +0900733
Casey Dahlin1ae2bc52015-10-07 18:49:10 -0700734 private:
Christopher Wiley8aa4d9f2015-11-16 19:10:45 -0800735 std::unique_ptr<AidlQualifiedName> name_;
Christopher Wiley8aa4d9f2015-11-16 19:10:45 -0800736 std::string cpp_header_;
Casey Dahlin59401da2015-10-09 18:16:45 -0700737
Casey Dahlin1ae2bc52015-10-07 18:49:10 -0700738 DISALLOW_COPY_AND_ASSIGN(AidlParcelable);
Casey Dahlin0a2f8be2015-09-28 16:15:29 -0700739};
Adam Lesinskiffa16862014-01-23 18:17:42 -0800740
Steven Moreland5557f1c2018-07-02 13:50:23 -0700741class AidlStructuredParcelable : public AidlParcelable {
742 public:
Steven Moreland46e9da82018-07-27 15:45:29 -0700743 AidlStructuredParcelable(const AidlLocation& location, AidlQualifiedName* name,
Jiyong Parka6605ab2018-11-11 14:30:21 +0900744 const std::vector<std::string>& package, const std::string& comments,
Steven Moreland5557f1c2018-07-02 13:50:23 -0700745 std::vector<std::unique_ptr<AidlVariableDeclaration>>* variables);
746
747 const std::vector<std::unique_ptr<AidlVariableDeclaration>>& GetFields() const {
748 return variables_;
749 }
750
Steven Morelandc258abc2018-07-10 14:03:38 -0700751 const AidlStructuredParcelable* AsStructuredParcelable() const override { return this; }
Steven Morelanded83a282018-07-17 13:27:29 -0700752 std::string GetPreprocessDeclarationName() const override { return "structured_parcelable"; }
Steven Moreland5557f1c2018-07-02 13:50:23 -0700753
Jeongik Cha997281d2020-01-16 15:23:59 +0900754 void Dump(CodeWriter* writer) const override;
Jiyong Park02da7422018-07-16 16:00:26 +0900755
Devin Moore24f68572020-02-26 13:20:59 -0800756 std::set<string> GetSupportedAnnotations() const override;
Jeongik Chadb0f59e2018-11-01 18:11:21 +0900757 bool CheckValid(const AidlTypenames& typenames) const override;
Jeongik Cha047c5ee2019-08-07 23:16:49 +0900758 bool LanguageSpecificCheckValid(Options::Language lang) const override;
Jeongik Chadb0f59e2018-11-01 18:11:21 +0900759
Steven Moreland5557f1c2018-07-02 13:50:23 -0700760 private:
761 const std::vector<std::unique_ptr<AidlVariableDeclaration>> variables_;
762
763 DISALLOW_COPY_AND_ASSIGN(AidlStructuredParcelable);
764};
765
Daniel Norman85aed542019-08-21 12:01:14 -0700766class AidlEnumerator : public AidlNode {
767 public:
Daniel Norman2e4112d2019-10-03 10:22:35 -0700768 AidlEnumerator(const AidlLocation& location, const std::string& name, AidlConstantValue* value,
769 const std::string& comments);
Daniel Norman85aed542019-08-21 12:01:14 -0700770 virtual ~AidlEnumerator() = default;
771
772 const std::string& GetName() const { return name_; }
Will McVickerd7d18df2019-09-12 13:40:50 -0700773 AidlConstantValue* GetValue() const { return value_.get(); }
Daniel Norman2e4112d2019-10-03 10:22:35 -0700774 const std::string& GetComments() const { return comments_; }
Daniel Norman85aed542019-08-21 12:01:14 -0700775 bool CheckValid(const AidlTypeSpecifier& enum_backing_type) const;
776
777 string ValueString(const AidlTypeSpecifier& backing_type,
778 const ConstantValueDecorator& decorator) const;
779
Daniel Normanb28684e2019-10-17 15:31:39 -0700780 void SetValue(std::unique_ptr<AidlConstantValue> value) { value_ = std::move(value); }
781
Daniel Norman85aed542019-08-21 12:01:14 -0700782 private:
783 const std::string name_;
Will McVickerd7d18df2019-09-12 13:40:50 -0700784 unique_ptr<AidlConstantValue> value_;
Daniel Norman2e4112d2019-10-03 10:22:35 -0700785 const std::string comments_;
Daniel Norman85aed542019-08-21 12:01:14 -0700786
787 DISALLOW_COPY_AND_ASSIGN(AidlEnumerator);
788};
789
790class AidlEnumDeclaration : public AidlDefinedType {
791 public:
Will McVickerd7d18df2019-09-12 13:40:50 -0700792 AidlEnumDeclaration(const AidlLocation& location, const string& name,
Daniel Norman85aed542019-08-21 12:01:14 -0700793 std::vector<std::unique_ptr<AidlEnumerator>>* enumerators,
Daniel Norman2e4112d2019-10-03 10:22:35 -0700794 const std::vector<std::string>& package, const std::string& comments);
Daniel Norman85aed542019-08-21 12:01:14 -0700795 virtual ~AidlEnumDeclaration() = default;
796
797 void SetBackingType(std::unique_ptr<const AidlTypeSpecifier> type);
798 const AidlTypeSpecifier& GetBackingType() const { return *backing_type_; }
799 const std::vector<std::unique_ptr<AidlEnumerator>>& GetEnumerators() const {
800 return enumerators_;
801 }
Steven Moreland59e53e42019-11-26 20:38:08 -0800802 bool Autofill();
Devin Moore24f68572020-02-26 13:20:59 -0800803 std::set<string> GetSupportedAnnotations() const override;
Daniel Norman85aed542019-08-21 12:01:14 -0700804 bool CheckValid(const AidlTypenames& typenames) const override;
805 bool LanguageSpecificCheckValid(Options::Language) const override { return true; }
806 std::string GetPreprocessDeclarationName() const override { return "enum"; }
Jeongik Cha997281d2020-01-16 15:23:59 +0900807 void Dump(CodeWriter* writer) const override;
Daniel Norman85aed542019-08-21 12:01:14 -0700808
809 const AidlEnumDeclaration* AsEnumDeclaration() const override { return this; }
810
811 private:
812 const std::string name_;
813 const std::vector<std::unique_ptr<AidlEnumerator>> enumerators_;
814 std::unique_ptr<const AidlTypeSpecifier> backing_type_;
815
816 DISALLOW_COPY_AND_ASSIGN(AidlEnumDeclaration);
817};
818
Jiyong Park1deecc32018-07-17 01:14:41 +0900819class AidlInterface final : public AidlDefinedType {
Casey Dahlin1ae2bc52015-10-07 18:49:10 -0700820 public:
Steven Moreland46e9da82018-07-27 15:45:29 -0700821 AidlInterface(const AidlLocation& location, const std::string& name, const std::string& comments,
822 bool oneway_, std::vector<std::unique_ptr<AidlMember>>* members,
Christopher Wileyd76067c2015-10-19 17:00:13 -0700823 const std::vector<std::string>& package);
Casey Dahlin1ae2bc52015-10-07 18:49:10 -0700824 virtual ~AidlInterface() = default;
825
Casey Dahlinfb7da2e2015-10-08 17:26:09 -0700826 const std::vector<std::unique_ptr<AidlMethod>>& GetMethods() const
827 { return methods_; }
Jiyong Park309668e2018-07-28 16:55:44 +0900828 std::vector<std::unique_ptr<AidlMethod>>& GetMutableMethods() { return methods_; }
Steven Moreland693640b2018-07-19 13:46:27 -0700829 const std::vector<std::unique_ptr<AidlConstantDeclaration>>& GetConstantDeclarations() const {
830 return constants_;
831 }
Casey Dahlina2f77c42015-12-01 18:26:02 -0800832
Steven Morelandc258abc2018-07-10 14:03:38 -0700833 const AidlInterface* AsInterface() const override { return this; }
Steven Morelanded83a282018-07-17 13:27:29 -0700834 std::string GetPreprocessDeclarationName() const override { return "interface"; }
Steven Moreland5557f1c2018-07-02 13:50:23 -0700835
Jeongik Cha997281d2020-01-16 15:23:59 +0900836 void Dump(CodeWriter* writer) const override;
Jiyong Park02da7422018-07-16 16:00:26 +0900837
Devin Moore24f68572020-02-26 13:20:59 -0800838 std::set<string> GetSupportedAnnotations() const override;
Jeongik Chadb0f59e2018-11-01 18:11:21 +0900839 bool CheckValid(const AidlTypenames& typenames) const override;
Jeongik Cha047c5ee2019-08-07 23:16:49 +0900840 bool LanguageSpecificCheckValid(Options::Language lang) const override;
Jeongik Chadb0f59e2018-11-01 18:11:21 +0900841
Casey Dahlin1ae2bc52015-10-07 18:49:10 -0700842 private:
Casey Dahlinfb7da2e2015-10-08 17:26:09 -0700843 std::vector<std::unique_ptr<AidlMethod>> methods_;
Steven Moreland693640b2018-07-19 13:46:27 -0700844 std::vector<std::unique_ptr<AidlConstantDeclaration>> constants_;
Casey Dahlinfb7da2e2015-10-08 17:26:09 -0700845
Casey Dahlin1ae2bc52015-10-07 18:49:10 -0700846 DISALLOW_COPY_AND_ASSIGN(AidlInterface);
Casey Dahlin0a2f8be2015-09-28 16:15:29 -0700847};
Adam Lesinskiffa16862014-01-23 18:17:42 -0800848
Casey Dahlin0edf3422015-10-07 12:34:59 -0700849class AidlImport : public AidlNode {
850 public:
Steven Moreland46e9da82018-07-27 15:45:29 -0700851 AidlImport(const AidlLocation& location, const std::string& needed_class);
Casey Dahlin0edf3422015-10-07 12:34:59 -0700852 virtual ~AidlImport() = default;
853
Casey Dahlin0edf3422015-10-07 12:34:59 -0700854 const std::string& GetFilename() const { return filename_; }
855 const std::string& GetNeededClass() const { return needed_class_; }
Casey Dahlin0edf3422015-10-07 12:34:59 -0700856
857 private:
Casey Dahlin0edf3422015-10-07 12:34:59 -0700858 std::string filename_;
859 std::string needed_class_;
Casey Dahlin0edf3422015-10-07 12:34:59 -0700860
861 DISALLOW_COPY_AND_ASSIGN(AidlImport);
Casey Dahline2507492015-09-14 17:11:20 -0700862};
863
864class Parser {
Casey Dahlindd691812015-09-09 17:59:06 -0700865 public:
Casey Dahline2507492015-09-14 17:11:20 -0700866 ~Parser();
Casey Dahlindd691812015-09-09 17:59:06 -0700867
Steven Moreland64e29be2018-08-08 18:52:19 -0700868 // Parse contents of file |filename|. Should only be called once.
869 static std::unique_ptr<Parser> Parse(const std::string& filename,
870 const android::aidl::IoDelegate& io_delegate,
871 AidlTypenames& typenames);
Casey Dahlin89d44842015-09-24 18:45:54 -0700872
Steven Moreland2ca4fcb2018-06-27 16:01:01 -0700873 void AddError() { error_++; }
Steven Moreland64e29be2018-08-08 18:52:19 -0700874 bool HasError() { return error_ != 0; }
Casey Dahlindd691812015-09-09 17:59:06 -0700875
Casey Dahlin3c6df362015-10-06 15:48:35 -0700876 const std::string& FileName() const { return filename_; }
Casey Dahlin42727f82015-10-12 19:23:40 -0700877 void* Scanner() const { return scanner_; }
Casey Dahlindd691812015-09-09 17:59:06 -0700878
Steven Morelandd1039a92020-01-23 09:49:43 -0800879 void AddImport(std::unique_ptr<AidlImport>&& import);
Christopher Wileyd76067c2015-10-19 17:00:13 -0700880 const std::vector<std::unique_ptr<AidlImport>>& GetImports() {
881 return imports_;
882 }
Casey Dahlindd691812015-09-09 17:59:06 -0700883
Jiyong Parkb034bf02018-07-30 17:44:33 +0900884 void SetPackage(unique_ptr<AidlQualifiedName> name) { package_ = std::move(name); }
885 std::vector<std::string> Package() const;
Jiyong Park1deecc32018-07-17 01:14:41 +0900886
887 void DeferResolution(AidlTypeSpecifier* typespec) {
888 unresolved_typespecs_.emplace_back(typespec);
889 }
890
Jiyong Parke59c3682018-09-11 23:10:25 +0900891 const vector<AidlTypeSpecifier*>& GetUnresolvedTypespecs() const { return unresolved_typespecs_; }
892
Jiyong Park1deecc32018-07-17 01:14:41 +0900893 bool Resolve();
894
Jiyong Parkb034bf02018-07-30 17:44:33 +0900895 void AddDefinedType(unique_ptr<AidlDefinedType> type) {
896 // Parser does NOT own AidlDefinedType, it just has references to the types
897 // that it encountered while parsing the input file.
898 defined_types_.emplace_back(type.get());
899
900 // AidlDefinedType IS owned by AidlTypenames
901 if (!typenames_.AddDefinedType(std::move(type))) {
902 AddError();
903 }
904 }
905
906 vector<AidlDefinedType*>& GetDefinedTypes() { return defined_types_; }
907
Casey Dahlindd691812015-09-09 17:59:06 -0700908 private:
Steven Moreland64e29be2018-08-08 18:52:19 -0700909 explicit Parser(const std::string& filename, std::string& raw_buffer,
910 android::aidl::AidlTypenames& typenames);
911
Casey Dahlindd691812015-09-09 17:59:06 -0700912 std::string filename_;
Christopher Wileyd76067c2015-10-19 17:00:13 -0700913 std::unique_ptr<AidlQualifiedName> package_;
Jiyong Parkb034bf02018-07-30 17:44:33 +0900914 AidlTypenames& typenames_;
Steven Moreland64e29be2018-08-08 18:52:19 -0700915
916 void* scanner_ = nullptr;
917 YY_BUFFER_STATE buffer_;
918 int error_ = 0;
919
920 std::vector<std::unique_ptr<AidlImport>> imports_;
Jiyong Parkb034bf02018-07-30 17:44:33 +0900921 vector<AidlDefinedType*> defined_types_;
Jiyong Park1deecc32018-07-17 01:14:41 +0900922 vector<AidlTypeSpecifier*> unresolved_typespecs_;
Casey Dahlindd691812015-09-09 17:59:06 -0700923
Casey Dahline2507492015-09-14 17:11:20 -0700924 DISALLOW_COPY_AND_ASSIGN(Parser);
Casey Dahlindd691812015-09-09 17:59:06 -0700925};