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