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