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