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