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