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