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 | 698e6af | 2018-12-12 14:39:55 +0900 | [diff] [blame] | 245 | bool IsSystemApi() const; |
Jeongik Cha | 88f95a8 | 2020-01-15 13:02:16 +0900 | [diff] [blame] | 246 | bool IsStableApiParcelable(Options::Language lang) const; |
Andrei Onea | 9445fc6 | 2019-06-27 18:11:59 +0100 | [diff] [blame] | 247 | |
| 248 | const AidlAnnotation* UnsupportedAppUsage() const; |
Daniel Norman | 716d311 | 2019-09-10 13:11:56 -0700 | [diff] [blame] | 249 | const AidlTypeSpecifier* BackingType(const AidlTypenames& typenames) const; |
Jiyong Park | 68bc77a | 2018-07-19 19:00:45 +0900 | [diff] [blame] | 250 | std::string ToString() const; |
Casey Dahlin | e792293 | 2016-02-29 17:23:01 -0800 | [diff] [blame] | 251 | |
Jiyong Park | a6605ab | 2018-11-11 14:30:21 +0900 | [diff] [blame] | 252 | const vector<AidlAnnotation>& GetAnnotations() const { return annotations_; } |
Andrei Onea | 9445fc6 | 2019-06-27 18:11:59 +0100 | [diff] [blame] | 253 | bool CheckValidAnnotations() const; |
Jiyong Park | 3656c3c | 2018-08-01 20:02:01 +0900 | [diff] [blame] | 254 | |
Casey Dahlin | e792293 | 2016-02-29 17:23:01 -0800 | [diff] [blame] | 255 | private: |
Jiyong Park | a6605ab | 2018-11-11 14:30:21 +0900 | [diff] [blame] | 256 | vector<AidlAnnotation> annotations_; |
Casey Dahlin | e792293 | 2016-02-29 17:23:01 -0800 | [diff] [blame] | 257 | }; |
| 258 | |
Jiyong Park | 1deecc3 | 2018-07-17 01:14:41 +0900 | [diff] [blame] | 259 | class AidlQualifiedName; |
| 260 | |
| 261 | // AidlTypeSpecifier represents a reference to either a built-in type, |
| 262 | // 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] | 263 | class AidlTypeSpecifier final : public AidlAnnotatable, |
| 264 | public AidlParameterizable<unique_ptr<AidlTypeSpecifier>> { |
Casey Dahlin | e792293 | 2016-02-29 17:23:01 -0800 | [diff] [blame] | 265 | public: |
Steven Moreland | 46e9da8 | 2018-07-27 15:45:29 -0700 | [diff] [blame] | 266 | AidlTypeSpecifier(const AidlLocation& location, const string& unresolved_name, bool is_array, |
| 267 | vector<unique_ptr<AidlTypeSpecifier>>* type_params, const string& comments); |
Jiyong Park | d59a10d | 2018-07-18 11:12:55 +0900 | [diff] [blame] | 268 | virtual ~AidlTypeSpecifier() = default; |
Casey Dahlin | 0ee3758 | 2015-09-30 16:31:55 -0700 | [diff] [blame] | 269 | |
Steven Moreland | 3f658cf | 2018-08-20 13:40:54 -0700 | [diff] [blame] | 270 | // Copy of this type which is not an array. |
| 271 | AidlTypeSpecifier ArrayBase() const; |
| 272 | |
Jiyong Park | 1deecc3 | 2018-07-17 01:14:41 +0900 | [diff] [blame] | 273 | // Returns the full-qualified name of the base type. |
| 274 | // int -> int |
| 275 | // int[] -> int |
| 276 | // List<String> -> List |
| 277 | // IFoo -> foo.bar.IFoo (if IFoo is in package foo.bar) |
| 278 | const string& GetName() const { |
| 279 | if (IsResolved()) { |
| 280 | return fully_qualified_name_; |
| 281 | } else { |
| 282 | return GetUnresolvedName(); |
| 283 | } |
| 284 | } |
Casey Dahlin | 0ee3758 | 2015-09-30 16:31:55 -0700 | [diff] [blame] | 285 | |
Jiyong Park | 1deecc3 | 2018-07-17 01:14:41 +0900 | [diff] [blame] | 286 | // Returns string representation of this type specifier. |
Artur Satayev | 91fe871 | 2019-07-29 13:06:01 +0100 | [diff] [blame] | 287 | // This is GetBaseTypeName() + array modifier or generic type parameters |
Jiyong Park | 1deecc3 | 2018-07-17 01:14:41 +0900 | [diff] [blame] | 288 | string ToString() const; |
| 289 | |
Jiyong Park | 02da742 | 2018-07-16 16:00:26 +0900 | [diff] [blame] | 290 | std::string Signature() const; |
| 291 | |
Jiyong Park | 1deecc3 | 2018-07-17 01:14:41 +0900 | [diff] [blame] | 292 | const string& GetUnresolvedName() const { return unresolved_name_; } |
| 293 | |
Jeongik Cha | 997281d | 2020-01-16 15:23:59 +0900 | [diff] [blame] | 294 | bool IsHidden() const; |
| 295 | |
Jiyong Park | 1deecc3 | 2018-07-17 01:14:41 +0900 | [diff] [blame] | 296 | const string& GetComments() const { return comments_; } |
| 297 | |
Jeongik Cha | 1a7ab64 | 2019-07-29 17:31:02 +0900 | [diff] [blame] | 298 | const std::vector<std::string> GetSplitName() const { return split_name_; } |
| 299 | |
Jiyong Park | a6605ab | 2018-11-11 14:30:21 +0900 | [diff] [blame] | 300 | void SetComments(const string& comment) { comments_ = comment; } |
| 301 | |
Jiyong Park | 1deecc3 | 2018-07-17 01:14:41 +0900 | [diff] [blame] | 302 | bool IsResolved() const { return fully_qualified_name_ != ""; } |
| 303 | |
| 304 | bool IsArray() const { return is_array_; } |
| 305 | |
Jiyong Park | 1deecc3 | 2018-07-17 01:14:41 +0900 | [diff] [blame] | 306 | // Resolve the base type name to a fully-qualified name. Return false if the |
| 307 | // resolution fails. |
Daniel Norman | 716d311 | 2019-09-10 13:11:56 -0700 | [diff] [blame] | 308 | bool Resolve(const AidlTypenames& typenames); |
Casey Dahlin | 0ee3758 | 2015-09-30 16:31:55 -0700 | [diff] [blame] | 309 | |
Jeongik Cha | db0f59e | 2018-11-01 18:11:21 +0900 | [diff] [blame] | 310 | bool CheckValid(const AidlTypenames& typenames) const; |
Jeongik Cha | 047c5ee | 2019-08-07 23:16:49 +0900 | [diff] [blame] | 311 | bool LanguageSpecificCheckValid(Options::Language lang) const; |
Jeongik Cha | df76dc7 | 2019-11-28 00:08:47 +0900 | [diff] [blame] | 312 | const AidlNode& AsAidlNode() const override { return *this; } |
Jiyong Park | 1d2df7d | 2018-07-23 15:22:50 +0900 | [diff] [blame] | 313 | |
Casey Dahlin | 0ee3758 | 2015-09-30 16:31:55 -0700 | [diff] [blame] | 314 | private: |
Steven Moreland | 3f658cf | 2018-08-20 13:40:54 -0700 | [diff] [blame] | 315 | AidlTypeSpecifier(const AidlTypeSpecifier&) = default; |
| 316 | |
Jiyong Park | 1deecc3 | 2018-07-17 01:14:41 +0900 | [diff] [blame] | 317 | const string unresolved_name_; |
| 318 | string fully_qualified_name_; |
Steven Moreland | 3f658cf | 2018-08-20 13:40:54 -0700 | [diff] [blame] | 319 | bool is_array_; |
Jiyong Park | a6605ab | 2018-11-11 14:30:21 +0900 | [diff] [blame] | 320 | string comments_; |
Jeongik Cha | 1a7ab64 | 2019-07-29 17:31:02 +0900 | [diff] [blame] | 321 | vector<string> split_name_; |
Casey Dahlin | 0ee3758 | 2015-09-30 16:31:55 -0700 | [diff] [blame] | 322 | }; |
| 323 | |
Steven Moreland | 860b194 | 2018-08-16 14:59:28 -0700 | [diff] [blame] | 324 | // Returns the universal value unaltered. |
| 325 | std::string AidlConstantValueDecorator(const AidlTypeSpecifier& type, const std::string& raw_value); |
| 326 | |
Steven Moreland | 9ea10e3 | 2018-07-19 15:26:09 -0700 | [diff] [blame] | 327 | class AidlConstantValue; |
Steven Moreland | 5557f1c | 2018-07-02 13:50:23 -0700 | [diff] [blame] | 328 | class AidlVariableDeclaration : public AidlNode { |
| 329 | public: |
Steven Moreland | 46e9da8 | 2018-07-27 15:45:29 -0700 | [diff] [blame] | 330 | AidlVariableDeclaration(const AidlLocation& location, AidlTypeSpecifier* type, |
| 331 | const std::string& name); |
| 332 | AidlVariableDeclaration(const AidlLocation& location, AidlTypeSpecifier* type, |
| 333 | const std::string& name, AidlConstantValue* default_value); |
Steven Moreland | 5557f1c | 2018-07-02 13:50:23 -0700 | [diff] [blame] | 334 | virtual ~AidlVariableDeclaration() = default; |
| 335 | |
| 336 | std::string GetName() const { return name_; } |
Jiyong Park | d59a10d | 2018-07-18 11:12:55 +0900 | [diff] [blame] | 337 | const AidlTypeSpecifier& GetType() const { return *type_; } |
Steven Moreland | 9ea10e3 | 2018-07-19 15:26:09 -0700 | [diff] [blame] | 338 | const AidlConstantValue* GetDefaultValue() const { return default_value_.get(); } |
| 339 | |
Jiyong Park | d59a10d | 2018-07-18 11:12:55 +0900 | [diff] [blame] | 340 | AidlTypeSpecifier* GetMutableType() { return type_.get(); } |
Steven Moreland | 5557f1c | 2018-07-02 13:50:23 -0700 | [diff] [blame] | 341 | |
Jeongik Cha | db0f59e | 2018-11-01 18:11:21 +0900 | [diff] [blame] | 342 | bool CheckValid(const AidlTypenames& typenames) const; |
Steven Moreland | 5557f1c | 2018-07-02 13:50:23 -0700 | [diff] [blame] | 343 | std::string ToString() const; |
Jiyong Park | 02da742 | 2018-07-16 16:00:26 +0900 | [diff] [blame] | 344 | std::string Signature() const; |
Steven Moreland | 5557f1c | 2018-07-02 13:50:23 -0700 | [diff] [blame] | 345 | |
Steven Moreland | 860b194 | 2018-08-16 14:59:28 -0700 | [diff] [blame] | 346 | std::string ValueString(const ConstantValueDecorator& decorator) const; |
Steven Moreland | 2529432 | 2018-08-07 18:13:55 -0700 | [diff] [blame] | 347 | |
Steven Moreland | 5557f1c | 2018-07-02 13:50:23 -0700 | [diff] [blame] | 348 | private: |
Jiyong Park | d59a10d | 2018-07-18 11:12:55 +0900 | [diff] [blame] | 349 | std::unique_ptr<AidlTypeSpecifier> type_; |
Steven Moreland | 5557f1c | 2018-07-02 13:50:23 -0700 | [diff] [blame] | 350 | std::string name_; |
Steven Moreland | 9ea10e3 | 2018-07-19 15:26:09 -0700 | [diff] [blame] | 351 | std::unique_ptr<AidlConstantValue> default_value_; |
Steven Moreland | 5557f1c | 2018-07-02 13:50:23 -0700 | [diff] [blame] | 352 | |
| 353 | DISALLOW_COPY_AND_ASSIGN(AidlVariableDeclaration); |
| 354 | }; |
| 355 | |
| 356 | class AidlArgument : public AidlVariableDeclaration { |
Casey Dahlin | bc7a50a | 2015-09-28 19:20:50 -0700 | [diff] [blame] | 357 | public: |
Casey Dahlin | c378c99 | 2015-09-29 16:50:40 -0700 | [diff] [blame] | 358 | enum Direction { IN_DIR = 1, OUT_DIR = 2, INOUT_DIR = 3 }; |
| 359 | |
Steven Moreland | 46e9da8 | 2018-07-27 15:45:29 -0700 | [diff] [blame] | 360 | AidlArgument(const AidlLocation& location, AidlArgument::Direction direction, |
| 361 | AidlTypeSpecifier* type, const std::string& name); |
| 362 | AidlArgument(const AidlLocation& location, AidlTypeSpecifier* type, const std::string& name); |
Casey Dahlin | bc7a50a | 2015-09-28 19:20:50 -0700 | [diff] [blame] | 363 | virtual ~AidlArgument() = default; |
| 364 | |
Casey Dahlin | c378c99 | 2015-09-29 16:50:40 -0700 | [diff] [blame] | 365 | Direction GetDirection() const { return direction_; } |
Christopher Wiley | ad33927 | 2015-10-05 19:11:58 -0700 | [diff] [blame] | 366 | bool IsOut() const { return direction_ & OUT_DIR; } |
| 367 | bool IsIn() const { return direction_ & IN_DIR; } |
Casey Dahlin | c378c99 | 2015-09-29 16:50:40 -0700 | [diff] [blame] | 368 | bool DirectionWasSpecified() const { return direction_specified_; } |
Jiyong Park | 3656c3c | 2018-08-01 20:02:01 +0900 | [diff] [blame] | 369 | string GetDirectionSpecifier() const; |
Christopher Wiley | ad33927 | 2015-10-05 19:11:58 -0700 | [diff] [blame] | 370 | |
Casey Dahlin | c378c99 | 2015-09-29 16:50:40 -0700 | [diff] [blame] | 371 | std::string ToString() const; |
Jiyong Park | 02da742 | 2018-07-16 16:00:26 +0900 | [diff] [blame] | 372 | std::string Signature() const; |
Casey Dahlin | c378c99 | 2015-09-29 16:50:40 -0700 | [diff] [blame] | 373 | |
Casey Dahlin | bc7a50a | 2015-09-28 19:20:50 -0700 | [diff] [blame] | 374 | private: |
Casey Dahlin | c378c99 | 2015-09-29 16:50:40 -0700 | [diff] [blame] | 375 | Direction direction_; |
| 376 | bool direction_specified_; |
| 377 | |
Casey Dahlin | bc7a50a | 2015-09-28 19:20:50 -0700 | [diff] [blame] | 378 | DISALLOW_COPY_AND_ASSIGN(AidlArgument); |
Casey Dahlin | a834dd4 | 2015-09-23 11:52:15 -0700 | [diff] [blame] | 379 | }; |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 380 | |
Casey Dahlin | d40e2fe | 2015-11-24 14:06:52 -0800 | [diff] [blame] | 381 | class AidlMethod; |
Steven Moreland | 693640b | 2018-07-19 13:46:27 -0700 | [diff] [blame] | 382 | class AidlConstantDeclaration; |
Daniel Norman | 85aed54 | 2019-08-21 12:01:14 -0700 | [diff] [blame] | 383 | class AidlEnumDeclaration; |
Casey Dahlin | d40e2fe | 2015-11-24 14:06:52 -0800 | [diff] [blame] | 384 | class AidlMember : public AidlNode { |
| 385 | public: |
Steven Moreland | 46e9da8 | 2018-07-27 15:45:29 -0700 | [diff] [blame] | 386 | AidlMember(const AidlLocation& location); |
Casey Dahlin | d40e2fe | 2015-11-24 14:06:52 -0800 | [diff] [blame] | 387 | virtual ~AidlMember() = default; |
| 388 | |
| 389 | virtual AidlMethod* AsMethod() { return nullptr; } |
Steven Moreland | 693640b | 2018-07-19 13:46:27 -0700 | [diff] [blame] | 390 | virtual AidlConstantDeclaration* AsConstantDeclaration() { return nullptr; } |
Casey Dahlin | d40e2fe | 2015-11-24 14:06:52 -0800 | [diff] [blame] | 391 | |
| 392 | private: |
| 393 | DISALLOW_COPY_AND_ASSIGN(AidlMember); |
| 394 | }; |
| 395 | |
Will McVicker | d7d18df | 2019-09-12 13:40:50 -0700 | [diff] [blame] | 396 | class AidlUnaryConstExpression; |
| 397 | class AidlBinaryConstExpression; |
| 398 | |
Steven Moreland | 693640b | 2018-07-19 13:46:27 -0700 | [diff] [blame] | 399 | class AidlConstantValue : public AidlNode { |
Casey Dahlin | d40e2fe | 2015-11-24 14:06:52 -0800 | [diff] [blame] | 400 | public: |
Will McVicker | d7d18df | 2019-09-12 13:40:50 -0700 | [diff] [blame] | 401 | enum class Type { |
| 402 | // WARNING: Don't change this order! The order is used to determine type |
| 403 | // promotion during a binary expression. |
| 404 | BOOLEAN, |
| 405 | INT8, |
| 406 | INT32, |
| 407 | INT64, |
| 408 | ARRAY, |
| 409 | CHARACTER, |
| 410 | STRING, |
| 411 | FLOATING, |
| 412 | UNARY, |
| 413 | BINARY, |
| 414 | ERROR, |
| 415 | }; |
| 416 | |
| 417 | /* |
| 418 | * Return the value casted to the given type. |
| 419 | */ |
| 420 | template <typename T> |
| 421 | T cast() const; |
Casey Dahlin | d40e2fe | 2015-11-24 14:06:52 -0800 | [diff] [blame] | 422 | |
Steven Moreland | 693640b | 2018-07-19 13:46:27 -0700 | [diff] [blame] | 423 | virtual ~AidlConstantValue() = default; |
Casey Dahlin | d40e2fe | 2015-11-24 14:06:52 -0800 | [diff] [blame] | 424 | |
Steven Moreland | 2529432 | 2018-08-07 18:13:55 -0700 | [diff] [blame] | 425 | static AidlConstantValue* Boolean(const AidlLocation& location, bool value); |
| 426 | static AidlConstantValue* Character(const AidlLocation& location, char value); |
Steven Moreland | 2529432 | 2018-08-07 18:13:55 -0700 | [diff] [blame] | 427 | // example: 123, -5498, maybe any size |
Will McVicker | d7d18df | 2019-09-12 13:40:50 -0700 | [diff] [blame] | 428 | static AidlConstantValue* Integral(const AidlLocation& location, const string& value); |
| 429 | static AidlConstantValue* Floating(const AidlLocation& location, const std::string& value); |
Steven Moreland | 860b194 | 2018-08-16 14:59:28 -0700 | [diff] [blame] | 430 | static AidlConstantValue* Array(const AidlLocation& location, |
Will McVicker | d7d18df | 2019-09-12 13:40:50 -0700 | [diff] [blame] | 431 | std::unique_ptr<vector<unique_ptr<AidlConstantValue>>> values); |
Steven Moreland | 693640b | 2018-07-19 13:46:27 -0700 | [diff] [blame] | 432 | // example: "\"asdf\"" |
Will McVicker | d7d18df | 2019-09-12 13:40:50 -0700 | [diff] [blame] | 433 | static AidlConstantValue* String(const AidlLocation& location, const string& value); |
Steven Moreland | 693640b | 2018-07-19 13:46:27 -0700 | [diff] [blame] | 434 | |
Daniel Norman | f0ca44f | 2019-10-25 09:59:44 -0700 | [diff] [blame] | 435 | // Construct an AidlConstantValue by evaluating the other integral constant's |
| 436 | // value string. This does not preserve the structure of the copied constant. |
Steven Moreland | 59e53e4 | 2019-11-26 20:38:08 -0800 | [diff] [blame] | 437 | // Returns nullptr and logs if value cannot be copied. |
Daniel Norman | f0ca44f | 2019-10-25 09:59:44 -0700 | [diff] [blame] | 438 | static AidlConstantValue* ShallowIntegralCopy(const AidlConstantValue& other); |
Daniel Norman | b28684e | 2019-10-17 15:31:39 -0700 | [diff] [blame] | 439 | |
Will McVicker | d7d18df | 2019-09-12 13:40:50 -0700 | [diff] [blame] | 440 | Type GetType() const { return final_type_; } |
Steven Moreland | 2529432 | 2018-08-07 18:13:55 -0700 | [diff] [blame] | 441 | |
Will McVicker | d7d18df | 2019-09-12 13:40:50 -0700 | [diff] [blame] | 442 | virtual bool CheckValid() const; |
Steven Moreland | 860b194 | 2018-08-16 14:59:28 -0700 | [diff] [blame] | 443 | |
| 444 | // 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] | 445 | string ValueString(const AidlTypeSpecifier& type, const ConstantValueDecorator& decorator) const; |
Casey Dahlin | d40e2fe | 2015-11-24 14:06:52 -0800 | [diff] [blame] | 446 | |
| 447 | private: |
Will McVicker | d7d18df | 2019-09-12 13:40:50 -0700 | [diff] [blame] | 448 | AidlConstantValue(const AidlLocation& location, Type parsed_type, int64_t parsed_value, |
| 449 | const string& checked_value); |
| 450 | AidlConstantValue(const AidlLocation& location, Type type, const string& checked_value); |
Steven Moreland | 860b194 | 2018-08-16 14:59:28 -0700 | [diff] [blame] | 451 | AidlConstantValue(const AidlLocation& location, Type type, |
Will McVicker | d7d18df | 2019-09-12 13:40:50 -0700 | [diff] [blame] | 452 | std::unique_ptr<vector<unique_ptr<AidlConstantValue>>> values); |
Steven Moreland | 2529432 | 2018-08-07 18:13:55 -0700 | [diff] [blame] | 453 | static string ToString(Type type); |
Will McVicker | d7d18df | 2019-09-12 13:40:50 -0700 | [diff] [blame] | 454 | static bool ParseIntegral(const string& value, int64_t* parsed_value, Type* parsed_type); |
| 455 | static bool IsHex(const string& value); |
Steven Moreland | 4bcb05c | 2019-11-27 18:57:47 -0800 | [diff] [blame] | 456 | |
Will McVicker | d7d18df | 2019-09-12 13:40:50 -0700 | [diff] [blame] | 457 | virtual bool evaluate(const AidlTypeSpecifier& type) const; |
Casey Dahlin | d40e2fe | 2015-11-24 14:06:52 -0800 | [diff] [blame] | 458 | |
Steven Moreland | 693640b | 2018-07-19 13:46:27 -0700 | [diff] [blame] | 459 | const Type type_ = Type::ERROR; |
Will McVicker | d7d18df | 2019-09-12 13:40:50 -0700 | [diff] [blame] | 460 | const vector<unique_ptr<AidlConstantValue>> values_; // if type_ == ARRAY |
| 461 | const string value_; // otherwise |
| 462 | |
| 463 | // State for tracking evaluation of expressions |
Steven Moreland | 4bcb05c | 2019-11-27 18:57:47 -0800 | [diff] [blame] | 464 | mutable bool is_valid_ = false; // cache of CheckValid, but may be marked false in evaluate |
| 465 | mutable bool is_evaluated_ = false; // whether evaluate has been called |
Will McVicker | d7d18df | 2019-09-12 13:40:50 -0700 | [diff] [blame] | 466 | mutable Type final_type_; |
| 467 | mutable int64_t final_value_; |
| 468 | mutable string final_string_value_ = ""; |
Steven Moreland | 693640b | 2018-07-19 13:46:27 -0700 | [diff] [blame] | 469 | |
| 470 | DISALLOW_COPY_AND_ASSIGN(AidlConstantValue); |
Will McVicker | d7d18df | 2019-09-12 13:40:50 -0700 | [diff] [blame] | 471 | |
| 472 | friend AidlUnaryConstExpression; |
| 473 | friend AidlBinaryConstExpression; |
| 474 | }; |
| 475 | |
| 476 | class AidlUnaryConstExpression : public AidlConstantValue { |
| 477 | public: |
| 478 | AidlUnaryConstExpression(const AidlLocation& location, const string& op, |
| 479 | std::unique_ptr<AidlConstantValue> rval); |
| 480 | |
| 481 | static bool IsCompatibleType(Type type, const string& op); |
| 482 | bool CheckValid() const override; |
Will McVicker | d7d18df | 2019-09-12 13:40:50 -0700 | [diff] [blame] | 483 | private: |
| 484 | bool evaluate(const AidlTypeSpecifier& type) const override; |
| 485 | |
| 486 | std::unique_ptr<AidlConstantValue> unary_; |
| 487 | const string op_; |
| 488 | }; |
| 489 | |
| 490 | class AidlBinaryConstExpression : public AidlConstantValue { |
| 491 | public: |
| 492 | AidlBinaryConstExpression(const AidlLocation& location, std::unique_ptr<AidlConstantValue> lval, |
| 493 | const string& op, std::unique_ptr<AidlConstantValue> rval); |
| 494 | |
| 495 | bool CheckValid() const override; |
Will McVicker | d7d18df | 2019-09-12 13:40:50 -0700 | [diff] [blame] | 496 | |
| 497 | static bool AreCompatibleTypes(Type t1, Type t2); |
| 498 | // Returns the promoted kind for both operands |
| 499 | static Type UsualArithmeticConversion(Type left, Type right); |
| 500 | // Returns the promoted integral type where INT32 is the smallest type |
| 501 | static Type IntegralPromotion(Type in); |
| 502 | |
| 503 | private: |
| 504 | bool evaluate(const AidlTypeSpecifier& type) const override; |
| 505 | |
| 506 | std::unique_ptr<AidlConstantValue> left_val_; |
| 507 | std::unique_ptr<AidlConstantValue> right_val_; |
| 508 | const string op_; |
Christopher Wiley | d6bdd8d | 2016-05-03 11:23:13 -0700 | [diff] [blame] | 509 | }; |
| 510 | |
Andrei Onea | 9445fc6 | 2019-06-27 18:11:59 +0100 | [diff] [blame] | 511 | struct AidlAnnotationParameter { |
| 512 | std::string name; |
| 513 | std::unique_ptr<AidlConstantValue> value; |
| 514 | }; |
| 515 | |
Steven Moreland | 693640b | 2018-07-19 13:46:27 -0700 | [diff] [blame] | 516 | class AidlConstantDeclaration : public AidlMember { |
Christopher Wiley | d6bdd8d | 2016-05-03 11:23:13 -0700 | [diff] [blame] | 517 | public: |
Steven Moreland | 46e9da8 | 2018-07-27 15:45:29 -0700 | [diff] [blame] | 518 | AidlConstantDeclaration(const AidlLocation& location, AidlTypeSpecifier* specifier, |
Will McVicker | d7d18df | 2019-09-12 13:40:50 -0700 | [diff] [blame] | 519 | const string& name, AidlConstantValue* value); |
Steven Moreland | 693640b | 2018-07-19 13:46:27 -0700 | [diff] [blame] | 520 | virtual ~AidlConstantDeclaration() = default; |
Christopher Wiley | d6bdd8d | 2016-05-03 11:23:13 -0700 | [diff] [blame] | 521 | |
Steven Moreland | 693640b | 2018-07-19 13:46:27 -0700 | [diff] [blame] | 522 | const AidlTypeSpecifier& GetType() const { return *type_; } |
Steven Moreland | 4d12f9a | 2018-10-31 14:30:55 -0700 | [diff] [blame] | 523 | AidlTypeSpecifier* GetMutableType() { return type_.get(); } |
Will McVicker | d7d18df | 2019-09-12 13:40:50 -0700 | [diff] [blame] | 524 | const string& GetName() const { return name_; } |
Steven Moreland | 693640b | 2018-07-19 13:46:27 -0700 | [diff] [blame] | 525 | const AidlConstantValue& GetValue() const { return *value_; } |
Jeongik Cha | db0f59e | 2018-11-01 18:11:21 +0900 | [diff] [blame] | 526 | bool CheckValid(const AidlTypenames& typenames) const; |
Christopher Wiley | d6bdd8d | 2016-05-03 11:23:13 -0700 | [diff] [blame] | 527 | |
Will McVicker | d7d18df | 2019-09-12 13:40:50 -0700 | [diff] [blame] | 528 | string ToString() const; |
| 529 | string Signature() const; |
Steven Moreland | 860b194 | 2018-08-16 14:59:28 -0700 | [diff] [blame] | 530 | string ValueString(const ConstantValueDecorator& decorator) const { |
Will McVicker | d7d18df | 2019-09-12 13:40:50 -0700 | [diff] [blame] | 531 | return value_->ValueString(GetType(), decorator); |
Steven Moreland | 860b194 | 2018-08-16 14:59:28 -0700 | [diff] [blame] | 532 | } |
Steven Moreland | 2529432 | 2018-08-07 18:13:55 -0700 | [diff] [blame] | 533 | |
Steven Moreland | 693640b | 2018-07-19 13:46:27 -0700 | [diff] [blame] | 534 | AidlConstantDeclaration* AsConstantDeclaration() override { return this; } |
Christopher Wiley | d6bdd8d | 2016-05-03 11:23:13 -0700 | [diff] [blame] | 535 | |
| 536 | private: |
Steven Moreland | 693640b | 2018-07-19 13:46:27 -0700 | [diff] [blame] | 537 | const unique_ptr<AidlTypeSpecifier> type_; |
Will McVicker | d7d18df | 2019-09-12 13:40:50 -0700 | [diff] [blame] | 538 | const string name_; |
| 539 | unique_ptr<AidlConstantValue> value_; |
Christopher Wiley | d6bdd8d | 2016-05-03 11:23:13 -0700 | [diff] [blame] | 540 | |
Steven Moreland | 693640b | 2018-07-19 13:46:27 -0700 | [diff] [blame] | 541 | DISALLOW_COPY_AND_ASSIGN(AidlConstantDeclaration); |
Casey Dahlin | d40e2fe | 2015-11-24 14:06:52 -0800 | [diff] [blame] | 542 | }; |
| 543 | |
| 544 | class AidlMethod : public AidlMember { |
Casey Dahlin | 5c69deb | 2015-10-01 14:44:12 -0700 | [diff] [blame] | 545 | public: |
Will McVicker | d7d18df | 2019-09-12 13:40:50 -0700 | [diff] [blame] | 546 | AidlMethod(const AidlLocation& location, bool oneway, AidlTypeSpecifier* type, const string& name, |
| 547 | vector<unique_ptr<AidlArgument>>* args, const string& comments); |
| 548 | AidlMethod(const AidlLocation& location, bool oneway, AidlTypeSpecifier* type, const string& name, |
| 549 | vector<unique_ptr<AidlArgument>>* args, const string& comments, int id, |
| 550 | bool is_user_defined = true); |
Casey Dahlin | 5c69deb | 2015-10-01 14:44:12 -0700 | [diff] [blame] | 551 | virtual ~AidlMethod() = default; |
| 552 | |
Casey Dahlin | d40e2fe | 2015-11-24 14:06:52 -0800 | [diff] [blame] | 553 | AidlMethod* AsMethod() override { return this; } |
Jeongik Cha | 997281d | 2020-01-16 15:23:59 +0900 | [diff] [blame] | 554 | bool IsHidden() const; |
Will McVicker | d7d18df | 2019-09-12 13:40:50 -0700 | [diff] [blame] | 555 | const string& GetComments() const { return comments_; } |
Jiyong Park | d59a10d | 2018-07-18 11:12:55 +0900 | [diff] [blame] | 556 | const AidlTypeSpecifier& GetType() const { return *type_; } |
| 557 | AidlTypeSpecifier* GetMutableType() { return type_.get(); } |
Steven Moreland | acd5347 | 2018-12-14 10:17:26 -0800 | [diff] [blame] | 558 | |
Steven Moreland | 8c70ba9 | 2018-12-17 10:20:31 -0800 | [diff] [blame] | 559 | // set if this method is part of an interface that is marked oneway |
| 560 | void ApplyInterfaceOneway(bool oneway) { oneway_ = oneway_ || oneway; } |
Casey Dahlin | f4a9311 | 2015-10-05 16:58:09 -0700 | [diff] [blame] | 561 | bool IsOneway() const { return oneway_; } |
Steven Moreland | acd5347 | 2018-12-14 10:17:26 -0800 | [diff] [blame] | 562 | |
Casey Dahlin | f4a9311 | 2015-10-05 16:58:09 -0700 | [diff] [blame] | 563 | const std::string& GetName() const { return name_; } |
Casey Dahlin | f4a9311 | 2015-10-05 16:58:09 -0700 | [diff] [blame] | 564 | bool HasId() const { return has_id_; } |
Jiyong Park | ed65bf4 | 2018-08-28 15:43:27 +0900 | [diff] [blame] | 565 | int GetId() const { return id_; } |
Casey Dahlin | f4a9311 | 2015-10-05 16:58:09 -0700 | [diff] [blame] | 566 | void SetId(unsigned id) { id_ = id; } |
Casey Dahlin | f2d23f7 | 2015-10-02 16:19:19 -0700 | [diff] [blame] | 567 | |
Jiyong Park | 309668e | 2018-07-28 16:55:44 +0900 | [diff] [blame] | 568 | bool IsUserDefined() const { return is_user_defined_; } |
| 569 | |
Casey Dahlin | f4a9311 | 2015-10-05 16:58:09 -0700 | [diff] [blame] | 570 | const std::vector<std::unique_ptr<AidlArgument>>& GetArguments() const { |
Christopher Wiley | ad33927 | 2015-10-05 19:11:58 -0700 | [diff] [blame] | 571 | return arguments_; |
| 572 | } |
| 573 | // An inout parameter will appear in both GetInArguments() |
| 574 | // and GetOutArguments(). AidlMethod retains ownership of the argument |
| 575 | // pointers returned in this way. |
| 576 | const std::vector<const AidlArgument*>& GetInArguments() const { |
| 577 | return in_arguments_; |
| 578 | } |
| 579 | const std::vector<const AidlArgument*>& GetOutArguments() const { |
| 580 | return out_arguments_; |
Casey Dahlin | f4a9311 | 2015-10-05 16:58:09 -0700 | [diff] [blame] | 581 | } |
Casey Dahlin | 5c69deb | 2015-10-01 14:44:12 -0700 | [diff] [blame] | 582 | |
Jiyong Park | 309668e | 2018-07-28 16:55:44 +0900 | [diff] [blame] | 583 | // name + type parameter types |
| 584 | // i.e, foo(int, String) |
Jiyong Park | 02da742 | 2018-07-16 16:00:26 +0900 | [diff] [blame] | 585 | std::string Signature() const; |
| 586 | |
Jiyong Park | 309668e | 2018-07-28 16:55:44 +0900 | [diff] [blame] | 587 | // return type + name + type parameter types + annotations |
| 588 | // i.e, boolean foo(int, @Nullable String) |
| 589 | std::string ToString() const; |
| 590 | |
Casey Dahlin | 5c69deb | 2015-10-01 14:44:12 -0700 | [diff] [blame] | 591 | private: |
Casey Dahlin | f4a9311 | 2015-10-05 16:58:09 -0700 | [diff] [blame] | 592 | bool oneway_; |
Casey Dahlin | f2d23f7 | 2015-10-02 16:19:19 -0700 | [diff] [blame] | 593 | std::string comments_; |
Jiyong Park | d59a10d | 2018-07-18 11:12:55 +0900 | [diff] [blame] | 594 | std::unique_ptr<AidlTypeSpecifier> type_; |
Casey Dahlin | f4a9311 | 2015-10-05 16:58:09 -0700 | [diff] [blame] | 595 | std::string name_; |
Christopher Wiley | ad33927 | 2015-10-05 19:11:58 -0700 | [diff] [blame] | 596 | const std::vector<std::unique_ptr<AidlArgument>> arguments_; |
| 597 | std::vector<const AidlArgument*> in_arguments_; |
| 598 | std::vector<const AidlArgument*> out_arguments_; |
Casey Dahlin | f4a9311 | 2015-10-05 16:58:09 -0700 | [diff] [blame] | 599 | bool has_id_; |
| 600 | int id_; |
Jiyong Park | 309668e | 2018-07-28 16:55:44 +0900 | [diff] [blame] | 601 | bool is_user_defined_ = true; |
Casey Dahlin | f2d23f7 | 2015-10-02 16:19:19 -0700 | [diff] [blame] | 602 | |
Casey Dahlin | 5c69deb | 2015-10-01 14:44:12 -0700 | [diff] [blame] | 603 | DISALLOW_COPY_AND_ASSIGN(AidlMethod); |
Casey Dahlin | 0a2f8be | 2015-09-28 16:15:29 -0700 | [diff] [blame] | 604 | }; |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 605 | |
Steven Moreland | c258abc | 2018-07-10 14:03:38 -0700 | [diff] [blame] | 606 | class AidlDefinedType; |
Jiyong Park | b034bf0 | 2018-07-30 17:44:33 +0900 | [diff] [blame] | 607 | class AidlInterface; |
| 608 | class AidlParcelable; |
| 609 | class AidlStructuredParcelable; |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 610 | |
Casey Dahlin | 2b2879b | 2015-10-13 16:59:44 -0700 | [diff] [blame] | 611 | class AidlQualifiedName : public AidlNode { |
| 612 | public: |
Steven Moreland | 46e9da8 | 2018-07-27 15:45:29 -0700 | [diff] [blame] | 613 | AidlQualifiedName(const AidlLocation& location, const std::string& term, |
| 614 | const std::string& comments); |
Casey Dahlin | 2b2879b | 2015-10-13 16:59:44 -0700 | [diff] [blame] | 615 | virtual ~AidlQualifiedName() = default; |
| 616 | |
| 617 | const std::vector<std::string>& GetTerms() const { return terms_; } |
| 618 | const std::string& GetComments() const { return comments_; } |
| 619 | std::string GetDotName() const { return android::base::Join(terms_, '.'); } |
Ningyuan Wang | d17c58b | 2016-09-29 14:33:14 -0700 | [diff] [blame] | 620 | std::string GetColonName() const { return android::base::Join(terms_, "::"); } |
Casey Dahlin | 2b2879b | 2015-10-13 16:59:44 -0700 | [diff] [blame] | 621 | |
Chih-Hung Hsieh | f05cc26 | 2016-07-27 11:42:51 -0700 | [diff] [blame] | 622 | void AddTerm(const std::string& term); |
Casey Dahlin | 2b2879b | 2015-10-13 16:59:44 -0700 | [diff] [blame] | 623 | |
| 624 | private: |
| 625 | std::vector<std::string> terms_; |
| 626 | std::string comments_; |
| 627 | |
| 628 | DISALLOW_COPY_AND_ASSIGN(AidlQualifiedName); |
| 629 | }; |
| 630 | |
Steven Moreland | 46e9da8 | 2018-07-27 15:45:29 -0700 | [diff] [blame] | 631 | class AidlInterface; |
| 632 | class AidlParcelable; |
| 633 | class AidlStructuredParcelable; |
Daniel Norman | 85aed54 | 2019-08-21 12:01:14 -0700 | [diff] [blame] | 634 | // AidlDefinedType represents either an interface, parcelable, or enum that is |
Jiyong Park | 1deecc3 | 2018-07-17 01:14:41 +0900 | [diff] [blame] | 635 | // defined in the source file. |
| 636 | class AidlDefinedType : public AidlAnnotatable { |
Steven Moreland | 787b043 | 2018-07-03 09:00:58 -0700 | [diff] [blame] | 637 | public: |
Steven Moreland | 46e9da8 | 2018-07-27 15:45:29 -0700 | [diff] [blame] | 638 | AidlDefinedType(const AidlLocation& location, const std::string& name, |
| 639 | const std::string& comments, const std::vector<std::string>& package); |
Steven Moreland | 787b043 | 2018-07-03 09:00:58 -0700 | [diff] [blame] | 640 | virtual ~AidlDefinedType() = default; |
| 641 | |
Jiyong Park | 1deecc3 | 2018-07-17 01:14:41 +0900 | [diff] [blame] | 642 | const std::string& GetName() const { return name_; }; |
Jeongik Cha | 997281d | 2020-01-16 15:23:59 +0900 | [diff] [blame] | 643 | bool IsHidden() const; |
Jiyong Park | 1deecc3 | 2018-07-17 01:14:41 +0900 | [diff] [blame] | 644 | const std::string& GetComments() const { return comments_; } |
Jiyong Park | a6605ab | 2018-11-11 14:30:21 +0900 | [diff] [blame] | 645 | void SetComments(const std::string comments) { comments_ = comments; } |
Jiyong Park | 1deecc3 | 2018-07-17 01:14:41 +0900 | [diff] [blame] | 646 | |
Steven Moreland | 787b043 | 2018-07-03 09:00:58 -0700 | [diff] [blame] | 647 | /* dot joined package, example: "android.package.foo" */ |
| 648 | std::string GetPackage() const; |
| 649 | /* dot joined package and name, example: "android.package.foo.IBar" */ |
| 650 | std::string GetCanonicalName() const; |
| 651 | const std::vector<std::string>& GetSplitPackage() const { return package_; } |
| 652 | |
Steven Moreland | ed83a28 | 2018-07-17 13:27:29 -0700 | [diff] [blame] | 653 | virtual std::string GetPreprocessDeclarationName() const = 0; |
Steven Moreland | c258abc | 2018-07-10 14:03:38 -0700 | [diff] [blame] | 654 | |
Steven Moreland | 5557f1c | 2018-07-02 13:50:23 -0700 | [diff] [blame] | 655 | virtual const AidlStructuredParcelable* AsStructuredParcelable() const { return nullptr; } |
Steven Moreland | c258abc | 2018-07-10 14:03:38 -0700 | [diff] [blame] | 656 | virtual const AidlParcelable* AsParcelable() const { return nullptr; } |
Daniel Norman | 85aed54 | 2019-08-21 12:01:14 -0700 | [diff] [blame] | 657 | virtual const AidlEnumDeclaration* AsEnumDeclaration() const { return nullptr; } |
Steven Moreland | 5557f1c | 2018-07-02 13:50:23 -0700 | [diff] [blame] | 658 | virtual const AidlInterface* AsInterface() const { return nullptr; } |
Jeongik Cha | df76dc7 | 2019-11-28 00:08:47 +0900 | [diff] [blame] | 659 | virtual const AidlParameterizable<std::string>* AsParameterizable() const { return nullptr; } |
Andrei Onea | 9445fc6 | 2019-06-27 18:11:59 +0100 | [diff] [blame] | 660 | virtual bool CheckValid(const AidlTypenames&) const { return CheckValidAnnotations(); } |
Jeongik Cha | 047c5ee | 2019-08-07 23:16:49 +0900 | [diff] [blame] | 661 | virtual bool LanguageSpecificCheckValid(Options::Language lang) const = 0; |
Steven Moreland | c258abc | 2018-07-10 14:03:38 -0700 | [diff] [blame] | 662 | AidlStructuredParcelable* AsStructuredParcelable() { |
| 663 | return const_cast<AidlStructuredParcelable*>( |
| 664 | const_cast<const AidlDefinedType*>(this)->AsStructuredParcelable()); |
| 665 | } |
| 666 | AidlParcelable* AsParcelable() { |
| 667 | return const_cast<AidlParcelable*>(const_cast<const AidlDefinedType*>(this)->AsParcelable()); |
| 668 | } |
Daniel Norman | 85aed54 | 2019-08-21 12:01:14 -0700 | [diff] [blame] | 669 | AidlEnumDeclaration* AsEnumDeclaration() { |
| 670 | return const_cast<AidlEnumDeclaration*>( |
| 671 | const_cast<const AidlDefinedType*>(this)->AsEnumDeclaration()); |
| 672 | } |
Steven Moreland | c258abc | 2018-07-10 14:03:38 -0700 | [diff] [blame] | 673 | AidlInterface* AsInterface() { |
| 674 | return const_cast<AidlInterface*>(const_cast<const AidlDefinedType*>(this)->AsInterface()); |
| 675 | } |
| 676 | |
Jeongik Cha | df76dc7 | 2019-11-28 00:08:47 +0900 | [diff] [blame] | 677 | AidlParameterizable<std::string>* AsParameterizable() { |
| 678 | return const_cast<AidlParameterizable<std::string>*>( |
| 679 | const_cast<const AidlDefinedType*>(this)->AsParameterizable()); |
| 680 | } |
| 681 | |
Steven Moreland | 6cee348 | 2018-07-18 14:39:58 -0700 | [diff] [blame] | 682 | const AidlParcelable* AsUnstructuredParcelable() const { |
| 683 | if (this->AsStructuredParcelable() != nullptr) return nullptr; |
| 684 | return this->AsParcelable(); |
| 685 | } |
| 686 | AidlParcelable* AsUnstructuredParcelable() { |
| 687 | return const_cast<AidlParcelable*>( |
| 688 | const_cast<const AidlDefinedType*>(this)->AsUnstructuredParcelable()); |
| 689 | } |
| 690 | |
Jeongik Cha | 997281d | 2020-01-16 15:23:59 +0900 | [diff] [blame] | 691 | virtual void Dump(CodeWriter* writer) const = 0; |
Jiyong Park | 02da742 | 2018-07-16 16:00:26 +0900 | [diff] [blame] | 692 | |
Steven Moreland | 787b043 | 2018-07-03 09:00:58 -0700 | [diff] [blame] | 693 | private: |
Jiyong Park | 1deecc3 | 2018-07-17 01:14:41 +0900 | [diff] [blame] | 694 | std::string name_; |
Jiyong Park | 1deecc3 | 2018-07-17 01:14:41 +0900 | [diff] [blame] | 695 | std::string comments_; |
Steven Moreland | 787b043 | 2018-07-03 09:00:58 -0700 | [diff] [blame] | 696 | const std::vector<std::string> package_; |
Steven Moreland | 5557f1c | 2018-07-02 13:50:23 -0700 | [diff] [blame] | 697 | |
| 698 | DISALLOW_COPY_AND_ASSIGN(AidlDefinedType); |
Steven Moreland | 787b043 | 2018-07-03 09:00:58 -0700 | [diff] [blame] | 699 | }; |
| 700 | |
Jeongik Cha | df76dc7 | 2019-11-28 00:08:47 +0900 | [diff] [blame] | 701 | class AidlParcelable : public AidlDefinedType, public AidlParameterizable<std::string> { |
Casey Dahlin | 1ae2bc5 | 2015-10-07 18:49:10 -0700 | [diff] [blame] | 702 | public: |
Steven Moreland | 46e9da8 | 2018-07-27 15:45:29 -0700 | [diff] [blame] | 703 | AidlParcelable(const AidlLocation& location, AidlQualifiedName* name, |
Jiyong Park | a6605ab | 2018-11-11 14:30:21 +0900 | [diff] [blame] | 704 | const std::vector<std::string>& package, const std::string& comments, |
Jeongik Cha | df76dc7 | 2019-11-28 00:08:47 +0900 | [diff] [blame] | 705 | const std::string& cpp_header = "", |
| 706 | std::vector<std::string>* type_params = nullptr); |
Casey Dahlin | 1ae2bc5 | 2015-10-07 18:49:10 -0700 | [diff] [blame] | 707 | virtual ~AidlParcelable() = default; |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 708 | |
Ningyuan Wang | d17c58b | 2016-09-29 14:33:14 -0700 | [diff] [blame] | 709 | // C++ uses "::" instead of "." to refer to a inner class. |
| 710 | std::string GetCppName() const { return name_->GetColonName(); } |
Christopher Wiley | 8aa4d9f | 2015-11-16 19:10:45 -0800 | [diff] [blame] | 711 | std::string GetCppHeader() const { return cpp_header_; } |
Christopher Wiley | 8aa4d9f | 2015-11-16 19:10:45 -0800 | [diff] [blame] | 712 | |
Jeongik Cha | 82317dd | 2019-02-27 20:26:11 +0900 | [diff] [blame] | 713 | bool CheckValid(const AidlTypenames& typenames) const override; |
Jeongik Cha | 047c5ee | 2019-08-07 23:16:49 +0900 | [diff] [blame] | 714 | bool LanguageSpecificCheckValid(Options::Language lang) const override; |
Steven Moreland | c258abc | 2018-07-10 14:03:38 -0700 | [diff] [blame] | 715 | const AidlParcelable* AsParcelable() const override { return this; } |
Jeongik Cha | df76dc7 | 2019-11-28 00:08:47 +0900 | [diff] [blame] | 716 | const AidlParameterizable<std::string>* AsParameterizable() const override { return this; } |
| 717 | const AidlNode& AsAidlNode() const override { return *this; } |
Steven Moreland | ed83a28 | 2018-07-17 13:27:29 -0700 | [diff] [blame] | 718 | std::string GetPreprocessDeclarationName() const override { return "parcelable"; } |
Steven Moreland | c258abc | 2018-07-10 14:03:38 -0700 | [diff] [blame] | 719 | |
Jeongik Cha | 997281d | 2020-01-16 15:23:59 +0900 | [diff] [blame] | 720 | void Dump(CodeWriter* writer) const override; |
Jiyong Park | 02da742 | 2018-07-16 16:00:26 +0900 | [diff] [blame] | 721 | |
Casey Dahlin | 1ae2bc5 | 2015-10-07 18:49:10 -0700 | [diff] [blame] | 722 | private: |
Christopher Wiley | 8aa4d9f | 2015-11-16 19:10:45 -0800 | [diff] [blame] | 723 | std::unique_ptr<AidlQualifiedName> name_; |
Christopher Wiley | 8aa4d9f | 2015-11-16 19:10:45 -0800 | [diff] [blame] | 724 | std::string cpp_header_; |
Casey Dahlin | 59401da | 2015-10-09 18:16:45 -0700 | [diff] [blame] | 725 | |
Casey Dahlin | 1ae2bc5 | 2015-10-07 18:49:10 -0700 | [diff] [blame] | 726 | DISALLOW_COPY_AND_ASSIGN(AidlParcelable); |
Casey Dahlin | 0a2f8be | 2015-09-28 16:15:29 -0700 | [diff] [blame] | 727 | }; |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 728 | |
Steven Moreland | 5557f1c | 2018-07-02 13:50:23 -0700 | [diff] [blame] | 729 | class AidlStructuredParcelable : public AidlParcelable { |
| 730 | public: |
Steven Moreland | 46e9da8 | 2018-07-27 15:45:29 -0700 | [diff] [blame] | 731 | AidlStructuredParcelable(const AidlLocation& location, AidlQualifiedName* name, |
Jiyong Park | a6605ab | 2018-11-11 14:30:21 +0900 | [diff] [blame] | 732 | const std::vector<std::string>& package, const std::string& comments, |
Steven Moreland | 5557f1c | 2018-07-02 13:50:23 -0700 | [diff] [blame] | 733 | std::vector<std::unique_ptr<AidlVariableDeclaration>>* variables); |
| 734 | |
| 735 | const std::vector<std::unique_ptr<AidlVariableDeclaration>>& GetFields() const { |
| 736 | return variables_; |
| 737 | } |
| 738 | |
Steven Moreland | c258abc | 2018-07-10 14:03:38 -0700 | [diff] [blame] | 739 | const AidlStructuredParcelable* AsStructuredParcelable() const override { return this; } |
Steven Moreland | ed83a28 | 2018-07-17 13:27:29 -0700 | [diff] [blame] | 740 | std::string GetPreprocessDeclarationName() const override { return "structured_parcelable"; } |
Steven Moreland | 5557f1c | 2018-07-02 13:50:23 -0700 | [diff] [blame] | 741 | |
Jeongik Cha | 997281d | 2020-01-16 15:23:59 +0900 | [diff] [blame] | 742 | void Dump(CodeWriter* writer) const override; |
Jiyong Park | 02da742 | 2018-07-16 16:00:26 +0900 | [diff] [blame] | 743 | |
Jeongik Cha | db0f59e | 2018-11-01 18:11:21 +0900 | [diff] [blame] | 744 | bool CheckValid(const AidlTypenames& typenames) const override; |
Jeongik Cha | 047c5ee | 2019-08-07 23:16:49 +0900 | [diff] [blame] | 745 | bool LanguageSpecificCheckValid(Options::Language lang) const override; |
Jeongik Cha | db0f59e | 2018-11-01 18:11:21 +0900 | [diff] [blame] | 746 | |
Steven Moreland | 5557f1c | 2018-07-02 13:50:23 -0700 | [diff] [blame] | 747 | private: |
| 748 | const std::vector<std::unique_ptr<AidlVariableDeclaration>> variables_; |
| 749 | |
| 750 | DISALLOW_COPY_AND_ASSIGN(AidlStructuredParcelable); |
| 751 | }; |
| 752 | |
Daniel Norman | 85aed54 | 2019-08-21 12:01:14 -0700 | [diff] [blame] | 753 | class AidlEnumerator : public AidlNode { |
| 754 | public: |
Daniel Norman | 2e4112d | 2019-10-03 10:22:35 -0700 | [diff] [blame] | 755 | AidlEnumerator(const AidlLocation& location, const std::string& name, AidlConstantValue* value, |
| 756 | const std::string& comments); |
Daniel Norman | 85aed54 | 2019-08-21 12:01:14 -0700 | [diff] [blame] | 757 | virtual ~AidlEnumerator() = default; |
| 758 | |
| 759 | const std::string& GetName() const { return name_; } |
Will McVicker | d7d18df | 2019-09-12 13:40:50 -0700 | [diff] [blame] | 760 | AidlConstantValue* GetValue() const { return value_.get(); } |
Daniel Norman | 2e4112d | 2019-10-03 10:22:35 -0700 | [diff] [blame] | 761 | const std::string& GetComments() const { return comments_; } |
Daniel Norman | 85aed54 | 2019-08-21 12:01:14 -0700 | [diff] [blame] | 762 | bool CheckValid(const AidlTypeSpecifier& enum_backing_type) const; |
| 763 | |
| 764 | string ValueString(const AidlTypeSpecifier& backing_type, |
| 765 | const ConstantValueDecorator& decorator) const; |
| 766 | |
Daniel Norman | b28684e | 2019-10-17 15:31:39 -0700 | [diff] [blame] | 767 | void SetValue(std::unique_ptr<AidlConstantValue> value) { value_ = std::move(value); } |
| 768 | |
Daniel Norman | 85aed54 | 2019-08-21 12:01:14 -0700 | [diff] [blame] | 769 | private: |
| 770 | const std::string name_; |
Will McVicker | d7d18df | 2019-09-12 13:40:50 -0700 | [diff] [blame] | 771 | unique_ptr<AidlConstantValue> value_; |
Daniel Norman | 2e4112d | 2019-10-03 10:22:35 -0700 | [diff] [blame] | 772 | const std::string comments_; |
Daniel Norman | 85aed54 | 2019-08-21 12:01:14 -0700 | [diff] [blame] | 773 | |
| 774 | DISALLOW_COPY_AND_ASSIGN(AidlEnumerator); |
| 775 | }; |
| 776 | |
| 777 | class AidlEnumDeclaration : public AidlDefinedType { |
| 778 | public: |
Will McVicker | d7d18df | 2019-09-12 13:40:50 -0700 | [diff] [blame] | 779 | AidlEnumDeclaration(const AidlLocation& location, const string& name, |
Daniel Norman | 85aed54 | 2019-08-21 12:01:14 -0700 | [diff] [blame] | 780 | std::vector<std::unique_ptr<AidlEnumerator>>* enumerators, |
Daniel Norman | 2e4112d | 2019-10-03 10:22:35 -0700 | [diff] [blame] | 781 | const std::vector<std::string>& package, const std::string& comments); |
Daniel Norman | 85aed54 | 2019-08-21 12:01:14 -0700 | [diff] [blame] | 782 | virtual ~AidlEnumDeclaration() = default; |
| 783 | |
| 784 | void SetBackingType(std::unique_ptr<const AidlTypeSpecifier> type); |
| 785 | const AidlTypeSpecifier& GetBackingType() const { return *backing_type_; } |
| 786 | const std::vector<std::unique_ptr<AidlEnumerator>>& GetEnumerators() const { |
| 787 | return enumerators_; |
| 788 | } |
Steven Moreland | 59e53e4 | 2019-11-26 20:38:08 -0800 | [diff] [blame] | 789 | bool Autofill(); |
Daniel Norman | 85aed54 | 2019-08-21 12:01:14 -0700 | [diff] [blame] | 790 | bool CheckValid(const AidlTypenames& typenames) const override; |
| 791 | bool LanguageSpecificCheckValid(Options::Language) const override { return true; } |
| 792 | std::string GetPreprocessDeclarationName() const override { return "enum"; } |
Jeongik Cha | 997281d | 2020-01-16 15:23:59 +0900 | [diff] [blame] | 793 | void Dump(CodeWriter* writer) const override; |
Daniel Norman | 85aed54 | 2019-08-21 12:01:14 -0700 | [diff] [blame] | 794 | |
| 795 | const AidlEnumDeclaration* AsEnumDeclaration() const override { return this; } |
| 796 | |
| 797 | private: |
| 798 | const std::string name_; |
| 799 | const std::vector<std::unique_ptr<AidlEnumerator>> enumerators_; |
| 800 | std::unique_ptr<const AidlTypeSpecifier> backing_type_; |
| 801 | |
| 802 | DISALLOW_COPY_AND_ASSIGN(AidlEnumDeclaration); |
| 803 | }; |
| 804 | |
Jiyong Park | 1deecc3 | 2018-07-17 01:14:41 +0900 | [diff] [blame] | 805 | class AidlInterface final : public AidlDefinedType { |
Casey Dahlin | 1ae2bc5 | 2015-10-07 18:49:10 -0700 | [diff] [blame] | 806 | public: |
Steven Moreland | 46e9da8 | 2018-07-27 15:45:29 -0700 | [diff] [blame] | 807 | AidlInterface(const AidlLocation& location, const std::string& name, const std::string& comments, |
| 808 | bool oneway_, std::vector<std::unique_ptr<AidlMember>>* members, |
Christopher Wiley | d76067c | 2015-10-19 17:00:13 -0700 | [diff] [blame] | 809 | const std::vector<std::string>& package); |
Casey Dahlin | 1ae2bc5 | 2015-10-07 18:49:10 -0700 | [diff] [blame] | 810 | virtual ~AidlInterface() = default; |
| 811 | |
Casey Dahlin | fb7da2e | 2015-10-08 17:26:09 -0700 | [diff] [blame] | 812 | const std::vector<std::unique_ptr<AidlMethod>>& GetMethods() const |
| 813 | { return methods_; } |
Jiyong Park | 309668e | 2018-07-28 16:55:44 +0900 | [diff] [blame] | 814 | std::vector<std::unique_ptr<AidlMethod>>& GetMutableMethods() { return methods_; } |
Steven Moreland | 693640b | 2018-07-19 13:46:27 -0700 | [diff] [blame] | 815 | const std::vector<std::unique_ptr<AidlConstantDeclaration>>& GetConstantDeclarations() const { |
| 816 | return constants_; |
| 817 | } |
Casey Dahlin | a2f77c4 | 2015-12-01 18:26:02 -0800 | [diff] [blame] | 818 | |
Steven Moreland | c258abc | 2018-07-10 14:03:38 -0700 | [diff] [blame] | 819 | const AidlInterface* AsInterface() const override { return this; } |
Steven Moreland | ed83a28 | 2018-07-17 13:27:29 -0700 | [diff] [blame] | 820 | std::string GetPreprocessDeclarationName() const override { return "interface"; } |
Steven Moreland | 5557f1c | 2018-07-02 13:50:23 -0700 | [diff] [blame] | 821 | |
Jeongik Cha | 997281d | 2020-01-16 15:23:59 +0900 | [diff] [blame] | 822 | void Dump(CodeWriter* writer) const override; |
Jiyong Park | 02da742 | 2018-07-16 16:00:26 +0900 | [diff] [blame] | 823 | |
Jeongik Cha | db0f59e | 2018-11-01 18:11:21 +0900 | [diff] [blame] | 824 | bool CheckValid(const AidlTypenames& typenames) const override; |
Jeongik Cha | 047c5ee | 2019-08-07 23:16:49 +0900 | [diff] [blame] | 825 | bool LanguageSpecificCheckValid(Options::Language lang) const override; |
Jeongik Cha | db0f59e | 2018-11-01 18:11:21 +0900 | [diff] [blame] | 826 | |
Casey Dahlin | 1ae2bc5 | 2015-10-07 18:49:10 -0700 | [diff] [blame] | 827 | private: |
Casey Dahlin | fb7da2e | 2015-10-08 17:26:09 -0700 | [diff] [blame] | 828 | std::vector<std::unique_ptr<AidlMethod>> methods_; |
Steven Moreland | 693640b | 2018-07-19 13:46:27 -0700 | [diff] [blame] | 829 | std::vector<std::unique_ptr<AidlConstantDeclaration>> constants_; |
Casey Dahlin | fb7da2e | 2015-10-08 17:26:09 -0700 | [diff] [blame] | 830 | |
Casey Dahlin | 1ae2bc5 | 2015-10-07 18:49:10 -0700 | [diff] [blame] | 831 | DISALLOW_COPY_AND_ASSIGN(AidlInterface); |
Casey Dahlin | 0a2f8be | 2015-09-28 16:15:29 -0700 | [diff] [blame] | 832 | }; |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 833 | |
Casey Dahlin | 0edf342 | 2015-10-07 12:34:59 -0700 | [diff] [blame] | 834 | class AidlImport : public AidlNode { |
| 835 | public: |
Steven Moreland | 46e9da8 | 2018-07-27 15:45:29 -0700 | [diff] [blame] | 836 | AidlImport(const AidlLocation& location, const std::string& needed_class); |
Casey Dahlin | 0edf342 | 2015-10-07 12:34:59 -0700 | [diff] [blame] | 837 | virtual ~AidlImport() = default; |
| 838 | |
Casey Dahlin | 0edf342 | 2015-10-07 12:34:59 -0700 | [diff] [blame] | 839 | const std::string& GetFilename() const { return filename_; } |
| 840 | const std::string& GetNeededClass() const { return needed_class_; } |
Casey Dahlin | 0edf342 | 2015-10-07 12:34:59 -0700 | [diff] [blame] | 841 | |
| 842 | private: |
Casey Dahlin | 0edf342 | 2015-10-07 12:34:59 -0700 | [diff] [blame] | 843 | std::string filename_; |
| 844 | std::string needed_class_; |
Casey Dahlin | 0edf342 | 2015-10-07 12:34:59 -0700 | [diff] [blame] | 845 | |
| 846 | DISALLOW_COPY_AND_ASSIGN(AidlImport); |
Casey Dahlin | e250749 | 2015-09-14 17:11:20 -0700 | [diff] [blame] | 847 | }; |
| 848 | |
| 849 | class Parser { |
Casey Dahlin | dd69181 | 2015-09-09 17:59:06 -0700 | [diff] [blame] | 850 | public: |
Casey Dahlin | e250749 | 2015-09-14 17:11:20 -0700 | [diff] [blame] | 851 | ~Parser(); |
Casey Dahlin | dd69181 | 2015-09-09 17:59:06 -0700 | [diff] [blame] | 852 | |
Steven Moreland | 64e29be | 2018-08-08 18:52:19 -0700 | [diff] [blame] | 853 | // Parse contents of file |filename|. Should only be called once. |
| 854 | static std::unique_ptr<Parser> Parse(const std::string& filename, |
| 855 | const android::aidl::IoDelegate& io_delegate, |
| 856 | AidlTypenames& typenames); |
Casey Dahlin | 89d4484 | 2015-09-24 18:45:54 -0700 | [diff] [blame] | 857 | |
Steven Moreland | 2ca4fcb | 2018-06-27 16:01:01 -0700 | [diff] [blame] | 858 | void AddError() { error_++; } |
Steven Moreland | 64e29be | 2018-08-08 18:52:19 -0700 | [diff] [blame] | 859 | bool HasError() { return error_ != 0; } |
Casey Dahlin | dd69181 | 2015-09-09 17:59:06 -0700 | [diff] [blame] | 860 | |
Casey Dahlin | 3c6df36 | 2015-10-06 15:48:35 -0700 | [diff] [blame] | 861 | const std::string& FileName() const { return filename_; } |
Casey Dahlin | 42727f8 | 2015-10-12 19:23:40 -0700 | [diff] [blame] | 862 | void* Scanner() const { return scanner_; } |
Casey Dahlin | dd69181 | 2015-09-09 17:59:06 -0700 | [diff] [blame] | 863 | |
Steven Moreland | d1039a9 | 2020-01-23 09:49:43 -0800 | [diff] [blame] | 864 | void AddImport(std::unique_ptr<AidlImport>&& import); |
Christopher Wiley | d76067c | 2015-10-19 17:00:13 -0700 | [diff] [blame] | 865 | const std::vector<std::unique_ptr<AidlImport>>& GetImports() { |
| 866 | return imports_; |
| 867 | } |
Casey Dahlin | dd69181 | 2015-09-09 17:59:06 -0700 | [diff] [blame] | 868 | |
Jiyong Park | b034bf0 | 2018-07-30 17:44:33 +0900 | [diff] [blame] | 869 | void SetPackage(unique_ptr<AidlQualifiedName> name) { package_ = std::move(name); } |
| 870 | std::vector<std::string> Package() const; |
Jiyong Park | 1deecc3 | 2018-07-17 01:14:41 +0900 | [diff] [blame] | 871 | |
| 872 | void DeferResolution(AidlTypeSpecifier* typespec) { |
| 873 | unresolved_typespecs_.emplace_back(typespec); |
| 874 | } |
| 875 | |
Jiyong Park | e59c368 | 2018-09-11 23:10:25 +0900 | [diff] [blame] | 876 | const vector<AidlTypeSpecifier*>& GetUnresolvedTypespecs() const { return unresolved_typespecs_; } |
| 877 | |
Jiyong Park | 1deecc3 | 2018-07-17 01:14:41 +0900 | [diff] [blame] | 878 | bool Resolve(); |
| 879 | |
Jiyong Park | b034bf0 | 2018-07-30 17:44:33 +0900 | [diff] [blame] | 880 | void AddDefinedType(unique_ptr<AidlDefinedType> type) { |
| 881 | // Parser does NOT own AidlDefinedType, it just has references to the types |
| 882 | // that it encountered while parsing the input file. |
| 883 | defined_types_.emplace_back(type.get()); |
| 884 | |
| 885 | // AidlDefinedType IS owned by AidlTypenames |
| 886 | if (!typenames_.AddDefinedType(std::move(type))) { |
| 887 | AddError(); |
| 888 | } |
| 889 | } |
| 890 | |
| 891 | vector<AidlDefinedType*>& GetDefinedTypes() { return defined_types_; } |
| 892 | |
Casey Dahlin | dd69181 | 2015-09-09 17:59:06 -0700 | [diff] [blame] | 893 | private: |
Steven Moreland | 64e29be | 2018-08-08 18:52:19 -0700 | [diff] [blame] | 894 | explicit Parser(const std::string& filename, std::string& raw_buffer, |
| 895 | android::aidl::AidlTypenames& typenames); |
| 896 | |
Casey Dahlin | dd69181 | 2015-09-09 17:59:06 -0700 | [diff] [blame] | 897 | std::string filename_; |
Christopher Wiley | d76067c | 2015-10-19 17:00:13 -0700 | [diff] [blame] | 898 | std::unique_ptr<AidlQualifiedName> package_; |
Jiyong Park | b034bf0 | 2018-07-30 17:44:33 +0900 | [diff] [blame] | 899 | AidlTypenames& typenames_; |
Steven Moreland | 64e29be | 2018-08-08 18:52:19 -0700 | [diff] [blame] | 900 | |
| 901 | void* scanner_ = nullptr; |
| 902 | YY_BUFFER_STATE buffer_; |
| 903 | int error_ = 0; |
| 904 | |
| 905 | std::vector<std::unique_ptr<AidlImport>> imports_; |
Jiyong Park | b034bf0 | 2018-07-30 17:44:33 +0900 | [diff] [blame] | 906 | vector<AidlDefinedType*> defined_types_; |
Jiyong Park | 1deecc3 | 2018-07-17 01:14:41 +0900 | [diff] [blame] | 907 | vector<AidlTypeSpecifier*> unresolved_typespecs_; |
Casey Dahlin | dd69181 | 2015-09-09 17:59:06 -0700 | [diff] [blame] | 908 | |
Casey Dahlin | e250749 | 2015-09-14 17:11:20 -0700 | [diff] [blame] | 909 | DISALLOW_COPY_AND_ASSIGN(Parser); |
Casey Dahlin | dd69181 | 2015-09-09 17:59:06 -0700 | [diff] [blame] | 910 | }; |