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