blob: 86bd928fb77a46b29381188de7764ab5f3e509b9 [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
Jiyong Parkb034bf02018-07-30 17:44:33 +090033using android::aidl::AidlTypenames;
Jiyong Park02da7422018-07-16 16:00:26 +090034using android::aidl::CodeWriter;
Jeongik Cha047c5ee2019-08-07 23:16:49 +090035using android::aidl::Options;
Steven Moreland3f658cf2018-08-20 13:40:54 -070036using std::shared_ptr;
Jiyong Park1deecc32018-07-17 01:14:41 +090037using std::string;
38using std::unique_ptr;
39using std::vector;
Andrei Onea8714b022019-02-01 18:55:54 +000040class AidlNode;
41
42namespace android {
43namespace aidl {
44namespace mappings {
45std::string dump_location(const AidlNode& method);
46} // namespace mappings
Mathew Inwoodadb74672019-11-29 14:01:53 +000047namespace java {
48std::string dump_location(const AidlNode& method);
49} // namespace java
Andrei Onea8714b022019-02-01 18:55:54 +000050} // namespace aidl
51} // namespace android
52
Casey Dahlincdbbc8c2015-10-14 15:31:04 -070053class AidlToken {
54 public:
55 AidlToken(const std::string& text, const std::string& comments);
Adam Lesinskiffa16862014-01-23 18:17:42 -080056
Casey Dahlincdbbc8c2015-10-14 15:31:04 -070057 const std::string& GetText() const { return text_; }
58 const std::string& GetComments() const { return comments_; }
Adam Lesinskiffa16862014-01-23 18:17:42 -080059
Casey Dahlincdbbc8c2015-10-14 15:31:04 -070060 private:
61 std::string text_;
62 std::string comments_;
Casey Dahlin082f1d12015-09-21 14:06:25 -070063
Casey Dahlincdbbc8c2015-10-14 15:31:04 -070064 DISALLOW_COPY_AND_ASSIGN(AidlToken);
Casey Dahlin0a2f8be2015-09-28 16:15:29 -070065};
Adam Lesinskiffa16862014-01-23 18:17:42 -080066
Steven Moreland46e9da82018-07-27 15:45:29 -070067class AidlLocation {
Casey Dahlinbc7a50a2015-09-28 19:20:50 -070068 public:
Steven Moreland46e9da82018-07-27 15:45:29 -070069 struct Point {
Dan Willemsen609ba6d2019-12-30 10:44:00 -080070 int line;
71 int column;
Steven Moreland46e9da82018-07-27 15:45:29 -070072 };
73
Devin Mooredf93ebb2020-03-25 14:03:35 -070074 enum class Source {
75 // From internal aidl source code
76 INTERNAL = 0,
77 // From a parsed file
78 EXTERNAL = 1
79 };
80
81 AidlLocation(const std::string& file, Point begin, Point end, Source source);
Devin Moore5de18ed2020-04-02 13:52:29 -070082 AidlLocation(const std::string& file, Source source)
83 : AidlLocation(file, {0, 0}, {0, 0}, source) {}
Devin Mooredf93ebb2020-03-25 14:03:35 -070084
85 bool IsInternal() const { return source_ == Source::INTERNAL; }
Steven Moreland46e9da82018-07-27 15:45:29 -070086
Devin Moore5de18ed2020-04-02 13:52:29 -070087 // The first line of a file is line 1.
88 bool LocationKnown() const { return begin_.line != 0; }
89
Steven Moreland46e9da82018-07-27 15:45:29 -070090 friend std::ostream& operator<<(std::ostream& os, const AidlLocation& l);
Andrei Onea8714b022019-02-01 18:55:54 +000091 friend class AidlNode;
Casey Dahlinbc7a50a2015-09-28 19:20:50 -070092
93 private:
Steven Moreland541788d2020-05-21 22:05:52 +000094 // INTENTIONALLY HIDDEN: only operator<< should access details here.
95 // Otherwise, locations should only ever be copied around to construct new
96 // objects.
Steven Moreland46e9da82018-07-27 15:45:29 -070097 const std::string file_;
98 Point begin_;
99 Point end_;
Devin Mooredf93ebb2020-03-25 14:03:35 -0700100 Source source_;
Steven Moreland46e9da82018-07-27 15:45:29 -0700101};
102
Devin Mooredf93ebb2020-03-25 14:03:35 -0700103#define AIDL_LOCATION_HERE \
104 AidlLocation { __FILE__, {__LINE__, 0}, {__LINE__, 0}, AidlLocation::Source::INTERNAL }
Steven Moreland02e012e2018-08-02 14:58:10 -0700105
Steven Moreland46e9da82018-07-27 15:45:29 -0700106std::ostream& operator<<(std::ostream& os, const AidlLocation& l);
107
108// Anything that is locatable in a .aidl file.
109class AidlNode {
110 public:
111 AidlNode(const AidlLocation& location);
Steven Moreland3f658cf2018-08-20 13:40:54 -0700112
113 AidlNode(const AidlNode&) = default;
Steven Moreland3be75772018-08-20 13:27:43 -0700114 AidlNode(AidlNode&&) = default;
Steven Moreland46e9da82018-07-27 15:45:29 -0700115 virtual ~AidlNode() = default;
116
Devin Mooredf93ebb2020-03-25 14:03:35 -0700117 // To be able to print AidlLocation
Steven Morelandb0d15a52020-03-31 14:03:47 -0700118 friend class AidlErrorLog;
Andrei Onea8714b022019-02-01 18:55:54 +0000119 friend std::string android::aidl::mappings::dump_location(const AidlNode&);
Mathew Inwoodadb74672019-11-29 14:01:53 +0000120 friend std::string android::aidl::java::dump_location(const AidlNode&);
Steven Moreland46e9da82018-07-27 15:45:29 -0700121
Devin Mooredf93ebb2020-03-25 14:03:35 -0700122 const AidlLocation& GetLocation() const { return location_; }
123
Steven Moreland46e9da82018-07-27 15:45:29 -0700124 private:
Mathew Inwoodadb74672019-11-29 14:01:53 +0000125 std::string PrintLine() const;
Andrei Onea8714b022019-02-01 18:55:54 +0000126 std::string PrintLocation() const;
Steven Moreland46e9da82018-07-27 15:45:29 -0700127 const AidlLocation location_;
Casey Dahlinbc7a50a2015-09-28 19:20:50 -0700128};
129
Steven Moreland46e9da82018-07-27 15:45:29 -0700130// Generic point for printing any error in the AIDL compiler.
Steven Morelandb0d15a52020-03-31 14:03:47 -0700131class AidlErrorLog {
Steven Moreland46e9da82018-07-27 15:45:29 -0700132 public:
Devin Moore5de18ed2020-04-02 13:52:29 -0700133 AidlErrorLog(bool fatal, const std::string& filename)
134 : AidlErrorLog(fatal, AidlLocation(filename, AidlLocation::Source::EXTERNAL)) {}
Steven Morelandb0d15a52020-03-31 14:03:47 -0700135 AidlErrorLog(bool fatal, const AidlLocation& location);
136 AidlErrorLog(bool fatal, const AidlNode& node) : AidlErrorLog(fatal, node.location_) {}
137 AidlErrorLog(bool fatal, const AidlNode* node) : AidlErrorLog(fatal, *node) {}
Steven Moreland92c55f12018-07-31 14:08:37 -0700138
139 template <typename T>
Steven Morelandb0d15a52020-03-31 14:03:47 -0700140 AidlErrorLog(bool fatal, const std::unique_ptr<T>& node) : AidlErrorLog(fatal, *node) {}
141 ~AidlErrorLog() {
Steven Moreland46e9da82018-07-27 15:45:29 -0700142 os_ << std::endl;
143 if (fatal_) abort();
Devin Moore5de18ed2020-04-02 13:52:29 -0700144 if (location_.IsInternal()) {
145 os_ << "Logging an internal location should not happen. Offending location: " << location_
146 << std::endl;
147 abort();
148 }
Steven Moreland46e9da82018-07-27 15:45:29 -0700149 }
150
151 std::ostream& os_;
152
Steven Moreland33efcf62020-04-10 16:40:43 -0700153 static void clearError() { sHadError = false; }
Steven Morelandfdb57cd2020-01-08 20:03:30 -0800154 static bool hadError() { return sHadError; }
155
Steven Moreland46e9da82018-07-27 15:45:29 -0700156 private:
Steven Moreland46e9da82018-07-27 15:45:29 -0700157
158 bool fatal_;
159
Devin Moore5de18ed2020-04-02 13:52:29 -0700160 const AidlLocation location_;
161
Steven Morelandfdb57cd2020-01-08 20:03:30 -0800162 static bool sHadError;
163
Steven Morelandb0d15a52020-03-31 14:03:47 -0700164 DISALLOW_COPY_AND_ASSIGN(AidlErrorLog);
Steven Moreland46e9da82018-07-27 15:45:29 -0700165};
166
Steven Morelandb0d15a52020-03-31 14:03:47 -0700167#define AIDL_ERROR(CONTEXT) ::AidlErrorLog(false /*fatal*/, (CONTEXT)).os_
168#define AIDL_FATAL(CONTEXT) ::AidlErrorLog(true /*fatal*/, (CONTEXT)).os_
Steven Moreland3f658cf2018-08-20 13:40:54 -0700169#define AIDL_FATAL_IF(CONDITION, CONTEXT) \
170 if (CONDITION) AIDL_FATAL(CONTEXT) << "Bad internal state: " << #CONDITION << ": "
Steven Moreland46e9da82018-07-27 15:45:29 -0700171
Casey Dahlina2f77c42015-12-01 18:26:02 -0800172namespace android {
173namespace aidl {
174
Jiyong Park1deecc32018-07-17 01:14:41 +0900175class AidlTypenames;
Casey Dahlina2f77c42015-12-01 18:26:02 -0800176
177} // namespace aidl
178} // namespace android
179
Jeongik Chadf76dc72019-11-28 00:08:47 +0900180// unique_ptr<AidlTypeSpecifier> for type arugment,
181// std::string for type parameter(T, U, and so on).
182template <typename T>
183class AidlParameterizable {
184 public:
185 AidlParameterizable(std::vector<T>* type_params) : type_params_(type_params) {}
186 virtual ~AidlParameterizable() = default;
187 bool IsGeneric() const { return type_params_ != nullptr; }
188 const std::vector<T>& GetTypeParameters() const { return *type_params_; }
189 bool CheckValid() const;
190
191 virtual const AidlNode& AsAidlNode() const = 0;
192
193 protected:
194 AidlParameterizable(const AidlParameterizable&);
195
196 private:
197 const unique_ptr<std::vector<T>> type_params_;
198 static_assert(std::is_same<T, unique_ptr<AidlTypeSpecifier>>::value ||
199 std::is_same<T, std::string>::value);
200};
201template <>
202bool AidlParameterizable<std::string>::CheckValid() const;
203
Andrei Onea9445fc62019-06-27 18:11:59 +0100204class AidlConstantValue;
205class AidlConstantDeclaration;
206
207// Transforms a value string into a language specific form. Raw value as produced by
208// AidlConstantValue.
209using ConstantValueDecorator =
210 std::function<std::string(const AidlTypeSpecifier& type, const std::string& raw_value)>;
211
Jiyong Park68bc77a2018-07-19 19:00:45 +0900212class AidlAnnotation : public AidlNode {
213 public:
Steven Moreland0cea4aa2020-04-20 21:06:02 -0700214 enum class Type {
215 BACKING = 1,
216 HIDE,
217 JAVA_STABLE_PARCELABLE,
218 UNSUPPORTED_APP_USAGE,
219 VINTF_STABILITY,
220 NULLABLE,
221 UTF8_IN_CPP,
Jiyong Parkbf5fd5c2020-06-05 19:48:05 +0900222 JAVA_PASSTHROUGH,
Jiyong Park43113fb2020-07-20 16:26:19 +0900223 JAVA_DEBUG,
Steven Moreland0cea4aa2020-04-20 21:06:02 -0700224 };
225 static std::string TypeToString(Type type);
226
Andrei Onea9445fc62019-06-27 18:11:59 +0100227 static AidlAnnotation* Parse(
228 const AidlLocation& location, const string& name,
229 std::map<std::string, std::shared_ptr<AidlConstantValue>>* parameter_list);
Steven Moreland46e9da82018-07-27 15:45:29 -0700230
Steven Moreland3f658cf2018-08-20 13:40:54 -0700231 AidlAnnotation(const AidlAnnotation&) = default;
Steven Moreland3be75772018-08-20 13:27:43 -0700232 AidlAnnotation(AidlAnnotation&&) = default;
Jiyong Park68bc77a2018-07-19 19:00:45 +0900233 virtual ~AidlAnnotation() = default;
Andrei Onea9445fc62019-06-27 18:11:59 +0100234 bool CheckValid() const;
Steven Moreland3be75772018-08-20 13:27:43 -0700235
Steven Moreland0cea4aa2020-04-20 21:06:02 -0700236 const string& GetName() const { return schema_.name; };
237 const Type& GetType() const { return schema_.type; }
Daniel Norman37d43dd2019-09-09 17:22:34 -0700238 string ToString(const ConstantValueDecorator& decorator) const;
Andrei Onea9445fc62019-06-27 18:11:59 +0100239 std::map<std::string, std::string> AnnotationParams(
240 const ConstantValueDecorator& decorator) const;
Jiyong Parka6605ab2018-11-11 14:30:21 +0900241 const string& GetComments() const { return comments_; }
242 void SetComments(const string& comments) { comments_ = comments; }
Jiyong Park68bc77a2018-07-19 19:00:45 +0900243
244 private:
Steven Moreland0cea4aa2020-04-20 21:06:02 -0700245 struct Schema {
246 AidlAnnotation::Type type;
247
248 // text name in .aidl file, e.g. "nullable"
249 std::string name;
250
251 // map from param name -> value type
252 std::map<std::string, std::string> supported_parameters;
253 };
254 static const std::vector<Schema>& AllSchemas();
255
256 AidlAnnotation(const AidlLocation& location, const Schema& schema,
Andrei Onea9445fc62019-06-27 18:11:59 +0100257 std::map<std::string, std::shared_ptr<AidlConstantValue>>&& parameters);
Steven Moreland0cea4aa2020-04-20 21:06:02 -0700258
259 const Schema& schema_;
Jiyong Parka6605ab2018-11-11 14:30:21 +0900260 string comments_;
Andrei Onea9445fc62019-06-27 18:11:59 +0100261 std::map<std::string, std::shared_ptr<AidlConstantValue>> parameters_;
Jiyong Park68bc77a2018-07-19 19:00:45 +0900262};
263
Steven Moreland3be75772018-08-20 13:27:43 -0700264static inline bool operator<(const AidlAnnotation& lhs, const AidlAnnotation& rhs) {
265 return lhs.GetName() < rhs.GetName();
266}
267static inline bool operator==(const AidlAnnotation& lhs, const AidlAnnotation& rhs) {
268 return lhs.GetName() == rhs.GetName();
269}
Jiyong Park3656c3c2018-08-01 20:02:01 +0900270
Casey Dahline7922932016-02-29 17:23:01 -0800271class AidlAnnotatable : public AidlNode {
Casey Dahlin0ee37582015-09-30 16:31:55 -0700272 public:
Steven Moreland46e9da82018-07-27 15:45:29 -0700273 AidlAnnotatable(const AidlLocation& location);
Steven Moreland3f658cf2018-08-20 13:40:54 -0700274
275 AidlAnnotatable(const AidlAnnotatable&) = default;
276 AidlAnnotatable(AidlAnnotatable&&) = default;
Casey Dahline7922932016-02-29 17:23:01 -0800277 virtual ~AidlAnnotatable() = default;
278
Artur Satayev91fe8712019-07-29 13:06:01 +0100279 void Annotate(vector<AidlAnnotation>&& annotations) {
280 for (auto& annotation : annotations) {
281 annotations_.emplace_back(std::move(annotation));
282 }
283 }
Jiyong Park68bc77a2018-07-19 19:00:45 +0900284 bool IsNullable() const;
Jiyong Park68bc77a2018-07-19 19:00:45 +0900285 bool IsUtf8InCpp() const;
Steven Morelanda57d0a62019-07-30 09:41:14 -0700286 bool IsVintfStability() const;
Jeongik Cha88f95a82020-01-15 13:02:16 +0900287 bool IsStableApiParcelable(Options::Language lang) const;
Makoto Onuki78a1c1c2020-03-04 16:57:23 -0800288 bool IsHide() const;
Jiyong Park43113fb2020-07-20 16:26:19 +0900289 bool IsJavaDebug() const;
Andrei Onea9445fc62019-06-27 18:11:59 +0100290
Steven Moreland7e4b9502020-02-20 18:10:42 -0800291 void DumpAnnotations(CodeWriter* writer) const;
292
Andrei Onea9445fc62019-06-27 18:11:59 +0100293 const AidlAnnotation* UnsupportedAppUsage() const;
Jiyong Parkbf5fd5c2020-06-05 19:48:05 +0900294 const AidlAnnotation* JavaPassthrough() const;
Daniel Norman716d3112019-09-10 13:11:56 -0700295 const AidlTypeSpecifier* BackingType(const AidlTypenames& typenames) const;
Jiyong Park68bc77a2018-07-19 19:00:45 +0900296 std::string ToString() const;
Casey Dahline7922932016-02-29 17:23:01 -0800297
Jiyong Parka6605ab2018-11-11 14:30:21 +0900298 const vector<AidlAnnotation>& GetAnnotations() const { return annotations_; }
Devin Moore24f68572020-02-26 13:20:59 -0800299 virtual bool CheckValid(const AidlTypenames&) const;
Jiyong Park3656c3c2018-08-01 20:02:01 +0900300
Steven Moreland181144c2020-04-20 19:57:56 -0700301 protected:
Steven Moreland0cea4aa2020-04-20 21:06:02 -0700302 virtual std::set<AidlAnnotation::Type> GetSupportedAnnotations() const = 0;
Steven Moreland181144c2020-04-20 19:57:56 -0700303
Casey Dahline7922932016-02-29 17:23:01 -0800304 private:
Jiyong Parka6605ab2018-11-11 14:30:21 +0900305 vector<AidlAnnotation> annotations_;
Casey Dahline7922932016-02-29 17:23:01 -0800306};
307
Jiyong Park1deecc32018-07-17 01:14:41 +0900308// AidlTypeSpecifier represents a reference to either a built-in type,
309// a defined type, or a variant (e.g., array of generic) of a type.
Jeongik Chadf76dc72019-11-28 00:08:47 +0900310class AidlTypeSpecifier final : public AidlAnnotatable,
311 public AidlParameterizable<unique_ptr<AidlTypeSpecifier>> {
Casey Dahline7922932016-02-29 17:23:01 -0800312 public:
Steven Moreland46e9da82018-07-27 15:45:29 -0700313 AidlTypeSpecifier(const AidlLocation& location, const string& unresolved_name, bool is_array,
314 vector<unique_ptr<AidlTypeSpecifier>>* type_params, const string& comments);
Jiyong Parkd59a10d2018-07-18 11:12:55 +0900315 virtual ~AidlTypeSpecifier() = default;
Casey Dahlin0ee37582015-09-30 16:31:55 -0700316
Steven Moreland3f658cf2018-08-20 13:40:54 -0700317 // Copy of this type which is not an array.
318 AidlTypeSpecifier ArrayBase() const;
319
Jiyong Park1deecc32018-07-17 01:14:41 +0900320 // Returns the full-qualified name of the base type.
321 // int -> int
322 // int[] -> int
323 // List<String> -> List
324 // IFoo -> foo.bar.IFoo (if IFoo is in package foo.bar)
325 const string& GetName() const {
326 if (IsResolved()) {
327 return fully_qualified_name_;
328 } else {
329 return GetUnresolvedName();
330 }
331 }
Casey Dahlin0ee37582015-09-30 16:31:55 -0700332
Jiyong Park1deecc32018-07-17 01:14:41 +0900333 // Returns string representation of this type specifier.
Artur Satayev91fe8712019-07-29 13:06:01 +0100334 // This is GetBaseTypeName() + array modifier or generic type parameters
Jiyong Park1deecc32018-07-17 01:14:41 +0900335 string ToString() const;
336
Jiyong Park02da7422018-07-16 16:00:26 +0900337 std::string Signature() const;
338
Jiyong Park1deecc32018-07-17 01:14:41 +0900339 const string& GetUnresolvedName() const { return unresolved_name_; }
340
Jeongik Cha997281d2020-01-16 15:23:59 +0900341 bool IsHidden() const;
342
Jiyong Park1deecc32018-07-17 01:14:41 +0900343 const string& GetComments() const { return comments_; }
344
Jeongik Cha1a7ab642019-07-29 17:31:02 +0900345 const std::vector<std::string> GetSplitName() const { return split_name_; }
346
Jiyong Parka6605ab2018-11-11 14:30:21 +0900347 void SetComments(const string& comment) { comments_ = comment; }
348
Jiyong Park1deecc32018-07-17 01:14:41 +0900349 bool IsResolved() const { return fully_qualified_name_ != ""; }
350
351 bool IsArray() const { return is_array_; }
352
Jiyong Park1deecc32018-07-17 01:14:41 +0900353 // Resolve the base type name to a fully-qualified name. Return false if the
354 // resolution fails.
Daniel Norman716d3112019-09-10 13:11:56 -0700355 bool Resolve(const AidlTypenames& typenames);
Casey Dahlin0ee37582015-09-30 16:31:55 -0700356
Steven Moreland0cea4aa2020-04-20 21:06:02 -0700357 std::set<AidlAnnotation::Type> GetSupportedAnnotations() const override;
Devin Moore24f68572020-02-26 13:20:59 -0800358 bool CheckValid(const AidlTypenames& typenames) const override;
Steven Morelandd59e3172020-05-11 16:42:09 -0700359 bool LanguageSpecificCheckValid(const AidlTypenames& typenames, Options::Language lang) const;
Jeongik Chadf76dc72019-11-28 00:08:47 +0900360 const AidlNode& AsAidlNode() const override { return *this; }
Jiyong Park1d2df7d2018-07-23 15:22:50 +0900361
Casey Dahlin0ee37582015-09-30 16:31:55 -0700362 private:
Steven Moreland3f658cf2018-08-20 13:40:54 -0700363 AidlTypeSpecifier(const AidlTypeSpecifier&) = default;
364
Jiyong Park1deecc32018-07-17 01:14:41 +0900365 const string unresolved_name_;
366 string fully_qualified_name_;
Steven Moreland3f658cf2018-08-20 13:40:54 -0700367 bool is_array_;
Jiyong Parka6605ab2018-11-11 14:30:21 +0900368 string comments_;
Jeongik Cha1a7ab642019-07-29 17:31:02 +0900369 vector<string> split_name_;
Casey Dahlin0ee37582015-09-30 16:31:55 -0700370};
371
Steven Moreland860b1942018-08-16 14:59:28 -0700372// Returns the universal value unaltered.
373std::string AidlConstantValueDecorator(const AidlTypeSpecifier& type, const std::string& raw_value);
374
Steven Moreland9ea10e32018-07-19 15:26:09 -0700375class AidlConstantValue;
Steven Moreland541788d2020-05-21 22:05:52 +0000376// TODO: This class is used for method arguments and also parcelable fields,
377// and it should be split up since default values don't apply to method
378// arguments
Steven Moreland5557f1c2018-07-02 13:50:23 -0700379class AidlVariableDeclaration : public AidlNode {
380 public:
Steven Moreland46e9da82018-07-27 15:45:29 -0700381 AidlVariableDeclaration(const AidlLocation& location, AidlTypeSpecifier* type,
382 const std::string& name);
383 AidlVariableDeclaration(const AidlLocation& location, AidlTypeSpecifier* type,
384 const std::string& name, AidlConstantValue* default_value);
Steven Moreland5557f1c2018-07-02 13:50:23 -0700385 virtual ~AidlVariableDeclaration() = default;
386
387 std::string GetName() const { return name_; }
Jiyong Parkd59a10d2018-07-18 11:12:55 +0900388 const AidlTypeSpecifier& GetType() const { return *type_; }
Steven Moreland541788d2020-05-21 22:05:52 +0000389 // if this was constructed explicitly with a default value
390 bool IsDefaultUserSpecified() const { return default_user_specified_; }
391 // will return the default value this is constructed with or a default value
392 // if one is available
Steven Moreland9ea10e32018-07-19 15:26:09 -0700393 const AidlConstantValue* GetDefaultValue() const { return default_value_.get(); }
394
Jiyong Parkd59a10d2018-07-18 11:12:55 +0900395 AidlTypeSpecifier* GetMutableType() { return type_.get(); }
Steven Moreland5557f1c2018-07-02 13:50:23 -0700396
Jeongik Chadb0f59e2018-11-01 18:11:21 +0900397 bool CheckValid(const AidlTypenames& typenames) const;
Steven Moreland5557f1c2018-07-02 13:50:23 -0700398 std::string ToString() const;
Jiyong Park02da7422018-07-16 16:00:26 +0900399 std::string Signature() const;
Steven Moreland5557f1c2018-07-02 13:50:23 -0700400
Steven Moreland860b1942018-08-16 14:59:28 -0700401 std::string ValueString(const ConstantValueDecorator& decorator) const;
Steven Moreland25294322018-08-07 18:13:55 -0700402
Steven Moreland5557f1c2018-07-02 13:50:23 -0700403 private:
Jiyong Parkd59a10d2018-07-18 11:12:55 +0900404 std::unique_ptr<AidlTypeSpecifier> type_;
Steven Moreland5557f1c2018-07-02 13:50:23 -0700405 std::string name_;
Steven Moreland541788d2020-05-21 22:05:52 +0000406 bool default_user_specified_;
Steven Moreland9ea10e32018-07-19 15:26:09 -0700407 std::unique_ptr<AidlConstantValue> default_value_;
Steven Moreland5557f1c2018-07-02 13:50:23 -0700408
409 DISALLOW_COPY_AND_ASSIGN(AidlVariableDeclaration);
410};
411
412class AidlArgument : public AidlVariableDeclaration {
Casey Dahlinbc7a50a2015-09-28 19:20:50 -0700413 public:
Casey Dahlinc378c992015-09-29 16:50:40 -0700414 enum Direction { IN_DIR = 1, OUT_DIR = 2, INOUT_DIR = 3 };
415
Steven Moreland46e9da82018-07-27 15:45:29 -0700416 AidlArgument(const AidlLocation& location, AidlArgument::Direction direction,
417 AidlTypeSpecifier* type, const std::string& name);
418 AidlArgument(const AidlLocation& location, AidlTypeSpecifier* type, const std::string& name);
Casey Dahlinbc7a50a2015-09-28 19:20:50 -0700419 virtual ~AidlArgument() = default;
420
Casey Dahlinc378c992015-09-29 16:50:40 -0700421 Direction GetDirection() const { return direction_; }
Christopher Wileyad339272015-10-05 19:11:58 -0700422 bool IsOut() const { return direction_ & OUT_DIR; }
423 bool IsIn() const { return direction_ & IN_DIR; }
Casey Dahlinc378c992015-09-29 16:50:40 -0700424 bool DirectionWasSpecified() const { return direction_specified_; }
Jiyong Park3656c3c2018-08-01 20:02:01 +0900425 string GetDirectionSpecifier() const;
Christopher Wileyad339272015-10-05 19:11:58 -0700426
Casey Dahlinc378c992015-09-29 16:50:40 -0700427 std::string ToString() const;
Jiyong Park02da7422018-07-16 16:00:26 +0900428 std::string Signature() const;
Casey Dahlinc378c992015-09-29 16:50:40 -0700429
Casey Dahlinbc7a50a2015-09-28 19:20:50 -0700430 private:
Casey Dahlinc378c992015-09-29 16:50:40 -0700431 Direction direction_;
432 bool direction_specified_;
433
Casey Dahlinbc7a50a2015-09-28 19:20:50 -0700434 DISALLOW_COPY_AND_ASSIGN(AidlArgument);
Casey Dahlina834dd42015-09-23 11:52:15 -0700435};
Adam Lesinskiffa16862014-01-23 18:17:42 -0800436
Casey Dahlind40e2fe2015-11-24 14:06:52 -0800437class AidlMethod;
Steven Moreland693640b2018-07-19 13:46:27 -0700438class AidlConstantDeclaration;
Daniel Norman85aed542019-08-21 12:01:14 -0700439class AidlEnumDeclaration;
Casey Dahlind40e2fe2015-11-24 14:06:52 -0800440class AidlMember : public AidlNode {
441 public:
Steven Moreland46e9da82018-07-27 15:45:29 -0700442 AidlMember(const AidlLocation& location);
Casey Dahlind40e2fe2015-11-24 14:06:52 -0800443 virtual ~AidlMember() = default;
444
445 virtual AidlMethod* AsMethod() { return nullptr; }
Steven Moreland693640b2018-07-19 13:46:27 -0700446 virtual AidlConstantDeclaration* AsConstantDeclaration() { return nullptr; }
Casey Dahlind40e2fe2015-11-24 14:06:52 -0800447
448 private:
449 DISALLOW_COPY_AND_ASSIGN(AidlMember);
450};
451
Will McVickerd7d18df2019-09-12 13:40:50 -0700452class AidlUnaryConstExpression;
453class AidlBinaryConstExpression;
454
Steven Moreland693640b2018-07-19 13:46:27 -0700455class AidlConstantValue : public AidlNode {
Casey Dahlind40e2fe2015-11-24 14:06:52 -0800456 public:
Will McVickerd7d18df2019-09-12 13:40:50 -0700457 enum class Type {
458 // WARNING: Don't change this order! The order is used to determine type
459 // promotion during a binary expression.
460 BOOLEAN,
461 INT8,
462 INT32,
463 INT64,
464 ARRAY,
465 CHARACTER,
466 STRING,
467 FLOATING,
468 UNARY,
469 BINARY,
470 ERROR,
471 };
472
473 /*
474 * Return the value casted to the given type.
475 */
476 template <typename T>
477 T cast() const;
Casey Dahlind40e2fe2015-11-24 14:06:52 -0800478
Steven Moreland693640b2018-07-19 13:46:27 -0700479 virtual ~AidlConstantValue() = default;
Casey Dahlind40e2fe2015-11-24 14:06:52 -0800480
Steven Moreland541788d2020-05-21 22:05:52 +0000481 // creates default value, when one isn't specified
482 // nullptr if no default available
483 static AidlConstantValue* Default(const AidlTypeSpecifier& specifier);
484
Steven Moreland25294322018-08-07 18:13:55 -0700485 static AidlConstantValue* Boolean(const AidlLocation& location, bool value);
486 static AidlConstantValue* Character(const AidlLocation& location, char value);
Steven Moreland25294322018-08-07 18:13:55 -0700487 // example: 123, -5498, maybe any size
Will McVickerd7d18df2019-09-12 13:40:50 -0700488 static AidlConstantValue* Integral(const AidlLocation& location, const string& value);
489 static AidlConstantValue* Floating(const AidlLocation& location, const std::string& value);
Steven Moreland860b1942018-08-16 14:59:28 -0700490 static AidlConstantValue* Array(const AidlLocation& location,
Will McVickerd7d18df2019-09-12 13:40:50 -0700491 std::unique_ptr<vector<unique_ptr<AidlConstantValue>>> values);
Steven Moreland693640b2018-07-19 13:46:27 -0700492 // example: "\"asdf\""
Will McVickerd7d18df2019-09-12 13:40:50 -0700493 static AidlConstantValue* String(const AidlLocation& location, const string& value);
Steven Moreland693640b2018-07-19 13:46:27 -0700494
Daniel Normanf0ca44f2019-10-25 09:59:44 -0700495 // Construct an AidlConstantValue by evaluating the other integral constant's
496 // value string. This does not preserve the structure of the copied constant.
Steven Moreland59e53e42019-11-26 20:38:08 -0800497 // Returns nullptr and logs if value cannot be copied.
Daniel Normanf0ca44f2019-10-25 09:59:44 -0700498 static AidlConstantValue* ShallowIntegralCopy(const AidlConstantValue& other);
Daniel Normanb28684e2019-10-17 15:31:39 -0700499
Will McVickerd7d18df2019-09-12 13:40:50 -0700500 Type GetType() const { return final_type_; }
Steven Moreland25294322018-08-07 18:13:55 -0700501
Will McVickerd7d18df2019-09-12 13:40:50 -0700502 virtual bool CheckValid() const;
Steven Moreland860b1942018-08-16 14:59:28 -0700503
504 // Raw value of type (currently valid in C++ and Java). Empty string on error.
Steven Moreland4bcb05c2019-11-27 18:57:47 -0800505 string ValueString(const AidlTypeSpecifier& type, const ConstantValueDecorator& decorator) const;
Casey Dahlind40e2fe2015-11-24 14:06:52 -0800506
507 private:
Will McVickerd7d18df2019-09-12 13:40:50 -0700508 AidlConstantValue(const AidlLocation& location, Type parsed_type, int64_t parsed_value,
509 const string& checked_value);
510 AidlConstantValue(const AidlLocation& location, Type type, const string& checked_value);
Steven Moreland860b1942018-08-16 14:59:28 -0700511 AidlConstantValue(const AidlLocation& location, Type type,
Will McVickerd7d18df2019-09-12 13:40:50 -0700512 std::unique_ptr<vector<unique_ptr<AidlConstantValue>>> values);
Steven Moreland25294322018-08-07 18:13:55 -0700513 static string ToString(Type type);
Will McVickerd7d18df2019-09-12 13:40:50 -0700514 static bool ParseIntegral(const string& value, int64_t* parsed_value, Type* parsed_type);
515 static bool IsHex(const string& value);
Steven Moreland4bcb05c2019-11-27 18:57:47 -0800516
Will McVickerd7d18df2019-09-12 13:40:50 -0700517 virtual bool evaluate(const AidlTypeSpecifier& type) const;
Casey Dahlind40e2fe2015-11-24 14:06:52 -0800518
Steven Moreland693640b2018-07-19 13:46:27 -0700519 const Type type_ = Type::ERROR;
Will McVickerd7d18df2019-09-12 13:40:50 -0700520 const vector<unique_ptr<AidlConstantValue>> values_; // if type_ == ARRAY
521 const string value_; // otherwise
522
523 // State for tracking evaluation of expressions
Steven Moreland4bcb05c2019-11-27 18:57:47 -0800524 mutable bool is_valid_ = false; // cache of CheckValid, but may be marked false in evaluate
525 mutable bool is_evaluated_ = false; // whether evaluate has been called
Will McVickerd7d18df2019-09-12 13:40:50 -0700526 mutable Type final_type_;
527 mutable int64_t final_value_;
528 mutable string final_string_value_ = "";
Steven Moreland693640b2018-07-19 13:46:27 -0700529
530 DISALLOW_COPY_AND_ASSIGN(AidlConstantValue);
Will McVickerd7d18df2019-09-12 13:40:50 -0700531
532 friend AidlUnaryConstExpression;
533 friend AidlBinaryConstExpression;
534};
535
536class AidlUnaryConstExpression : public AidlConstantValue {
537 public:
538 AidlUnaryConstExpression(const AidlLocation& location, const string& op,
539 std::unique_ptr<AidlConstantValue> rval);
540
541 static bool IsCompatibleType(Type type, const string& op);
542 bool CheckValid() const override;
Will McVickerd7d18df2019-09-12 13:40:50 -0700543 private:
544 bool evaluate(const AidlTypeSpecifier& type) const override;
545
546 std::unique_ptr<AidlConstantValue> unary_;
547 const string op_;
548};
549
550class AidlBinaryConstExpression : public AidlConstantValue {
551 public:
552 AidlBinaryConstExpression(const AidlLocation& location, std::unique_ptr<AidlConstantValue> lval,
553 const string& op, std::unique_ptr<AidlConstantValue> rval);
554
555 bool CheckValid() const override;
Will McVickerd7d18df2019-09-12 13:40:50 -0700556
557 static bool AreCompatibleTypes(Type t1, Type t2);
558 // Returns the promoted kind for both operands
559 static Type UsualArithmeticConversion(Type left, Type right);
560 // Returns the promoted integral type where INT32 is the smallest type
561 static Type IntegralPromotion(Type in);
562
563 private:
564 bool evaluate(const AidlTypeSpecifier& type) const override;
565
566 std::unique_ptr<AidlConstantValue> left_val_;
567 std::unique_ptr<AidlConstantValue> right_val_;
568 const string op_;
Christopher Wileyd6bdd8d2016-05-03 11:23:13 -0700569};
570
Andrei Onea9445fc62019-06-27 18:11:59 +0100571struct AidlAnnotationParameter {
572 std::string name;
573 std::unique_ptr<AidlConstantValue> value;
574};
575
Steven Moreland693640b2018-07-19 13:46:27 -0700576class AidlConstantDeclaration : public AidlMember {
Christopher Wileyd6bdd8d2016-05-03 11:23:13 -0700577 public:
Steven Moreland46e9da82018-07-27 15:45:29 -0700578 AidlConstantDeclaration(const AidlLocation& location, AidlTypeSpecifier* specifier,
Will McVickerd7d18df2019-09-12 13:40:50 -0700579 const string& name, AidlConstantValue* value);
Steven Moreland693640b2018-07-19 13:46:27 -0700580 virtual ~AidlConstantDeclaration() = default;
Christopher Wileyd6bdd8d2016-05-03 11:23:13 -0700581
Steven Moreland693640b2018-07-19 13:46:27 -0700582 const AidlTypeSpecifier& GetType() const { return *type_; }
Steven Moreland4d12f9a2018-10-31 14:30:55 -0700583 AidlTypeSpecifier* GetMutableType() { return type_.get(); }
Will McVickerd7d18df2019-09-12 13:40:50 -0700584 const string& GetName() const { return name_; }
Steven Moreland693640b2018-07-19 13:46:27 -0700585 const AidlConstantValue& GetValue() const { return *value_; }
Jeongik Chadb0f59e2018-11-01 18:11:21 +0900586 bool CheckValid(const AidlTypenames& typenames) const;
Christopher Wileyd6bdd8d2016-05-03 11:23:13 -0700587
Will McVickerd7d18df2019-09-12 13:40:50 -0700588 string ToString() const;
589 string Signature() const;
Steven Moreland860b1942018-08-16 14:59:28 -0700590 string ValueString(const ConstantValueDecorator& decorator) const {
Will McVickerd7d18df2019-09-12 13:40:50 -0700591 return value_->ValueString(GetType(), decorator);
Steven Moreland860b1942018-08-16 14:59:28 -0700592 }
Steven Moreland25294322018-08-07 18:13:55 -0700593
Steven Moreland693640b2018-07-19 13:46:27 -0700594 AidlConstantDeclaration* AsConstantDeclaration() override { return this; }
Christopher Wileyd6bdd8d2016-05-03 11:23:13 -0700595
596 private:
Steven Moreland693640b2018-07-19 13:46:27 -0700597 const unique_ptr<AidlTypeSpecifier> type_;
Will McVickerd7d18df2019-09-12 13:40:50 -0700598 const string name_;
599 unique_ptr<AidlConstantValue> value_;
Christopher Wileyd6bdd8d2016-05-03 11:23:13 -0700600
Steven Moreland693640b2018-07-19 13:46:27 -0700601 DISALLOW_COPY_AND_ASSIGN(AidlConstantDeclaration);
Casey Dahlind40e2fe2015-11-24 14:06:52 -0800602};
603
604class AidlMethod : public AidlMember {
Casey Dahlin5c69deb2015-10-01 14:44:12 -0700605 public:
Will McVickerd7d18df2019-09-12 13:40:50 -0700606 AidlMethod(const AidlLocation& location, bool oneway, AidlTypeSpecifier* type, const string& name,
607 vector<unique_ptr<AidlArgument>>* args, const string& comments);
608 AidlMethod(const AidlLocation& location, bool oneway, AidlTypeSpecifier* type, const string& name,
609 vector<unique_ptr<AidlArgument>>* args, const string& comments, int id,
610 bool is_user_defined = true);
Casey Dahlin5c69deb2015-10-01 14:44:12 -0700611 virtual ~AidlMethod() = default;
612
Casey Dahlind40e2fe2015-11-24 14:06:52 -0800613 AidlMethod* AsMethod() override { return this; }
Jeongik Cha997281d2020-01-16 15:23:59 +0900614 bool IsHidden() const;
Will McVickerd7d18df2019-09-12 13:40:50 -0700615 const string& GetComments() const { return comments_; }
Jiyong Parkd59a10d2018-07-18 11:12:55 +0900616 const AidlTypeSpecifier& GetType() const { return *type_; }
617 AidlTypeSpecifier* GetMutableType() { return type_.get(); }
Steven Morelandacd53472018-12-14 10:17:26 -0800618
Steven Moreland8c70ba92018-12-17 10:20:31 -0800619 // set if this method is part of an interface that is marked oneway
620 void ApplyInterfaceOneway(bool oneway) { oneway_ = oneway_ || oneway; }
Casey Dahlinf4a93112015-10-05 16:58:09 -0700621 bool IsOneway() const { return oneway_; }
Steven Morelandacd53472018-12-14 10:17:26 -0800622
Casey Dahlinf4a93112015-10-05 16:58:09 -0700623 const std::string& GetName() const { return name_; }
Casey Dahlinf4a93112015-10-05 16:58:09 -0700624 bool HasId() const { return has_id_; }
Jiyong Parked65bf42018-08-28 15:43:27 +0900625 int GetId() const { return id_; }
Casey Dahlinf4a93112015-10-05 16:58:09 -0700626 void SetId(unsigned id) { id_ = id; }
Casey Dahlinf2d23f72015-10-02 16:19:19 -0700627
Jiyong Park309668e2018-07-28 16:55:44 +0900628 bool IsUserDefined() const { return is_user_defined_; }
629
Casey Dahlinf4a93112015-10-05 16:58:09 -0700630 const std::vector<std::unique_ptr<AidlArgument>>& GetArguments() const {
Christopher Wileyad339272015-10-05 19:11:58 -0700631 return arguments_;
632 }
633 // An inout parameter will appear in both GetInArguments()
634 // and GetOutArguments(). AidlMethod retains ownership of the argument
635 // pointers returned in this way.
636 const std::vector<const AidlArgument*>& GetInArguments() const {
637 return in_arguments_;
638 }
639 const std::vector<const AidlArgument*>& GetOutArguments() const {
640 return out_arguments_;
Casey Dahlinf4a93112015-10-05 16:58:09 -0700641 }
Casey Dahlin5c69deb2015-10-01 14:44:12 -0700642
Jiyong Park309668e2018-07-28 16:55:44 +0900643 // name + type parameter types
644 // i.e, foo(int, String)
Jiyong Park02da7422018-07-16 16:00:26 +0900645 std::string Signature() const;
646
Jiyong Park309668e2018-07-28 16:55:44 +0900647 // return type + name + type parameter types + annotations
648 // i.e, boolean foo(int, @Nullable String)
649 std::string ToString() const;
650
Casey Dahlin5c69deb2015-10-01 14:44:12 -0700651 private:
Casey Dahlinf4a93112015-10-05 16:58:09 -0700652 bool oneway_;
Casey Dahlinf2d23f72015-10-02 16:19:19 -0700653 std::string comments_;
Jiyong Parkd59a10d2018-07-18 11:12:55 +0900654 std::unique_ptr<AidlTypeSpecifier> type_;
Casey Dahlinf4a93112015-10-05 16:58:09 -0700655 std::string name_;
Christopher Wileyad339272015-10-05 19:11:58 -0700656 const std::vector<std::unique_ptr<AidlArgument>> arguments_;
657 std::vector<const AidlArgument*> in_arguments_;
658 std::vector<const AidlArgument*> out_arguments_;
Casey Dahlinf4a93112015-10-05 16:58:09 -0700659 bool has_id_;
660 int id_;
Jiyong Park309668e2018-07-28 16:55:44 +0900661 bool is_user_defined_ = true;
Casey Dahlinf2d23f72015-10-02 16:19:19 -0700662
Casey Dahlin5c69deb2015-10-01 14:44:12 -0700663 DISALLOW_COPY_AND_ASSIGN(AidlMethod);
Casey Dahlin0a2f8be2015-09-28 16:15:29 -0700664};
Adam Lesinskiffa16862014-01-23 18:17:42 -0800665
Steven Morelandc258abc2018-07-10 14:03:38 -0700666class AidlDefinedType;
Jiyong Parkb034bf02018-07-30 17:44:33 +0900667class AidlInterface;
668class AidlParcelable;
669class AidlStructuredParcelable;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800670
Steven Moreland46e9da82018-07-27 15:45:29 -0700671class AidlInterface;
672class AidlParcelable;
673class AidlStructuredParcelable;
Daniel Norman85aed542019-08-21 12:01:14 -0700674// AidlDefinedType represents either an interface, parcelable, or enum that is
Jiyong Park1deecc32018-07-17 01:14:41 +0900675// defined in the source file.
676class AidlDefinedType : public AidlAnnotatable {
Steven Moreland787b0432018-07-03 09:00:58 -0700677 public:
Steven Moreland46e9da82018-07-27 15:45:29 -0700678 AidlDefinedType(const AidlLocation& location, const std::string& name,
Jiyong Park18132182020-06-08 20:24:40 +0900679 const std::string& comments, const std::string& package);
Steven Moreland787b0432018-07-03 09:00:58 -0700680 virtual ~AidlDefinedType() = default;
681
Jiyong Park1deecc32018-07-17 01:14:41 +0900682 const std::string& GetName() const { return name_; };
Jeongik Cha997281d2020-01-16 15:23:59 +0900683 bool IsHidden() const;
Jiyong Park1deecc32018-07-17 01:14:41 +0900684 const std::string& GetComments() const { return comments_; }
Jiyong Parka6605ab2018-11-11 14:30:21 +0900685 void SetComments(const std::string comments) { comments_ = comments; }
Jiyong Park1deecc32018-07-17 01:14:41 +0900686
Steven Moreland787b0432018-07-03 09:00:58 -0700687 /* dot joined package, example: "android.package.foo" */
Jiyong Park18132182020-06-08 20:24:40 +0900688 std::string GetPackage() const { return package_; }
Steven Moreland787b0432018-07-03 09:00:58 -0700689 /* dot joined package and name, example: "android.package.foo.IBar" */
690 std::string GetCanonicalName() const;
Jiyong Park18132182020-06-08 20:24:40 +0900691 const std::vector<std::string>& GetSplitPackage() const { return split_package_; }
Steven Moreland787b0432018-07-03 09:00:58 -0700692
Steven Morelanded83a282018-07-17 13:27:29 -0700693 virtual std::string GetPreprocessDeclarationName() const = 0;
Steven Morelandc258abc2018-07-10 14:03:38 -0700694
Steven Moreland5557f1c2018-07-02 13:50:23 -0700695 virtual const AidlStructuredParcelable* AsStructuredParcelable() const { return nullptr; }
Steven Morelandc258abc2018-07-10 14:03:38 -0700696 virtual const AidlParcelable* AsParcelable() const { return nullptr; }
Daniel Norman85aed542019-08-21 12:01:14 -0700697 virtual const AidlEnumDeclaration* AsEnumDeclaration() const { return nullptr; }
Steven Moreland5557f1c2018-07-02 13:50:23 -0700698 virtual const AidlInterface* AsInterface() const { return nullptr; }
Jeongik Chadf76dc72019-11-28 00:08:47 +0900699 virtual const AidlParameterizable<std::string>* AsParameterizable() const { return nullptr; }
Devin Moore24f68572020-02-26 13:20:59 -0800700 bool CheckValid(const AidlTypenames& typenames) const override;
Steven Morelandd59e3172020-05-11 16:42:09 -0700701 virtual bool LanguageSpecificCheckValid(const AidlTypenames& typenames,
702 Options::Language lang) const = 0;
Steven Morelandc258abc2018-07-10 14:03:38 -0700703 AidlStructuredParcelable* AsStructuredParcelable() {
704 return const_cast<AidlStructuredParcelable*>(
705 const_cast<const AidlDefinedType*>(this)->AsStructuredParcelable());
706 }
707 AidlParcelable* AsParcelable() {
708 return const_cast<AidlParcelable*>(const_cast<const AidlDefinedType*>(this)->AsParcelable());
709 }
Daniel Norman85aed542019-08-21 12:01:14 -0700710 AidlEnumDeclaration* AsEnumDeclaration() {
711 return const_cast<AidlEnumDeclaration*>(
712 const_cast<const AidlDefinedType*>(this)->AsEnumDeclaration());
713 }
Steven Morelandc258abc2018-07-10 14:03:38 -0700714 AidlInterface* AsInterface() {
715 return const_cast<AidlInterface*>(const_cast<const AidlDefinedType*>(this)->AsInterface());
716 }
717
Jeongik Chadf76dc72019-11-28 00:08:47 +0900718 AidlParameterizable<std::string>* AsParameterizable() {
719 return const_cast<AidlParameterizable<std::string>*>(
720 const_cast<const AidlDefinedType*>(this)->AsParameterizable());
721 }
722
Steven Moreland6cee3482018-07-18 14:39:58 -0700723 const AidlParcelable* AsUnstructuredParcelable() const {
724 if (this->AsStructuredParcelable() != nullptr) return nullptr;
725 return this->AsParcelable();
726 }
727 AidlParcelable* AsUnstructuredParcelable() {
728 return const_cast<AidlParcelable*>(
729 const_cast<const AidlDefinedType*>(this)->AsUnstructuredParcelable());
730 }
731
Jeongik Cha997281d2020-01-16 15:23:59 +0900732 virtual void Dump(CodeWriter* writer) const = 0;
Steven Morelanda5d9c5c2020-02-21 16:01:09 -0800733 void DumpHeader(CodeWriter* writer) const;
Jiyong Park02da7422018-07-16 16:00:26 +0900734
Steven Moreland787b0432018-07-03 09:00:58 -0700735 private:
Jiyong Park1deecc32018-07-17 01:14:41 +0900736 std::string name_;
Jiyong Park1deecc32018-07-17 01:14:41 +0900737 std::string comments_;
Jiyong Park18132182020-06-08 20:24:40 +0900738 const std::string package_;
739 const std::vector<std::string> split_package_;
Steven Moreland5557f1c2018-07-02 13:50:23 -0700740
741 DISALLOW_COPY_AND_ASSIGN(AidlDefinedType);
Jiyong Park8e79b7f2020-07-20 20:52:38 +0900742 AidlDefinedType(AidlDefinedType&&) = delete;
743 AidlDefinedType& operator=(AidlDefinedType&&) = delete;
Steven Moreland787b0432018-07-03 09:00:58 -0700744};
745
Jeongik Chadf76dc72019-11-28 00:08:47 +0900746class AidlParcelable : public AidlDefinedType, public AidlParameterizable<std::string> {
Casey Dahlin1ae2bc52015-10-07 18:49:10 -0700747 public:
Jiyong Park18132182020-06-08 20:24:40 +0900748 AidlParcelable(const AidlLocation& location, const std::string& name, const std::string& package,
749 const std::string& comments, const std::string& cpp_header = "",
Jeongik Chadf76dc72019-11-28 00:08:47 +0900750 std::vector<std::string>* type_params = nullptr);
Casey Dahlin1ae2bc52015-10-07 18:49:10 -0700751 virtual ~AidlParcelable() = default;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800752
Christopher Wiley8aa4d9f2015-11-16 19:10:45 -0800753 std::string GetCppHeader() const { return cpp_header_; }
Christopher Wiley8aa4d9f2015-11-16 19:10:45 -0800754
Steven Moreland0cea4aa2020-04-20 21:06:02 -0700755 std::set<AidlAnnotation::Type> GetSupportedAnnotations() const override;
Jeongik Cha82317dd2019-02-27 20:26:11 +0900756 bool CheckValid(const AidlTypenames& typenames) const override;
Steven Morelandd59e3172020-05-11 16:42:09 -0700757 bool LanguageSpecificCheckValid(const AidlTypenames& typenames,
758 Options::Language lang) const override;
Steven Morelandc258abc2018-07-10 14:03:38 -0700759 const AidlParcelable* AsParcelable() const override { return this; }
Jeongik Chadf76dc72019-11-28 00:08:47 +0900760 const AidlParameterizable<std::string>* AsParameterizable() const override { return this; }
761 const AidlNode& AsAidlNode() const override { return *this; }
Steven Morelanded83a282018-07-17 13:27:29 -0700762 std::string GetPreprocessDeclarationName() const override { return "parcelable"; }
Steven Morelandc258abc2018-07-10 14:03:38 -0700763
Jeongik Cha997281d2020-01-16 15:23:59 +0900764 void Dump(CodeWriter* writer) const override;
Jiyong Park02da7422018-07-16 16:00:26 +0900765
Casey Dahlin1ae2bc52015-10-07 18:49:10 -0700766 private:
Christopher Wiley8aa4d9f2015-11-16 19:10:45 -0800767 std::string cpp_header_;
Casey Dahlin59401da2015-10-09 18:16:45 -0700768
Casey Dahlin1ae2bc52015-10-07 18:49:10 -0700769 DISALLOW_COPY_AND_ASSIGN(AidlParcelable);
Casey Dahlin0a2f8be2015-09-28 16:15:29 -0700770};
Adam Lesinskiffa16862014-01-23 18:17:42 -0800771
Steven Moreland5557f1c2018-07-02 13:50:23 -0700772class AidlStructuredParcelable : public AidlParcelable {
773 public:
Jiyong Park18132182020-06-08 20:24:40 +0900774 AidlStructuredParcelable(const AidlLocation& location, const std::string& name,
775 const std::string& package, const std::string& comments,
Steven Moreland5557f1c2018-07-02 13:50:23 -0700776 std::vector<std::unique_ptr<AidlVariableDeclaration>>* variables);
777
778 const std::vector<std::unique_ptr<AidlVariableDeclaration>>& GetFields() const {
779 return variables_;
780 }
781
Steven Morelandc258abc2018-07-10 14:03:38 -0700782 const AidlStructuredParcelable* AsStructuredParcelable() const override { return this; }
Steven Morelanded83a282018-07-17 13:27:29 -0700783 std::string GetPreprocessDeclarationName() const override { return "structured_parcelable"; }
Steven Moreland5557f1c2018-07-02 13:50:23 -0700784
Jeongik Cha997281d2020-01-16 15:23:59 +0900785 void Dump(CodeWriter* writer) const override;
Jiyong Park02da7422018-07-16 16:00:26 +0900786
Steven Moreland0cea4aa2020-04-20 21:06:02 -0700787 std::set<AidlAnnotation::Type> GetSupportedAnnotations() const override;
Jeongik Chadb0f59e2018-11-01 18:11:21 +0900788 bool CheckValid(const AidlTypenames& typenames) const override;
Steven Morelandd59e3172020-05-11 16:42:09 -0700789 bool LanguageSpecificCheckValid(const AidlTypenames& typenames,
790 Options::Language lang) const override;
Jeongik Chadb0f59e2018-11-01 18:11:21 +0900791
Steven Moreland5557f1c2018-07-02 13:50:23 -0700792 private:
793 const std::vector<std::unique_ptr<AidlVariableDeclaration>> variables_;
794
795 DISALLOW_COPY_AND_ASSIGN(AidlStructuredParcelable);
796};
797
Daniel Norman85aed542019-08-21 12:01:14 -0700798class AidlEnumerator : public AidlNode {
799 public:
Daniel Norman2e4112d2019-10-03 10:22:35 -0700800 AidlEnumerator(const AidlLocation& location, const std::string& name, AidlConstantValue* value,
801 const std::string& comments);
Daniel Norman85aed542019-08-21 12:01:14 -0700802 virtual ~AidlEnumerator() = default;
803
804 const std::string& GetName() const { return name_; }
Will McVickerd7d18df2019-09-12 13:40:50 -0700805 AidlConstantValue* GetValue() const { return value_.get(); }
Daniel Norman2e4112d2019-10-03 10:22:35 -0700806 const std::string& GetComments() const { return comments_; }
Daniel Norman85aed542019-08-21 12:01:14 -0700807 bool CheckValid(const AidlTypeSpecifier& enum_backing_type) const;
808
809 string ValueString(const AidlTypeSpecifier& backing_type,
810 const ConstantValueDecorator& decorator) const;
811
Daniel Normanb28684e2019-10-17 15:31:39 -0700812 void SetValue(std::unique_ptr<AidlConstantValue> value) { value_ = std::move(value); }
813
Daniel Norman85aed542019-08-21 12:01:14 -0700814 private:
815 const std::string name_;
Will McVickerd7d18df2019-09-12 13:40:50 -0700816 unique_ptr<AidlConstantValue> value_;
Daniel Norman2e4112d2019-10-03 10:22:35 -0700817 const std::string comments_;
Daniel Norman85aed542019-08-21 12:01:14 -0700818
819 DISALLOW_COPY_AND_ASSIGN(AidlEnumerator);
820};
821
822class AidlEnumDeclaration : public AidlDefinedType {
823 public:
Will McVickerd7d18df2019-09-12 13:40:50 -0700824 AidlEnumDeclaration(const AidlLocation& location, const string& name,
Daniel Norman85aed542019-08-21 12:01:14 -0700825 std::vector<std::unique_ptr<AidlEnumerator>>* enumerators,
Jiyong Park18132182020-06-08 20:24:40 +0900826 const std::string& package, const std::string& comments);
Daniel Norman85aed542019-08-21 12:01:14 -0700827 virtual ~AidlEnumDeclaration() = default;
828
829 void SetBackingType(std::unique_ptr<const AidlTypeSpecifier> type);
830 const AidlTypeSpecifier& GetBackingType() const { return *backing_type_; }
831 const std::vector<std::unique_ptr<AidlEnumerator>>& GetEnumerators() const {
832 return enumerators_;
833 }
Steven Moreland59e53e42019-11-26 20:38:08 -0800834 bool Autofill();
Steven Moreland0cea4aa2020-04-20 21:06:02 -0700835 std::set<AidlAnnotation::Type> GetSupportedAnnotations() const override;
Daniel Norman85aed542019-08-21 12:01:14 -0700836 bool CheckValid(const AidlTypenames& typenames) const override;
Steven Morelandd59e3172020-05-11 16:42:09 -0700837 bool LanguageSpecificCheckValid(const AidlTypenames& /*typenames*/,
838 Options::Language) const override {
839 return true;
840 }
Daniel Norman85aed542019-08-21 12:01:14 -0700841 std::string GetPreprocessDeclarationName() const override { return "enum"; }
Jeongik Cha997281d2020-01-16 15:23:59 +0900842 void Dump(CodeWriter* writer) const override;
Daniel Norman85aed542019-08-21 12:01:14 -0700843
844 const AidlEnumDeclaration* AsEnumDeclaration() const override { return this; }
845
846 private:
847 const std::string name_;
848 const std::vector<std::unique_ptr<AidlEnumerator>> enumerators_;
849 std::unique_ptr<const AidlTypeSpecifier> backing_type_;
850
851 DISALLOW_COPY_AND_ASSIGN(AidlEnumDeclaration);
852};
853
Jiyong Park1deecc32018-07-17 01:14:41 +0900854class AidlInterface final : public AidlDefinedType {
Casey Dahlin1ae2bc52015-10-07 18:49:10 -0700855 public:
Steven Moreland46e9da82018-07-27 15:45:29 -0700856 AidlInterface(const AidlLocation& location, const std::string& name, const std::string& comments,
857 bool oneway_, std::vector<std::unique_ptr<AidlMember>>* members,
Jiyong Park18132182020-06-08 20:24:40 +0900858 const std::string& package);
Casey Dahlin1ae2bc52015-10-07 18:49:10 -0700859 virtual ~AidlInterface() = default;
860
Casey Dahlinfb7da2e2015-10-08 17:26:09 -0700861 const std::vector<std::unique_ptr<AidlMethod>>& GetMethods() const
862 { return methods_; }
Jiyong Park309668e2018-07-28 16:55:44 +0900863 std::vector<std::unique_ptr<AidlMethod>>& GetMutableMethods() { return methods_; }
Steven Moreland693640b2018-07-19 13:46:27 -0700864 const std::vector<std::unique_ptr<AidlConstantDeclaration>>& GetConstantDeclarations() const {
865 return constants_;
866 }
Casey Dahlina2f77c42015-12-01 18:26:02 -0800867
Steven Morelandc258abc2018-07-10 14:03:38 -0700868 const AidlInterface* AsInterface() const override { return this; }
Steven Morelanded83a282018-07-17 13:27:29 -0700869 std::string GetPreprocessDeclarationName() const override { return "interface"; }
Steven Moreland5557f1c2018-07-02 13:50:23 -0700870
Jeongik Cha997281d2020-01-16 15:23:59 +0900871 void Dump(CodeWriter* writer) const override;
Jiyong Park02da7422018-07-16 16:00:26 +0900872
Steven Moreland0cea4aa2020-04-20 21:06:02 -0700873 std::set<AidlAnnotation::Type> GetSupportedAnnotations() const override;
Jeongik Chadb0f59e2018-11-01 18:11:21 +0900874 bool CheckValid(const AidlTypenames& typenames) const override;
Steven Morelandd59e3172020-05-11 16:42:09 -0700875 bool LanguageSpecificCheckValid(const AidlTypenames& typenames,
876 Options::Language lang) const override;
Jeongik Chadb0f59e2018-11-01 18:11:21 +0900877
Casey Dahlin1ae2bc52015-10-07 18:49:10 -0700878 private:
Casey Dahlinfb7da2e2015-10-08 17:26:09 -0700879 std::vector<std::unique_ptr<AidlMethod>> methods_;
Steven Moreland693640b2018-07-19 13:46:27 -0700880 std::vector<std::unique_ptr<AidlConstantDeclaration>> constants_;
Casey Dahlinfb7da2e2015-10-08 17:26:09 -0700881
Casey Dahlin1ae2bc52015-10-07 18:49:10 -0700882 DISALLOW_COPY_AND_ASSIGN(AidlInterface);
Casey Dahlin0a2f8be2015-09-28 16:15:29 -0700883};
Adam Lesinskiffa16862014-01-23 18:17:42 -0800884
Casey Dahlin0edf3422015-10-07 12:34:59 -0700885class AidlImport : public AidlNode {
886 public:
Steven Moreland46e9da82018-07-27 15:45:29 -0700887 AidlImport(const AidlLocation& location, const std::string& needed_class);
Casey Dahlin0edf3422015-10-07 12:34:59 -0700888 virtual ~AidlImport() = default;
889
Casey Dahlin0edf3422015-10-07 12:34:59 -0700890 const std::string& GetFilename() const { return filename_; }
891 const std::string& GetNeededClass() const { return needed_class_; }
Casey Dahlin0edf3422015-10-07 12:34:59 -0700892
893 private:
Casey Dahlin0edf3422015-10-07 12:34:59 -0700894 std::string filename_;
895 std::string needed_class_;
Casey Dahlin0edf3422015-10-07 12:34:59 -0700896
897 DISALLOW_COPY_AND_ASSIGN(AidlImport);
Casey Dahline2507492015-09-14 17:11:20 -0700898};
899
Jiyong Park62515512020-06-08 15:57:11 +0900900// AidlDocument models an AIDL file
901class AidlDocument : public AidlNode {
902 public:
903 AidlDocument(const AidlLocation& location, std::vector<std::unique_ptr<AidlImport>>& imports,
Jiyong Park8e79b7f2020-07-20 20:52:38 +0900904 std::vector<std::unique_ptr<AidlDefinedType>>&& defined_types)
905 : AidlNode(location),
906 imports_(std::move(imports)),
907 defined_types_(std::move(defined_types)) {}
Jiyong Park62515512020-06-08 15:57:11 +0900908 const std::vector<std::unique_ptr<AidlImport>>& Imports() const { return imports_; }
Jiyong Park8e79b7f2020-07-20 20:52:38 +0900909 const std::vector<std::unique_ptr<AidlDefinedType>>& DefinedTypes() const {
910 return defined_types_;
911 }
912 AidlDocument(const AidlDocument&) = delete;
913 AidlDocument(AidlDocument&&) = delete;
914 AidlDocument& operator=(const AidlDocument&) = delete;
915 AidlDocument& operator=(AidlDocument&&) = delete;
916 ~AidlDocument() = default;
Jiyong Park62515512020-06-08 15:57:11 +0900917
918 private:
919 const std::vector<std::unique_ptr<AidlImport>> imports_;
Jiyong Park8e79b7f2020-07-20 20:52:38 +0900920 const std::vector<std::unique_ptr<AidlDefinedType>> defined_types_;
Jiyong Park62515512020-06-08 15:57:11 +0900921};