Will McVicker | d7d18df | 2019-09-12 13:40:50 -0700 | [diff] [blame] | 1 | /* |
| 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 Moreland | 9fccf58 | 2018-08-27 20:36:27 -0700 | [diff] [blame] | 17 | #pragma once |
Christopher Wiley | ec31a05 | 2016-01-25 07:28:51 -0800 | [diff] [blame] | 18 | |
Casey Dahlin | bc7a50a | 2015-09-28 19:20:50 -0700 | [diff] [blame] | 19 | #include <memory> |
Jeongik Cha | 997281d | 2020-01-16 15:23:59 +0900 | [diff] [blame] | 20 | #include <regex> |
Casey Dahlin | dd69181 | 2015-09-09 17:59:06 -0700 | [diff] [blame] | 21 | #include <string> |
Jeongik Cha | df76dc7 | 2019-11-28 00:08:47 +0900 | [diff] [blame] | 22 | #include <unordered_set> |
Casey Dahlin | bc7a50a | 2015-09-28 19:20:50 -0700 | [diff] [blame] | 23 | #include <vector> |
Casey Dahlin | dd69181 | 2015-09-09 17:59:06 -0700 | [diff] [blame] | 24 | |
Elliott Hughes | 0a62067 | 2015-12-04 13:53:18 -0800 | [diff] [blame] | 25 | #include <android-base/strings.h> |
Casey Dahlin | 73d46b0 | 2015-09-11 02:47:54 +0000 | [diff] [blame] | 26 | |
Jooyung Han | 888c5bc | 2020-12-22 17:28:47 +0900 | [diff] [blame] | 27 | #include "aidl_typenames.h" |
| 28 | #include "code_writer.h" |
| 29 | #include "diagnostics.h" |
| 30 | #include "io_delegate.h" |
| 31 | #include "options.h" |
| 32 | |
Jiyong Park | b034bf0 | 2018-07-30 17:44:33 +0900 | [diff] [blame] | 33 | using android::aidl::AidlTypenames; |
Jiyong Park | 02da742 | 2018-07-16 16:00:26 +0900 | [diff] [blame] | 34 | using android::aidl::CodeWriter; |
Jooyung Han | 888c5bc | 2020-12-22 17:28:47 +0900 | [diff] [blame] | 35 | using android::aidl::DiagnosticsContext; |
Jeongik Cha | 047c5ee | 2019-08-07 23:16:49 +0900 | [diff] [blame] | 36 | using android::aidl::Options; |
Steven Moreland | 3f658cf | 2018-08-20 13:40:54 -0700 | [diff] [blame] | 37 | using std::shared_ptr; |
Jiyong Park | 1deecc3 | 2018-07-17 01:14:41 +0900 | [diff] [blame] | 38 | using std::string; |
| 39 | using std::unique_ptr; |
| 40 | using std::vector; |
Andrei Onea | 8714b02 | 2019-02-01 18:55:54 +0000 | [diff] [blame] | 41 | class AidlNode; |
| 42 | |
| 43 | namespace android { |
| 44 | namespace aidl { |
| 45 | namespace mappings { |
| 46 | std::string dump_location(const AidlNode& method); |
| 47 | } // namespace mappings |
Mathew Inwood | adb7467 | 2019-11-29 14:01:53 +0000 | [diff] [blame] | 48 | namespace java { |
| 49 | std::string dump_location(const AidlNode& method); |
| 50 | } // namespace java |
Andrei Onea | 8714b02 | 2019-02-01 18:55:54 +0000 | [diff] [blame] | 51 | } // namespace aidl |
| 52 | } // namespace android |
| 53 | |
Steven Moreland | 46e9da8 | 2018-07-27 15:45:29 -0700 | [diff] [blame] | 54 | class AidlLocation { |
Casey Dahlin | bc7a50a | 2015-09-28 19:20:50 -0700 | [diff] [blame] | 55 | public: |
Steven Moreland | 46e9da8 | 2018-07-27 15:45:29 -0700 | [diff] [blame] | 56 | struct Point { |
Dan Willemsen | 609ba6d | 2019-12-30 10:44:00 -0800 | [diff] [blame] | 57 | int line; |
| 58 | int column; |
Steven Moreland | 46e9da8 | 2018-07-27 15:45:29 -0700 | [diff] [blame] | 59 | }; |
| 60 | |
Devin Moore | df93ebb | 2020-03-25 14:03:35 -0700 | [diff] [blame] | 61 | enum class Source { |
| 62 | // From internal aidl source code |
| 63 | INTERNAL = 0, |
| 64 | // From a parsed file |
| 65 | EXTERNAL = 1 |
| 66 | }; |
| 67 | |
| 68 | AidlLocation(const std::string& file, Point begin, Point end, Source source); |
Devin Moore | 5de18ed | 2020-04-02 13:52:29 -0700 | [diff] [blame] | 69 | AidlLocation(const std::string& file, Source source) |
| 70 | : AidlLocation(file, {0, 0}, {0, 0}, source) {} |
Devin Moore | df93ebb | 2020-03-25 14:03:35 -0700 | [diff] [blame] | 71 | |
| 72 | bool IsInternal() const { return source_ == Source::INTERNAL; } |
Steven Moreland | 46e9da8 | 2018-07-27 15:45:29 -0700 | [diff] [blame] | 73 | |
Devin Moore | 5de18ed | 2020-04-02 13:52:29 -0700 | [diff] [blame] | 74 | // The first line of a file is line 1. |
| 75 | bool LocationKnown() const { return begin_.line != 0; } |
| 76 | |
Steven Moreland | 46e9da8 | 2018-07-27 15:45:29 -0700 | [diff] [blame] | 77 | friend std::ostream& operator<<(std::ostream& os, const AidlLocation& l); |
Andrei Onea | 8714b02 | 2019-02-01 18:55:54 +0000 | [diff] [blame] | 78 | friend class AidlNode; |
Casey Dahlin | bc7a50a | 2015-09-28 19:20:50 -0700 | [diff] [blame] | 79 | |
| 80 | private: |
Steven Moreland | 541788d | 2020-05-21 22:05:52 +0000 | [diff] [blame] | 81 | // INTENTIONALLY HIDDEN: only operator<< should access details here. |
| 82 | // Otherwise, locations should only ever be copied around to construct new |
| 83 | // objects. |
Steven Moreland | 46e9da8 | 2018-07-27 15:45:29 -0700 | [diff] [blame] | 84 | const std::string file_; |
| 85 | Point begin_; |
| 86 | Point end_; |
Devin Moore | df93ebb | 2020-03-25 14:03:35 -0700 | [diff] [blame] | 87 | Source source_; |
Steven Moreland | 46e9da8 | 2018-07-27 15:45:29 -0700 | [diff] [blame] | 88 | }; |
| 89 | |
Devin Moore | df93ebb | 2020-03-25 14:03:35 -0700 | [diff] [blame] | 90 | #define AIDL_LOCATION_HERE \ |
Steven Moreland | 2178081 | 2020-09-11 01:29:45 +0000 | [diff] [blame] | 91 | (AidlLocation{__FILE__, {__LINE__, 0}, {__LINE__, 0}, AidlLocation::Source::INTERNAL}) |
Steven Moreland | 02e012e | 2018-08-02 14:58:10 -0700 | [diff] [blame] | 92 | |
Steven Moreland | 46e9da8 | 2018-07-27 15:45:29 -0700 | [diff] [blame] | 93 | std::ostream& operator<<(std::ostream& os, const AidlLocation& l); |
| 94 | |
| 95 | // Anything that is locatable in a .aidl file. |
| 96 | class AidlNode { |
| 97 | public: |
| 98 | AidlNode(const AidlLocation& location); |
Steven Moreland | 3f658cf | 2018-08-20 13:40:54 -0700 | [diff] [blame] | 99 | |
| 100 | AidlNode(const AidlNode&) = default; |
Steven Moreland | 46e9da8 | 2018-07-27 15:45:29 -0700 | [diff] [blame] | 101 | virtual ~AidlNode() = default; |
| 102 | |
Jiyong Park | d800fef | 2020-07-22 18:09:43 +0900 | [diff] [blame] | 103 | AidlNode(AidlNode&&) = delete; |
| 104 | AidlNode& operator=(AidlNode&&) = delete; |
| 105 | |
Devin Moore | df93ebb | 2020-03-25 14:03:35 -0700 | [diff] [blame] | 106 | // To be able to print AidlLocation |
Steven Moreland | b0d15a5 | 2020-03-31 14:03:47 -0700 | [diff] [blame] | 107 | friend class AidlErrorLog; |
Andrei Onea | 8714b02 | 2019-02-01 18:55:54 +0000 | [diff] [blame] | 108 | friend std::string android::aidl::mappings::dump_location(const AidlNode&); |
Mathew Inwood | adb7467 | 2019-11-29 14:01:53 +0000 | [diff] [blame] | 109 | friend std::string android::aidl::java::dump_location(const AidlNode&); |
Steven Moreland | 46e9da8 | 2018-07-27 15:45:29 -0700 | [diff] [blame] | 110 | |
Devin Moore | df93ebb | 2020-03-25 14:03:35 -0700 | [diff] [blame] | 111 | const AidlLocation& GetLocation() const { return location_; } |
| 112 | |
Steven Moreland | 46e9da8 | 2018-07-27 15:45:29 -0700 | [diff] [blame] | 113 | private: |
Mathew Inwood | adb7467 | 2019-11-29 14:01:53 +0000 | [diff] [blame] | 114 | std::string PrintLine() const; |
Andrei Onea | 8714b02 | 2019-02-01 18:55:54 +0000 | [diff] [blame] | 115 | std::string PrintLocation() const; |
Steven Moreland | 46e9da8 | 2018-07-27 15:45:29 -0700 | [diff] [blame] | 116 | const AidlLocation location_; |
Casey Dahlin | bc7a50a | 2015-09-28 19:20:50 -0700 | [diff] [blame] | 117 | }; |
| 118 | |
Jeongik Cha | df76dc7 | 2019-11-28 00:08:47 +0900 | [diff] [blame] | 119 | // unique_ptr<AidlTypeSpecifier> for type arugment, |
| 120 | // std::string for type parameter(T, U, and so on). |
| 121 | template <typename T> |
| 122 | class AidlParameterizable { |
| 123 | public: |
| 124 | AidlParameterizable(std::vector<T>* type_params) : type_params_(type_params) {} |
| 125 | virtual ~AidlParameterizable() = default; |
| 126 | bool IsGeneric() const { return type_params_ != nullptr; } |
| 127 | const std::vector<T>& GetTypeParameters() const { return *type_params_; } |
| 128 | bool CheckValid() const; |
| 129 | |
Steven Moreland | 6c07b83 | 2020-10-29 23:39:53 +0000 | [diff] [blame] | 130 | __attribute__((warn_unused_result)) bool SetTypeParameters(std::vector<T>* type_params) { |
| 131 | if (type_params_) return false; |
| 132 | type_params_.reset(type_params); |
| 133 | return true; |
| 134 | } |
| 135 | |
Jeongik Cha | df76dc7 | 2019-11-28 00:08:47 +0900 | [diff] [blame] | 136 | virtual const AidlNode& AsAidlNode() const = 0; |
| 137 | |
| 138 | protected: |
| 139 | AidlParameterizable(const AidlParameterizable&); |
| 140 | |
| 141 | private: |
Steven Moreland | 6c07b83 | 2020-10-29 23:39:53 +0000 | [diff] [blame] | 142 | unique_ptr<std::vector<T>> type_params_; |
Jeongik Cha | df76dc7 | 2019-11-28 00:08:47 +0900 | [diff] [blame] | 143 | static_assert(std::is_same<T, unique_ptr<AidlTypeSpecifier>>::value || |
| 144 | std::is_same<T, std::string>::value); |
| 145 | }; |
| 146 | template <> |
| 147 | bool AidlParameterizable<std::string>::CheckValid() const; |
| 148 | |
Andrei Onea | 9445fc6 | 2019-06-27 18:11:59 +0100 | [diff] [blame] | 149 | class AidlConstantValue; |
| 150 | class AidlConstantDeclaration; |
| 151 | |
| 152 | // Transforms a value string into a language specific form. Raw value as produced by |
| 153 | // AidlConstantValue. |
| 154 | using ConstantValueDecorator = |
| 155 | std::function<std::string(const AidlTypeSpecifier& type, const std::string& raw_value)>; |
| 156 | |
Jiyong Park | 68bc77a | 2018-07-19 19:00:45 +0900 | [diff] [blame] | 157 | class AidlAnnotation : public AidlNode { |
| 158 | public: |
Steven Moreland | 0cea4aa | 2020-04-20 21:06:02 -0700 | [diff] [blame] | 159 | enum class Type { |
| 160 | BACKING = 1, |
| 161 | HIDE, |
| 162 | JAVA_STABLE_PARCELABLE, |
| 163 | UNSUPPORTED_APP_USAGE, |
| 164 | VINTF_STABILITY, |
| 165 | NULLABLE, |
| 166 | UTF8_IN_CPP, |
Steven Moreland | a7764e5 | 2020-10-27 17:29:29 +0000 | [diff] [blame] | 167 | SENSITIVE_DATA, |
Jiyong Park | bf5fd5c | 2020-06-05 19:48:05 +0900 | [diff] [blame] | 168 | JAVA_PASSTHROUGH, |
Jooyung Han | 9034500 | 2020-10-23 15:28:53 +0900 | [diff] [blame] | 169 | JAVA_DERIVE, |
Jeongik Cha | d0a1027 | 2020-08-06 16:33:36 +0900 | [diff] [blame] | 170 | JAVA_ONLY_IMMUTABLE, |
Devin Moore | c7e47a3 | 2020-08-07 10:55:25 -0700 | [diff] [blame] | 171 | FIXED_SIZE, |
Jiyong Park | 27fd7fd | 2020-08-27 16:25:09 +0900 | [diff] [blame] | 172 | DESCRIPTOR, |
Andrei Homescu | e61feb5 | 2020-08-18 15:44:24 -0700 | [diff] [blame] | 173 | RUST_DERIVE, |
Steven Moreland | 0cea4aa | 2020-04-20 21:06:02 -0700 | [diff] [blame] | 174 | }; |
| 175 | static std::string TypeToString(Type type); |
| 176 | |
Andrei Onea | 9445fc6 | 2019-06-27 18:11:59 +0100 | [diff] [blame] | 177 | static AidlAnnotation* Parse( |
| 178 | const AidlLocation& location, const string& name, |
| 179 | std::map<std::string, std::shared_ptr<AidlConstantValue>>* parameter_list); |
Steven Moreland | 46e9da8 | 2018-07-27 15:45:29 -0700 | [diff] [blame] | 180 | |
Steven Moreland | 3f658cf | 2018-08-20 13:40:54 -0700 | [diff] [blame] | 181 | AidlAnnotation(const AidlAnnotation&) = default; |
Steven Moreland | 3be7577 | 2018-08-20 13:27:43 -0700 | [diff] [blame] | 182 | AidlAnnotation(AidlAnnotation&&) = default; |
Jiyong Park | 68bc77a | 2018-07-19 19:00:45 +0900 | [diff] [blame] | 183 | virtual ~AidlAnnotation() = default; |
Andrei Onea | 9445fc6 | 2019-06-27 18:11:59 +0100 | [diff] [blame] | 184 | bool CheckValid() const; |
Steven Moreland | 3be7577 | 2018-08-20 13:27:43 -0700 | [diff] [blame] | 185 | |
Jooyung Han | d902a97 | 2020-10-23 17:32:44 +0900 | [diff] [blame] | 186 | const string& GetName() const { return schema_.name; } |
Steven Moreland | 0cea4aa | 2020-04-20 21:06:02 -0700 | [diff] [blame] | 187 | const Type& GetType() const { return schema_.type; } |
Jooyung Han | d902a97 | 2020-10-23 17:32:44 +0900 | [diff] [blame] | 188 | bool Repeatable() const { return schema_.repeatable; } |
Jooyung Han | 965e31d | 2020-11-27 12:30:16 +0900 | [diff] [blame] | 189 | |
| 190 | // ToString is for dumping AIDL. |
| 191 | // Returns string representation of this annotation. |
| 192 | // e.g) "@RustDerive(Clone=true, Copy=true)" |
| 193 | string ToString() const; |
| 194 | |
Jooyung Han | b3c77ed | 2020-12-26 02:02:45 +0900 | [diff] [blame^] | 195 | template <typename T> |
| 196 | std::optional<T> ParamValue(const std::string& param_name) const; |
| 197 | |
Andrei Onea | 9445fc6 | 2019-06-27 18:11:59 +0100 | [diff] [blame] | 198 | std::map<std::string, std::string> AnnotationParams( |
| 199 | const ConstantValueDecorator& decorator) const; |
Jiyong Park | a6605ab | 2018-11-11 14:30:21 +0900 | [diff] [blame] | 200 | const string& GetComments() const { return comments_; } |
| 201 | void SetComments(const string& comments) { comments_ = comments; } |
Jiyong Park | 68bc77a | 2018-07-19 19:00:45 +0900 | [diff] [blame] | 202 | |
| 203 | private: |
Steven Moreland | 0cea4aa | 2020-04-20 21:06:02 -0700 | [diff] [blame] | 204 | struct Schema { |
| 205 | AidlAnnotation::Type type; |
| 206 | |
| 207 | // text name in .aidl file, e.g. "nullable" |
| 208 | std::string name; |
| 209 | |
| 210 | // map from param name -> value type |
Jooyung Han | 5c2fcae | 2020-12-26 00:04:39 +0900 | [diff] [blame] | 211 | std::map<std::string, const AidlTypeSpecifier&> supported_parameters; |
Jooyung Han | d902a97 | 2020-10-23 17:32:44 +0900 | [diff] [blame] | 212 | |
| 213 | bool repeatable; |
Jooyung Han | 5721a23 | 2020-12-24 04:34:55 +0900 | [diff] [blame] | 214 | |
| 215 | std::vector<std::string> required_parameters = {}; |
Steven Moreland | 0cea4aa | 2020-04-20 21:06:02 -0700 | [diff] [blame] | 216 | }; |
| 217 | static const std::vector<Schema>& AllSchemas(); |
| 218 | |
| 219 | AidlAnnotation(const AidlLocation& location, const Schema& schema, |
Andrei Onea | 9445fc6 | 2019-06-27 18:11:59 +0100 | [diff] [blame] | 220 | std::map<std::string, std::shared_ptr<AidlConstantValue>>&& parameters); |
Steven Moreland | 0cea4aa | 2020-04-20 21:06:02 -0700 | [diff] [blame] | 221 | |
| 222 | const Schema& schema_; |
Jiyong Park | a6605ab | 2018-11-11 14:30:21 +0900 | [diff] [blame] | 223 | string comments_; |
Andrei Onea | 9445fc6 | 2019-06-27 18:11:59 +0100 | [diff] [blame] | 224 | std::map<std::string, std::shared_ptr<AidlConstantValue>> parameters_; |
Jiyong Park | 68bc77a | 2018-07-19 19:00:45 +0900 | [diff] [blame] | 225 | }; |
| 226 | |
Steven Moreland | 3be7577 | 2018-08-20 13:27:43 -0700 | [diff] [blame] | 227 | static inline bool operator<(const AidlAnnotation& lhs, const AidlAnnotation& rhs) { |
| 228 | return lhs.GetName() < rhs.GetName(); |
| 229 | } |
| 230 | static inline bool operator==(const AidlAnnotation& lhs, const AidlAnnotation& rhs) { |
| 231 | return lhs.GetName() == rhs.GetName(); |
| 232 | } |
Jiyong Park | 3656c3c | 2018-08-01 20:02:01 +0900 | [diff] [blame] | 233 | |
Casey Dahlin | e792293 | 2016-02-29 17:23:01 -0800 | [diff] [blame] | 234 | class AidlAnnotatable : public AidlNode { |
Casey Dahlin | 0ee3758 | 2015-09-30 16:31:55 -0700 | [diff] [blame] | 235 | public: |
Steven Moreland | 46e9da8 | 2018-07-27 15:45:29 -0700 | [diff] [blame] | 236 | AidlAnnotatable(const AidlLocation& location); |
Steven Moreland | 3f658cf | 2018-08-20 13:40:54 -0700 | [diff] [blame] | 237 | |
| 238 | AidlAnnotatable(const AidlAnnotatable&) = default; |
| 239 | AidlAnnotatable(AidlAnnotatable&&) = default; |
Casey Dahlin | e792293 | 2016-02-29 17:23:01 -0800 | [diff] [blame] | 240 | virtual ~AidlAnnotatable() = default; |
| 241 | |
Artur Satayev | 91fe871 | 2019-07-29 13:06:01 +0100 | [diff] [blame] | 242 | void Annotate(vector<AidlAnnotation>&& annotations) { |
| 243 | for (auto& annotation : annotations) { |
| 244 | annotations_.emplace_back(std::move(annotation)); |
| 245 | } |
| 246 | } |
Jiyong Park | 68bc77a | 2018-07-19 19:00:45 +0900 | [diff] [blame] | 247 | bool IsNullable() const; |
Jiyong Park | 68bc77a | 2018-07-19 19:00:45 +0900 | [diff] [blame] | 248 | bool IsUtf8InCpp() const; |
Steven Moreland | a7764e5 | 2020-10-27 17:29:29 +0000 | [diff] [blame] | 249 | bool IsSensitiveData() const; |
Steven Moreland | a57d0a6 | 2019-07-30 09:41:14 -0700 | [diff] [blame] | 250 | bool IsVintfStability() const; |
Jeongik Cha | d0a1027 | 2020-08-06 16:33:36 +0900 | [diff] [blame] | 251 | bool IsJavaOnlyImmutable() const; |
Devin Moore | c7e47a3 | 2020-08-07 10:55:25 -0700 | [diff] [blame] | 252 | bool IsFixedSize() const; |
Jeongik Cha | 88f95a8 | 2020-01-15 13:02:16 +0900 | [diff] [blame] | 253 | bool IsStableApiParcelable(Options::Language lang) const; |
Makoto Onuki | 78a1c1c | 2020-03-04 16:57:23 -0800 | [diff] [blame] | 254 | bool IsHide() const; |
Jooyung Han | 829ec7c | 2020-12-02 12:07:36 +0900 | [diff] [blame] | 255 | bool JavaDerive(const std::string& method) const; |
Jiyong Park | 27fd7fd | 2020-08-27 16:25:09 +0900 | [diff] [blame] | 256 | std::string GetDescriptor() const; |
Andrei Onea | 9445fc6 | 2019-06-27 18:11:59 +0100 | [diff] [blame] | 257 | |
Steven Moreland | 7e4b950 | 2020-02-20 18:10:42 -0800 | [diff] [blame] | 258 | void DumpAnnotations(CodeWriter* writer) const; |
| 259 | |
Andrei Onea | 9445fc6 | 2019-06-27 18:11:59 +0100 | [diff] [blame] | 260 | const AidlAnnotation* UnsupportedAppUsage() const; |
Andrei Homescu | e61feb5 | 2020-08-18 15:44:24 -0700 | [diff] [blame] | 261 | const AidlAnnotation* RustDerive() const; |
Jooyung Han | 672557b | 2020-12-24 05:18:00 +0900 | [diff] [blame] | 262 | const AidlAnnotation* BackingType() const; |
Jooyung Han | 965e31d | 2020-11-27 12:30:16 +0900 | [diff] [blame] | 263 | |
| 264 | // ToString is for dumping AIDL. |
| 265 | // Returns string representation of annotations. |
| 266 | // e.g) "@JavaDerive(toString=true) @RustDerive(Clone=true, Copy=true)" |
Jiyong Park | 68bc77a | 2018-07-19 19:00:45 +0900 | [diff] [blame] | 267 | std::string ToString() const; |
Casey Dahlin | e792293 | 2016-02-29 17:23:01 -0800 | [diff] [blame] | 268 | |
Jiyong Park | a6605ab | 2018-11-11 14:30:21 +0900 | [diff] [blame] | 269 | const vector<AidlAnnotation>& GetAnnotations() const { return annotations_; } |
Jooyung Han | 888c5bc | 2020-12-22 17:28:47 +0900 | [diff] [blame] | 270 | bool CheckValid(const AidlTypenames&) const; |
Jiyong Park | 3656c3c | 2018-08-01 20:02:01 +0900 | [diff] [blame] | 271 | |
Steven Moreland | 181144c | 2020-04-20 19:57:56 -0700 | [diff] [blame] | 272 | protected: |
Steven Moreland | 0cea4aa | 2020-04-20 21:06:02 -0700 | [diff] [blame] | 273 | virtual std::set<AidlAnnotation::Type> GetSupportedAnnotations() const = 0; |
Steven Moreland | 181144c | 2020-04-20 19:57:56 -0700 | [diff] [blame] | 274 | |
Casey Dahlin | e792293 | 2016-02-29 17:23:01 -0800 | [diff] [blame] | 275 | private: |
Jiyong Park | a6605ab | 2018-11-11 14:30:21 +0900 | [diff] [blame] | 276 | vector<AidlAnnotation> annotations_; |
Casey Dahlin | e792293 | 2016-02-29 17:23:01 -0800 | [diff] [blame] | 277 | }; |
| 278 | |
Jiyong Park | 1deecc3 | 2018-07-17 01:14:41 +0900 | [diff] [blame] | 279 | // AidlTypeSpecifier represents a reference to either a built-in type, |
| 280 | // a defined type, or a variant (e.g., array of generic) of a type. |
Jeongik Cha | df76dc7 | 2019-11-28 00:08:47 +0900 | [diff] [blame] | 281 | class AidlTypeSpecifier final : public AidlAnnotatable, |
| 282 | public AidlParameterizable<unique_ptr<AidlTypeSpecifier>> { |
Casey Dahlin | e792293 | 2016-02-29 17:23:01 -0800 | [diff] [blame] | 283 | public: |
Steven Moreland | 46e9da8 | 2018-07-27 15:45:29 -0700 | [diff] [blame] | 284 | AidlTypeSpecifier(const AidlLocation& location, const string& unresolved_name, bool is_array, |
| 285 | vector<unique_ptr<AidlTypeSpecifier>>* type_params, const string& comments); |
Jiyong Park | d59a10d | 2018-07-18 11:12:55 +0900 | [diff] [blame] | 286 | virtual ~AidlTypeSpecifier() = default; |
Casey Dahlin | 0ee3758 | 2015-09-30 16:31:55 -0700 | [diff] [blame] | 287 | |
Steven Moreland | 3f658cf | 2018-08-20 13:40:54 -0700 | [diff] [blame] | 288 | // Copy of this type which is not an array. |
Jooyung Han | d2fa023 | 2020-10-19 02:51:41 +0900 | [diff] [blame] | 289 | const AidlTypeSpecifier& ArrayBase() const; |
Steven Moreland | 3f658cf | 2018-08-20 13:40:54 -0700 | [diff] [blame] | 290 | |
Jiyong Park | 1deecc3 | 2018-07-17 01:14:41 +0900 | [diff] [blame] | 291 | // Returns the full-qualified name of the base type. |
| 292 | // int -> int |
| 293 | // int[] -> int |
| 294 | // List<String> -> List |
| 295 | // IFoo -> foo.bar.IFoo (if IFoo is in package foo.bar) |
| 296 | const string& GetName() const { |
| 297 | if (IsResolved()) { |
| 298 | return fully_qualified_name_; |
| 299 | } else { |
| 300 | return GetUnresolvedName(); |
| 301 | } |
| 302 | } |
Casey Dahlin | 0ee3758 | 2015-09-30 16:31:55 -0700 | [diff] [blame] | 303 | |
Jooyung Han | 965e31d | 2020-11-27 12:30:16 +0900 | [diff] [blame] | 304 | // ToString is for dumping AIDL. |
| 305 | // Returns string representation of this type specifier including annotations. |
| 306 | // This is "annotations type_name type_params? array_marker?". |
| 307 | // e.g) "@utf8InCpp String[]"; |
| 308 | std::string ToString() const; |
Jiyong Park | 1deecc3 | 2018-07-17 01:14:41 +0900 | [diff] [blame] | 309 | |
Jooyung Han | 965e31d | 2020-11-27 12:30:16 +0900 | [diff] [blame] | 310 | // Signature is for comparing AIDL types. |
| 311 | // Returns string representation of this type specifier. |
| 312 | // This is "type_name type_params? array_marker?". |
| 313 | // e.g.) "String[]" (even if it is annotated with @utf8InCpp) |
Jiyong Park | 02da742 | 2018-07-16 16:00:26 +0900 | [diff] [blame] | 314 | std::string Signature() const; |
| 315 | |
Jiyong Park | 1deecc3 | 2018-07-17 01:14:41 +0900 | [diff] [blame] | 316 | const string& GetUnresolvedName() const { return unresolved_name_; } |
| 317 | |
Jeongik Cha | 997281d | 2020-01-16 15:23:59 +0900 | [diff] [blame] | 318 | bool IsHidden() const; |
| 319 | |
Jiyong Park | 1deecc3 | 2018-07-17 01:14:41 +0900 | [diff] [blame] | 320 | const string& GetComments() const { return comments_; } |
| 321 | |
Jeongik Cha | 1a7ab64 | 2019-07-29 17:31:02 +0900 | [diff] [blame] | 322 | const std::vector<std::string> GetSplitName() const { return split_name_; } |
| 323 | |
Jiyong Park | a6605ab | 2018-11-11 14:30:21 +0900 | [diff] [blame] | 324 | void SetComments(const string& comment) { comments_ = comment; } |
| 325 | |
Jiyong Park | 1deecc3 | 2018-07-17 01:14:41 +0900 | [diff] [blame] | 326 | bool IsResolved() const { return fully_qualified_name_ != ""; } |
| 327 | |
| 328 | bool IsArray() const { return is_array_; } |
| 329 | |
Steven Moreland | 6c07b83 | 2020-10-29 23:39:53 +0000 | [diff] [blame] | 330 | __attribute__((warn_unused_result)) bool SetArray() { |
| 331 | if (is_array_) return false; |
| 332 | is_array_ = true; |
| 333 | return true; |
| 334 | } |
| 335 | |
Jiyong Park | 1deecc3 | 2018-07-17 01:14:41 +0900 | [diff] [blame] | 336 | // Resolve the base type name to a fully-qualified name. Return false if the |
| 337 | // resolution fails. |
Daniel Norman | 716d311 | 2019-09-10 13:11:56 -0700 | [diff] [blame] | 338 | bool Resolve(const AidlTypenames& typenames); |
Casey Dahlin | 0ee3758 | 2015-09-30 16:31:55 -0700 | [diff] [blame] | 339 | |
Steven Moreland | 0cea4aa | 2020-04-20 21:06:02 -0700 | [diff] [blame] | 340 | std::set<AidlAnnotation::Type> GetSupportedAnnotations() const override; |
Jooyung Han | 888c5bc | 2020-12-22 17:28:47 +0900 | [diff] [blame] | 341 | bool CheckValid(const AidlTypenames& typenames) const; |
Steven Moreland | d59e317 | 2020-05-11 16:42:09 -0700 | [diff] [blame] | 342 | bool LanguageSpecificCheckValid(const AidlTypenames& typenames, Options::Language lang) const; |
Jeongik Cha | df76dc7 | 2019-11-28 00:08:47 +0900 | [diff] [blame] | 343 | const AidlNode& AsAidlNode() const override { return *this; } |
Jiyong Park | 1d2df7d | 2018-07-23 15:22:50 +0900 | [diff] [blame] | 344 | |
Jooyung Han | e9bb9de | 2020-11-01 22:16:57 +0900 | [diff] [blame] | 345 | const AidlDefinedType* GetDefinedType() const; |
| 346 | |
Casey Dahlin | 0ee3758 | 2015-09-30 16:31:55 -0700 | [diff] [blame] | 347 | private: |
Steven Moreland | 3f658cf | 2018-08-20 13:40:54 -0700 | [diff] [blame] | 348 | AidlTypeSpecifier(const AidlTypeSpecifier&) = default; |
| 349 | |
Jiyong Park | 1deecc3 | 2018-07-17 01:14:41 +0900 | [diff] [blame] | 350 | const string unresolved_name_; |
| 351 | string fully_qualified_name_; |
Steven Moreland | 3f658cf | 2018-08-20 13:40:54 -0700 | [diff] [blame] | 352 | bool is_array_; |
Jiyong Park | a6605ab | 2018-11-11 14:30:21 +0900 | [diff] [blame] | 353 | string comments_; |
Jeongik Cha | 1a7ab64 | 2019-07-29 17:31:02 +0900 | [diff] [blame] | 354 | vector<string> split_name_; |
Jooyung Han | 690f584 | 2020-12-04 13:02:04 +0900 | [diff] [blame] | 355 | const AidlDefinedType* defined_type_ = nullptr; // set when Resolve() for defined types |
Jooyung Han | d2fa023 | 2020-10-19 02:51:41 +0900 | [diff] [blame] | 356 | mutable shared_ptr<AidlTypeSpecifier> array_base_; |
Casey Dahlin | 0ee3758 | 2015-09-30 16:31:55 -0700 | [diff] [blame] | 357 | }; |
| 358 | |
Steven Moreland | 860b194 | 2018-08-16 14:59:28 -0700 | [diff] [blame] | 359 | // Returns the universal value unaltered. |
| 360 | std::string AidlConstantValueDecorator(const AidlTypeSpecifier& type, const std::string& raw_value); |
| 361 | |
Steven Moreland | 9ea10e3 | 2018-07-19 15:26:09 -0700 | [diff] [blame] | 362 | class AidlConstantValue; |
Jooyung Han | 3f347ca | 2020-12-01 12:41:50 +0900 | [diff] [blame] | 363 | class AidlMethod; |
| 364 | class AidlConstantDeclaration; |
| 365 | class AidlVariableDeclaration; |
| 366 | |
| 367 | class AidlMember : public AidlNode { |
| 368 | public: |
| 369 | AidlMember(const AidlLocation& location); |
| 370 | virtual ~AidlMember() = default; |
| 371 | |
| 372 | // non-copyable, non-movable |
| 373 | AidlMember(const AidlMember&) = delete; |
| 374 | AidlMember(AidlMember&&) = delete; |
| 375 | AidlMember& operator=(const AidlMember&) = delete; |
| 376 | AidlMember& operator=(AidlMember&&) = delete; |
| 377 | |
Jooyung Han | 829ec7c | 2020-12-02 12:07:36 +0900 | [diff] [blame] | 378 | virtual const AidlMethod* AsMethod() const { return nullptr; } |
| 379 | virtual const AidlConstantDeclaration* AsConstantDeclaration() const { return nullptr; } |
| 380 | virtual const AidlVariableDeclaration* AsVariableDeclaration() const { return nullptr; } |
| 381 | |
| 382 | AidlMethod* AsMethod() { |
| 383 | return const_cast<AidlMethod*>(const_cast<const AidlMember*>(this)->AsMethod()); |
| 384 | } |
| 385 | AidlConstantDeclaration* AsConstantDeclaration() { |
| 386 | return const_cast<AidlConstantDeclaration*>( |
| 387 | const_cast<const AidlMember*>(this)->AsConstantDeclaration()); |
| 388 | } |
| 389 | AidlVariableDeclaration* AsVariableDeclaration() { |
| 390 | return const_cast<AidlVariableDeclaration*>( |
| 391 | const_cast<const AidlMember*>(this)->AsVariableDeclaration()); |
| 392 | } |
Jooyung Han | 3f347ca | 2020-12-01 12:41:50 +0900 | [diff] [blame] | 393 | }; |
| 394 | |
Steven Moreland | 541788d | 2020-05-21 22:05:52 +0000 | [diff] [blame] | 395 | // TODO: This class is used for method arguments and also parcelable fields, |
| 396 | // and it should be split up since default values don't apply to method |
| 397 | // arguments |
Jooyung Han | 3f347ca | 2020-12-01 12:41:50 +0900 | [diff] [blame] | 398 | class AidlVariableDeclaration : public AidlMember { |
Steven Moreland | 5557f1c | 2018-07-02 13:50:23 -0700 | [diff] [blame] | 399 | public: |
Steven Moreland | 46e9da8 | 2018-07-27 15:45:29 -0700 | [diff] [blame] | 400 | AidlVariableDeclaration(const AidlLocation& location, AidlTypeSpecifier* type, |
| 401 | const std::string& name); |
| 402 | AidlVariableDeclaration(const AidlLocation& location, AidlTypeSpecifier* type, |
| 403 | const std::string& name, AidlConstantValue* default_value); |
Steven Moreland | 5557f1c | 2018-07-02 13:50:23 -0700 | [diff] [blame] | 404 | virtual ~AidlVariableDeclaration() = default; |
| 405 | |
Jiyong Park | d800fef | 2020-07-22 18:09:43 +0900 | [diff] [blame] | 406 | // non-copyable, non-movable |
| 407 | AidlVariableDeclaration(const AidlVariableDeclaration&) = delete; |
| 408 | AidlVariableDeclaration(AidlVariableDeclaration&&) = delete; |
| 409 | AidlVariableDeclaration& operator=(const AidlVariableDeclaration&) = delete; |
| 410 | AidlVariableDeclaration& operator=(AidlVariableDeclaration&&) = delete; |
| 411 | |
Jooyung Han | 829ec7c | 2020-12-02 12:07:36 +0900 | [diff] [blame] | 412 | const AidlVariableDeclaration* AsVariableDeclaration() const override { return this; } |
Jooyung Han | 3f347ca | 2020-12-01 12:41:50 +0900 | [diff] [blame] | 413 | |
Steven Moreland | 5557f1c | 2018-07-02 13:50:23 -0700 | [diff] [blame] | 414 | std::string GetName() const { return name_; } |
Jooyung Han | acae85d | 2020-10-28 16:39:09 +0900 | [diff] [blame] | 415 | std::string GetCapitalizedName() const; |
Jiyong Park | d59a10d | 2018-07-18 11:12:55 +0900 | [diff] [blame] | 416 | const AidlTypeSpecifier& GetType() const { return *type_; } |
Steven Moreland | 541788d | 2020-05-21 22:05:52 +0000 | [diff] [blame] | 417 | // if this was constructed explicitly with a default value |
| 418 | bool IsDefaultUserSpecified() const { return default_user_specified_; } |
| 419 | // will return the default value this is constructed with or a default value |
| 420 | // if one is available |
Steven Moreland | 9ea10e3 | 2018-07-19 15:26:09 -0700 | [diff] [blame] | 421 | const AidlConstantValue* GetDefaultValue() const { return default_value_.get(); } |
Jooyung Han | 53fb424 | 2020-12-17 16:03:49 +0900 | [diff] [blame] | 422 | bool HasUsefulDefaultValue() const; |
Steven Moreland | 9ea10e3 | 2018-07-19 15:26:09 -0700 | [diff] [blame] | 423 | |
Jiyong Park | d59a10d | 2018-07-18 11:12:55 +0900 | [diff] [blame] | 424 | AidlTypeSpecifier* GetMutableType() { return type_.get(); } |
Steven Moreland | 5557f1c | 2018-07-02 13:50:23 -0700 | [diff] [blame] | 425 | |
Jeongik Cha | db0f59e | 2018-11-01 18:11:21 +0900 | [diff] [blame] | 426 | bool CheckValid(const AidlTypenames& typenames) const; |
Jooyung Han | 965e31d | 2020-11-27 12:30:16 +0900 | [diff] [blame] | 427 | |
| 428 | // ToString is for dumping AIDL. |
| 429 | // Returns string representation of this variable decl including default value. |
| 430 | // This is "annotations type name default_value?". |
| 431 | // e.g) "@utf8InCpp String[] names = {"hello"}" |
Steven Moreland | 5557f1c | 2018-07-02 13:50:23 -0700 | [diff] [blame] | 432 | std::string ToString() const; |
Jooyung Han | 965e31d | 2020-11-27 12:30:16 +0900 | [diff] [blame] | 433 | |
| 434 | // Signature is for comparing AIDL types. |
| 435 | // Returns string representation of this variable decl. |
| 436 | // This is "type name". |
| 437 | // e.g) "String[] name" (even if it is annotated with @utf8InCpp and has a default value.) |
Jiyong Park | 02da742 | 2018-07-16 16:00:26 +0900 | [diff] [blame] | 438 | std::string Signature() const; |
Steven Moreland | 5557f1c | 2018-07-02 13:50:23 -0700 | [diff] [blame] | 439 | |
Steven Moreland | 860b194 | 2018-08-16 14:59:28 -0700 | [diff] [blame] | 440 | std::string ValueString(const ConstantValueDecorator& decorator) const; |
Steven Moreland | 2529432 | 2018-08-07 18:13:55 -0700 | [diff] [blame] | 441 | |
Steven Moreland | 5557f1c | 2018-07-02 13:50:23 -0700 | [diff] [blame] | 442 | private: |
Jiyong Park | d59a10d | 2018-07-18 11:12:55 +0900 | [diff] [blame] | 443 | std::unique_ptr<AidlTypeSpecifier> type_; |
Steven Moreland | 5557f1c | 2018-07-02 13:50:23 -0700 | [diff] [blame] | 444 | std::string name_; |
Steven Moreland | 541788d | 2020-05-21 22:05:52 +0000 | [diff] [blame] | 445 | bool default_user_specified_; |
Steven Moreland | 9ea10e3 | 2018-07-19 15:26:09 -0700 | [diff] [blame] | 446 | std::unique_ptr<AidlConstantValue> default_value_; |
Steven Moreland | 5557f1c | 2018-07-02 13:50:23 -0700 | [diff] [blame] | 447 | }; |
| 448 | |
| 449 | class AidlArgument : public AidlVariableDeclaration { |
Casey Dahlin | bc7a50a | 2015-09-28 19:20:50 -0700 | [diff] [blame] | 450 | public: |
Casey Dahlin | c378c99 | 2015-09-29 16:50:40 -0700 | [diff] [blame] | 451 | enum Direction { IN_DIR = 1, OUT_DIR = 2, INOUT_DIR = 3 }; |
| 452 | |
Steven Moreland | 46e9da8 | 2018-07-27 15:45:29 -0700 | [diff] [blame] | 453 | AidlArgument(const AidlLocation& location, AidlArgument::Direction direction, |
| 454 | AidlTypeSpecifier* type, const std::string& name); |
| 455 | AidlArgument(const AidlLocation& location, AidlTypeSpecifier* type, const std::string& name); |
Casey Dahlin | bc7a50a | 2015-09-28 19:20:50 -0700 | [diff] [blame] | 456 | virtual ~AidlArgument() = default; |
| 457 | |
Jiyong Park | d800fef | 2020-07-22 18:09:43 +0900 | [diff] [blame] | 458 | // non-copyable, non-movable |
| 459 | AidlArgument(const AidlArgument&) = delete; |
| 460 | AidlArgument(AidlArgument&&) = delete; |
| 461 | AidlArgument& operator=(const AidlArgument&) = delete; |
| 462 | AidlArgument& operator=(AidlArgument&&) = delete; |
| 463 | |
Casey Dahlin | c378c99 | 2015-09-29 16:50:40 -0700 | [diff] [blame] | 464 | Direction GetDirection() const { return direction_; } |
Christopher Wiley | ad33927 | 2015-10-05 19:11:58 -0700 | [diff] [blame] | 465 | bool IsOut() const { return direction_ & OUT_DIR; } |
| 466 | bool IsIn() const { return direction_ & IN_DIR; } |
Casey Dahlin | c378c99 | 2015-09-29 16:50:40 -0700 | [diff] [blame] | 467 | bool DirectionWasSpecified() const { return direction_specified_; } |
Jiyong Park | 3656c3c | 2018-08-01 20:02:01 +0900 | [diff] [blame] | 468 | string GetDirectionSpecifier() const; |
Christopher Wiley | ad33927 | 2015-10-05 19:11:58 -0700 | [diff] [blame] | 469 | |
Jooyung Han | 965e31d | 2020-11-27 12:30:16 +0900 | [diff] [blame] | 470 | // ToString is for dumping AIDL. |
| 471 | // Returns string representation of this argument including direction |
| 472 | // This is "direction annotations type name". |
| 473 | // e.g) "in @utf8InCpp String[] names" |
Casey Dahlin | c378c99 | 2015-09-29 16:50:40 -0700 | [diff] [blame] | 474 | std::string ToString() const; |
| 475 | |
Casey Dahlin | bc7a50a | 2015-09-28 19:20:50 -0700 | [diff] [blame] | 476 | private: |
Casey Dahlin | c378c99 | 2015-09-29 16:50:40 -0700 | [diff] [blame] | 477 | Direction direction_; |
| 478 | bool direction_specified_; |
Casey Dahlin | a834dd4 | 2015-09-23 11:52:15 -0700 | [diff] [blame] | 479 | }; |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 480 | |
Will McVicker | d7d18df | 2019-09-12 13:40:50 -0700 | [diff] [blame] | 481 | class AidlUnaryConstExpression; |
| 482 | class AidlBinaryConstExpression; |
Jooyung Han | 690f584 | 2020-12-04 13:02:04 +0900 | [diff] [blame] | 483 | class AidlConstantReference; |
Will McVicker | d7d18df | 2019-09-12 13:40:50 -0700 | [diff] [blame] | 484 | |
Steven Moreland | 693640b | 2018-07-19 13:46:27 -0700 | [diff] [blame] | 485 | class AidlConstantValue : public AidlNode { |
Casey Dahlin | d40e2fe | 2015-11-24 14:06:52 -0800 | [diff] [blame] | 486 | public: |
Will McVicker | d7d18df | 2019-09-12 13:40:50 -0700 | [diff] [blame] | 487 | enum class Type { |
| 488 | // WARNING: Don't change this order! The order is used to determine type |
| 489 | // promotion during a binary expression. |
| 490 | BOOLEAN, |
| 491 | INT8, |
| 492 | INT32, |
| 493 | INT64, |
| 494 | ARRAY, |
| 495 | CHARACTER, |
| 496 | STRING, |
Jooyung Han | 690f584 | 2020-12-04 13:02:04 +0900 | [diff] [blame] | 497 | REF, |
Will McVicker | d7d18df | 2019-09-12 13:40:50 -0700 | [diff] [blame] | 498 | FLOATING, |
| 499 | UNARY, |
| 500 | BINARY, |
| 501 | ERROR, |
| 502 | }; |
| 503 | |
Jooyung Han | 690f584 | 2020-12-04 13:02:04 +0900 | [diff] [blame] | 504 | struct Visitor { |
| 505 | virtual ~Visitor() {} |
| 506 | virtual void Visit(AidlConstantValue&) = 0; |
| 507 | virtual void Visit(AidlConstantReference&) = 0; |
| 508 | virtual void Visit(AidlUnaryConstExpression&) = 0; |
| 509 | virtual void Visit(AidlBinaryConstExpression&) = 0; |
| 510 | }; |
| 511 | |
Will McVicker | d7d18df | 2019-09-12 13:40:50 -0700 | [diff] [blame] | 512 | /* |
| 513 | * Return the value casted to the given type. |
| 514 | */ |
| 515 | template <typename T> |
Jooyung Han | 71a1b58 | 2020-12-25 23:58:41 +0900 | [diff] [blame] | 516 | T Cast() const; |
Casey Dahlin | d40e2fe | 2015-11-24 14:06:52 -0800 | [diff] [blame] | 517 | |
Steven Moreland | 693640b | 2018-07-19 13:46:27 -0700 | [diff] [blame] | 518 | virtual ~AidlConstantValue() = default; |
Casey Dahlin | d40e2fe | 2015-11-24 14:06:52 -0800 | [diff] [blame] | 519 | |
Jiyong Park | d800fef | 2020-07-22 18:09:43 +0900 | [diff] [blame] | 520 | // non-copyable, non-movable |
| 521 | AidlConstantValue(const AidlConstantValue&) = delete; |
| 522 | AidlConstantValue(AidlConstantValue&&) = delete; |
| 523 | AidlConstantValue& operator=(const AidlConstantValue&) = delete; |
| 524 | AidlConstantValue& operator=(AidlConstantValue&&) = delete; |
| 525 | |
Steven Moreland | 541788d | 2020-05-21 22:05:52 +0000 | [diff] [blame] | 526 | // creates default value, when one isn't specified |
| 527 | // nullptr if no default available |
| 528 | static AidlConstantValue* Default(const AidlTypeSpecifier& specifier); |
| 529 | |
Steven Moreland | 2529432 | 2018-08-07 18:13:55 -0700 | [diff] [blame] | 530 | static AidlConstantValue* Boolean(const AidlLocation& location, bool value); |
| 531 | static AidlConstantValue* Character(const AidlLocation& location, char value); |
Steven Moreland | 2529432 | 2018-08-07 18:13:55 -0700 | [diff] [blame] | 532 | // example: 123, -5498, maybe any size |
Will McVicker | d7d18df | 2019-09-12 13:40:50 -0700 | [diff] [blame] | 533 | static AidlConstantValue* Integral(const AidlLocation& location, const string& value); |
| 534 | static AidlConstantValue* Floating(const AidlLocation& location, const std::string& value); |
Steven Moreland | 860b194 | 2018-08-16 14:59:28 -0700 | [diff] [blame] | 535 | static AidlConstantValue* Array(const AidlLocation& location, |
Will McVicker | d7d18df | 2019-09-12 13:40:50 -0700 | [diff] [blame] | 536 | std::unique_ptr<vector<unique_ptr<AidlConstantValue>>> values); |
Steven Moreland | 693640b | 2018-07-19 13:46:27 -0700 | [diff] [blame] | 537 | // example: "\"asdf\"" |
Will McVicker | d7d18df | 2019-09-12 13:40:50 -0700 | [diff] [blame] | 538 | static AidlConstantValue* String(const AidlLocation& location, const string& value); |
Steven Moreland | 693640b | 2018-07-19 13:46:27 -0700 | [diff] [blame] | 539 | |
Will McVicker | d7d18df | 2019-09-12 13:40:50 -0700 | [diff] [blame] | 540 | Type GetType() const { return final_type_; } |
Jooyung Han | 2981384 | 2020-12-08 01:28:03 +0900 | [diff] [blame] | 541 | const std::string& Literal() const { return value_; } |
Steven Moreland | 2529432 | 2018-08-07 18:13:55 -0700 | [diff] [blame] | 542 | |
Will McVicker | d7d18df | 2019-09-12 13:40:50 -0700 | [diff] [blame] | 543 | virtual bool CheckValid() const; |
Steven Moreland | 860b194 | 2018-08-16 14:59:28 -0700 | [diff] [blame] | 544 | |
| 545 | // Raw value of type (currently valid in C++ and Java). Empty string on error. |
Steven Moreland | 4bcb05c | 2019-11-27 18:57:47 -0800 | [diff] [blame] | 546 | string ValueString(const AidlTypeSpecifier& type, const ConstantValueDecorator& decorator) const; |
Jooyung Han | 2981384 | 2020-12-08 01:28:03 +0900 | [diff] [blame] | 547 | virtual void Accept(Visitor& visitor) { |
| 548 | visitor.Visit(*this); |
| 549 | if (type_ == Type::ARRAY) { |
| 550 | for (const auto& v : values_) { |
| 551 | v.get()->Accept(visitor); |
| 552 | } |
| 553 | } |
| 554 | } |
Casey Dahlin | d40e2fe | 2015-11-24 14:06:52 -0800 | [diff] [blame] | 555 | |
| 556 | private: |
Will McVicker | d7d18df | 2019-09-12 13:40:50 -0700 | [diff] [blame] | 557 | AidlConstantValue(const AidlLocation& location, Type parsed_type, int64_t parsed_value, |
| 558 | const string& checked_value); |
| 559 | AidlConstantValue(const AidlLocation& location, Type type, const string& checked_value); |
Steven Moreland | 860b194 | 2018-08-16 14:59:28 -0700 | [diff] [blame] | 560 | AidlConstantValue(const AidlLocation& location, Type type, |
Jooyung Han | 2981384 | 2020-12-08 01:28:03 +0900 | [diff] [blame] | 561 | std::unique_ptr<vector<unique_ptr<AidlConstantValue>>> values, |
| 562 | const std::string& value); |
Steven Moreland | 2529432 | 2018-08-07 18:13:55 -0700 | [diff] [blame] | 563 | static string ToString(Type type); |
Will McVicker | d7d18df | 2019-09-12 13:40:50 -0700 | [diff] [blame] | 564 | static bool ParseIntegral(const string& value, int64_t* parsed_value, Type* parsed_type); |
| 565 | static bool IsHex(const string& value); |
Steven Moreland | 4bcb05c | 2019-11-27 18:57:47 -0800 | [diff] [blame] | 566 | |
Jooyung Han | 74675c2 | 2020-12-15 08:39:57 +0900 | [diff] [blame] | 567 | virtual bool evaluate() const; |
Casey Dahlin | d40e2fe | 2015-11-24 14:06:52 -0800 | [diff] [blame] | 568 | |
Steven Moreland | 693640b | 2018-07-19 13:46:27 -0700 | [diff] [blame] | 569 | const Type type_ = Type::ERROR; |
Will McVicker | d7d18df | 2019-09-12 13:40:50 -0700 | [diff] [blame] | 570 | const vector<unique_ptr<AidlConstantValue>> values_; // if type_ == ARRAY |
| 571 | const string value_; // otherwise |
| 572 | |
| 573 | // State for tracking evaluation of expressions |
Steven Moreland | 4bcb05c | 2019-11-27 18:57:47 -0800 | [diff] [blame] | 574 | mutable bool is_valid_ = false; // cache of CheckValid, but may be marked false in evaluate |
| 575 | mutable bool is_evaluated_ = false; // whether evaluate has been called |
Will McVicker | d7d18df | 2019-09-12 13:40:50 -0700 | [diff] [blame] | 576 | mutable Type final_type_; |
| 577 | mutable int64_t final_value_; |
| 578 | mutable string final_string_value_ = ""; |
Steven Moreland | 693640b | 2018-07-19 13:46:27 -0700 | [diff] [blame] | 579 | |
Will McVicker | d7d18df | 2019-09-12 13:40:50 -0700 | [diff] [blame] | 580 | friend AidlUnaryConstExpression; |
| 581 | friend AidlBinaryConstExpression; |
Jooyung Han | 690f584 | 2020-12-04 13:02:04 +0900 | [diff] [blame] | 582 | friend AidlConstantReference; |
| 583 | }; |
| 584 | |
| 585 | // Represents "<type>.<field>" which resolves to a constant which is one of |
| 586 | // - constant declartion |
| 587 | // - enumerator |
| 588 | // When a <type> is missing, <field> is of the enclosing type. |
| 589 | class AidlConstantReference : public AidlConstantValue { |
| 590 | public: |
| 591 | AidlConstantReference(const AidlLocation& location, const std::string& value, |
| 592 | const std::string& comments); |
| 593 | |
| 594 | const std::unique_ptr<AidlTypeSpecifier>& GetRefType() const { return ref_type_; } |
| 595 | void SetRefType(std::unique_ptr<AidlTypeSpecifier> type) { ref_type_ = std::move(type); } |
| 596 | const std::string& GetFieldName() const { return field_name_; } |
| 597 | const std::string& GetComments() const { return comments_; } |
| 598 | |
| 599 | bool CheckValid() const override; |
| 600 | void Accept(Visitor& visitor) override { visitor.Visit(*this); } |
Jooyung Han | 2981384 | 2020-12-08 01:28:03 +0900 | [diff] [blame] | 601 | const AidlConstantValue* Resolve(); |
Jooyung Han | 690f584 | 2020-12-04 13:02:04 +0900 | [diff] [blame] | 602 | |
| 603 | private: |
Jooyung Han | 74675c2 | 2020-12-15 08:39:57 +0900 | [diff] [blame] | 604 | bool evaluate() const override; |
Jooyung Han | 690f584 | 2020-12-04 13:02:04 +0900 | [diff] [blame] | 605 | |
| 606 | std::unique_ptr<AidlTypeSpecifier> ref_type_; |
| 607 | std::string field_name_; |
| 608 | const std::string comments_; |
Jooyung Han | 2981384 | 2020-12-08 01:28:03 +0900 | [diff] [blame] | 609 | const AidlConstantValue* resolved_ = nullptr; |
Will McVicker | d7d18df | 2019-09-12 13:40:50 -0700 | [diff] [blame] | 610 | }; |
| 611 | |
| 612 | class AidlUnaryConstExpression : public AidlConstantValue { |
| 613 | public: |
| 614 | AidlUnaryConstExpression(const AidlLocation& location, const string& op, |
| 615 | std::unique_ptr<AidlConstantValue> rval); |
| 616 | |
| 617 | static bool IsCompatibleType(Type type, const string& op); |
| 618 | bool CheckValid() const override; |
Jooyung Han | 690f584 | 2020-12-04 13:02:04 +0900 | [diff] [blame] | 619 | void Accept(Visitor& visitor) override { |
| 620 | visitor.Visit(*this); |
| 621 | unary_->Accept(visitor); |
| 622 | } |
| 623 | |
Will McVicker | d7d18df | 2019-09-12 13:40:50 -0700 | [diff] [blame] | 624 | private: |
Jooyung Han | 74675c2 | 2020-12-15 08:39:57 +0900 | [diff] [blame] | 625 | bool evaluate() const override; |
Will McVicker | d7d18df | 2019-09-12 13:40:50 -0700 | [diff] [blame] | 626 | |
| 627 | std::unique_ptr<AidlConstantValue> unary_; |
| 628 | const string op_; |
| 629 | }; |
| 630 | |
| 631 | class AidlBinaryConstExpression : public AidlConstantValue { |
| 632 | public: |
| 633 | AidlBinaryConstExpression(const AidlLocation& location, std::unique_ptr<AidlConstantValue> lval, |
| 634 | const string& op, std::unique_ptr<AidlConstantValue> rval); |
| 635 | |
| 636 | bool CheckValid() const override; |
Will McVicker | d7d18df | 2019-09-12 13:40:50 -0700 | [diff] [blame] | 637 | |
| 638 | static bool AreCompatibleTypes(Type t1, Type t2); |
| 639 | // Returns the promoted kind for both operands |
| 640 | static Type UsualArithmeticConversion(Type left, Type right); |
| 641 | // Returns the promoted integral type where INT32 is the smallest type |
| 642 | static Type IntegralPromotion(Type in); |
Jooyung Han | 690f584 | 2020-12-04 13:02:04 +0900 | [diff] [blame] | 643 | void Accept(Visitor& visitor) override { |
| 644 | visitor.Visit(*this); |
| 645 | left_val_->Accept(visitor); |
| 646 | right_val_->Accept(visitor); |
| 647 | } |
Will McVicker | d7d18df | 2019-09-12 13:40:50 -0700 | [diff] [blame] | 648 | |
| 649 | private: |
Jooyung Han | 74675c2 | 2020-12-15 08:39:57 +0900 | [diff] [blame] | 650 | bool evaluate() const override; |
Will McVicker | d7d18df | 2019-09-12 13:40:50 -0700 | [diff] [blame] | 651 | |
| 652 | std::unique_ptr<AidlConstantValue> left_val_; |
| 653 | std::unique_ptr<AidlConstantValue> right_val_; |
| 654 | const string op_; |
Christopher Wiley | d6bdd8d | 2016-05-03 11:23:13 -0700 | [diff] [blame] | 655 | }; |
| 656 | |
Andrei Onea | 9445fc6 | 2019-06-27 18:11:59 +0100 | [diff] [blame] | 657 | struct AidlAnnotationParameter { |
| 658 | std::string name; |
| 659 | std::unique_ptr<AidlConstantValue> value; |
| 660 | }; |
| 661 | |
Steven Moreland | 693640b | 2018-07-19 13:46:27 -0700 | [diff] [blame] | 662 | class AidlConstantDeclaration : public AidlMember { |
Christopher Wiley | d6bdd8d | 2016-05-03 11:23:13 -0700 | [diff] [blame] | 663 | public: |
Steven Moreland | 46e9da8 | 2018-07-27 15:45:29 -0700 | [diff] [blame] | 664 | AidlConstantDeclaration(const AidlLocation& location, AidlTypeSpecifier* specifier, |
Will McVicker | d7d18df | 2019-09-12 13:40:50 -0700 | [diff] [blame] | 665 | const string& name, AidlConstantValue* value); |
Steven Moreland | 693640b | 2018-07-19 13:46:27 -0700 | [diff] [blame] | 666 | virtual ~AidlConstantDeclaration() = default; |
Christopher Wiley | d6bdd8d | 2016-05-03 11:23:13 -0700 | [diff] [blame] | 667 | |
Jiyong Park | d800fef | 2020-07-22 18:09:43 +0900 | [diff] [blame] | 668 | // non-copyable, non-movable |
| 669 | AidlConstantDeclaration(const AidlConstantDeclaration&) = delete; |
| 670 | AidlConstantDeclaration(AidlConstantDeclaration&&) = delete; |
| 671 | AidlConstantDeclaration& operator=(const AidlConstantDeclaration&) = delete; |
| 672 | AidlConstantDeclaration& operator=(AidlConstantDeclaration&&) = delete; |
| 673 | |
Steven Moreland | 693640b | 2018-07-19 13:46:27 -0700 | [diff] [blame] | 674 | const AidlTypeSpecifier& GetType() const { return *type_; } |
Steven Moreland | 4d12f9a | 2018-10-31 14:30:55 -0700 | [diff] [blame] | 675 | AidlTypeSpecifier* GetMutableType() { return type_.get(); } |
Will McVicker | d7d18df | 2019-09-12 13:40:50 -0700 | [diff] [blame] | 676 | const string& GetName() const { return name_; } |
Steven Moreland | 693640b | 2018-07-19 13:46:27 -0700 | [diff] [blame] | 677 | const AidlConstantValue& GetValue() const { return *value_; } |
Jeongik Cha | db0f59e | 2018-11-01 18:11:21 +0900 | [diff] [blame] | 678 | bool CheckValid(const AidlTypenames& typenames) const; |
Christopher Wiley | d6bdd8d | 2016-05-03 11:23:13 -0700 | [diff] [blame] | 679 | |
Jooyung Han | 965e31d | 2020-11-27 12:30:16 +0900 | [diff] [blame] | 680 | // ToString is for dumping AIDL. |
| 681 | // Returns string representation of this const decl including a const value. |
| 682 | // This is "`const` annotations type name value". |
| 683 | // e.g) "const @utf8InCpp String[] names = { "hello" }" |
Will McVicker | d7d18df | 2019-09-12 13:40:50 -0700 | [diff] [blame] | 684 | string ToString() const; |
Jooyung Han | 965e31d | 2020-11-27 12:30:16 +0900 | [diff] [blame] | 685 | |
| 686 | // Signature is for comparing types. |
| 687 | // Returns string representation of this const decl. |
| 688 | // This is "direction annotations type name". |
| 689 | // e.g) "String[] names" |
Will McVicker | d7d18df | 2019-09-12 13:40:50 -0700 | [diff] [blame] | 690 | string Signature() const; |
Jooyung Han | 965e31d | 2020-11-27 12:30:16 +0900 | [diff] [blame] | 691 | |
Steven Moreland | 860b194 | 2018-08-16 14:59:28 -0700 | [diff] [blame] | 692 | string ValueString(const ConstantValueDecorator& decorator) const { |
Will McVicker | d7d18df | 2019-09-12 13:40:50 -0700 | [diff] [blame] | 693 | return value_->ValueString(GetType(), decorator); |
Steven Moreland | 860b194 | 2018-08-16 14:59:28 -0700 | [diff] [blame] | 694 | } |
Steven Moreland | 2529432 | 2018-08-07 18:13:55 -0700 | [diff] [blame] | 695 | |
Jooyung Han | 829ec7c | 2020-12-02 12:07:36 +0900 | [diff] [blame] | 696 | const AidlConstantDeclaration* AsConstantDeclaration() const override { return this; } |
Christopher Wiley | d6bdd8d | 2016-05-03 11:23:13 -0700 | [diff] [blame] | 697 | |
| 698 | private: |
Steven Moreland | 693640b | 2018-07-19 13:46:27 -0700 | [diff] [blame] | 699 | const unique_ptr<AidlTypeSpecifier> type_; |
Will McVicker | d7d18df | 2019-09-12 13:40:50 -0700 | [diff] [blame] | 700 | const string name_; |
| 701 | unique_ptr<AidlConstantValue> value_; |
Casey Dahlin | d40e2fe | 2015-11-24 14:06:52 -0800 | [diff] [blame] | 702 | }; |
| 703 | |
| 704 | class AidlMethod : public AidlMember { |
Casey Dahlin | 5c69deb | 2015-10-01 14:44:12 -0700 | [diff] [blame] | 705 | public: |
Will McVicker | d7d18df | 2019-09-12 13:40:50 -0700 | [diff] [blame] | 706 | AidlMethod(const AidlLocation& location, bool oneway, AidlTypeSpecifier* type, const string& name, |
| 707 | vector<unique_ptr<AidlArgument>>* args, const string& comments); |
| 708 | AidlMethod(const AidlLocation& location, bool oneway, AidlTypeSpecifier* type, const string& name, |
| 709 | vector<unique_ptr<AidlArgument>>* args, const string& comments, int id, |
| 710 | bool is_user_defined = true); |
Casey Dahlin | 5c69deb | 2015-10-01 14:44:12 -0700 | [diff] [blame] | 711 | virtual ~AidlMethod() = default; |
| 712 | |
Jiyong Park | d800fef | 2020-07-22 18:09:43 +0900 | [diff] [blame] | 713 | // non-copyable, non-movable |
| 714 | AidlMethod(const AidlMethod&) = delete; |
| 715 | AidlMethod(AidlMethod&&) = delete; |
| 716 | AidlMethod& operator=(const AidlMethod&) = delete; |
| 717 | AidlMethod& operator=(AidlMethod&&) = delete; |
| 718 | |
Jooyung Han | 829ec7c | 2020-12-02 12:07:36 +0900 | [diff] [blame] | 719 | const AidlMethod* AsMethod() const override { return this; } |
Jeongik Cha | 997281d | 2020-01-16 15:23:59 +0900 | [diff] [blame] | 720 | bool IsHidden() const; |
Will McVicker | d7d18df | 2019-09-12 13:40:50 -0700 | [diff] [blame] | 721 | const string& GetComments() const { return comments_; } |
Jiyong Park | d59a10d | 2018-07-18 11:12:55 +0900 | [diff] [blame] | 722 | const AidlTypeSpecifier& GetType() const { return *type_; } |
| 723 | AidlTypeSpecifier* GetMutableType() { return type_.get(); } |
Steven Moreland | acd5347 | 2018-12-14 10:17:26 -0800 | [diff] [blame] | 724 | |
Steven Moreland | 8c70ba9 | 2018-12-17 10:20:31 -0800 | [diff] [blame] | 725 | // set if this method is part of an interface that is marked oneway |
| 726 | void ApplyInterfaceOneway(bool oneway) { oneway_ = oneway_ || oneway; } |
Casey Dahlin | f4a9311 | 2015-10-05 16:58:09 -0700 | [diff] [blame] | 727 | bool IsOneway() const { return oneway_; } |
Steven Moreland | acd5347 | 2018-12-14 10:17:26 -0800 | [diff] [blame] | 728 | |
Casey Dahlin | f4a9311 | 2015-10-05 16:58:09 -0700 | [diff] [blame] | 729 | const std::string& GetName() const { return name_; } |
Casey Dahlin | f4a9311 | 2015-10-05 16:58:09 -0700 | [diff] [blame] | 730 | bool HasId() const { return has_id_; } |
Jiyong Park | ed65bf4 | 2018-08-28 15:43:27 +0900 | [diff] [blame] | 731 | int GetId() const { return id_; } |
Casey Dahlin | f4a9311 | 2015-10-05 16:58:09 -0700 | [diff] [blame] | 732 | void SetId(unsigned id) { id_ = id; } |
Casey Dahlin | f2d23f7 | 2015-10-02 16:19:19 -0700 | [diff] [blame] | 733 | |
Jiyong Park | 309668e | 2018-07-28 16:55:44 +0900 | [diff] [blame] | 734 | bool IsUserDefined() const { return is_user_defined_; } |
| 735 | |
Casey Dahlin | f4a9311 | 2015-10-05 16:58:09 -0700 | [diff] [blame] | 736 | const std::vector<std::unique_ptr<AidlArgument>>& GetArguments() const { |
Christopher Wiley | ad33927 | 2015-10-05 19:11:58 -0700 | [diff] [blame] | 737 | return arguments_; |
| 738 | } |
| 739 | // An inout parameter will appear in both GetInArguments() |
| 740 | // and GetOutArguments(). AidlMethod retains ownership of the argument |
| 741 | // pointers returned in this way. |
| 742 | const std::vector<const AidlArgument*>& GetInArguments() const { |
| 743 | return in_arguments_; |
| 744 | } |
| 745 | const std::vector<const AidlArgument*>& GetOutArguments() const { |
| 746 | return out_arguments_; |
Casey Dahlin | f4a9311 | 2015-10-05 16:58:09 -0700 | [diff] [blame] | 747 | } |
Casey Dahlin | 5c69deb | 2015-10-01 14:44:12 -0700 | [diff] [blame] | 748 | |
Jooyung Han | 965e31d | 2020-11-27 12:30:16 +0900 | [diff] [blame] | 749 | // ToString is for dumping AIDL. |
| 750 | // Returns string representation of this method including everything. |
| 751 | // This is "ret_type name ( arg_list ) = id". |
| 752 | // e.g) "boolean foo(int, @Nullable String) = 1" |
Jiyong Park | 309668e | 2018-07-28 16:55:44 +0900 | [diff] [blame] | 753 | std::string ToString() const; |
| 754 | |
Jooyung Han | 965e31d | 2020-11-27 12:30:16 +0900 | [diff] [blame] | 755 | // Signature is for comparing AIDL types. |
| 756 | // Returns string representation of this method's name & type. |
| 757 | // e.g) "foo(int, String)" |
| 758 | std::string Signature() const; |
| 759 | |
Casey Dahlin | 5c69deb | 2015-10-01 14:44:12 -0700 | [diff] [blame] | 760 | private: |
Casey Dahlin | f4a9311 | 2015-10-05 16:58:09 -0700 | [diff] [blame] | 761 | bool oneway_; |
Casey Dahlin | f2d23f7 | 2015-10-02 16:19:19 -0700 | [diff] [blame] | 762 | std::string comments_; |
Jiyong Park | d59a10d | 2018-07-18 11:12:55 +0900 | [diff] [blame] | 763 | std::unique_ptr<AidlTypeSpecifier> type_; |
Casey Dahlin | f4a9311 | 2015-10-05 16:58:09 -0700 | [diff] [blame] | 764 | std::string name_; |
Christopher Wiley | ad33927 | 2015-10-05 19:11:58 -0700 | [diff] [blame] | 765 | const std::vector<std::unique_ptr<AidlArgument>> arguments_; |
| 766 | std::vector<const AidlArgument*> in_arguments_; |
| 767 | std::vector<const AidlArgument*> out_arguments_; |
Casey Dahlin | f4a9311 | 2015-10-05 16:58:09 -0700 | [diff] [blame] | 768 | bool has_id_; |
| 769 | int id_; |
Jiyong Park | 309668e | 2018-07-28 16:55:44 +0900 | [diff] [blame] | 770 | bool is_user_defined_ = true; |
Casey Dahlin | 0a2f8be | 2015-09-28 16:15:29 -0700 | [diff] [blame] | 771 | }; |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 772 | |
Steven Moreland | c258abc | 2018-07-10 14:03:38 -0700 | [diff] [blame] | 773 | class AidlDefinedType; |
Jiyong Park | b034bf0 | 2018-07-30 17:44:33 +0900 | [diff] [blame] | 774 | class AidlInterface; |
| 775 | class AidlParcelable; |
| 776 | class AidlStructuredParcelable; |
Jooyung Han | 2946afc | 2020-10-05 20:29:16 +0900 | [diff] [blame] | 777 | class AidlUnionDecl; |
Jooyung Han | 3f347ca | 2020-12-01 12:41:50 +0900 | [diff] [blame] | 778 | |
Daniel Norman | 85aed54 | 2019-08-21 12:01:14 -0700 | [diff] [blame] | 779 | // AidlDefinedType represents either an interface, parcelable, or enum that is |
Jiyong Park | 1deecc3 | 2018-07-17 01:14:41 +0900 | [diff] [blame] | 780 | // defined in the source file. |
| 781 | class AidlDefinedType : public AidlAnnotatable { |
Steven Moreland | 787b043 | 2018-07-03 09:00:58 -0700 | [diff] [blame] | 782 | public: |
Steven Moreland | 46e9da8 | 2018-07-27 15:45:29 -0700 | [diff] [blame] | 783 | AidlDefinedType(const AidlLocation& location, const std::string& name, |
Jooyung Han | 829ec7c | 2020-12-02 12:07:36 +0900 | [diff] [blame] | 784 | const std::string& comments, const std::string& package, |
| 785 | std::vector<std::unique_ptr<AidlMember>>* members); |
Steven Moreland | 787b043 | 2018-07-03 09:00:58 -0700 | [diff] [blame] | 786 | virtual ~AidlDefinedType() = default; |
| 787 | |
Jiyong Park | d800fef | 2020-07-22 18:09:43 +0900 | [diff] [blame] | 788 | // non-copyable, non-movable |
| 789 | AidlDefinedType(const AidlDefinedType&) = delete; |
| 790 | AidlDefinedType(AidlDefinedType&&) = delete; |
| 791 | AidlDefinedType& operator=(const AidlDefinedType&) = delete; |
| 792 | AidlDefinedType& operator=(AidlDefinedType&&) = delete; |
| 793 | |
Jiyong Park | 1deecc3 | 2018-07-17 01:14:41 +0900 | [diff] [blame] | 794 | const std::string& GetName() const { return name_; }; |
Jeongik Cha | 997281d | 2020-01-16 15:23:59 +0900 | [diff] [blame] | 795 | bool IsHidden() const; |
Jiyong Park | 1deecc3 | 2018-07-17 01:14:41 +0900 | [diff] [blame] | 796 | const std::string& GetComments() const { return comments_; } |
Jiyong Park | a6605ab | 2018-11-11 14:30:21 +0900 | [diff] [blame] | 797 | void SetComments(const std::string comments) { comments_ = comments; } |
Jiyong Park | 1deecc3 | 2018-07-17 01:14:41 +0900 | [diff] [blame] | 798 | |
Steven Moreland | 787b043 | 2018-07-03 09:00:58 -0700 | [diff] [blame] | 799 | /* dot joined package, example: "android.package.foo" */ |
Jiyong Park | 1813218 | 2020-06-08 20:24:40 +0900 | [diff] [blame] | 800 | std::string GetPackage() const { return package_; } |
Steven Moreland | 787b043 | 2018-07-03 09:00:58 -0700 | [diff] [blame] | 801 | /* dot joined package and name, example: "android.package.foo.IBar" */ |
| 802 | std::string GetCanonicalName() const; |
Jiyong Park | 1813218 | 2020-06-08 20:24:40 +0900 | [diff] [blame] | 803 | const std::vector<std::string>& GetSplitPackage() const { return split_package_; } |
Steven Moreland | 787b043 | 2018-07-03 09:00:58 -0700 | [diff] [blame] | 804 | |
Steven Moreland | ed83a28 | 2018-07-17 13:27:29 -0700 | [diff] [blame] | 805 | virtual std::string GetPreprocessDeclarationName() const = 0; |
Steven Moreland | c258abc | 2018-07-10 14:03:38 -0700 | [diff] [blame] | 806 | |
Steven Moreland | 5557f1c | 2018-07-02 13:50:23 -0700 | [diff] [blame] | 807 | virtual const AidlStructuredParcelable* AsStructuredParcelable() const { return nullptr; } |
Steven Moreland | c258abc | 2018-07-10 14:03:38 -0700 | [diff] [blame] | 808 | virtual const AidlParcelable* AsParcelable() const { return nullptr; } |
Daniel Norman | 85aed54 | 2019-08-21 12:01:14 -0700 | [diff] [blame] | 809 | virtual const AidlEnumDeclaration* AsEnumDeclaration() const { return nullptr; } |
Jooyung Han | 2946afc | 2020-10-05 20:29:16 +0900 | [diff] [blame] | 810 | virtual const AidlUnionDecl* AsUnionDeclaration() const { return nullptr; } |
Steven Moreland | 5557f1c | 2018-07-02 13:50:23 -0700 | [diff] [blame] | 811 | virtual const AidlInterface* AsInterface() const { return nullptr; } |
Jeongik Cha | df76dc7 | 2019-11-28 00:08:47 +0900 | [diff] [blame] | 812 | virtual const AidlParameterizable<std::string>* AsParameterizable() const { return nullptr; } |
Jooyung Han | 888c5bc | 2020-12-22 17:28:47 +0900 | [diff] [blame] | 813 | virtual bool CheckValid(const AidlTypenames& typenames, DiagnosticsContext& context) const; |
Steven Moreland | d59e317 | 2020-05-11 16:42:09 -0700 | [diff] [blame] | 814 | virtual bool LanguageSpecificCheckValid(const AidlTypenames& typenames, |
| 815 | Options::Language lang) const = 0; |
Steven Moreland | c258abc | 2018-07-10 14:03:38 -0700 | [diff] [blame] | 816 | AidlStructuredParcelable* AsStructuredParcelable() { |
| 817 | return const_cast<AidlStructuredParcelable*>( |
| 818 | const_cast<const AidlDefinedType*>(this)->AsStructuredParcelable()); |
| 819 | } |
| 820 | AidlParcelable* AsParcelable() { |
| 821 | return const_cast<AidlParcelable*>(const_cast<const AidlDefinedType*>(this)->AsParcelable()); |
| 822 | } |
Daniel Norman | 85aed54 | 2019-08-21 12:01:14 -0700 | [diff] [blame] | 823 | AidlEnumDeclaration* AsEnumDeclaration() { |
| 824 | return const_cast<AidlEnumDeclaration*>( |
| 825 | const_cast<const AidlDefinedType*>(this)->AsEnumDeclaration()); |
| 826 | } |
Jooyung Han | 2946afc | 2020-10-05 20:29:16 +0900 | [diff] [blame] | 827 | AidlUnionDecl* AsUnionDeclaration() { |
| 828 | return const_cast<AidlUnionDecl*>( |
| 829 | const_cast<const AidlDefinedType*>(this)->AsUnionDeclaration()); |
| 830 | } |
Steven Moreland | c258abc | 2018-07-10 14:03:38 -0700 | [diff] [blame] | 831 | AidlInterface* AsInterface() { |
| 832 | return const_cast<AidlInterface*>(const_cast<const AidlDefinedType*>(this)->AsInterface()); |
| 833 | } |
| 834 | |
Jeongik Cha | df76dc7 | 2019-11-28 00:08:47 +0900 | [diff] [blame] | 835 | AidlParameterizable<std::string>* AsParameterizable() { |
| 836 | return const_cast<AidlParameterizable<std::string>*>( |
| 837 | const_cast<const AidlDefinedType*>(this)->AsParameterizable()); |
| 838 | } |
| 839 | |
Steven Moreland | 6cee348 | 2018-07-18 14:39:58 -0700 | [diff] [blame] | 840 | const AidlParcelable* AsUnstructuredParcelable() const { |
| 841 | if (this->AsStructuredParcelable() != nullptr) return nullptr; |
Jooyung Han | 2946afc | 2020-10-05 20:29:16 +0900 | [diff] [blame] | 842 | if (this->AsUnionDeclaration() != nullptr) return nullptr; |
Steven Moreland | 6cee348 | 2018-07-18 14:39:58 -0700 | [diff] [blame] | 843 | return this->AsParcelable(); |
| 844 | } |
| 845 | AidlParcelable* AsUnstructuredParcelable() { |
| 846 | return const_cast<AidlParcelable*>( |
| 847 | const_cast<const AidlDefinedType*>(this)->AsUnstructuredParcelable()); |
| 848 | } |
| 849 | |
Jeongik Cha | 997281d | 2020-01-16 15:23:59 +0900 | [diff] [blame] | 850 | virtual void Dump(CodeWriter* writer) const = 0; |
Steven Moreland | a5d9c5c | 2020-02-21 16:01:09 -0800 | [diff] [blame] | 851 | void DumpHeader(CodeWriter* writer) const; |
Jiyong Park | 02da742 | 2018-07-16 16:00:26 +0900 | [diff] [blame] | 852 | |
Jooyung Han | 829ec7c | 2020-12-02 12:07:36 +0900 | [diff] [blame] | 853 | const std::vector<std::unique_ptr<AidlVariableDeclaration>>& GetFields() const { |
| 854 | return variables_; |
| 855 | } |
| 856 | const std::vector<std::unique_ptr<AidlConstantDeclaration>>& GetConstantDeclarations() const { |
| 857 | return constants_; |
| 858 | } |
| 859 | const std::vector<std::unique_ptr<AidlMethod>>& GetMethods() const { return methods_; } |
| 860 | void AddMethod(std::unique_ptr<AidlMethod> method) { methods_.push_back(std::move(method)); } |
| 861 | const std::vector<const AidlMember*>& GetMembers() const { return members_; } |
| 862 | |
| 863 | protected: |
| 864 | // utility for subclasses with getter names |
| 865 | bool CheckValidForGetterNames() const; |
| 866 | |
Steven Moreland | 787b043 | 2018-07-03 09:00:58 -0700 | [diff] [blame] | 867 | private: |
Jooyung Han | 829ec7c | 2020-12-02 12:07:36 +0900 | [diff] [blame] | 868 | bool CheckValidWithMembers(const AidlTypenames& typenames) const; |
| 869 | |
Jiyong Park | 1deecc3 | 2018-07-17 01:14:41 +0900 | [diff] [blame] | 870 | std::string name_; |
Jiyong Park | 1deecc3 | 2018-07-17 01:14:41 +0900 | [diff] [blame] | 871 | std::string comments_; |
Jiyong Park | 1813218 | 2020-06-08 20:24:40 +0900 | [diff] [blame] | 872 | const std::string package_; |
| 873 | const std::vector<std::string> split_package_; |
Jooyung Han | 829ec7c | 2020-12-02 12:07:36 +0900 | [diff] [blame] | 874 | std::vector<std::unique_ptr<AidlVariableDeclaration>> variables_; |
| 875 | std::vector<std::unique_ptr<AidlConstantDeclaration>> constants_; |
| 876 | std::vector<std::unique_ptr<AidlMethod>> methods_; |
| 877 | std::vector<const AidlMember*> members_; // keep members in order of appearance. |
Steven Moreland | 787b043 | 2018-07-03 09:00:58 -0700 | [diff] [blame] | 878 | }; |
| 879 | |
Jeongik Cha | df76dc7 | 2019-11-28 00:08:47 +0900 | [diff] [blame] | 880 | class AidlParcelable : public AidlDefinedType, public AidlParameterizable<std::string> { |
Casey Dahlin | 1ae2bc5 | 2015-10-07 18:49:10 -0700 | [diff] [blame] | 881 | public: |
Jiyong Park | 1813218 | 2020-06-08 20:24:40 +0900 | [diff] [blame] | 882 | AidlParcelable(const AidlLocation& location, const std::string& name, const std::string& package, |
| 883 | const std::string& comments, const std::string& cpp_header = "", |
Jooyung Han | 829ec7c | 2020-12-02 12:07:36 +0900 | [diff] [blame] | 884 | std::vector<std::string>* type_params = nullptr, |
| 885 | std::vector<std::unique_ptr<AidlMember>>* members = nullptr); |
Casey Dahlin | 1ae2bc5 | 2015-10-07 18:49:10 -0700 | [diff] [blame] | 886 | virtual ~AidlParcelable() = default; |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 887 | |
Jiyong Park | d800fef | 2020-07-22 18:09:43 +0900 | [diff] [blame] | 888 | // non-copyable, non-movable |
| 889 | AidlParcelable(const AidlParcelable&) = delete; |
| 890 | AidlParcelable(AidlParcelable&&) = delete; |
| 891 | AidlParcelable& operator=(const AidlParcelable&) = delete; |
| 892 | AidlParcelable& operator=(AidlParcelable&&) = delete; |
| 893 | |
Christopher Wiley | 8aa4d9f | 2015-11-16 19:10:45 -0800 | [diff] [blame] | 894 | std::string GetCppHeader() const { return cpp_header_; } |
Christopher Wiley | 8aa4d9f | 2015-11-16 19:10:45 -0800 | [diff] [blame] | 895 | |
Steven Moreland | 0cea4aa | 2020-04-20 21:06:02 -0700 | [diff] [blame] | 896 | std::set<AidlAnnotation::Type> GetSupportedAnnotations() const override; |
Jooyung Han | 888c5bc | 2020-12-22 17:28:47 +0900 | [diff] [blame] | 897 | bool CheckValid(const AidlTypenames& typenames, DiagnosticsContext& context) const override; |
Steven Moreland | d59e317 | 2020-05-11 16:42:09 -0700 | [diff] [blame] | 898 | bool LanguageSpecificCheckValid(const AidlTypenames& typenames, |
| 899 | Options::Language lang) const override; |
Steven Moreland | c258abc | 2018-07-10 14:03:38 -0700 | [diff] [blame] | 900 | const AidlParcelable* AsParcelable() const override { return this; } |
Jeongik Cha | df76dc7 | 2019-11-28 00:08:47 +0900 | [diff] [blame] | 901 | const AidlParameterizable<std::string>* AsParameterizable() const override { return this; } |
| 902 | const AidlNode& AsAidlNode() const override { return *this; } |
Steven Moreland | ed83a28 | 2018-07-17 13:27:29 -0700 | [diff] [blame] | 903 | std::string GetPreprocessDeclarationName() const override { return "parcelable"; } |
Steven Moreland | c258abc | 2018-07-10 14:03:38 -0700 | [diff] [blame] | 904 | |
Jeongik Cha | 997281d | 2020-01-16 15:23:59 +0900 | [diff] [blame] | 905 | void Dump(CodeWriter* writer) const override; |
Jiyong Park | 02da742 | 2018-07-16 16:00:26 +0900 | [diff] [blame] | 906 | |
Casey Dahlin | 1ae2bc5 | 2015-10-07 18:49:10 -0700 | [diff] [blame] | 907 | private: |
Christopher Wiley | 8aa4d9f | 2015-11-16 19:10:45 -0800 | [diff] [blame] | 908 | std::string cpp_header_; |
Casey Dahlin | 0a2f8be | 2015-09-28 16:15:29 -0700 | [diff] [blame] | 909 | }; |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 910 | |
Jooyung Han | 829ec7c | 2020-12-02 12:07:36 +0900 | [diff] [blame] | 911 | class AidlStructuredParcelable : public AidlParcelable { |
Steven Moreland | 5557f1c | 2018-07-02 13:50:23 -0700 | [diff] [blame] | 912 | public: |
Jiyong Park | 1813218 | 2020-06-08 20:24:40 +0900 | [diff] [blame] | 913 | AidlStructuredParcelable(const AidlLocation& location, const std::string& name, |
| 914 | const std::string& package, const std::string& comments, |
Jooyung Han | 829ec7c | 2020-12-02 12:07:36 +0900 | [diff] [blame] | 915 | std::vector<std::string>* type_params, |
| 916 | std::vector<std::unique_ptr<AidlMember>>* members); |
Jiyong Park | d800fef | 2020-07-22 18:09:43 +0900 | [diff] [blame] | 917 | virtual ~AidlStructuredParcelable() = default; |
| 918 | |
| 919 | // non-copyable, non-movable |
| 920 | AidlStructuredParcelable(const AidlStructuredParcelable&) = delete; |
| 921 | AidlStructuredParcelable(AidlStructuredParcelable&&) = delete; |
| 922 | AidlStructuredParcelable& operator=(const AidlStructuredParcelable&) = delete; |
| 923 | AidlStructuredParcelable& operator=(AidlStructuredParcelable&&) = delete; |
Steven Moreland | 5557f1c | 2018-07-02 13:50:23 -0700 | [diff] [blame] | 924 | |
Steven Moreland | c258abc | 2018-07-10 14:03:38 -0700 | [diff] [blame] | 925 | const AidlStructuredParcelable* AsStructuredParcelable() const override { return this; } |
Steven Moreland | ed83a28 | 2018-07-17 13:27:29 -0700 | [diff] [blame] | 926 | std::string GetPreprocessDeclarationName() const override { return "structured_parcelable"; } |
Steven Moreland | 5557f1c | 2018-07-02 13:50:23 -0700 | [diff] [blame] | 927 | |
Jeongik Cha | 997281d | 2020-01-16 15:23:59 +0900 | [diff] [blame] | 928 | void Dump(CodeWriter* writer) const override; |
Jiyong Park | 02da742 | 2018-07-16 16:00:26 +0900 | [diff] [blame] | 929 | |
Steven Moreland | 0cea4aa | 2020-04-20 21:06:02 -0700 | [diff] [blame] | 930 | std::set<AidlAnnotation::Type> GetSupportedAnnotations() const override; |
Jooyung Han | 888c5bc | 2020-12-22 17:28:47 +0900 | [diff] [blame] | 931 | bool CheckValid(const AidlTypenames& typenames, DiagnosticsContext& context) const override; |
Steven Moreland | d59e317 | 2020-05-11 16:42:09 -0700 | [diff] [blame] | 932 | bool LanguageSpecificCheckValid(const AidlTypenames& typenames, |
| 933 | Options::Language lang) const override; |
Steven Moreland | 5557f1c | 2018-07-02 13:50:23 -0700 | [diff] [blame] | 934 | }; |
| 935 | |
Daniel Norman | 85aed54 | 2019-08-21 12:01:14 -0700 | [diff] [blame] | 936 | class AidlEnumerator : public AidlNode { |
| 937 | public: |
Daniel Norman | 2e4112d | 2019-10-03 10:22:35 -0700 | [diff] [blame] | 938 | AidlEnumerator(const AidlLocation& location, const std::string& name, AidlConstantValue* value, |
| 939 | const std::string& comments); |
Daniel Norman | 85aed54 | 2019-08-21 12:01:14 -0700 | [diff] [blame] | 940 | virtual ~AidlEnumerator() = default; |
| 941 | |
Jiyong Park | d800fef | 2020-07-22 18:09:43 +0900 | [diff] [blame] | 942 | // non-copyable, non-movable |
| 943 | AidlEnumerator(const AidlEnumerator&) = delete; |
| 944 | AidlEnumerator(AidlEnumerator&&) = delete; |
| 945 | AidlEnumerator& operator=(const AidlEnumerator&) = delete; |
| 946 | AidlEnumerator& operator=(AidlEnumerator&&) = delete; |
| 947 | |
Daniel Norman | 85aed54 | 2019-08-21 12:01:14 -0700 | [diff] [blame] | 948 | const std::string& GetName() const { return name_; } |
Will McVicker | d7d18df | 2019-09-12 13:40:50 -0700 | [diff] [blame] | 949 | AidlConstantValue* GetValue() const { return value_.get(); } |
Daniel Norman | 2e4112d | 2019-10-03 10:22:35 -0700 | [diff] [blame] | 950 | const std::string& GetComments() const { return comments_; } |
Daniel Norman | 85aed54 | 2019-08-21 12:01:14 -0700 | [diff] [blame] | 951 | bool CheckValid(const AidlTypeSpecifier& enum_backing_type) const; |
| 952 | |
| 953 | string ValueString(const AidlTypeSpecifier& backing_type, |
| 954 | const ConstantValueDecorator& decorator) const; |
| 955 | |
Daniel Norman | b28684e | 2019-10-17 15:31:39 -0700 | [diff] [blame] | 956 | void SetValue(std::unique_ptr<AidlConstantValue> value) { value_ = std::move(value); } |
Jooyung Han | 2981384 | 2020-12-08 01:28:03 +0900 | [diff] [blame] | 957 | bool IsValueUserSpecified() const { return value_user_specified_; } |
Daniel Norman | b28684e | 2019-10-17 15:31:39 -0700 | [diff] [blame] | 958 | |
Daniel Norman | 85aed54 | 2019-08-21 12:01:14 -0700 | [diff] [blame] | 959 | private: |
| 960 | const std::string name_; |
Will McVicker | d7d18df | 2019-09-12 13:40:50 -0700 | [diff] [blame] | 961 | unique_ptr<AidlConstantValue> value_; |
Daniel Norman | 2e4112d | 2019-10-03 10:22:35 -0700 | [diff] [blame] | 962 | const std::string comments_; |
Jooyung Han | 2981384 | 2020-12-08 01:28:03 +0900 | [diff] [blame] | 963 | const bool value_user_specified_; |
Daniel Norman | 85aed54 | 2019-08-21 12:01:14 -0700 | [diff] [blame] | 964 | }; |
| 965 | |
| 966 | class AidlEnumDeclaration : public AidlDefinedType { |
| 967 | public: |
Will McVicker | d7d18df | 2019-09-12 13:40:50 -0700 | [diff] [blame] | 968 | AidlEnumDeclaration(const AidlLocation& location, const string& name, |
Daniel Norman | 85aed54 | 2019-08-21 12:01:14 -0700 | [diff] [blame] | 969 | std::vector<std::unique_ptr<AidlEnumerator>>* enumerators, |
Jiyong Park | 1813218 | 2020-06-08 20:24:40 +0900 | [diff] [blame] | 970 | const std::string& package, const std::string& comments); |
Daniel Norman | 85aed54 | 2019-08-21 12:01:14 -0700 | [diff] [blame] | 971 | virtual ~AidlEnumDeclaration() = default; |
| 972 | |
Jiyong Park | d800fef | 2020-07-22 18:09:43 +0900 | [diff] [blame] | 973 | // non-copyable, non-movable |
| 974 | AidlEnumDeclaration(const AidlEnumDeclaration&) = delete; |
| 975 | AidlEnumDeclaration(AidlEnumDeclaration&&) = delete; |
| 976 | AidlEnumDeclaration& operator=(const AidlEnumDeclaration&) = delete; |
| 977 | AidlEnumDeclaration& operator=(AidlEnumDeclaration&&) = delete; |
| 978 | |
Jooyung Han | 672557b | 2020-12-24 05:18:00 +0900 | [diff] [blame] | 979 | bool Autofill(const AidlTypenames&); |
Daniel Norman | 85aed54 | 2019-08-21 12:01:14 -0700 | [diff] [blame] | 980 | const AidlTypeSpecifier& GetBackingType() const { return *backing_type_; } |
| 981 | const std::vector<std::unique_ptr<AidlEnumerator>>& GetEnumerators() const { |
| 982 | return enumerators_; |
| 983 | } |
Steven Moreland | 0cea4aa | 2020-04-20 21:06:02 -0700 | [diff] [blame] | 984 | std::set<AidlAnnotation::Type> GetSupportedAnnotations() const override; |
Jooyung Han | 888c5bc | 2020-12-22 17:28:47 +0900 | [diff] [blame] | 985 | bool CheckValid(const AidlTypenames& typenames, DiagnosticsContext& context) const override; |
Steven Moreland | d59e317 | 2020-05-11 16:42:09 -0700 | [diff] [blame] | 986 | bool LanguageSpecificCheckValid(const AidlTypenames& /*typenames*/, |
| 987 | Options::Language) const override { |
| 988 | return true; |
| 989 | } |
Daniel Norman | 85aed54 | 2019-08-21 12:01:14 -0700 | [diff] [blame] | 990 | std::string GetPreprocessDeclarationName() const override { return "enum"; } |
Jeongik Cha | 997281d | 2020-01-16 15:23:59 +0900 | [diff] [blame] | 991 | void Dump(CodeWriter* writer) const override; |
Daniel Norman | 85aed54 | 2019-08-21 12:01:14 -0700 | [diff] [blame] | 992 | |
| 993 | const AidlEnumDeclaration* AsEnumDeclaration() const override { return this; } |
| 994 | |
| 995 | private: |
Jooyung Han | 2981384 | 2020-12-08 01:28:03 +0900 | [diff] [blame] | 996 | |
Daniel Norman | 85aed54 | 2019-08-21 12:01:14 -0700 | [diff] [blame] | 997 | const std::string name_; |
| 998 | const std::vector<std::unique_ptr<AidlEnumerator>> enumerators_; |
Jooyung Han | 672557b | 2020-12-24 05:18:00 +0900 | [diff] [blame] | 999 | std::unique_ptr<AidlTypeSpecifier> backing_type_; |
Daniel Norman | 85aed54 | 2019-08-21 12:01:14 -0700 | [diff] [blame] | 1000 | }; |
| 1001 | |
Jooyung Han | 829ec7c | 2020-12-02 12:07:36 +0900 | [diff] [blame] | 1002 | class AidlUnionDecl : public AidlParcelable { |
Jooyung Han | 2946afc | 2020-10-05 20:29:16 +0900 | [diff] [blame] | 1003 | public: |
| 1004 | AidlUnionDecl(const AidlLocation& location, const std::string& name, const std::string& package, |
Jooyung Han | 829ec7c | 2020-12-02 12:07:36 +0900 | [diff] [blame] | 1005 | const std::string& comments, std::vector<std::string>* type_params, |
| 1006 | std::vector<std::unique_ptr<AidlMember>>* members); |
Jooyung Han | 2946afc | 2020-10-05 20:29:16 +0900 | [diff] [blame] | 1007 | virtual ~AidlUnionDecl() = default; |
| 1008 | |
| 1009 | // non-copyable, non-movable |
| 1010 | AidlUnionDecl(const AidlUnionDecl&) = delete; |
| 1011 | AidlUnionDecl(AidlUnionDecl&&) = delete; |
| 1012 | AidlUnionDecl& operator=(const AidlUnionDecl&) = delete; |
| 1013 | AidlUnionDecl& operator=(AidlUnionDecl&&) = delete; |
| 1014 | |
| 1015 | std::set<AidlAnnotation::Type> GetSupportedAnnotations() const override; |
| 1016 | |
| 1017 | const AidlNode& AsAidlNode() const override { return *this; } |
| 1018 | |
Jooyung Han | 888c5bc | 2020-12-22 17:28:47 +0900 | [diff] [blame] | 1019 | bool CheckValid(const AidlTypenames& typenames, DiagnosticsContext& context) const override; |
Jooyung Han | fe89f12 | 2020-10-14 03:49:18 +0900 | [diff] [blame] | 1020 | bool LanguageSpecificCheckValid(const AidlTypenames& typenames, |
| 1021 | Options::Language lang) const override; |
Jooyung Han | 2946afc | 2020-10-05 20:29:16 +0900 | [diff] [blame] | 1022 | std::string GetPreprocessDeclarationName() const override { return "union"; } |
| 1023 | |
| 1024 | void Dump(CodeWriter* writer) const override; |
| 1025 | const AidlUnionDecl* AsUnionDeclaration() const override { return this; } |
Jooyung Han | 2946afc | 2020-10-05 20:29:16 +0900 | [diff] [blame] | 1026 | }; |
| 1027 | |
Jiyong Park | 1deecc3 | 2018-07-17 01:14:41 +0900 | [diff] [blame] | 1028 | class AidlInterface final : public AidlDefinedType { |
Casey Dahlin | 1ae2bc5 | 2015-10-07 18:49:10 -0700 | [diff] [blame] | 1029 | public: |
Steven Moreland | 46e9da8 | 2018-07-27 15:45:29 -0700 | [diff] [blame] | 1030 | AidlInterface(const AidlLocation& location, const std::string& name, const std::string& comments, |
Jooyung Han | 829ec7c | 2020-12-02 12:07:36 +0900 | [diff] [blame] | 1031 | bool oneway_, const std::string& package, |
| 1032 | std::vector<std::unique_ptr<AidlMember>>* members); |
Casey Dahlin | 1ae2bc5 | 2015-10-07 18:49:10 -0700 | [diff] [blame] | 1033 | virtual ~AidlInterface() = default; |
| 1034 | |
Jiyong Park | d800fef | 2020-07-22 18:09:43 +0900 | [diff] [blame] | 1035 | // non-copyable, non-movable |
| 1036 | AidlInterface(const AidlInterface&) = delete; |
| 1037 | AidlInterface(AidlInterface&&) = delete; |
| 1038 | AidlInterface& operator=(const AidlInterface&) = delete; |
| 1039 | AidlInterface& operator=(AidlInterface&&) = delete; |
| 1040 | |
Steven Moreland | c258abc | 2018-07-10 14:03:38 -0700 | [diff] [blame] | 1041 | const AidlInterface* AsInterface() const override { return this; } |
Steven Moreland | ed83a28 | 2018-07-17 13:27:29 -0700 | [diff] [blame] | 1042 | std::string GetPreprocessDeclarationName() const override { return "interface"; } |
Steven Moreland | 5557f1c | 2018-07-02 13:50:23 -0700 | [diff] [blame] | 1043 | |
Jeongik Cha | 997281d | 2020-01-16 15:23:59 +0900 | [diff] [blame] | 1044 | void Dump(CodeWriter* writer) const override; |
Jiyong Park | 02da742 | 2018-07-16 16:00:26 +0900 | [diff] [blame] | 1045 | |
Steven Moreland | 0cea4aa | 2020-04-20 21:06:02 -0700 | [diff] [blame] | 1046 | std::set<AidlAnnotation::Type> GetSupportedAnnotations() const override; |
Jooyung Han | 888c5bc | 2020-12-22 17:28:47 +0900 | [diff] [blame] | 1047 | bool CheckValid(const AidlTypenames& typenames, DiagnosticsContext& context) const override; |
Steven Moreland | d59e317 | 2020-05-11 16:42:09 -0700 | [diff] [blame] | 1048 | bool LanguageSpecificCheckValid(const AidlTypenames& typenames, |
| 1049 | Options::Language lang) const override; |
Jeongik Cha | db0f59e | 2018-11-01 18:11:21 +0900 | [diff] [blame] | 1050 | |
Jiyong Park | 27fd7fd | 2020-08-27 16:25:09 +0900 | [diff] [blame] | 1051 | std::string GetDescriptor() const; |
Casey Dahlin | 0a2f8be | 2015-09-28 16:15:29 -0700 | [diff] [blame] | 1052 | }; |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 1053 | |
Casey Dahlin | 0edf342 | 2015-10-07 12:34:59 -0700 | [diff] [blame] | 1054 | class AidlImport : public AidlNode { |
| 1055 | public: |
Steven Moreland | 46e9da8 | 2018-07-27 15:45:29 -0700 | [diff] [blame] | 1056 | AidlImport(const AidlLocation& location, const std::string& needed_class); |
Casey Dahlin | 0edf342 | 2015-10-07 12:34:59 -0700 | [diff] [blame] | 1057 | virtual ~AidlImport() = default; |
| 1058 | |
Jiyong Park | d800fef | 2020-07-22 18:09:43 +0900 | [diff] [blame] | 1059 | // non-copyable, non-movable |
| 1060 | AidlImport(const AidlImport&) = delete; |
| 1061 | AidlImport(AidlImport&&) = delete; |
| 1062 | AidlImport& operator=(const AidlImport&) = delete; |
| 1063 | AidlImport& operator=(AidlImport&&) = delete; |
| 1064 | |
Casey Dahlin | 0edf342 | 2015-10-07 12:34:59 -0700 | [diff] [blame] | 1065 | const std::string& GetNeededClass() const { return needed_class_; } |
Casey Dahlin | 0edf342 | 2015-10-07 12:34:59 -0700 | [diff] [blame] | 1066 | |
| 1067 | private: |
Casey Dahlin | 0edf342 | 2015-10-07 12:34:59 -0700 | [diff] [blame] | 1068 | std::string needed_class_; |
Casey Dahlin | e250749 | 2015-09-14 17:11:20 -0700 | [diff] [blame] | 1069 | }; |
| 1070 | |
Jiyong Park | 6251551 | 2020-06-08 15:57:11 +0900 | [diff] [blame] | 1071 | // AidlDocument models an AIDL file |
| 1072 | class AidlDocument : public AidlNode { |
| 1073 | public: |
| 1074 | AidlDocument(const AidlLocation& location, std::vector<std::unique_ptr<AidlImport>>& imports, |
Jiyong Park | 8e79b7f | 2020-07-20 20:52:38 +0900 | [diff] [blame] | 1075 | std::vector<std::unique_ptr<AidlDefinedType>>&& defined_types) |
| 1076 | : AidlNode(location), |
| 1077 | imports_(std::move(imports)), |
| 1078 | defined_types_(std::move(defined_types)) {} |
Jiyong Park | d800fef | 2020-07-22 18:09:43 +0900 | [diff] [blame] | 1079 | ~AidlDocument() = default; |
| 1080 | |
| 1081 | // non-copyable, non-movable |
Jiyong Park | 8e79b7f | 2020-07-20 20:52:38 +0900 | [diff] [blame] | 1082 | AidlDocument(const AidlDocument&) = delete; |
| 1083 | AidlDocument(AidlDocument&&) = delete; |
| 1084 | AidlDocument& operator=(const AidlDocument&) = delete; |
| 1085 | AidlDocument& operator=(AidlDocument&&) = delete; |
Jiyong Park | d800fef | 2020-07-22 18:09:43 +0900 | [diff] [blame] | 1086 | |
Jooyung Han | 3557e48 | 2020-12-24 05:11:15 +0900 | [diff] [blame] | 1087 | bool CheckValid(const AidlTypenames& typenames, DiagnosticsContext& diag) const; |
Jooyung Han | 2981384 | 2020-12-08 01:28:03 +0900 | [diff] [blame] | 1088 | std::optional<std::string> ResolveName(const std::string& unresolved_type) const; |
Jiyong Park | d800fef | 2020-07-22 18:09:43 +0900 | [diff] [blame] | 1089 | const std::vector<std::unique_ptr<AidlImport>>& Imports() const { return imports_; } |
| 1090 | const std::vector<std::unique_ptr<AidlDefinedType>>& DefinedTypes() const { |
| 1091 | return defined_types_; |
| 1092 | } |
Jiyong Park | 6251551 | 2020-06-08 15:57:11 +0900 | [diff] [blame] | 1093 | |
| 1094 | private: |
| 1095 | const std::vector<std::unique_ptr<AidlImport>> imports_; |
Jiyong Park | 8e79b7f | 2020-07-20 20:52:38 +0900 | [diff] [blame] | 1096 | const std::vector<std::unique_ptr<AidlDefinedType>> defined_types_; |
Jiyong Park | 6251551 | 2020-06-08 15:57:11 +0900 | [diff] [blame] | 1097 | }; |
Jooyung Han | b3c77ed | 2020-12-26 02:02:45 +0900 | [diff] [blame^] | 1098 | |
| 1099 | template <typename T> |
| 1100 | std::optional<T> AidlAnnotation::ParamValue(const std::string& param_name) const { |
| 1101 | auto it = parameters_.find(param_name); |
| 1102 | if (it == parameters_.end()) { |
| 1103 | return std::nullopt; |
| 1104 | } |
| 1105 | return it->second->Cast<T>(); |
| 1106 | } |