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 | |
Jiyong Park | 1deecc3 | 2018-07-17 01:14:41 +0900 | [diff] [blame] | 19 | #include "aidl_typenames.h" |
Jiyong Park | 02da742 | 2018-07-16 16:00:26 +0900 | [diff] [blame] | 20 | #include "code_writer.h" |
Jiyong Park | 1deecc3 | 2018-07-17 01:14:41 +0900 | [diff] [blame] | 21 | #include "io_delegate.h" |
Jeongik Cha | 047c5ee | 2019-08-07 23:16:49 +0900 | [diff] [blame] | 22 | #include "options.h" |
Jiyong Park | 1deecc3 | 2018-07-17 01:14:41 +0900 | [diff] [blame] | 23 | |
Casey Dahlin | bc7a50a | 2015-09-28 19:20:50 -0700 | [diff] [blame] | 24 | #include <memory> |
Jeongik Cha | 997281d | 2020-01-16 15:23:59 +0900 | [diff] [blame] | 25 | #include <regex> |
Casey Dahlin | dd69181 | 2015-09-09 17:59:06 -0700 | [diff] [blame] | 26 | #include <string> |
Jeongik Cha | df76dc7 | 2019-11-28 00:08:47 +0900 | [diff] [blame] | 27 | #include <unordered_set> |
Casey Dahlin | bc7a50a | 2015-09-28 19:20:50 -0700 | [diff] [blame] | 28 | #include <vector> |
Casey Dahlin | dd69181 | 2015-09-09 17:59:06 -0700 | [diff] [blame] | 29 | |
Elliott Hughes | 0a62067 | 2015-12-04 13:53:18 -0800 | [diff] [blame] | 30 | #include <android-base/macros.h> |
| 31 | #include <android-base/strings.h> |
Casey Dahlin | 73d46b0 | 2015-09-11 02:47:54 +0000 | [diff] [blame] | 32 | |
Casey Dahlin | 89d4484 | 2015-09-24 18:45:54 -0700 | [diff] [blame] | 33 | struct yy_buffer_state; |
| 34 | typedef yy_buffer_state* YY_BUFFER_STATE; |
| 35 | |
Jiyong Park | b034bf0 | 2018-07-30 17:44:33 +0900 | [diff] [blame] | 36 | using android::aidl::AidlTypenames; |
Jiyong Park | 02da742 | 2018-07-16 16:00:26 +0900 | [diff] [blame] | 37 | using android::aidl::CodeWriter; |
Jeongik Cha | 047c5ee | 2019-08-07 23:16:49 +0900 | [diff] [blame] | 38 | using android::aidl::Options; |
Steven Moreland | 3f658cf | 2018-08-20 13:40:54 -0700 | [diff] [blame] | 39 | using std::shared_ptr; |
Jiyong Park | 1deecc3 | 2018-07-17 01:14:41 +0900 | [diff] [blame] | 40 | using std::string; |
| 41 | using std::unique_ptr; |
| 42 | using std::vector; |
Andrei Onea | 8714b02 | 2019-02-01 18:55:54 +0000 | [diff] [blame] | 43 | class AidlNode; |
| 44 | |
| 45 | namespace android { |
| 46 | namespace aidl { |
| 47 | namespace mappings { |
| 48 | std::string dump_location(const AidlNode& method); |
| 49 | } // namespace mappings |
Mathew Inwood | adb7467 | 2019-11-29 14:01:53 +0000 | [diff] [blame] | 50 | namespace java { |
| 51 | std::string dump_location(const AidlNode& method); |
| 52 | } // namespace java |
Andrei Onea | 8714b02 | 2019-02-01 18:55:54 +0000 | [diff] [blame] | 53 | } // namespace aidl |
| 54 | } // namespace android |
| 55 | |
Casey Dahlin | cdbbc8c | 2015-10-14 15:31:04 -0700 | [diff] [blame] | 56 | class AidlToken { |
| 57 | public: |
| 58 | AidlToken(const std::string& text, const std::string& comments); |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 59 | |
Casey Dahlin | cdbbc8c | 2015-10-14 15:31:04 -0700 | [diff] [blame] | 60 | const std::string& GetText() const { return text_; } |
| 61 | const std::string& GetComments() const { return comments_; } |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 62 | |
Casey Dahlin | cdbbc8c | 2015-10-14 15:31:04 -0700 | [diff] [blame] | 63 | private: |
| 64 | std::string text_; |
| 65 | std::string comments_; |
Casey Dahlin | 082f1d1 | 2015-09-21 14:06:25 -0700 | [diff] [blame] | 66 | |
Casey Dahlin | cdbbc8c | 2015-10-14 15:31:04 -0700 | [diff] [blame] | 67 | DISALLOW_COPY_AND_ASSIGN(AidlToken); |
Casey Dahlin | 0a2f8be | 2015-09-28 16:15:29 -0700 | [diff] [blame] | 68 | }; |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 69 | |
Steven Moreland | 46e9da8 | 2018-07-27 15:45:29 -0700 | [diff] [blame] | 70 | class AidlLocation { |
Casey Dahlin | bc7a50a | 2015-09-28 19:20:50 -0700 | [diff] [blame] | 71 | public: |
Steven Moreland | 46e9da8 | 2018-07-27 15:45:29 -0700 | [diff] [blame] | 72 | struct Point { |
Dan Willemsen | 609ba6d | 2019-12-30 10:44:00 -0800 | [diff] [blame] | 73 | int line; |
| 74 | int column; |
Steven Moreland | 46e9da8 | 2018-07-27 15:45:29 -0700 | [diff] [blame] | 75 | }; |
| 76 | |
Steven Moreland | 46e9da8 | 2018-07-27 15:45:29 -0700 | [diff] [blame] | 77 | AidlLocation(const std::string& file, Point begin, Point end); |
| 78 | |
Steven Moreland | 46e9da8 | 2018-07-27 15:45:29 -0700 | [diff] [blame] | 79 | friend std::ostream& operator<<(std::ostream& os, const AidlLocation& l); |
Andrei Onea | 8714b02 | 2019-02-01 18:55:54 +0000 | [diff] [blame] | 80 | friend class AidlNode; |
Casey Dahlin | bc7a50a | 2015-09-28 19:20:50 -0700 | [diff] [blame] | 81 | |
| 82 | private: |
Steven Moreland | 46e9da8 | 2018-07-27 15:45:29 -0700 | [diff] [blame] | 83 | const std::string file_; |
| 84 | Point begin_; |
| 85 | Point end_; |
| 86 | }; |
| 87 | |
Steven Moreland | 02e012e | 2018-08-02 14:58:10 -0700 | [diff] [blame] | 88 | #define AIDL_LOCATION_HERE \ |
| 89 | AidlLocation { \ |
| 90 | __FILE__, {__LINE__, 0}, { __LINE__, 0 } \ |
| 91 | } |
| 92 | |
Steven Moreland | 46e9da8 | 2018-07-27 15:45:29 -0700 | [diff] [blame] | 93 | std::ostream& operator<<(std::ostream& os, const AidlLocation& l); |
| 94 | |
| 95 | // Anything that is locatable in a .aidl file. |
| 96 | class AidlNode { |
| 97 | public: |
| 98 | AidlNode(const AidlLocation& location); |
Steven Moreland | 3f658cf | 2018-08-20 13:40:54 -0700 | [diff] [blame] | 99 | |
| 100 | AidlNode(const AidlNode&) = default; |
Steven Moreland | 3be7577 | 2018-08-20 13:27:43 -0700 | [diff] [blame] | 101 | AidlNode(AidlNode&&) = default; |
Steven Moreland | 46e9da8 | 2018-07-27 15:45:29 -0700 | [diff] [blame] | 102 | virtual ~AidlNode() = default; |
| 103 | |
Steven Moreland | 46e9da8 | 2018-07-27 15:45:29 -0700 | [diff] [blame] | 104 | // DO NOT ADD. This is intentionally omitted. Nothing should refer to the location |
| 105 | // for a functional purpose. It is only for error messages. |
| 106 | // NO const AidlLocation& GetLocation() const { return location_; } NO |
| 107 | |
| 108 | // To be able to print AidlLocation (nothing else should use this information) |
| 109 | friend class AidlError; |
Andrei Onea | 8714b02 | 2019-02-01 18:55:54 +0000 | [diff] [blame] | 110 | friend std::string android::aidl::mappings::dump_location(const AidlNode&); |
Mathew Inwood | adb7467 | 2019-11-29 14:01:53 +0000 | [diff] [blame] | 111 | friend std::string android::aidl::java::dump_location(const AidlNode&); |
Steven Moreland | 46e9da8 | 2018-07-27 15:45:29 -0700 | [diff] [blame] | 112 | |
| 113 | private: |
Mathew Inwood | adb7467 | 2019-11-29 14:01:53 +0000 | [diff] [blame] | 114 | std::string PrintLine() const; |
Andrei Onea | 8714b02 | 2019-02-01 18:55:54 +0000 | [diff] [blame] | 115 | std::string PrintLocation() const; |
Steven Moreland | 46e9da8 | 2018-07-27 15:45:29 -0700 | [diff] [blame] | 116 | const AidlLocation location_; |
Casey Dahlin | bc7a50a | 2015-09-28 19:20:50 -0700 | [diff] [blame] | 117 | }; |
| 118 | |
Steven Moreland | 46e9da8 | 2018-07-27 15:45:29 -0700 | [diff] [blame] | 119 | // Generic point for printing any error in the AIDL compiler. |
| 120 | class AidlError { |
| 121 | public: |
Steven Moreland | 92c55f1 | 2018-07-31 14:08:37 -0700 | [diff] [blame] | 122 | AidlError(bool fatal, const std::string& filename) : AidlError(fatal) { os_ << filename << ": "; } |
| 123 | AidlError(bool fatal, const AidlLocation& location) : AidlError(fatal) { |
Steven Moreland | 46e9da8 | 2018-07-27 15:45:29 -0700 | [diff] [blame] | 124 | os_ << location << ": "; |
| 125 | } |
Steven Moreland | 92c55f1 | 2018-07-31 14:08:37 -0700 | [diff] [blame] | 126 | AidlError(bool fatal, const AidlNode& node) : AidlError(fatal, node.location_) {} |
| 127 | AidlError(bool fatal, const AidlNode* node) : AidlError(fatal, *node) {} |
| 128 | |
| 129 | template <typename T> |
| 130 | AidlError(bool fatal, const std::unique_ptr<T>& node) : AidlError(fatal, *node) {} |
Steven Moreland | 46e9da8 | 2018-07-27 15:45:29 -0700 | [diff] [blame] | 131 | ~AidlError() { |
| 132 | os_ << std::endl; |
| 133 | if (fatal_) abort(); |
| 134 | } |
| 135 | |
| 136 | std::ostream& os_; |
| 137 | |
Steven Moreland | fdb57cd | 2020-01-08 20:03:30 -0800 | [diff] [blame] | 138 | static bool hadError() { return sHadError; } |
| 139 | |
Steven Moreland | 46e9da8 | 2018-07-27 15:45:29 -0700 | [diff] [blame] | 140 | private: |
Steven Moreland | 92c55f1 | 2018-07-31 14:08:37 -0700 | [diff] [blame] | 141 | AidlError(bool fatal); |
Steven Moreland | 46e9da8 | 2018-07-27 15:45:29 -0700 | [diff] [blame] | 142 | |
| 143 | bool fatal_; |
| 144 | |
Steven Moreland | fdb57cd | 2020-01-08 20:03:30 -0800 | [diff] [blame] | 145 | static bool sHadError; |
| 146 | |
Steven Moreland | 46e9da8 | 2018-07-27 15:45:29 -0700 | [diff] [blame] | 147 | DISALLOW_COPY_AND_ASSIGN(AidlError); |
| 148 | }; |
| 149 | |
Steven Moreland | 92c55f1 | 2018-07-31 14:08:37 -0700 | [diff] [blame] | 150 | #define AIDL_ERROR(CONTEXT) ::AidlError(false /*fatal*/, (CONTEXT)).os_ |
| 151 | #define AIDL_FATAL(CONTEXT) ::AidlError(true /*fatal*/, (CONTEXT)).os_ |
Steven Moreland | 3f658cf | 2018-08-20 13:40:54 -0700 | [diff] [blame] | 152 | #define AIDL_FATAL_IF(CONDITION, CONTEXT) \ |
| 153 | if (CONDITION) AIDL_FATAL(CONTEXT) << "Bad internal state: " << #CONDITION << ": " |
Steven Moreland | 46e9da8 | 2018-07-27 15:45:29 -0700 | [diff] [blame] | 154 | |
Casey Dahlin | a2f77c4 | 2015-12-01 18:26:02 -0800 | [diff] [blame] | 155 | namespace android { |
| 156 | namespace aidl { |
| 157 | |
Jiyong Park | 1deecc3 | 2018-07-17 01:14:41 +0900 | [diff] [blame] | 158 | class AidlTypenames; |
Casey Dahlin | a2f77c4 | 2015-12-01 18:26:02 -0800 | [diff] [blame] | 159 | |
| 160 | } // namespace aidl |
| 161 | } // namespace android |
| 162 | |
Jeongik Cha | df76dc7 | 2019-11-28 00:08:47 +0900 | [diff] [blame] | 163 | // unique_ptr<AidlTypeSpecifier> for type arugment, |
| 164 | // std::string for type parameter(T, U, and so on). |
| 165 | template <typename T> |
| 166 | class AidlParameterizable { |
| 167 | public: |
| 168 | AidlParameterizable(std::vector<T>* type_params) : type_params_(type_params) {} |
| 169 | virtual ~AidlParameterizable() = default; |
| 170 | bool IsGeneric() const { return type_params_ != nullptr; } |
| 171 | const std::vector<T>& GetTypeParameters() const { return *type_params_; } |
| 172 | bool CheckValid() const; |
| 173 | |
| 174 | virtual const AidlNode& AsAidlNode() const = 0; |
| 175 | |
| 176 | protected: |
| 177 | AidlParameterizable(const AidlParameterizable&); |
| 178 | |
| 179 | private: |
| 180 | const unique_ptr<std::vector<T>> type_params_; |
| 181 | static_assert(std::is_same<T, unique_ptr<AidlTypeSpecifier>>::value || |
| 182 | std::is_same<T, std::string>::value); |
| 183 | }; |
| 184 | template <> |
| 185 | bool AidlParameterizable<std::string>::CheckValid() const; |
| 186 | |
Andrei Onea | 9445fc6 | 2019-06-27 18:11:59 +0100 | [diff] [blame] | 187 | class AidlConstantValue; |
| 188 | class AidlConstantDeclaration; |
| 189 | |
| 190 | // Transforms a value string into a language specific form. Raw value as produced by |
| 191 | // AidlConstantValue. |
| 192 | using ConstantValueDecorator = |
| 193 | std::function<std::string(const AidlTypeSpecifier& type, const std::string& raw_value)>; |
| 194 | |
Jiyong Park | 68bc77a | 2018-07-19 19:00:45 +0900 | [diff] [blame] | 195 | class AidlAnnotation : public AidlNode { |
| 196 | public: |
Andrei Onea | 9445fc6 | 2019-06-27 18:11:59 +0100 | [diff] [blame] | 197 | static AidlAnnotation* Parse( |
| 198 | const AidlLocation& location, const string& name, |
| 199 | std::map<std::string, std::shared_ptr<AidlConstantValue>>* parameter_list); |
Steven Moreland | 46e9da8 | 2018-07-27 15:45:29 -0700 | [diff] [blame] | 200 | |
Steven Moreland | 3f658cf | 2018-08-20 13:40:54 -0700 | [diff] [blame] | 201 | AidlAnnotation(const AidlAnnotation&) = default; |
Steven Moreland | 3be7577 | 2018-08-20 13:27:43 -0700 | [diff] [blame] | 202 | AidlAnnotation(AidlAnnotation&&) = default; |
Jiyong Park | 68bc77a | 2018-07-19 19:00:45 +0900 | [diff] [blame] | 203 | virtual ~AidlAnnotation() = default; |
Andrei Onea | 9445fc6 | 2019-06-27 18:11:59 +0100 | [diff] [blame] | 204 | bool CheckValid() const; |
Steven Moreland | 3be7577 | 2018-08-20 13:27:43 -0700 | [diff] [blame] | 205 | |
Jiyong Park | 68bc77a | 2018-07-19 19:00:45 +0900 | [diff] [blame] | 206 | const string& GetName() const { return name_; } |
Daniel Norman | 37d43dd | 2019-09-09 17:22:34 -0700 | [diff] [blame] | 207 | string ToString(const ConstantValueDecorator& decorator) const; |
Andrei Onea | 9445fc6 | 2019-06-27 18:11:59 +0100 | [diff] [blame] | 208 | std::map<std::string, std::string> AnnotationParams( |
| 209 | const ConstantValueDecorator& decorator) const; |
Jiyong Park | a6605ab | 2018-11-11 14:30:21 +0900 | [diff] [blame] | 210 | const string& GetComments() const { return comments_; } |
| 211 | void SetComments(const string& comments) { comments_ = comments; } |
Jiyong Park | 68bc77a | 2018-07-19 19:00:45 +0900 | [diff] [blame] | 212 | |
| 213 | private: |
Steven Moreland | 46e9da8 | 2018-07-27 15:45:29 -0700 | [diff] [blame] | 214 | AidlAnnotation(const AidlLocation& location, const string& name); |
Andrei Onea | 9445fc6 | 2019-06-27 18:11:59 +0100 | [diff] [blame] | 215 | AidlAnnotation(const AidlLocation& location, const string& name, |
| 216 | std::map<std::string, std::shared_ptr<AidlConstantValue>>&& parameters); |
Jiyong Park | 68bc77a | 2018-07-19 19:00:45 +0900 | [diff] [blame] | 217 | const string name_; |
Jiyong Park | a6605ab | 2018-11-11 14:30:21 +0900 | [diff] [blame] | 218 | string comments_; |
Andrei Onea | 9445fc6 | 2019-06-27 18:11:59 +0100 | [diff] [blame] | 219 | std::map<std::string, std::shared_ptr<AidlConstantValue>> parameters_; |
Jiyong Park | 68bc77a | 2018-07-19 19:00:45 +0900 | [diff] [blame] | 220 | }; |
| 221 | |
Steven Moreland | 3be7577 | 2018-08-20 13:27:43 -0700 | [diff] [blame] | 222 | static inline bool operator<(const AidlAnnotation& lhs, const AidlAnnotation& rhs) { |
| 223 | return lhs.GetName() < rhs.GetName(); |
| 224 | } |
| 225 | static inline bool operator==(const AidlAnnotation& lhs, const AidlAnnotation& rhs) { |
| 226 | return lhs.GetName() == rhs.GetName(); |
| 227 | } |
Jiyong Park | 3656c3c | 2018-08-01 20:02:01 +0900 | [diff] [blame] | 228 | |
Casey Dahlin | e792293 | 2016-02-29 17:23:01 -0800 | [diff] [blame] | 229 | class AidlAnnotatable : public AidlNode { |
Casey Dahlin | 0ee3758 | 2015-09-30 16:31:55 -0700 | [diff] [blame] | 230 | public: |
Steven Moreland | 46e9da8 | 2018-07-27 15:45:29 -0700 | [diff] [blame] | 231 | AidlAnnotatable(const AidlLocation& location); |
Steven Moreland | 3f658cf | 2018-08-20 13:40:54 -0700 | [diff] [blame] | 232 | |
| 233 | AidlAnnotatable(const AidlAnnotatable&) = default; |
| 234 | AidlAnnotatable(AidlAnnotatable&&) = default; |
Casey Dahlin | e792293 | 2016-02-29 17:23:01 -0800 | [diff] [blame] | 235 | virtual ~AidlAnnotatable() = default; |
| 236 | |
Artur Satayev | 91fe871 | 2019-07-29 13:06:01 +0100 | [diff] [blame] | 237 | void Annotate(vector<AidlAnnotation>&& annotations) { |
| 238 | for (auto& annotation : annotations) { |
| 239 | annotations_.emplace_back(std::move(annotation)); |
| 240 | } |
| 241 | } |
Jiyong Park | 68bc77a | 2018-07-19 19:00:45 +0900 | [diff] [blame] | 242 | bool IsNullable() const; |
Jiyong Park | 68bc77a | 2018-07-19 19:00:45 +0900 | [diff] [blame] | 243 | bool IsUtf8InCpp() const; |
Steven Moreland | a57d0a6 | 2019-07-30 09:41:14 -0700 | [diff] [blame] | 244 | bool IsVintfStability() const; |
Jeongik Cha | 88f95a8 | 2020-01-15 13:02:16 +0900 | [diff] [blame] | 245 | bool IsStableApiParcelable(Options::Language lang) const; |
Makoto Onuki | 78a1c1c | 2020-03-04 16:57:23 -0800 | [diff] [blame] | 246 | bool IsHide() const; |
Andrei Onea | 9445fc6 | 2019-06-27 18:11:59 +0100 | [diff] [blame] | 247 | |
Steven Moreland | 7e4b950 | 2020-02-20 18:10:42 -0800 | [diff] [blame] | 248 | void DumpAnnotations(CodeWriter* writer) const; |
| 249 | |
Andrei Onea | 9445fc6 | 2019-06-27 18:11:59 +0100 | [diff] [blame] | 250 | const AidlAnnotation* UnsupportedAppUsage() const; |
Daniel Norman | 716d311 | 2019-09-10 13:11:56 -0700 | [diff] [blame] | 251 | const AidlTypeSpecifier* BackingType(const AidlTypenames& typenames) const; |
Jiyong Park | 68bc77a | 2018-07-19 19:00:45 +0900 | [diff] [blame] | 252 | std::string ToString() const; |
Casey Dahlin | e792293 | 2016-02-29 17:23:01 -0800 | [diff] [blame] | 253 | |
Jiyong Park | a6605ab | 2018-11-11 14:30:21 +0900 | [diff] [blame] | 254 | const vector<AidlAnnotation>& GetAnnotations() const { return annotations_; } |
Devin Moore | 24f6857 | 2020-02-26 13:20:59 -0800 | [diff] [blame] | 255 | virtual std::set<string> GetSupportedAnnotations() const = 0; |
| 256 | virtual bool CheckValid(const AidlTypenames&) const; |
Jiyong Park | 3656c3c | 2018-08-01 20:02:01 +0900 | [diff] [blame] | 257 | |
Casey Dahlin | e792293 | 2016-02-29 17:23:01 -0800 | [diff] [blame] | 258 | private: |
Jiyong Park | a6605ab | 2018-11-11 14:30:21 +0900 | [diff] [blame] | 259 | vector<AidlAnnotation> annotations_; |
Casey Dahlin | e792293 | 2016-02-29 17:23:01 -0800 | [diff] [blame] | 260 | }; |
| 261 | |
Jiyong Park | 1deecc3 | 2018-07-17 01:14:41 +0900 | [diff] [blame] | 262 | class AidlQualifiedName; |
| 263 | |
| 264 | // AidlTypeSpecifier represents a reference to either a built-in type, |
| 265 | // 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] | 266 | class AidlTypeSpecifier final : public AidlAnnotatable, |
| 267 | public AidlParameterizable<unique_ptr<AidlTypeSpecifier>> { |
Casey Dahlin | e792293 | 2016-02-29 17:23:01 -0800 | [diff] [blame] | 268 | public: |
Steven Moreland | 46e9da8 | 2018-07-27 15:45:29 -0700 | [diff] [blame] | 269 | AidlTypeSpecifier(const AidlLocation& location, const string& unresolved_name, bool is_array, |
| 270 | vector<unique_ptr<AidlTypeSpecifier>>* type_params, const string& comments); |
Jiyong Park | d59a10d | 2018-07-18 11:12:55 +0900 | [diff] [blame] | 271 | virtual ~AidlTypeSpecifier() = default; |
Casey Dahlin | 0ee3758 | 2015-09-30 16:31:55 -0700 | [diff] [blame] | 272 | |
Steven Moreland | 3f658cf | 2018-08-20 13:40:54 -0700 | [diff] [blame] | 273 | // Copy of this type which is not an array. |
| 274 | AidlTypeSpecifier ArrayBase() const; |
| 275 | |
Jiyong Park | 1deecc3 | 2018-07-17 01:14:41 +0900 | [diff] [blame] | 276 | // Returns the full-qualified name of the base type. |
| 277 | // int -> int |
| 278 | // int[] -> int |
| 279 | // List<String> -> List |
| 280 | // IFoo -> foo.bar.IFoo (if IFoo is in package foo.bar) |
| 281 | const string& GetName() const { |
| 282 | if (IsResolved()) { |
| 283 | return fully_qualified_name_; |
| 284 | } else { |
| 285 | return GetUnresolvedName(); |
| 286 | } |
| 287 | } |
Casey Dahlin | 0ee3758 | 2015-09-30 16:31:55 -0700 | [diff] [blame] | 288 | |
Jiyong Park | 1deecc3 | 2018-07-17 01:14:41 +0900 | [diff] [blame] | 289 | // Returns string representation of this type specifier. |
Artur Satayev | 91fe871 | 2019-07-29 13:06:01 +0100 | [diff] [blame] | 290 | // This is GetBaseTypeName() + array modifier or generic type parameters |
Jiyong Park | 1deecc3 | 2018-07-17 01:14:41 +0900 | [diff] [blame] | 291 | string ToString() const; |
| 292 | |
Jiyong Park | 02da742 | 2018-07-16 16:00:26 +0900 | [diff] [blame] | 293 | std::string Signature() const; |
| 294 | |
Jiyong Park | 1deecc3 | 2018-07-17 01:14:41 +0900 | [diff] [blame] | 295 | const string& GetUnresolvedName() const { return unresolved_name_; } |
| 296 | |
Jeongik Cha | 997281d | 2020-01-16 15:23:59 +0900 | [diff] [blame] | 297 | bool IsHidden() const; |
| 298 | |
Jiyong Park | 1deecc3 | 2018-07-17 01:14:41 +0900 | [diff] [blame] | 299 | const string& GetComments() const { return comments_; } |
| 300 | |
Jeongik Cha | 1a7ab64 | 2019-07-29 17:31:02 +0900 | [diff] [blame] | 301 | const std::vector<std::string> GetSplitName() const { return split_name_; } |
| 302 | |
Jiyong Park | a6605ab | 2018-11-11 14:30:21 +0900 | [diff] [blame] | 303 | void SetComments(const string& comment) { comments_ = comment; } |
| 304 | |
Jiyong Park | 1deecc3 | 2018-07-17 01:14:41 +0900 | [diff] [blame] | 305 | bool IsResolved() const { return fully_qualified_name_ != ""; } |
| 306 | |
| 307 | bool IsArray() const { return is_array_; } |
| 308 | |
Jiyong Park | 1deecc3 | 2018-07-17 01:14:41 +0900 | [diff] [blame] | 309 | // Resolve the base type name to a fully-qualified name. Return false if the |
| 310 | // resolution fails. |
Daniel Norman | 716d311 | 2019-09-10 13:11:56 -0700 | [diff] [blame] | 311 | bool Resolve(const AidlTypenames& typenames); |
Casey Dahlin | 0ee3758 | 2015-09-30 16:31:55 -0700 | [diff] [blame] | 312 | |
Devin Moore | 24f6857 | 2020-02-26 13:20:59 -0800 | [diff] [blame] | 313 | std::set<string> GetSupportedAnnotations() const override; |
| 314 | bool CheckValid(const AidlTypenames& typenames) const override; |
Jeongik Cha | 047c5ee | 2019-08-07 23:16:49 +0900 | [diff] [blame] | 315 | bool LanguageSpecificCheckValid(Options::Language lang) const; |
Jeongik Cha | df76dc7 | 2019-11-28 00:08:47 +0900 | [diff] [blame] | 316 | const AidlNode& AsAidlNode() const override { return *this; } |
Jiyong Park | 1d2df7d | 2018-07-23 15:22:50 +0900 | [diff] [blame] | 317 | |
Casey Dahlin | 0ee3758 | 2015-09-30 16:31:55 -0700 | [diff] [blame] | 318 | private: |
Steven Moreland | 3f658cf | 2018-08-20 13:40:54 -0700 | [diff] [blame] | 319 | AidlTypeSpecifier(const AidlTypeSpecifier&) = default; |
| 320 | |
Jiyong Park | 1deecc3 | 2018-07-17 01:14:41 +0900 | [diff] [blame] | 321 | const string unresolved_name_; |
| 322 | string fully_qualified_name_; |
Steven Moreland | 3f658cf | 2018-08-20 13:40:54 -0700 | [diff] [blame] | 323 | bool is_array_; |
Jiyong Park | a6605ab | 2018-11-11 14:30:21 +0900 | [diff] [blame] | 324 | string comments_; |
Jeongik Cha | 1a7ab64 | 2019-07-29 17:31:02 +0900 | [diff] [blame] | 325 | vector<string> split_name_; |
Casey Dahlin | 0ee3758 | 2015-09-30 16:31:55 -0700 | [diff] [blame] | 326 | }; |
| 327 | |
Steven Moreland | 860b194 | 2018-08-16 14:59:28 -0700 | [diff] [blame] | 328 | // Returns the universal value unaltered. |
| 329 | std::string AidlConstantValueDecorator(const AidlTypeSpecifier& type, const std::string& raw_value); |
| 330 | |
Steven Moreland | 9ea10e3 | 2018-07-19 15:26:09 -0700 | [diff] [blame] | 331 | class AidlConstantValue; |
Steven Moreland | 5557f1c | 2018-07-02 13:50:23 -0700 | [diff] [blame] | 332 | class AidlVariableDeclaration : public AidlNode { |
| 333 | public: |
Steven Moreland | 46e9da8 | 2018-07-27 15:45:29 -0700 | [diff] [blame] | 334 | AidlVariableDeclaration(const AidlLocation& location, AidlTypeSpecifier* type, |
| 335 | const std::string& name); |
| 336 | AidlVariableDeclaration(const AidlLocation& location, AidlTypeSpecifier* type, |
| 337 | const std::string& name, AidlConstantValue* default_value); |
Steven Moreland | 5557f1c | 2018-07-02 13:50:23 -0700 | [diff] [blame] | 338 | virtual ~AidlVariableDeclaration() = default; |
| 339 | |
| 340 | std::string GetName() const { return name_; } |
Jiyong Park | d59a10d | 2018-07-18 11:12:55 +0900 | [diff] [blame] | 341 | const AidlTypeSpecifier& GetType() const { return *type_; } |
Steven Moreland | 9ea10e3 | 2018-07-19 15:26:09 -0700 | [diff] [blame] | 342 | const AidlConstantValue* GetDefaultValue() const { return default_value_.get(); } |
| 343 | |
Jiyong Park | d59a10d | 2018-07-18 11:12:55 +0900 | [diff] [blame] | 344 | AidlTypeSpecifier* GetMutableType() { return type_.get(); } |
Steven Moreland | 5557f1c | 2018-07-02 13:50:23 -0700 | [diff] [blame] | 345 | |
Jeongik Cha | db0f59e | 2018-11-01 18:11:21 +0900 | [diff] [blame] | 346 | bool CheckValid(const AidlTypenames& typenames) const; |
Steven Moreland | 5557f1c | 2018-07-02 13:50:23 -0700 | [diff] [blame] | 347 | std::string ToString() const; |
Jiyong Park | 02da742 | 2018-07-16 16:00:26 +0900 | [diff] [blame] | 348 | std::string Signature() const; |
Steven Moreland | 5557f1c | 2018-07-02 13:50:23 -0700 | [diff] [blame] | 349 | |
Steven Moreland | 860b194 | 2018-08-16 14:59:28 -0700 | [diff] [blame] | 350 | std::string ValueString(const ConstantValueDecorator& decorator) const; |
Steven Moreland | 2529432 | 2018-08-07 18:13:55 -0700 | [diff] [blame] | 351 | |
Steven Moreland | 5557f1c | 2018-07-02 13:50:23 -0700 | [diff] [blame] | 352 | private: |
Jiyong Park | d59a10d | 2018-07-18 11:12:55 +0900 | [diff] [blame] | 353 | std::unique_ptr<AidlTypeSpecifier> type_; |
Steven Moreland | 5557f1c | 2018-07-02 13:50:23 -0700 | [diff] [blame] | 354 | std::string name_; |
Steven Moreland | 9ea10e3 | 2018-07-19 15:26:09 -0700 | [diff] [blame] | 355 | std::unique_ptr<AidlConstantValue> default_value_; |
Steven Moreland | 5557f1c | 2018-07-02 13:50:23 -0700 | [diff] [blame] | 356 | |
| 357 | DISALLOW_COPY_AND_ASSIGN(AidlVariableDeclaration); |
| 358 | }; |
| 359 | |
| 360 | class AidlArgument : public AidlVariableDeclaration { |
Casey Dahlin | bc7a50a | 2015-09-28 19:20:50 -0700 | [diff] [blame] | 361 | public: |
Casey Dahlin | c378c99 | 2015-09-29 16:50:40 -0700 | [diff] [blame] | 362 | enum Direction { IN_DIR = 1, OUT_DIR = 2, INOUT_DIR = 3 }; |
| 363 | |
Steven Moreland | 46e9da8 | 2018-07-27 15:45:29 -0700 | [diff] [blame] | 364 | AidlArgument(const AidlLocation& location, AidlArgument::Direction direction, |
| 365 | AidlTypeSpecifier* type, const std::string& name); |
| 366 | AidlArgument(const AidlLocation& location, AidlTypeSpecifier* type, const std::string& name); |
Casey Dahlin | bc7a50a | 2015-09-28 19:20:50 -0700 | [diff] [blame] | 367 | virtual ~AidlArgument() = default; |
| 368 | |
Casey Dahlin | c378c99 | 2015-09-29 16:50:40 -0700 | [diff] [blame] | 369 | Direction GetDirection() const { return direction_; } |
Christopher Wiley | ad33927 | 2015-10-05 19:11:58 -0700 | [diff] [blame] | 370 | bool IsOut() const { return direction_ & OUT_DIR; } |
| 371 | bool IsIn() const { return direction_ & IN_DIR; } |
Casey Dahlin | c378c99 | 2015-09-29 16:50:40 -0700 | [diff] [blame] | 372 | bool DirectionWasSpecified() const { return direction_specified_; } |
Jiyong Park | 3656c3c | 2018-08-01 20:02:01 +0900 | [diff] [blame] | 373 | string GetDirectionSpecifier() const; |
Christopher Wiley | ad33927 | 2015-10-05 19:11:58 -0700 | [diff] [blame] | 374 | |
Casey Dahlin | c378c99 | 2015-09-29 16:50:40 -0700 | [diff] [blame] | 375 | std::string ToString() const; |
Jiyong Park | 02da742 | 2018-07-16 16:00:26 +0900 | [diff] [blame] | 376 | std::string Signature() const; |
Casey Dahlin | c378c99 | 2015-09-29 16:50:40 -0700 | [diff] [blame] | 377 | |
Casey Dahlin | bc7a50a | 2015-09-28 19:20:50 -0700 | [diff] [blame] | 378 | private: |
Casey Dahlin | c378c99 | 2015-09-29 16:50:40 -0700 | [diff] [blame] | 379 | Direction direction_; |
| 380 | bool direction_specified_; |
| 381 | |
Casey Dahlin | bc7a50a | 2015-09-28 19:20:50 -0700 | [diff] [blame] | 382 | DISALLOW_COPY_AND_ASSIGN(AidlArgument); |
Casey Dahlin | a834dd4 | 2015-09-23 11:52:15 -0700 | [diff] [blame] | 383 | }; |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 384 | |
Casey Dahlin | d40e2fe | 2015-11-24 14:06:52 -0800 | [diff] [blame] | 385 | class AidlMethod; |
Steven Moreland | 693640b | 2018-07-19 13:46:27 -0700 | [diff] [blame] | 386 | class AidlConstantDeclaration; |
Daniel Norman | 85aed54 | 2019-08-21 12:01:14 -0700 | [diff] [blame] | 387 | class AidlEnumDeclaration; |
Casey Dahlin | d40e2fe | 2015-11-24 14:06:52 -0800 | [diff] [blame] | 388 | class AidlMember : public AidlNode { |
| 389 | public: |
Steven Moreland | 46e9da8 | 2018-07-27 15:45:29 -0700 | [diff] [blame] | 390 | AidlMember(const AidlLocation& location); |
Casey Dahlin | d40e2fe | 2015-11-24 14:06:52 -0800 | [diff] [blame] | 391 | virtual ~AidlMember() = default; |
| 392 | |
| 393 | virtual AidlMethod* AsMethod() { return nullptr; } |
Steven Moreland | 693640b | 2018-07-19 13:46:27 -0700 | [diff] [blame] | 394 | virtual AidlConstantDeclaration* AsConstantDeclaration() { return nullptr; } |
Casey Dahlin | d40e2fe | 2015-11-24 14:06:52 -0800 | [diff] [blame] | 395 | |
| 396 | private: |
| 397 | DISALLOW_COPY_AND_ASSIGN(AidlMember); |
| 398 | }; |
| 399 | |
Will McVicker | d7d18df | 2019-09-12 13:40:50 -0700 | [diff] [blame] | 400 | class AidlUnaryConstExpression; |
| 401 | class AidlBinaryConstExpression; |
| 402 | |
Steven Moreland | 693640b | 2018-07-19 13:46:27 -0700 | [diff] [blame] | 403 | class AidlConstantValue : public AidlNode { |
Casey Dahlin | d40e2fe | 2015-11-24 14:06:52 -0800 | [diff] [blame] | 404 | public: |
Will McVicker | d7d18df | 2019-09-12 13:40:50 -0700 | [diff] [blame] | 405 | enum class Type { |
| 406 | // WARNING: Don't change this order! The order is used to determine type |
| 407 | // promotion during a binary expression. |
| 408 | BOOLEAN, |
| 409 | INT8, |
| 410 | INT32, |
| 411 | INT64, |
| 412 | ARRAY, |
| 413 | CHARACTER, |
| 414 | STRING, |
| 415 | FLOATING, |
| 416 | UNARY, |
| 417 | BINARY, |
| 418 | ERROR, |
| 419 | }; |
| 420 | |
| 421 | /* |
| 422 | * Return the value casted to the given type. |
| 423 | */ |
| 424 | template <typename T> |
| 425 | T cast() const; |
Casey Dahlin | d40e2fe | 2015-11-24 14:06:52 -0800 | [diff] [blame] | 426 | |
Steven Moreland | 693640b | 2018-07-19 13:46:27 -0700 | [diff] [blame] | 427 | virtual ~AidlConstantValue() = default; |
Casey Dahlin | d40e2fe | 2015-11-24 14:06:52 -0800 | [diff] [blame] | 428 | |
Steven Moreland | 2529432 | 2018-08-07 18:13:55 -0700 | [diff] [blame] | 429 | static AidlConstantValue* Boolean(const AidlLocation& location, bool value); |
| 430 | static AidlConstantValue* Character(const AidlLocation& location, char value); |
Steven Moreland | 2529432 | 2018-08-07 18:13:55 -0700 | [diff] [blame] | 431 | // example: 123, -5498, maybe any size |
Will McVicker | d7d18df | 2019-09-12 13:40:50 -0700 | [diff] [blame] | 432 | static AidlConstantValue* Integral(const AidlLocation& location, const string& value); |
| 433 | static AidlConstantValue* Floating(const AidlLocation& location, const std::string& value); |
Steven Moreland | 860b194 | 2018-08-16 14:59:28 -0700 | [diff] [blame] | 434 | static AidlConstantValue* Array(const AidlLocation& location, |
Will McVicker | d7d18df | 2019-09-12 13:40:50 -0700 | [diff] [blame] | 435 | std::unique_ptr<vector<unique_ptr<AidlConstantValue>>> values); |
Steven Moreland | 693640b | 2018-07-19 13:46:27 -0700 | [diff] [blame] | 436 | // example: "\"asdf\"" |
Will McVicker | d7d18df | 2019-09-12 13:40:50 -0700 | [diff] [blame] | 437 | static AidlConstantValue* String(const AidlLocation& location, const string& value); |
Steven Moreland | 693640b | 2018-07-19 13:46:27 -0700 | [diff] [blame] | 438 | |
Daniel Norman | f0ca44f | 2019-10-25 09:59:44 -0700 | [diff] [blame] | 439 | // Construct an AidlConstantValue by evaluating the other integral constant's |
| 440 | // value string. This does not preserve the structure of the copied constant. |
Steven Moreland | 59e53e4 | 2019-11-26 20:38:08 -0800 | [diff] [blame] | 441 | // Returns nullptr and logs if value cannot be copied. |
Daniel Norman | f0ca44f | 2019-10-25 09:59:44 -0700 | [diff] [blame] | 442 | static AidlConstantValue* ShallowIntegralCopy(const AidlConstantValue& other); |
Daniel Norman | b28684e | 2019-10-17 15:31:39 -0700 | [diff] [blame] | 443 | |
Will McVicker | d7d18df | 2019-09-12 13:40:50 -0700 | [diff] [blame] | 444 | Type GetType() const { return final_type_; } |
Steven Moreland | 2529432 | 2018-08-07 18:13:55 -0700 | [diff] [blame] | 445 | |
Will McVicker | d7d18df | 2019-09-12 13:40:50 -0700 | [diff] [blame] | 446 | virtual bool CheckValid() const; |
Steven Moreland | 860b194 | 2018-08-16 14:59:28 -0700 | [diff] [blame] | 447 | |
| 448 | // 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] | 449 | string ValueString(const AidlTypeSpecifier& type, const ConstantValueDecorator& decorator) const; |
Casey Dahlin | d40e2fe | 2015-11-24 14:06:52 -0800 | [diff] [blame] | 450 | |
| 451 | private: |
Will McVicker | d7d18df | 2019-09-12 13:40:50 -0700 | [diff] [blame] | 452 | AidlConstantValue(const AidlLocation& location, Type parsed_type, int64_t parsed_value, |
| 453 | const string& checked_value); |
| 454 | AidlConstantValue(const AidlLocation& location, Type type, const string& checked_value); |
Steven Moreland | 860b194 | 2018-08-16 14:59:28 -0700 | [diff] [blame] | 455 | AidlConstantValue(const AidlLocation& location, Type type, |
Will McVicker | d7d18df | 2019-09-12 13:40:50 -0700 | [diff] [blame] | 456 | std::unique_ptr<vector<unique_ptr<AidlConstantValue>>> values); |
Steven Moreland | 2529432 | 2018-08-07 18:13:55 -0700 | [diff] [blame] | 457 | static string ToString(Type type); |
Will McVicker | d7d18df | 2019-09-12 13:40:50 -0700 | [diff] [blame] | 458 | static bool ParseIntegral(const string& value, int64_t* parsed_value, Type* parsed_type); |
| 459 | static bool IsHex(const string& value); |
Steven Moreland | 4bcb05c | 2019-11-27 18:57:47 -0800 | [diff] [blame] | 460 | |
Will McVicker | d7d18df | 2019-09-12 13:40:50 -0700 | [diff] [blame] | 461 | virtual bool evaluate(const AidlTypeSpecifier& type) const; |
Casey Dahlin | d40e2fe | 2015-11-24 14:06:52 -0800 | [diff] [blame] | 462 | |
Steven Moreland | 693640b | 2018-07-19 13:46:27 -0700 | [diff] [blame] | 463 | const Type type_ = Type::ERROR; |
Will McVicker | d7d18df | 2019-09-12 13:40:50 -0700 | [diff] [blame] | 464 | const vector<unique_ptr<AidlConstantValue>> values_; // if type_ == ARRAY |
| 465 | const string value_; // otherwise |
| 466 | |
| 467 | // State for tracking evaluation of expressions |
Steven Moreland | 4bcb05c | 2019-11-27 18:57:47 -0800 | [diff] [blame] | 468 | mutable bool is_valid_ = false; // cache of CheckValid, but may be marked false in evaluate |
| 469 | mutable bool is_evaluated_ = false; // whether evaluate has been called |
Will McVicker | d7d18df | 2019-09-12 13:40:50 -0700 | [diff] [blame] | 470 | mutable Type final_type_; |
| 471 | mutable int64_t final_value_; |
| 472 | mutable string final_string_value_ = ""; |
Steven Moreland | 693640b | 2018-07-19 13:46:27 -0700 | [diff] [blame] | 473 | |
| 474 | DISALLOW_COPY_AND_ASSIGN(AidlConstantValue); |
Will McVicker | d7d18df | 2019-09-12 13:40:50 -0700 | [diff] [blame] | 475 | |
| 476 | friend AidlUnaryConstExpression; |
| 477 | friend AidlBinaryConstExpression; |
| 478 | }; |
| 479 | |
| 480 | class AidlUnaryConstExpression : public AidlConstantValue { |
| 481 | public: |
| 482 | AidlUnaryConstExpression(const AidlLocation& location, const string& op, |
| 483 | std::unique_ptr<AidlConstantValue> rval); |
| 484 | |
| 485 | static bool IsCompatibleType(Type type, const string& op); |
| 486 | bool CheckValid() const override; |
Will McVicker | d7d18df | 2019-09-12 13:40:50 -0700 | [diff] [blame] | 487 | private: |
| 488 | bool evaluate(const AidlTypeSpecifier& type) const override; |
| 489 | |
| 490 | std::unique_ptr<AidlConstantValue> unary_; |
| 491 | const string op_; |
| 492 | }; |
| 493 | |
| 494 | class AidlBinaryConstExpression : public AidlConstantValue { |
| 495 | public: |
| 496 | AidlBinaryConstExpression(const AidlLocation& location, std::unique_ptr<AidlConstantValue> lval, |
| 497 | const string& op, std::unique_ptr<AidlConstantValue> rval); |
| 498 | |
| 499 | bool CheckValid() const override; |
Will McVicker | d7d18df | 2019-09-12 13:40:50 -0700 | [diff] [blame] | 500 | |
| 501 | static bool AreCompatibleTypes(Type t1, Type t2); |
| 502 | // Returns the promoted kind for both operands |
| 503 | static Type UsualArithmeticConversion(Type left, Type right); |
| 504 | // Returns the promoted integral type where INT32 is the smallest type |
| 505 | static Type IntegralPromotion(Type in); |
| 506 | |
| 507 | private: |
| 508 | bool evaluate(const AidlTypeSpecifier& type) const override; |
| 509 | |
| 510 | std::unique_ptr<AidlConstantValue> left_val_; |
| 511 | std::unique_ptr<AidlConstantValue> right_val_; |
| 512 | const string op_; |
Christopher Wiley | d6bdd8d | 2016-05-03 11:23:13 -0700 | [diff] [blame] | 513 | }; |
| 514 | |
Andrei Onea | 9445fc6 | 2019-06-27 18:11:59 +0100 | [diff] [blame] | 515 | struct AidlAnnotationParameter { |
| 516 | std::string name; |
| 517 | std::unique_ptr<AidlConstantValue> value; |
| 518 | }; |
| 519 | |
Steven Moreland | 693640b | 2018-07-19 13:46:27 -0700 | [diff] [blame] | 520 | class AidlConstantDeclaration : public AidlMember { |
Christopher Wiley | d6bdd8d | 2016-05-03 11:23:13 -0700 | [diff] [blame] | 521 | public: |
Steven Moreland | 46e9da8 | 2018-07-27 15:45:29 -0700 | [diff] [blame] | 522 | AidlConstantDeclaration(const AidlLocation& location, AidlTypeSpecifier* specifier, |
Will McVicker | d7d18df | 2019-09-12 13:40:50 -0700 | [diff] [blame] | 523 | const string& name, AidlConstantValue* value); |
Steven Moreland | 693640b | 2018-07-19 13:46:27 -0700 | [diff] [blame] | 524 | virtual ~AidlConstantDeclaration() = default; |
Christopher Wiley | d6bdd8d | 2016-05-03 11:23:13 -0700 | [diff] [blame] | 525 | |
Steven Moreland | 693640b | 2018-07-19 13:46:27 -0700 | [diff] [blame] | 526 | const AidlTypeSpecifier& GetType() const { return *type_; } |
Steven Moreland | 4d12f9a | 2018-10-31 14:30:55 -0700 | [diff] [blame] | 527 | AidlTypeSpecifier* GetMutableType() { return type_.get(); } |
Will McVicker | d7d18df | 2019-09-12 13:40:50 -0700 | [diff] [blame] | 528 | const string& GetName() const { return name_; } |
Steven Moreland | 693640b | 2018-07-19 13:46:27 -0700 | [diff] [blame] | 529 | const AidlConstantValue& GetValue() const { return *value_; } |
Jeongik Cha | db0f59e | 2018-11-01 18:11:21 +0900 | [diff] [blame] | 530 | bool CheckValid(const AidlTypenames& typenames) const; |
Christopher Wiley | d6bdd8d | 2016-05-03 11:23:13 -0700 | [diff] [blame] | 531 | |
Will McVicker | d7d18df | 2019-09-12 13:40:50 -0700 | [diff] [blame] | 532 | string ToString() const; |
| 533 | string Signature() const; |
Steven Moreland | 860b194 | 2018-08-16 14:59:28 -0700 | [diff] [blame] | 534 | string ValueString(const ConstantValueDecorator& decorator) const { |
Will McVicker | d7d18df | 2019-09-12 13:40:50 -0700 | [diff] [blame] | 535 | return value_->ValueString(GetType(), decorator); |
Steven Moreland | 860b194 | 2018-08-16 14:59:28 -0700 | [diff] [blame] | 536 | } |
Steven Moreland | 2529432 | 2018-08-07 18:13:55 -0700 | [diff] [blame] | 537 | |
Steven Moreland | 693640b | 2018-07-19 13:46:27 -0700 | [diff] [blame] | 538 | AidlConstantDeclaration* AsConstantDeclaration() override { return this; } |
Christopher Wiley | d6bdd8d | 2016-05-03 11:23:13 -0700 | [diff] [blame] | 539 | |
| 540 | private: |
Steven Moreland | 693640b | 2018-07-19 13:46:27 -0700 | [diff] [blame] | 541 | const unique_ptr<AidlTypeSpecifier> type_; |
Will McVicker | d7d18df | 2019-09-12 13:40:50 -0700 | [diff] [blame] | 542 | const string name_; |
| 543 | unique_ptr<AidlConstantValue> value_; |
Christopher Wiley | d6bdd8d | 2016-05-03 11:23:13 -0700 | [diff] [blame] | 544 | |
Steven Moreland | 693640b | 2018-07-19 13:46:27 -0700 | [diff] [blame] | 545 | DISALLOW_COPY_AND_ASSIGN(AidlConstantDeclaration); |
Casey Dahlin | d40e2fe | 2015-11-24 14:06:52 -0800 | [diff] [blame] | 546 | }; |
| 547 | |
| 548 | class AidlMethod : public AidlMember { |
Casey Dahlin | 5c69deb | 2015-10-01 14:44:12 -0700 | [diff] [blame] | 549 | public: |
Will McVicker | d7d18df | 2019-09-12 13:40:50 -0700 | [diff] [blame] | 550 | AidlMethod(const AidlLocation& location, bool oneway, AidlTypeSpecifier* type, const string& name, |
| 551 | vector<unique_ptr<AidlArgument>>* args, const string& comments); |
| 552 | AidlMethod(const AidlLocation& location, bool oneway, AidlTypeSpecifier* type, const string& name, |
| 553 | vector<unique_ptr<AidlArgument>>* args, const string& comments, int id, |
| 554 | bool is_user_defined = true); |
Casey Dahlin | 5c69deb | 2015-10-01 14:44:12 -0700 | [diff] [blame] | 555 | virtual ~AidlMethod() = default; |
| 556 | |
Casey Dahlin | d40e2fe | 2015-11-24 14:06:52 -0800 | [diff] [blame] | 557 | AidlMethod* AsMethod() override { return this; } |
Jeongik Cha | 997281d | 2020-01-16 15:23:59 +0900 | [diff] [blame] | 558 | bool IsHidden() const; |
Will McVicker | d7d18df | 2019-09-12 13:40:50 -0700 | [diff] [blame] | 559 | const string& GetComments() const { return comments_; } |
Jiyong Park | d59a10d | 2018-07-18 11:12:55 +0900 | [diff] [blame] | 560 | const AidlTypeSpecifier& GetType() const { return *type_; } |
| 561 | AidlTypeSpecifier* GetMutableType() { return type_.get(); } |
Steven Moreland | acd5347 | 2018-12-14 10:17:26 -0800 | [diff] [blame] | 562 | |
Steven Moreland | 8c70ba9 | 2018-12-17 10:20:31 -0800 | [diff] [blame] | 563 | // set if this method is part of an interface that is marked oneway |
| 564 | void ApplyInterfaceOneway(bool oneway) { oneway_ = oneway_ || oneway; } |
Casey Dahlin | f4a9311 | 2015-10-05 16:58:09 -0700 | [diff] [blame] | 565 | bool IsOneway() const { return oneway_; } |
Steven Moreland | acd5347 | 2018-12-14 10:17:26 -0800 | [diff] [blame] | 566 | |
Casey Dahlin | f4a9311 | 2015-10-05 16:58:09 -0700 | [diff] [blame] | 567 | const std::string& GetName() const { return name_; } |
Casey Dahlin | f4a9311 | 2015-10-05 16:58:09 -0700 | [diff] [blame] | 568 | bool HasId() const { return has_id_; } |
Jiyong Park | ed65bf4 | 2018-08-28 15:43:27 +0900 | [diff] [blame] | 569 | int GetId() const { return id_; } |
Casey Dahlin | f4a9311 | 2015-10-05 16:58:09 -0700 | [diff] [blame] | 570 | void SetId(unsigned id) { id_ = id; } |
Casey Dahlin | f2d23f7 | 2015-10-02 16:19:19 -0700 | [diff] [blame] | 571 | |
Jiyong Park | 309668e | 2018-07-28 16:55:44 +0900 | [diff] [blame] | 572 | bool IsUserDefined() const { return is_user_defined_; } |
| 573 | |
Casey Dahlin | f4a9311 | 2015-10-05 16:58:09 -0700 | [diff] [blame] | 574 | const std::vector<std::unique_ptr<AidlArgument>>& GetArguments() const { |
Christopher Wiley | ad33927 | 2015-10-05 19:11:58 -0700 | [diff] [blame] | 575 | return arguments_; |
| 576 | } |
| 577 | // An inout parameter will appear in both GetInArguments() |
| 578 | // and GetOutArguments(). AidlMethod retains ownership of the argument |
| 579 | // pointers returned in this way. |
| 580 | const std::vector<const AidlArgument*>& GetInArguments() const { |
| 581 | return in_arguments_; |
| 582 | } |
| 583 | const std::vector<const AidlArgument*>& GetOutArguments() const { |
| 584 | return out_arguments_; |
Casey Dahlin | f4a9311 | 2015-10-05 16:58:09 -0700 | [diff] [blame] | 585 | } |
Casey Dahlin | 5c69deb | 2015-10-01 14:44:12 -0700 | [diff] [blame] | 586 | |
Jiyong Park | 309668e | 2018-07-28 16:55:44 +0900 | [diff] [blame] | 587 | // name + type parameter types |
| 588 | // i.e, foo(int, String) |
Jiyong Park | 02da742 | 2018-07-16 16:00:26 +0900 | [diff] [blame] | 589 | std::string Signature() const; |
| 590 | |
Jiyong Park | 309668e | 2018-07-28 16:55:44 +0900 | [diff] [blame] | 591 | // return type + name + type parameter types + annotations |
| 592 | // i.e, boolean foo(int, @Nullable String) |
| 593 | std::string ToString() const; |
| 594 | |
Casey Dahlin | 5c69deb | 2015-10-01 14:44:12 -0700 | [diff] [blame] | 595 | private: |
Casey Dahlin | f4a9311 | 2015-10-05 16:58:09 -0700 | [diff] [blame] | 596 | bool oneway_; |
Casey Dahlin | f2d23f7 | 2015-10-02 16:19:19 -0700 | [diff] [blame] | 597 | std::string comments_; |
Jiyong Park | d59a10d | 2018-07-18 11:12:55 +0900 | [diff] [blame] | 598 | std::unique_ptr<AidlTypeSpecifier> type_; |
Casey Dahlin | f4a9311 | 2015-10-05 16:58:09 -0700 | [diff] [blame] | 599 | std::string name_; |
Christopher Wiley | ad33927 | 2015-10-05 19:11:58 -0700 | [diff] [blame] | 600 | const std::vector<std::unique_ptr<AidlArgument>> arguments_; |
| 601 | std::vector<const AidlArgument*> in_arguments_; |
| 602 | std::vector<const AidlArgument*> out_arguments_; |
Casey Dahlin | f4a9311 | 2015-10-05 16:58:09 -0700 | [diff] [blame] | 603 | bool has_id_; |
| 604 | int id_; |
Jiyong Park | 309668e | 2018-07-28 16:55:44 +0900 | [diff] [blame] | 605 | bool is_user_defined_ = true; |
Casey Dahlin | f2d23f7 | 2015-10-02 16:19:19 -0700 | [diff] [blame] | 606 | |
Casey Dahlin | 5c69deb | 2015-10-01 14:44:12 -0700 | [diff] [blame] | 607 | DISALLOW_COPY_AND_ASSIGN(AidlMethod); |
Casey Dahlin | 0a2f8be | 2015-09-28 16:15:29 -0700 | [diff] [blame] | 608 | }; |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 609 | |
Steven Moreland | c258abc | 2018-07-10 14:03:38 -0700 | [diff] [blame] | 610 | class AidlDefinedType; |
Jiyong Park | b034bf0 | 2018-07-30 17:44:33 +0900 | [diff] [blame] | 611 | class AidlInterface; |
| 612 | class AidlParcelable; |
| 613 | class AidlStructuredParcelable; |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 614 | |
Casey Dahlin | 2b2879b | 2015-10-13 16:59:44 -0700 | [diff] [blame] | 615 | class AidlQualifiedName : public AidlNode { |
| 616 | public: |
Steven Moreland | 46e9da8 | 2018-07-27 15:45:29 -0700 | [diff] [blame] | 617 | AidlQualifiedName(const AidlLocation& location, const std::string& term, |
| 618 | const std::string& comments); |
Casey Dahlin | 2b2879b | 2015-10-13 16:59:44 -0700 | [diff] [blame] | 619 | virtual ~AidlQualifiedName() = default; |
| 620 | |
| 621 | const std::vector<std::string>& GetTerms() const { return terms_; } |
| 622 | const std::string& GetComments() const { return comments_; } |
| 623 | std::string GetDotName() const { return android::base::Join(terms_, '.'); } |
Ningyuan Wang | d17c58b | 2016-09-29 14:33:14 -0700 | [diff] [blame] | 624 | std::string GetColonName() const { return android::base::Join(terms_, "::"); } |
Casey Dahlin | 2b2879b | 2015-10-13 16:59:44 -0700 | [diff] [blame] | 625 | |
Chih-Hung Hsieh | f05cc26 | 2016-07-27 11:42:51 -0700 | [diff] [blame] | 626 | void AddTerm(const std::string& term); |
Casey Dahlin | 2b2879b | 2015-10-13 16:59:44 -0700 | [diff] [blame] | 627 | |
| 628 | private: |
| 629 | std::vector<std::string> terms_; |
| 630 | std::string comments_; |
| 631 | |
| 632 | DISALLOW_COPY_AND_ASSIGN(AidlQualifiedName); |
| 633 | }; |
| 634 | |
Steven Moreland | 46e9da8 | 2018-07-27 15:45:29 -0700 | [diff] [blame] | 635 | class AidlInterface; |
| 636 | class AidlParcelable; |
| 637 | class AidlStructuredParcelable; |
Daniel Norman | 85aed54 | 2019-08-21 12:01:14 -0700 | [diff] [blame] | 638 | // AidlDefinedType represents either an interface, parcelable, or enum that is |
Jiyong Park | 1deecc3 | 2018-07-17 01:14:41 +0900 | [diff] [blame] | 639 | // defined in the source file. |
| 640 | class AidlDefinedType : public AidlAnnotatable { |
Steven Moreland | 787b043 | 2018-07-03 09:00:58 -0700 | [diff] [blame] | 641 | public: |
Steven Moreland | 46e9da8 | 2018-07-27 15:45:29 -0700 | [diff] [blame] | 642 | AidlDefinedType(const AidlLocation& location, const std::string& name, |
| 643 | const std::string& comments, const std::vector<std::string>& package); |
Steven Moreland | 787b043 | 2018-07-03 09:00:58 -0700 | [diff] [blame] | 644 | virtual ~AidlDefinedType() = default; |
| 645 | |
Jiyong Park | 1deecc3 | 2018-07-17 01:14:41 +0900 | [diff] [blame] | 646 | const std::string& GetName() const { return name_; }; |
Jeongik Cha | 997281d | 2020-01-16 15:23:59 +0900 | [diff] [blame] | 647 | bool IsHidden() const; |
Jiyong Park | 1deecc3 | 2018-07-17 01:14:41 +0900 | [diff] [blame] | 648 | const std::string& GetComments() const { return comments_; } |
Jiyong Park | a6605ab | 2018-11-11 14:30:21 +0900 | [diff] [blame] | 649 | void SetComments(const std::string comments) { comments_ = comments; } |
Jiyong Park | 1deecc3 | 2018-07-17 01:14:41 +0900 | [diff] [blame] | 650 | |
Steven Moreland | 787b043 | 2018-07-03 09:00:58 -0700 | [diff] [blame] | 651 | /* dot joined package, example: "android.package.foo" */ |
| 652 | std::string GetPackage() const; |
| 653 | /* dot joined package and name, example: "android.package.foo.IBar" */ |
| 654 | std::string GetCanonicalName() const; |
| 655 | const std::vector<std::string>& GetSplitPackage() const { return package_; } |
| 656 | |
Steven Moreland | ed83a28 | 2018-07-17 13:27:29 -0700 | [diff] [blame] | 657 | virtual std::string GetPreprocessDeclarationName() const = 0; |
Steven Moreland | c258abc | 2018-07-10 14:03:38 -0700 | [diff] [blame] | 658 | |
Steven Moreland | 5557f1c | 2018-07-02 13:50:23 -0700 | [diff] [blame] | 659 | virtual const AidlStructuredParcelable* AsStructuredParcelable() const { return nullptr; } |
Steven Moreland | c258abc | 2018-07-10 14:03:38 -0700 | [diff] [blame] | 660 | virtual const AidlParcelable* AsParcelable() const { return nullptr; } |
Daniel Norman | 85aed54 | 2019-08-21 12:01:14 -0700 | [diff] [blame] | 661 | virtual const AidlEnumDeclaration* AsEnumDeclaration() const { return nullptr; } |
Steven Moreland | 5557f1c | 2018-07-02 13:50:23 -0700 | [diff] [blame] | 662 | virtual const AidlInterface* AsInterface() const { return nullptr; } |
Jeongik Cha | df76dc7 | 2019-11-28 00:08:47 +0900 | [diff] [blame] | 663 | virtual const AidlParameterizable<std::string>* AsParameterizable() const { return nullptr; } |
Devin Moore | 24f6857 | 2020-02-26 13:20:59 -0800 | [diff] [blame] | 664 | bool CheckValid(const AidlTypenames& typenames) const override; |
Jeongik Cha | 047c5ee | 2019-08-07 23:16:49 +0900 | [diff] [blame] | 665 | virtual bool LanguageSpecificCheckValid(Options::Language lang) const = 0; |
Steven Moreland | c258abc | 2018-07-10 14:03:38 -0700 | [diff] [blame] | 666 | AidlStructuredParcelable* AsStructuredParcelable() { |
| 667 | return const_cast<AidlStructuredParcelable*>( |
| 668 | const_cast<const AidlDefinedType*>(this)->AsStructuredParcelable()); |
| 669 | } |
| 670 | AidlParcelable* AsParcelable() { |
| 671 | return const_cast<AidlParcelable*>(const_cast<const AidlDefinedType*>(this)->AsParcelable()); |
| 672 | } |
Daniel Norman | 85aed54 | 2019-08-21 12:01:14 -0700 | [diff] [blame] | 673 | AidlEnumDeclaration* AsEnumDeclaration() { |
| 674 | return const_cast<AidlEnumDeclaration*>( |
| 675 | const_cast<const AidlDefinedType*>(this)->AsEnumDeclaration()); |
| 676 | } |
Steven Moreland | c258abc | 2018-07-10 14:03:38 -0700 | [diff] [blame] | 677 | AidlInterface* AsInterface() { |
| 678 | return const_cast<AidlInterface*>(const_cast<const AidlDefinedType*>(this)->AsInterface()); |
| 679 | } |
| 680 | |
Jeongik Cha | df76dc7 | 2019-11-28 00:08:47 +0900 | [diff] [blame] | 681 | AidlParameterizable<std::string>* AsParameterizable() { |
| 682 | return const_cast<AidlParameterizable<std::string>*>( |
| 683 | const_cast<const AidlDefinedType*>(this)->AsParameterizable()); |
| 684 | } |
| 685 | |
Steven Moreland | 6cee348 | 2018-07-18 14:39:58 -0700 | [diff] [blame] | 686 | const AidlParcelable* AsUnstructuredParcelable() const { |
| 687 | if (this->AsStructuredParcelable() != nullptr) return nullptr; |
| 688 | return this->AsParcelable(); |
| 689 | } |
| 690 | AidlParcelable* AsUnstructuredParcelable() { |
| 691 | return const_cast<AidlParcelable*>( |
| 692 | const_cast<const AidlDefinedType*>(this)->AsUnstructuredParcelable()); |
| 693 | } |
| 694 | |
Jeongik Cha | 997281d | 2020-01-16 15:23:59 +0900 | [diff] [blame] | 695 | virtual void Dump(CodeWriter* writer) const = 0; |
Steven Moreland | a5d9c5c | 2020-02-21 16:01:09 -0800 | [diff] [blame] | 696 | void DumpHeader(CodeWriter* writer) const; |
Jiyong Park | 02da742 | 2018-07-16 16:00:26 +0900 | [diff] [blame] | 697 | |
Steven Moreland | 787b043 | 2018-07-03 09:00:58 -0700 | [diff] [blame] | 698 | private: |
Jiyong Park | 1deecc3 | 2018-07-17 01:14:41 +0900 | [diff] [blame] | 699 | std::string name_; |
Jiyong Park | 1deecc3 | 2018-07-17 01:14:41 +0900 | [diff] [blame] | 700 | std::string comments_; |
Steven Moreland | 787b043 | 2018-07-03 09:00:58 -0700 | [diff] [blame] | 701 | const std::vector<std::string> package_; |
Steven Moreland | 5557f1c | 2018-07-02 13:50:23 -0700 | [diff] [blame] | 702 | |
| 703 | DISALLOW_COPY_AND_ASSIGN(AidlDefinedType); |
Steven Moreland | 787b043 | 2018-07-03 09:00:58 -0700 | [diff] [blame] | 704 | }; |
| 705 | |
Jeongik Cha | df76dc7 | 2019-11-28 00:08:47 +0900 | [diff] [blame] | 706 | class AidlParcelable : public AidlDefinedType, public AidlParameterizable<std::string> { |
Casey Dahlin | 1ae2bc5 | 2015-10-07 18:49:10 -0700 | [diff] [blame] | 707 | public: |
Steven Moreland | 46e9da8 | 2018-07-27 15:45:29 -0700 | [diff] [blame] | 708 | AidlParcelable(const AidlLocation& location, AidlQualifiedName* name, |
Jiyong Park | a6605ab | 2018-11-11 14:30:21 +0900 | [diff] [blame] | 709 | const std::vector<std::string>& package, const std::string& comments, |
Jeongik Cha | df76dc7 | 2019-11-28 00:08:47 +0900 | [diff] [blame] | 710 | const std::string& cpp_header = "", |
| 711 | std::vector<std::string>* type_params = nullptr); |
Casey Dahlin | 1ae2bc5 | 2015-10-07 18:49:10 -0700 | [diff] [blame] | 712 | virtual ~AidlParcelable() = default; |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 713 | |
Ningyuan Wang | d17c58b | 2016-09-29 14:33:14 -0700 | [diff] [blame] | 714 | // C++ uses "::" instead of "." to refer to a inner class. |
| 715 | std::string GetCppName() const { return name_->GetColonName(); } |
Christopher Wiley | 8aa4d9f | 2015-11-16 19:10:45 -0800 | [diff] [blame] | 716 | std::string GetCppHeader() const { return cpp_header_; } |
Christopher Wiley | 8aa4d9f | 2015-11-16 19:10:45 -0800 | [diff] [blame] | 717 | |
Devin Moore | 24f6857 | 2020-02-26 13:20:59 -0800 | [diff] [blame] | 718 | std::set<string> GetSupportedAnnotations() const override; |
Jeongik Cha | 82317dd | 2019-02-27 20:26:11 +0900 | [diff] [blame] | 719 | bool CheckValid(const AidlTypenames& typenames) const override; |
Jeongik Cha | 047c5ee | 2019-08-07 23:16:49 +0900 | [diff] [blame] | 720 | bool LanguageSpecificCheckValid(Options::Language lang) const override; |
Steven Moreland | c258abc | 2018-07-10 14:03:38 -0700 | [diff] [blame] | 721 | const AidlParcelable* AsParcelable() const override { return this; } |
Jeongik Cha | df76dc7 | 2019-11-28 00:08:47 +0900 | [diff] [blame] | 722 | const AidlParameterizable<std::string>* AsParameterizable() const override { return this; } |
| 723 | const AidlNode& AsAidlNode() const override { return *this; } |
Steven Moreland | ed83a28 | 2018-07-17 13:27:29 -0700 | [diff] [blame] | 724 | std::string GetPreprocessDeclarationName() const override { return "parcelable"; } |
Steven Moreland | c258abc | 2018-07-10 14:03:38 -0700 | [diff] [blame] | 725 | |
Jeongik Cha | 997281d | 2020-01-16 15:23:59 +0900 | [diff] [blame] | 726 | void Dump(CodeWriter* writer) const override; |
Jiyong Park | 02da742 | 2018-07-16 16:00:26 +0900 | [diff] [blame] | 727 | |
Casey Dahlin | 1ae2bc5 | 2015-10-07 18:49:10 -0700 | [diff] [blame] | 728 | private: |
Christopher Wiley | 8aa4d9f | 2015-11-16 19:10:45 -0800 | [diff] [blame] | 729 | std::unique_ptr<AidlQualifiedName> name_; |
Christopher Wiley | 8aa4d9f | 2015-11-16 19:10:45 -0800 | [diff] [blame] | 730 | std::string cpp_header_; |
Casey Dahlin | 59401da | 2015-10-09 18:16:45 -0700 | [diff] [blame] | 731 | |
Casey Dahlin | 1ae2bc5 | 2015-10-07 18:49:10 -0700 | [diff] [blame] | 732 | DISALLOW_COPY_AND_ASSIGN(AidlParcelable); |
Casey Dahlin | 0a2f8be | 2015-09-28 16:15:29 -0700 | [diff] [blame] | 733 | }; |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 734 | |
Steven Moreland | 5557f1c | 2018-07-02 13:50:23 -0700 | [diff] [blame] | 735 | class AidlStructuredParcelable : public AidlParcelable { |
| 736 | public: |
Steven Moreland | 46e9da8 | 2018-07-27 15:45:29 -0700 | [diff] [blame] | 737 | AidlStructuredParcelable(const AidlLocation& location, AidlQualifiedName* name, |
Jiyong Park | a6605ab | 2018-11-11 14:30:21 +0900 | [diff] [blame] | 738 | const std::vector<std::string>& package, const std::string& comments, |
Steven Moreland | 5557f1c | 2018-07-02 13:50:23 -0700 | [diff] [blame] | 739 | std::vector<std::unique_ptr<AidlVariableDeclaration>>* variables); |
| 740 | |
| 741 | const std::vector<std::unique_ptr<AidlVariableDeclaration>>& GetFields() const { |
| 742 | return variables_; |
| 743 | } |
| 744 | |
Steven Moreland | c258abc | 2018-07-10 14:03:38 -0700 | [diff] [blame] | 745 | const AidlStructuredParcelable* AsStructuredParcelable() const override { return this; } |
Steven Moreland | ed83a28 | 2018-07-17 13:27:29 -0700 | [diff] [blame] | 746 | std::string GetPreprocessDeclarationName() const override { return "structured_parcelable"; } |
Steven Moreland | 5557f1c | 2018-07-02 13:50:23 -0700 | [diff] [blame] | 747 | |
Jeongik Cha | 997281d | 2020-01-16 15:23:59 +0900 | [diff] [blame] | 748 | void Dump(CodeWriter* writer) const override; |
Jiyong Park | 02da742 | 2018-07-16 16:00:26 +0900 | [diff] [blame] | 749 | |
Devin Moore | 24f6857 | 2020-02-26 13:20:59 -0800 | [diff] [blame] | 750 | std::set<string> GetSupportedAnnotations() const override; |
Jeongik Cha | db0f59e | 2018-11-01 18:11:21 +0900 | [diff] [blame] | 751 | bool CheckValid(const AidlTypenames& typenames) const override; |
Jeongik Cha | 047c5ee | 2019-08-07 23:16:49 +0900 | [diff] [blame] | 752 | bool LanguageSpecificCheckValid(Options::Language lang) const override; |
Jeongik Cha | db0f59e | 2018-11-01 18:11:21 +0900 | [diff] [blame] | 753 | |
Steven Moreland | 5557f1c | 2018-07-02 13:50:23 -0700 | [diff] [blame] | 754 | private: |
| 755 | const std::vector<std::unique_ptr<AidlVariableDeclaration>> variables_; |
| 756 | |
| 757 | DISALLOW_COPY_AND_ASSIGN(AidlStructuredParcelable); |
| 758 | }; |
| 759 | |
Daniel Norman | 85aed54 | 2019-08-21 12:01:14 -0700 | [diff] [blame] | 760 | class AidlEnumerator : public AidlNode { |
| 761 | public: |
Daniel Norman | 2e4112d | 2019-10-03 10:22:35 -0700 | [diff] [blame] | 762 | AidlEnumerator(const AidlLocation& location, const std::string& name, AidlConstantValue* value, |
| 763 | const std::string& comments); |
Daniel Norman | 85aed54 | 2019-08-21 12:01:14 -0700 | [diff] [blame] | 764 | virtual ~AidlEnumerator() = default; |
| 765 | |
| 766 | const std::string& GetName() const { return name_; } |
Will McVicker | d7d18df | 2019-09-12 13:40:50 -0700 | [diff] [blame] | 767 | AidlConstantValue* GetValue() const { return value_.get(); } |
Daniel Norman | 2e4112d | 2019-10-03 10:22:35 -0700 | [diff] [blame] | 768 | const std::string& GetComments() const { return comments_; } |
Daniel Norman | 85aed54 | 2019-08-21 12:01:14 -0700 | [diff] [blame] | 769 | bool CheckValid(const AidlTypeSpecifier& enum_backing_type) const; |
| 770 | |
| 771 | string ValueString(const AidlTypeSpecifier& backing_type, |
| 772 | const ConstantValueDecorator& decorator) const; |
| 773 | |
Daniel Norman | b28684e | 2019-10-17 15:31:39 -0700 | [diff] [blame] | 774 | void SetValue(std::unique_ptr<AidlConstantValue> value) { value_ = std::move(value); } |
| 775 | |
Daniel Norman | 85aed54 | 2019-08-21 12:01:14 -0700 | [diff] [blame] | 776 | private: |
| 777 | const std::string name_; |
Will McVicker | d7d18df | 2019-09-12 13:40:50 -0700 | [diff] [blame] | 778 | unique_ptr<AidlConstantValue> value_; |
Daniel Norman | 2e4112d | 2019-10-03 10:22:35 -0700 | [diff] [blame] | 779 | const std::string comments_; |
Daniel Norman | 85aed54 | 2019-08-21 12:01:14 -0700 | [diff] [blame] | 780 | |
| 781 | DISALLOW_COPY_AND_ASSIGN(AidlEnumerator); |
| 782 | }; |
| 783 | |
| 784 | class AidlEnumDeclaration : public AidlDefinedType { |
| 785 | public: |
Will McVicker | d7d18df | 2019-09-12 13:40:50 -0700 | [diff] [blame] | 786 | AidlEnumDeclaration(const AidlLocation& location, const string& name, |
Daniel Norman | 85aed54 | 2019-08-21 12:01:14 -0700 | [diff] [blame] | 787 | std::vector<std::unique_ptr<AidlEnumerator>>* enumerators, |
Daniel Norman | 2e4112d | 2019-10-03 10:22:35 -0700 | [diff] [blame] | 788 | const std::vector<std::string>& package, const std::string& comments); |
Daniel Norman | 85aed54 | 2019-08-21 12:01:14 -0700 | [diff] [blame] | 789 | virtual ~AidlEnumDeclaration() = default; |
| 790 | |
| 791 | void SetBackingType(std::unique_ptr<const AidlTypeSpecifier> type); |
| 792 | const AidlTypeSpecifier& GetBackingType() const { return *backing_type_; } |
| 793 | const std::vector<std::unique_ptr<AidlEnumerator>>& GetEnumerators() const { |
| 794 | return enumerators_; |
| 795 | } |
Steven Moreland | 59e53e4 | 2019-11-26 20:38:08 -0800 | [diff] [blame] | 796 | bool Autofill(); |
Devin Moore | 24f6857 | 2020-02-26 13:20:59 -0800 | [diff] [blame] | 797 | std::set<string> GetSupportedAnnotations() const override; |
Daniel Norman | 85aed54 | 2019-08-21 12:01:14 -0700 | [diff] [blame] | 798 | bool CheckValid(const AidlTypenames& typenames) const override; |
| 799 | bool LanguageSpecificCheckValid(Options::Language) const override { return true; } |
| 800 | std::string GetPreprocessDeclarationName() const override { return "enum"; } |
Jeongik Cha | 997281d | 2020-01-16 15:23:59 +0900 | [diff] [blame] | 801 | void Dump(CodeWriter* writer) const override; |
Daniel Norman | 85aed54 | 2019-08-21 12:01:14 -0700 | [diff] [blame] | 802 | |
| 803 | const AidlEnumDeclaration* AsEnumDeclaration() const override { return this; } |
| 804 | |
| 805 | private: |
| 806 | const std::string name_; |
| 807 | const std::vector<std::unique_ptr<AidlEnumerator>> enumerators_; |
| 808 | std::unique_ptr<const AidlTypeSpecifier> backing_type_; |
| 809 | |
| 810 | DISALLOW_COPY_AND_ASSIGN(AidlEnumDeclaration); |
| 811 | }; |
| 812 | |
Jiyong Park | 1deecc3 | 2018-07-17 01:14:41 +0900 | [diff] [blame] | 813 | class AidlInterface final : public AidlDefinedType { |
Casey Dahlin | 1ae2bc5 | 2015-10-07 18:49:10 -0700 | [diff] [blame] | 814 | public: |
Steven Moreland | 46e9da8 | 2018-07-27 15:45:29 -0700 | [diff] [blame] | 815 | AidlInterface(const AidlLocation& location, const std::string& name, const std::string& comments, |
| 816 | bool oneway_, std::vector<std::unique_ptr<AidlMember>>* members, |
Christopher Wiley | d76067c | 2015-10-19 17:00:13 -0700 | [diff] [blame] | 817 | const std::vector<std::string>& package); |
Casey Dahlin | 1ae2bc5 | 2015-10-07 18:49:10 -0700 | [diff] [blame] | 818 | virtual ~AidlInterface() = default; |
| 819 | |
Casey Dahlin | fb7da2e | 2015-10-08 17:26:09 -0700 | [diff] [blame] | 820 | const std::vector<std::unique_ptr<AidlMethod>>& GetMethods() const |
| 821 | { return methods_; } |
Jiyong Park | 309668e | 2018-07-28 16:55:44 +0900 | [diff] [blame] | 822 | std::vector<std::unique_ptr<AidlMethod>>& GetMutableMethods() { return methods_; } |
Steven Moreland | 693640b | 2018-07-19 13:46:27 -0700 | [diff] [blame] | 823 | const std::vector<std::unique_ptr<AidlConstantDeclaration>>& GetConstantDeclarations() const { |
| 824 | return constants_; |
| 825 | } |
Casey Dahlin | a2f77c4 | 2015-12-01 18:26:02 -0800 | [diff] [blame] | 826 | |
Steven Moreland | c258abc | 2018-07-10 14:03:38 -0700 | [diff] [blame] | 827 | const AidlInterface* AsInterface() const override { return this; } |
Steven Moreland | ed83a28 | 2018-07-17 13:27:29 -0700 | [diff] [blame] | 828 | std::string GetPreprocessDeclarationName() const override { return "interface"; } |
Steven Moreland | 5557f1c | 2018-07-02 13:50:23 -0700 | [diff] [blame] | 829 | |
Jeongik Cha | 997281d | 2020-01-16 15:23:59 +0900 | [diff] [blame] | 830 | void Dump(CodeWriter* writer) const override; |
Jiyong Park | 02da742 | 2018-07-16 16:00:26 +0900 | [diff] [blame] | 831 | |
Devin Moore | 24f6857 | 2020-02-26 13:20:59 -0800 | [diff] [blame] | 832 | std::set<string> GetSupportedAnnotations() const override; |
Jeongik Cha | db0f59e | 2018-11-01 18:11:21 +0900 | [diff] [blame] | 833 | bool CheckValid(const AidlTypenames& typenames) const override; |
Jeongik Cha | 047c5ee | 2019-08-07 23:16:49 +0900 | [diff] [blame] | 834 | bool LanguageSpecificCheckValid(Options::Language lang) const override; |
Jeongik Cha | db0f59e | 2018-11-01 18:11:21 +0900 | [diff] [blame] | 835 | |
Casey Dahlin | 1ae2bc5 | 2015-10-07 18:49:10 -0700 | [diff] [blame] | 836 | private: |
Casey Dahlin | fb7da2e | 2015-10-08 17:26:09 -0700 | [diff] [blame] | 837 | std::vector<std::unique_ptr<AidlMethod>> methods_; |
Steven Moreland | 693640b | 2018-07-19 13:46:27 -0700 | [diff] [blame] | 838 | std::vector<std::unique_ptr<AidlConstantDeclaration>> constants_; |
Casey Dahlin | fb7da2e | 2015-10-08 17:26:09 -0700 | [diff] [blame] | 839 | |
Casey Dahlin | 1ae2bc5 | 2015-10-07 18:49:10 -0700 | [diff] [blame] | 840 | DISALLOW_COPY_AND_ASSIGN(AidlInterface); |
Casey Dahlin | 0a2f8be | 2015-09-28 16:15:29 -0700 | [diff] [blame] | 841 | }; |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 842 | |
Casey Dahlin | 0edf342 | 2015-10-07 12:34:59 -0700 | [diff] [blame] | 843 | class AidlImport : public AidlNode { |
| 844 | public: |
Steven Moreland | 46e9da8 | 2018-07-27 15:45:29 -0700 | [diff] [blame] | 845 | AidlImport(const AidlLocation& location, const std::string& needed_class); |
Casey Dahlin | 0edf342 | 2015-10-07 12:34:59 -0700 | [diff] [blame] | 846 | virtual ~AidlImport() = default; |
| 847 | |
Casey Dahlin | 0edf342 | 2015-10-07 12:34:59 -0700 | [diff] [blame] | 848 | const std::string& GetFilename() const { return filename_; } |
| 849 | const std::string& GetNeededClass() const { return needed_class_; } |
Casey Dahlin | 0edf342 | 2015-10-07 12:34:59 -0700 | [diff] [blame] | 850 | |
| 851 | private: |
Casey Dahlin | 0edf342 | 2015-10-07 12:34:59 -0700 | [diff] [blame] | 852 | std::string filename_; |
| 853 | std::string needed_class_; |
Casey Dahlin | 0edf342 | 2015-10-07 12:34:59 -0700 | [diff] [blame] | 854 | |
| 855 | DISALLOW_COPY_AND_ASSIGN(AidlImport); |
Casey Dahlin | e250749 | 2015-09-14 17:11:20 -0700 | [diff] [blame] | 856 | }; |
| 857 | |
| 858 | class Parser { |
Casey Dahlin | dd69181 | 2015-09-09 17:59:06 -0700 | [diff] [blame] | 859 | public: |
Casey Dahlin | e250749 | 2015-09-14 17:11:20 -0700 | [diff] [blame] | 860 | ~Parser(); |
Casey Dahlin | dd69181 | 2015-09-09 17:59:06 -0700 | [diff] [blame] | 861 | |
Steven Moreland | 64e29be | 2018-08-08 18:52:19 -0700 | [diff] [blame] | 862 | // Parse contents of file |filename|. Should only be called once. |
| 863 | static std::unique_ptr<Parser> Parse(const std::string& filename, |
| 864 | const android::aidl::IoDelegate& io_delegate, |
| 865 | AidlTypenames& typenames); |
Casey Dahlin | 89d4484 | 2015-09-24 18:45:54 -0700 | [diff] [blame] | 866 | |
Steven Moreland | 2ca4fcb | 2018-06-27 16:01:01 -0700 | [diff] [blame] | 867 | void AddError() { error_++; } |
Steven Moreland | 64e29be | 2018-08-08 18:52:19 -0700 | [diff] [blame] | 868 | bool HasError() { return error_ != 0; } |
Casey Dahlin | dd69181 | 2015-09-09 17:59:06 -0700 | [diff] [blame] | 869 | |
Casey Dahlin | 3c6df36 | 2015-10-06 15:48:35 -0700 | [diff] [blame] | 870 | const std::string& FileName() const { return filename_; } |
Casey Dahlin | 42727f8 | 2015-10-12 19:23:40 -0700 | [diff] [blame] | 871 | void* Scanner() const { return scanner_; } |
Casey Dahlin | dd69181 | 2015-09-09 17:59:06 -0700 | [diff] [blame] | 872 | |
Steven Moreland | d1039a9 | 2020-01-23 09:49:43 -0800 | [diff] [blame] | 873 | void AddImport(std::unique_ptr<AidlImport>&& import); |
Christopher Wiley | d76067c | 2015-10-19 17:00:13 -0700 | [diff] [blame] | 874 | const std::vector<std::unique_ptr<AidlImport>>& GetImports() { |
| 875 | return imports_; |
| 876 | } |
Casey Dahlin | dd69181 | 2015-09-09 17:59:06 -0700 | [diff] [blame] | 877 | |
Jiyong Park | b034bf0 | 2018-07-30 17:44:33 +0900 | [diff] [blame] | 878 | void SetPackage(unique_ptr<AidlQualifiedName> name) { package_ = std::move(name); } |
| 879 | std::vector<std::string> Package() const; |
Jiyong Park | 1deecc3 | 2018-07-17 01:14:41 +0900 | [diff] [blame] | 880 | |
| 881 | void DeferResolution(AidlTypeSpecifier* typespec) { |
| 882 | unresolved_typespecs_.emplace_back(typespec); |
| 883 | } |
| 884 | |
Jiyong Park | e59c368 | 2018-09-11 23:10:25 +0900 | [diff] [blame] | 885 | const vector<AidlTypeSpecifier*>& GetUnresolvedTypespecs() const { return unresolved_typespecs_; } |
| 886 | |
Jiyong Park | 1deecc3 | 2018-07-17 01:14:41 +0900 | [diff] [blame] | 887 | bool Resolve(); |
| 888 | |
Jiyong Park | b034bf0 | 2018-07-30 17:44:33 +0900 | [diff] [blame] | 889 | void AddDefinedType(unique_ptr<AidlDefinedType> type) { |
| 890 | // Parser does NOT own AidlDefinedType, it just has references to the types |
| 891 | // that it encountered while parsing the input file. |
| 892 | defined_types_.emplace_back(type.get()); |
| 893 | |
| 894 | // AidlDefinedType IS owned by AidlTypenames |
| 895 | if (!typenames_.AddDefinedType(std::move(type))) { |
| 896 | AddError(); |
| 897 | } |
| 898 | } |
| 899 | |
| 900 | vector<AidlDefinedType*>& GetDefinedTypes() { return defined_types_; } |
| 901 | |
Casey Dahlin | dd69181 | 2015-09-09 17:59:06 -0700 | [diff] [blame] | 902 | private: |
Steven Moreland | 64e29be | 2018-08-08 18:52:19 -0700 | [diff] [blame] | 903 | explicit Parser(const std::string& filename, std::string& raw_buffer, |
| 904 | android::aidl::AidlTypenames& typenames); |
| 905 | |
Casey Dahlin | dd69181 | 2015-09-09 17:59:06 -0700 | [diff] [blame] | 906 | std::string filename_; |
Christopher Wiley | d76067c | 2015-10-19 17:00:13 -0700 | [diff] [blame] | 907 | std::unique_ptr<AidlQualifiedName> package_; |
Jiyong Park | b034bf0 | 2018-07-30 17:44:33 +0900 | [diff] [blame] | 908 | AidlTypenames& typenames_; |
Steven Moreland | 64e29be | 2018-08-08 18:52:19 -0700 | [diff] [blame] | 909 | |
| 910 | void* scanner_ = nullptr; |
| 911 | YY_BUFFER_STATE buffer_; |
| 912 | int error_ = 0; |
| 913 | |
| 914 | std::vector<std::unique_ptr<AidlImport>> imports_; |
Jiyong Park | b034bf0 | 2018-07-30 17:44:33 +0900 | [diff] [blame] | 915 | vector<AidlDefinedType*> defined_types_; |
Jiyong Park | 1deecc3 | 2018-07-17 01:14:41 +0900 | [diff] [blame] | 916 | vector<AidlTypeSpecifier*> unresolved_typespecs_; |
Casey Dahlin | dd69181 | 2015-09-09 17:59:06 -0700 | [diff] [blame] | 917 | |
Casey Dahlin | e250749 | 2015-09-14 17:11:20 -0700 | [diff] [blame] | 918 | DISALLOW_COPY_AND_ASSIGN(Parser); |
Casey Dahlin | dd69181 | 2015-09-09 17:59:06 -0700 | [diff] [blame] | 919 | }; |