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