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