Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 1 | #include "aidl_language.h" |
Christopher Wiley | f690be5 | 2015-09-14 15:19:10 -0700 | [diff] [blame] | 2 | |
| 3 | #include <iostream> |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 4 | #include <stdio.h> |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 5 | #include <stdlib.h> |
Christopher Wiley | 4a2884b | 2015-10-07 11:27:45 -0700 | [diff] [blame] | 6 | #include <string.h> |
Casey Dahlin | dd69181 | 2015-09-09 17:59:06 -0700 | [diff] [blame] | 7 | #include <string> |
Christopher Wiley | f690be5 | 2015-09-14 15:19:10 -0700 | [diff] [blame] | 8 | |
Roshan Pius | 9d7810a | 2016-07-28 08:57:50 -0700 | [diff] [blame^] | 9 | #include <android-base/parseint.h> |
Elliott Hughes | 0a62067 | 2015-12-04 13:53:18 -0800 | [diff] [blame] | 10 | #include <android-base/strings.h> |
Christopher Wiley | d76067c | 2015-10-19 17:00:13 -0700 | [diff] [blame] | 11 | |
Ying Wang | 3000e75 | 2016-01-11 18:05:59 -0800 | [diff] [blame] | 12 | #include "aidl_language_y.h" |
Christopher Wiley | 4a2884b | 2015-10-07 11:27:45 -0700 | [diff] [blame] | 13 | #include "logging.h" |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 14 | |
Casey Dahlin | 07b9dde | 2015-09-10 19:13:49 -0700 | [diff] [blame] | 15 | #ifdef _WIN32 |
| 16 | int isatty(int fd) |
| 17 | { |
| 18 | return (fd == 0); |
| 19 | } |
| 20 | #endif |
| 21 | |
Christopher Wiley | 4a2884b | 2015-10-07 11:27:45 -0700 | [diff] [blame] | 22 | using android::aidl::IoDelegate; |
Christopher Wiley | d76067c | 2015-10-19 17:00:13 -0700 | [diff] [blame] | 23 | using android::base::Join; |
Christopher Wiley | 8aa4d9f | 2015-11-16 19:10:45 -0800 | [diff] [blame] | 24 | using android::base::Split; |
Casey Dahlin | dd69181 | 2015-09-09 17:59:06 -0700 | [diff] [blame] | 25 | using std::cerr; |
| 26 | using std::endl; |
Christopher Wiley | 4a2884b | 2015-10-07 11:27:45 -0700 | [diff] [blame] | 27 | using std::string; |
| 28 | using std::unique_ptr; |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 29 | |
Casey Dahlin | dd69181 | 2015-09-09 17:59:06 -0700 | [diff] [blame] | 30 | void yylex_init(void **); |
| 31 | void yylex_destroy(void *); |
| 32 | void yyset_in(FILE *f, void *); |
Casey Dahlin | e250749 | 2015-09-14 17:11:20 -0700 | [diff] [blame] | 33 | int yyparse(Parser*); |
Christopher Wiley | 4a2884b | 2015-10-07 11:27:45 -0700 | [diff] [blame] | 34 | YY_BUFFER_STATE yy_scan_buffer(char *, size_t, void *); |
Casey Dahlin | 89d4484 | 2015-09-24 18:45:54 -0700 | [diff] [blame] | 35 | void yy_delete_buffer(YY_BUFFER_STATE, void *); |
Casey Dahlin | dd69181 | 2015-09-09 17:59:06 -0700 | [diff] [blame] | 36 | |
Casey Dahlin | cdbbc8c | 2015-10-14 15:31:04 -0700 | [diff] [blame] | 37 | AidlToken::AidlToken(const std::string& text, const std::string& comments) |
| 38 | : text_(text), |
| 39 | comments_(comments) {} |
Casey Dahlin | 98a544b | 2015-10-14 14:22:55 -0700 | [diff] [blame] | 40 | |
Casey Dahlin | f2d23f7 | 2015-10-02 16:19:19 -0700 | [diff] [blame] | 41 | AidlType::AidlType(const std::string& name, unsigned line, |
Casey Dahlin | f7a421c | 2015-10-05 17:24:28 -0700 | [diff] [blame] | 42 | const std::string& comments, bool is_array) |
Casey Dahlin | f2d23f7 | 2015-10-02 16:19:19 -0700 | [diff] [blame] | 43 | : name_(name), |
| 44 | line_(line), |
Casey Dahlin | f7a421c | 2015-10-05 17:24:28 -0700 | [diff] [blame] | 45 | is_array_(is_array), |
Casey Dahlin | f2d23f7 | 2015-10-02 16:19:19 -0700 | [diff] [blame] | 46 | comments_(comments) {} |
| 47 | |
Casey Dahlin | 98a544b | 2015-10-14 14:22:55 -0700 | [diff] [blame] | 48 | string AidlType::ToString() const { |
| 49 | return name_ + (is_array_ ? "[]" : ""); |
Casey Dahlin | 70078e6 | 2015-09-30 17:01:30 -0700 | [diff] [blame] | 50 | } |
| 51 | |
Casey Dahlin | 0ee3758 | 2015-09-30 16:31:55 -0700 | [diff] [blame] | 52 | AidlArgument::AidlArgument(AidlArgument::Direction direction, AidlType* type, |
Casey Dahlin | 308f9d4 | 2015-10-05 18:48:42 -0700 | [diff] [blame] | 53 | std::string name, unsigned line) |
Casey Dahlin | 0ee3758 | 2015-09-30 16:31:55 -0700 | [diff] [blame] | 54 | : type_(type), |
Casey Dahlin | fd6fb48 | 2015-09-30 14:48:18 -0700 | [diff] [blame] | 55 | direction_(direction), |
| 56 | direction_specified_(true), |
Casey Dahlin | 308f9d4 | 2015-10-05 18:48:42 -0700 | [diff] [blame] | 57 | name_(name), |
| 58 | line_(line) {} |
Casey Dahlin | c378c99 | 2015-09-29 16:50:40 -0700 | [diff] [blame] | 59 | |
Casey Dahlin | 308f9d4 | 2015-10-05 18:48:42 -0700 | [diff] [blame] | 60 | AidlArgument::AidlArgument(AidlType* type, std::string name, unsigned line) |
Casey Dahlin | 0ee3758 | 2015-09-30 16:31:55 -0700 | [diff] [blame] | 61 | : type_(type), |
Casey Dahlin | fd6fb48 | 2015-09-30 14:48:18 -0700 | [diff] [blame] | 62 | direction_(AidlArgument::IN_DIR), |
| 63 | direction_specified_(false), |
Casey Dahlin | 308f9d4 | 2015-10-05 18:48:42 -0700 | [diff] [blame] | 64 | name_(name), |
| 65 | line_(line) {} |
Casey Dahlin | c378c99 | 2015-09-29 16:50:40 -0700 | [diff] [blame] | 66 | |
| 67 | string AidlArgument::ToString() const { |
| 68 | string ret; |
| 69 | |
| 70 | if (direction_specified_) { |
| 71 | switch(direction_) { |
| 72 | case AidlArgument::IN_DIR: |
| 73 | ret += "in "; |
| 74 | break; |
| 75 | case AidlArgument::OUT_DIR: |
| 76 | ret += "out "; |
| 77 | break; |
| 78 | case AidlArgument::INOUT_DIR: |
| 79 | ret += "inout "; |
| 80 | break; |
| 81 | } |
| 82 | } |
| 83 | |
Casey Dahlin | 70078e6 | 2015-09-30 17:01:30 -0700 | [diff] [blame] | 84 | ret += type_->ToString(); |
Casey Dahlin | c378c99 | 2015-09-29 16:50:40 -0700 | [diff] [blame] | 85 | ret += " "; |
Casey Dahlin | d127b50 | 2015-09-30 12:51:08 -0700 | [diff] [blame] | 86 | ret += name_; |
Casey Dahlin | c378c99 | 2015-09-29 16:50:40 -0700 | [diff] [blame] | 87 | |
| 88 | return ret; |
| 89 | } |
Casey Dahlin | bc7a50a | 2015-09-28 19:20:50 -0700 | [diff] [blame] | 90 | |
Christopher Wiley | d6bdd8d | 2016-05-03 11:23:13 -0700 | [diff] [blame] | 91 | AidlIntConstant::AidlIntConstant(std::string name, int32_t value) |
Casey Dahlin | d40e2fe | 2015-11-24 14:06:52 -0800 | [diff] [blame] | 92 | : name_(name), |
Roshan Pius | 3b2203d | 2016-07-22 16:13:20 -0700 | [diff] [blame] | 93 | value_(value), |
| 94 | is_valid_(true) {} |
| 95 | |
| 96 | AidlIntConstant::AidlIntConstant(std::string name, |
| 97 | std::string value, |
| 98 | unsigned line_number) |
| 99 | : name_(name) { |
Roshan Pius | 9d7810a | 2016-07-28 08:57:50 -0700 | [diff] [blame^] | 100 | uint32_t unsigned_val; |
| 101 | if (!android::base::ParseUint(value.c_str(), &unsigned_val)) { |
Roshan Pius | 3b2203d | 2016-07-22 16:13:20 -0700 | [diff] [blame] | 102 | is_valid_ = false; |
| 103 | LOG(ERROR) << "Found invalid int value '" << value |
| 104 | << "' on line " << line_number; |
| 105 | } else { |
Roshan Pius | 9d7810a | 2016-07-28 08:57:50 -0700 | [diff] [blame^] | 106 | // Converting from unsigned to signed integer. |
| 107 | value_ = unsigned_val; |
Roshan Pius | 3b2203d | 2016-07-22 16:13:20 -0700 | [diff] [blame] | 108 | is_valid_ = true; |
| 109 | } |
| 110 | } |
Casey Dahlin | d40e2fe | 2015-11-24 14:06:52 -0800 | [diff] [blame] | 111 | |
Christopher Wiley | d6bdd8d | 2016-05-03 11:23:13 -0700 | [diff] [blame] | 112 | AidlStringConstant::AidlStringConstant(std::string name, |
| 113 | std::string value, |
| 114 | unsigned line_number) |
| 115 | : name_(name), |
| 116 | value_(value) { |
| 117 | is_valid_ = true; |
| 118 | for (size_t i = 0; i < value_.length(); ++i) { |
| 119 | const char& c = value_[i]; |
| 120 | if (c <= 0x1f || // control characters are < 0x20 |
| 121 | c >= 0x7f || // DEL is 0x7f |
| 122 | c == '\\') { // Disallow backslashes for future proofing. |
| 123 | LOG(ERROR) << "Found invalid character at index " << i |
| 124 | << " in string constant '" << value_ |
| 125 | << "' beginning on line " << line_number; |
| 126 | is_valid_ = false; |
| 127 | break; |
| 128 | } |
| 129 | } |
| 130 | } |
| 131 | |
Casey Dahlin | f4a9311 | 2015-10-05 16:58:09 -0700 | [diff] [blame] | 132 | AidlMethod::AidlMethod(bool oneway, AidlType* type, std::string name, |
| 133 | std::vector<std::unique_ptr<AidlArgument>>* args, |
Casey Dahlin | fb7da2e | 2015-10-08 17:26:09 -0700 | [diff] [blame] | 134 | unsigned line, const std::string& comments, int id) |
Casey Dahlin | f4a9311 | 2015-10-05 16:58:09 -0700 | [diff] [blame] | 135 | : oneway_(oneway), |
| 136 | comments_(comments), |
| 137 | type_(type), |
| 138 | name_(name), |
| 139 | line_(line), |
| 140 | arguments_(std::move(*args)), |
| 141 | id_(id) { |
| 142 | has_id_ = true; |
| 143 | delete args; |
Christopher Wiley | ad33927 | 2015-10-05 19:11:58 -0700 | [diff] [blame] | 144 | for (const unique_ptr<AidlArgument>& a : arguments_) { |
| 145 | if (a->IsIn()) { in_arguments_.push_back(a.get()); } |
| 146 | if (a->IsOut()) { out_arguments_.push_back(a.get()); } |
| 147 | } |
Casey Dahlin | f4a9311 | 2015-10-05 16:58:09 -0700 | [diff] [blame] | 148 | } |
| 149 | |
| 150 | AidlMethod::AidlMethod(bool oneway, AidlType* type, std::string name, |
| 151 | std::vector<std::unique_ptr<AidlArgument>>* args, |
Casey Dahlin | fb7da2e | 2015-10-08 17:26:09 -0700 | [diff] [blame] | 152 | unsigned line, const std::string& comments) |
Casey Dahlin | f4a9311 | 2015-10-05 16:58:09 -0700 | [diff] [blame] | 153 | : AidlMethod(oneway, type, name, args, line, comments, 0) { |
| 154 | has_id_ = false; |
| 155 | } |
Casey Dahlin | f2d23f7 | 2015-10-02 16:19:19 -0700 | [diff] [blame] | 156 | |
Christopher Wiley | 4a2884b | 2015-10-07 11:27:45 -0700 | [diff] [blame] | 157 | Parser::Parser(const IoDelegate& io_delegate) |
| 158 | : io_delegate_(io_delegate) { |
| 159 | yylex_init(&scanner_); |
| 160 | } |
| 161 | |
Casey Dahlin | 2b2879b | 2015-10-13 16:59:44 -0700 | [diff] [blame] | 162 | AidlParcelable::AidlParcelable(AidlQualifiedName* name, unsigned line, |
Christopher Wiley | 8aa4d9f | 2015-11-16 19:10:45 -0800 | [diff] [blame] | 163 | const std::vector<std::string>& package, |
| 164 | const std::string& cpp_header) |
Casey Dahlin | 59401da | 2015-10-09 18:16:45 -0700 | [diff] [blame] | 165 | : name_(name), |
| 166 | line_(line), |
Christopher Wiley | 8aa4d9f | 2015-11-16 19:10:45 -0800 | [diff] [blame] | 167 | package_(package), |
| 168 | cpp_header_(cpp_header) { |
| 169 | // Strip off quotation marks if we actually have a cpp header. |
| 170 | if (cpp_header_.length() >= 2) { |
| 171 | cpp_header_ = cpp_header_.substr(1, cpp_header_.length() - 2); |
| 172 | } |
Casey Dahlin | 59401da | 2015-10-09 18:16:45 -0700 | [diff] [blame] | 173 | } |
| 174 | |
Christopher Wiley | d76067c | 2015-10-19 17:00:13 -0700 | [diff] [blame] | 175 | std::string AidlParcelable::GetPackage() const { |
| 176 | return Join(package_, '.'); |
| 177 | } |
| 178 | |
Casey Dahlin | c1f39b4 | 2015-11-24 10:34:34 -0800 | [diff] [blame] | 179 | std::string AidlParcelable::GetCanonicalName() const { |
| 180 | if (package_.empty()) { |
| 181 | return GetName(); |
| 182 | } |
| 183 | return GetPackage() + "." + GetName(); |
| 184 | } |
| 185 | |
Casey Dahlin | fb7da2e | 2015-10-08 17:26:09 -0700 | [diff] [blame] | 186 | AidlInterface::AidlInterface(const std::string& name, unsigned line, |
| 187 | const std::string& comments, bool oneway, |
Casey Dahlin | d40e2fe | 2015-11-24 14:06:52 -0800 | [diff] [blame] | 188 | std::vector<std::unique_ptr<AidlMember>>* members, |
Christopher Wiley | d76067c | 2015-10-19 17:00:13 -0700 | [diff] [blame] | 189 | const std::vector<std::string>& package) |
Casey Dahlin | fb7da2e | 2015-10-08 17:26:09 -0700 | [diff] [blame] | 190 | : name_(name), |
| 191 | comments_(comments), |
| 192 | line_(line), |
| 193 | oneway_(oneway), |
Casey Dahlin | fb7da2e | 2015-10-08 17:26:09 -0700 | [diff] [blame] | 194 | package_(package) { |
Casey Dahlin | d40e2fe | 2015-11-24 14:06:52 -0800 | [diff] [blame] | 195 | for (auto& member : *members) { |
| 196 | AidlMember* local = member.release(); |
| 197 | AidlMethod* method = local->AsMethod(); |
Christopher Wiley | d6bdd8d | 2016-05-03 11:23:13 -0700 | [diff] [blame] | 198 | AidlIntConstant* int_constant = local->AsIntConstant(); |
| 199 | AidlStringConstant* string_constant = local->AsStringConstant(); |
Casey Dahlin | d40e2fe | 2015-11-24 14:06:52 -0800 | [diff] [blame] | 200 | |
| 201 | if (method) { |
| 202 | methods_.emplace_back(method); |
Christopher Wiley | d6bdd8d | 2016-05-03 11:23:13 -0700 | [diff] [blame] | 203 | } else if (int_constant) { |
| 204 | int_constants_.emplace_back(int_constant); |
| 205 | } else if (string_constant) { |
| 206 | string_constants_.emplace_back(string_constant); |
Casey Dahlin | d40e2fe | 2015-11-24 14:06:52 -0800 | [diff] [blame] | 207 | } else { |
| 208 | LOG(FATAL) << "Member is neither method nor constant!"; |
| 209 | } |
| 210 | } |
| 211 | |
| 212 | delete members; |
Casey Dahlin | fb7da2e | 2015-10-08 17:26:09 -0700 | [diff] [blame] | 213 | } |
| 214 | |
Christopher Wiley | d76067c | 2015-10-19 17:00:13 -0700 | [diff] [blame] | 215 | std::string AidlInterface::GetPackage() const { |
| 216 | return Join(package_, '.'); |
| 217 | } |
| 218 | |
| 219 | std::string AidlInterface::GetCanonicalName() const { |
| 220 | if (package_.empty()) { |
| 221 | return GetName(); |
| 222 | } |
| 223 | return GetPackage() + "." + GetName(); |
| 224 | } |
| 225 | |
Casey Dahlin | c1f39b4 | 2015-11-24 10:34:34 -0800 | [diff] [blame] | 226 | AidlDocument::AidlDocument(AidlInterface* interface) |
| 227 | : interface_(interface) {} |
| 228 | |
Casey Dahlin | 2b2879b | 2015-10-13 16:59:44 -0700 | [diff] [blame] | 229 | AidlQualifiedName::AidlQualifiedName(std::string term, |
| 230 | std::string comments) |
| 231 | : terms_({term}), |
| 232 | comments_(comments) { |
Christopher Wiley | 8aa4d9f | 2015-11-16 19:10:45 -0800 | [diff] [blame] | 233 | if (term.find('.') != string::npos) { |
| 234 | terms_ = Split(term, "."); |
| 235 | for (const auto& term: terms_) { |
| 236 | if (term.empty()) { |
| 237 | LOG(FATAL) << "Malformed qualified identifier: '" << term << "'"; |
| 238 | } |
| 239 | } |
| 240 | } |
Casey Dahlin | 2b2879b | 2015-10-13 16:59:44 -0700 | [diff] [blame] | 241 | } |
| 242 | |
Chih-Hung Hsieh | f05cc26 | 2016-07-27 11:42:51 -0700 | [diff] [blame] | 243 | void AidlQualifiedName::AddTerm(const std::string& term) { |
Casey Dahlin | 2b2879b | 2015-10-13 16:59:44 -0700 | [diff] [blame] | 244 | terms_.push_back(term); |
| 245 | } |
| 246 | |
Casey Dahlin | 0edf342 | 2015-10-07 12:34:59 -0700 | [diff] [blame] | 247 | AidlImport::AidlImport(const std::string& from, |
| 248 | const std::string& needed_class, unsigned line) |
| 249 | : from_(from), |
| 250 | needed_class_(needed_class), |
| 251 | line_(line) {} |
| 252 | |
Christopher Wiley | 4a2884b | 2015-10-07 11:27:45 -0700 | [diff] [blame] | 253 | Parser::~Parser() { |
| 254 | if (raw_buffer_) { |
| 255 | yy_delete_buffer(buffer_, scanner_); |
| 256 | raw_buffer_.reset(); |
| 257 | } |
| 258 | yylex_destroy(scanner_); |
| 259 | } |
| 260 | |
| 261 | bool Parser::ParseFile(const string& filename) { |
| 262 | // Make sure we can read the file first, before trashing previous state. |
| 263 | unique_ptr<string> new_buffer = io_delegate_.GetFileContents(filename); |
| 264 | if (!new_buffer) { |
| 265 | LOG(ERROR) << "Error while opening file for parsing: '" << filename << "'"; |
| 266 | return false; |
| 267 | } |
| 268 | |
| 269 | // Throw away old parsing state if we have any. |
| 270 | if (raw_buffer_) { |
| 271 | yy_delete_buffer(buffer_, scanner_); |
| 272 | raw_buffer_.reset(); |
| 273 | } |
| 274 | |
| 275 | raw_buffer_ = std::move(new_buffer); |
| 276 | // We're going to scan this buffer in place, and yacc demands we put two |
| 277 | // nulls at the end. |
| 278 | raw_buffer_->append(2u, '\0'); |
| 279 | filename_ = filename; |
Christopher Wiley | d76067c | 2015-10-19 17:00:13 -0700 | [diff] [blame] | 280 | package_.reset(); |
Christopher Wiley | 4a2884b | 2015-10-07 11:27:45 -0700 | [diff] [blame] | 281 | error_ = 0; |
Casey Dahlin | c1f39b4 | 2015-11-24 10:34:34 -0800 | [diff] [blame] | 282 | document_.reset(); |
Christopher Wiley | 4a2884b | 2015-10-07 11:27:45 -0700 | [diff] [blame] | 283 | |
| 284 | buffer_ = yy_scan_buffer(&(*raw_buffer_)[0], raw_buffer_->length(), scanner_); |
| 285 | |
Casey Dahlin | c1f39b4 | 2015-11-24 10:34:34 -0800 | [diff] [blame] | 286 | if (yy::parser(this).parse() != 0 || error_ != 0) { |
| 287 | return false;} |
Christopher Wiley | 4a2884b | 2015-10-07 11:27:45 -0700 | [diff] [blame] | 288 | |
Casey Dahlin | c1f39b4 | 2015-11-24 10:34:34 -0800 | [diff] [blame] | 289 | if (document_.get() != nullptr) |
| 290 | return true; |
| 291 | |
| 292 | LOG(ERROR) << "Parser succeeded but yielded no document!"; |
| 293 | return false; |
Christopher Wiley | 4a2884b | 2015-10-07 11:27:45 -0700 | [diff] [blame] | 294 | } |
| 295 | |
Casey Dahlin | 98a544b | 2015-10-14 14:22:55 -0700 | [diff] [blame] | 296 | void Parser::ReportError(const string& err, unsigned line) { |
| 297 | cerr << filename_ << ":" << line << ": " << err << endl; |
Casey Dahlin | dd69181 | 2015-09-09 17:59:06 -0700 | [diff] [blame] | 298 | error_ = 1; |
| 299 | } |
| 300 | |
Christopher Wiley | 90be4e3 | 2015-10-20 14:55:25 -0700 | [diff] [blame] | 301 | std::vector<std::string> Parser::Package() const { |
| 302 | if (!package_) { |
| 303 | return {}; |
| 304 | } |
| 305 | return package_->GetTerms(); |
| 306 | } |
| 307 | |
Casey Dahlin | 2b2879b | 2015-10-13 16:59:44 -0700 | [diff] [blame] | 308 | void Parser::AddImport(AidlQualifiedName* name, unsigned line) { |
| 309 | imports_.emplace_back(new AidlImport(this->FileName(), |
| 310 | name->GetDotName(), line)); |
| 311 | delete name; |
Casey Dahlin | e250749 | 2015-09-14 17:11:20 -0700 | [diff] [blame] | 312 | } |