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