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