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