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