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