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