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 | |
| 9 | #include "aidl_language_y.hpp" |
Christopher Wiley | 4a2884b | 2015-10-07 11:27:45 -0700 | [diff] [blame] | 10 | #include "logging.h" |
Christopher Wiley | f690be5 | 2015-09-14 15:19:10 -0700 | [diff] [blame] | 11 | #include "parse_helpers.h" |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 12 | |
Casey Dahlin | 07b9dde | 2015-09-10 19:13:49 -0700 | [diff] [blame] | 13 | #ifdef _WIN32 |
| 14 | int isatty(int fd) |
| 15 | { |
| 16 | return (fd == 0); |
| 17 | } |
| 18 | #endif |
| 19 | |
Christopher Wiley | 4a2884b | 2015-10-07 11:27:45 -0700 | [diff] [blame] | 20 | using android::aidl::IoDelegate; |
| 21 | using android::aidl::cpp_strdup; |
Casey Dahlin | dd69181 | 2015-09-09 17:59:06 -0700 | [diff] [blame] | 22 | using std::cerr; |
| 23 | using std::endl; |
Christopher Wiley | 4a2884b | 2015-10-07 11:27:45 -0700 | [diff] [blame] | 24 | using std::string; |
| 25 | using std::unique_ptr; |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 26 | |
Casey Dahlin | dd69181 | 2015-09-09 17:59:06 -0700 | [diff] [blame] | 27 | void yylex_init(void **); |
| 28 | void yylex_destroy(void *); |
| 29 | void yyset_in(FILE *f, void *); |
Casey Dahlin | e250749 | 2015-09-14 17:11:20 -0700 | [diff] [blame] | 30 | int yyparse(Parser*); |
Christopher Wiley | 4a2884b | 2015-10-07 11:27:45 -0700 | [diff] [blame] | 31 | YY_BUFFER_STATE yy_scan_buffer(char *, size_t, void *); |
Casey Dahlin | 89d4484 | 2015-09-24 18:45:54 -0700 | [diff] [blame] | 32 | void yy_delete_buffer(YY_BUFFER_STATE, void *); |
Casey Dahlin | dd69181 | 2015-09-09 17:59:06 -0700 | [diff] [blame] | 33 | |
Casey Dahlin | f2d23f7 | 2015-10-02 16:19:19 -0700 | [diff] [blame] | 34 | AidlType::AidlType(const std::string& name, unsigned line, |
Casey Dahlin | f7a421c | 2015-10-05 17:24:28 -0700 | [diff] [blame] | 35 | const std::string& comments, bool is_array) |
Casey Dahlin | f2d23f7 | 2015-10-02 16:19:19 -0700 | [diff] [blame] | 36 | : name_(name), |
| 37 | line_(line), |
Casey Dahlin | f7a421c | 2015-10-05 17:24:28 -0700 | [diff] [blame] | 38 | is_array_(is_array), |
Casey Dahlin | f2d23f7 | 2015-10-02 16:19:19 -0700 | [diff] [blame] | 39 | comments_(comments) {} |
| 40 | |
Christopher Wiley | 4a2884b | 2015-10-07 11:27:45 -0700 | [diff] [blame] | 41 | string AidlType::ToString() const { return name_ + (is_array_ ? "[]" : ""); |
Casey Dahlin | 70078e6 | 2015-09-30 17:01:30 -0700 | [diff] [blame] | 42 | } |
| 43 | |
Casey Dahlin | 0ee3758 | 2015-09-30 16:31:55 -0700 | [diff] [blame] | 44 | AidlArgument::AidlArgument(AidlArgument::Direction direction, AidlType* type, |
Casey Dahlin | 308f9d4 | 2015-10-05 18:48:42 -0700 | [diff] [blame] | 45 | std::string name, unsigned line) |
Casey Dahlin | 0ee3758 | 2015-09-30 16:31:55 -0700 | [diff] [blame] | 46 | : type_(type), |
Casey Dahlin | fd6fb48 | 2015-09-30 14:48:18 -0700 | [diff] [blame] | 47 | direction_(direction), |
| 48 | direction_specified_(true), |
Casey Dahlin | 308f9d4 | 2015-10-05 18:48:42 -0700 | [diff] [blame] | 49 | name_(name), |
| 50 | line_(line) {} |
Casey Dahlin | c378c99 | 2015-09-29 16:50:40 -0700 | [diff] [blame] | 51 | |
Casey Dahlin | 308f9d4 | 2015-10-05 18:48:42 -0700 | [diff] [blame] | 52 | AidlArgument::AidlArgument(AidlType* type, 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_(AidlArgument::IN_DIR), |
| 55 | direction_specified_(false), |
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 | |
| 59 | string AidlArgument::ToString() const { |
| 60 | string ret; |
| 61 | |
| 62 | if (direction_specified_) { |
| 63 | switch(direction_) { |
| 64 | case AidlArgument::IN_DIR: |
| 65 | ret += "in "; |
| 66 | break; |
| 67 | case AidlArgument::OUT_DIR: |
| 68 | ret += "out "; |
| 69 | break; |
| 70 | case AidlArgument::INOUT_DIR: |
| 71 | ret += "inout "; |
| 72 | break; |
| 73 | } |
| 74 | } |
| 75 | |
Casey Dahlin | 70078e6 | 2015-09-30 17:01:30 -0700 | [diff] [blame] | 76 | ret += type_->ToString(); |
Casey Dahlin | c378c99 | 2015-09-29 16:50:40 -0700 | [diff] [blame] | 77 | ret += " "; |
Casey Dahlin | d127b50 | 2015-09-30 12:51:08 -0700 | [diff] [blame] | 78 | ret += name_; |
Casey Dahlin | c378c99 | 2015-09-29 16:50:40 -0700 | [diff] [blame] | 79 | |
| 80 | return ret; |
| 81 | } |
Casey Dahlin | bc7a50a | 2015-09-28 19:20:50 -0700 | [diff] [blame] | 82 | |
Casey Dahlin | f4a9311 | 2015-10-05 16:58:09 -0700 | [diff] [blame] | 83 | AidlMethod::AidlMethod(bool oneway, AidlType* type, std::string name, |
| 84 | std::vector<std::unique_ptr<AidlArgument>>* args, |
Casey Dahlin | fb7da2e | 2015-10-08 17:26:09 -0700 | [diff] [blame] | 85 | unsigned line, const std::string& comments, int id) |
Casey Dahlin | f4a9311 | 2015-10-05 16:58:09 -0700 | [diff] [blame] | 86 | : oneway_(oneway), |
| 87 | comments_(comments), |
| 88 | type_(type), |
| 89 | name_(name), |
| 90 | line_(line), |
| 91 | arguments_(std::move(*args)), |
| 92 | id_(id) { |
| 93 | has_id_ = true; |
| 94 | delete args; |
Christopher Wiley | ad33927 | 2015-10-05 19:11:58 -0700 | [diff] [blame] | 95 | for (const unique_ptr<AidlArgument>& a : arguments_) { |
| 96 | if (a->IsIn()) { in_arguments_.push_back(a.get()); } |
| 97 | if (a->IsOut()) { out_arguments_.push_back(a.get()); } |
| 98 | } |
Casey Dahlin | f4a9311 | 2015-10-05 16:58:09 -0700 | [diff] [blame] | 99 | } |
| 100 | |
| 101 | AidlMethod::AidlMethod(bool oneway, AidlType* type, std::string name, |
| 102 | std::vector<std::unique_ptr<AidlArgument>>* args, |
Casey Dahlin | fb7da2e | 2015-10-08 17:26:09 -0700 | [diff] [blame] | 103 | unsigned line, const std::string& comments) |
Casey Dahlin | f4a9311 | 2015-10-05 16:58:09 -0700 | [diff] [blame] | 104 | : AidlMethod(oneway, type, name, args, line, comments, 0) { |
| 105 | has_id_ = false; |
| 106 | } |
Casey Dahlin | f2d23f7 | 2015-10-02 16:19:19 -0700 | [diff] [blame] | 107 | |
Christopher Wiley | 4a2884b | 2015-10-07 11:27:45 -0700 | [diff] [blame] | 108 | Parser::Parser(const IoDelegate& io_delegate) |
| 109 | : io_delegate_(io_delegate) { |
| 110 | yylex_init(&scanner_); |
| 111 | } |
| 112 | |
Casey Dahlin | 2b2879b | 2015-10-13 16:59:44 -0700 | [diff] [blame^] | 113 | AidlParcelable::AidlParcelable(AidlQualifiedName* name, unsigned line, |
| 114 | const std::string& package) |
| 115 | : AidlParcelable(name->GetDotName(), line, package) { |
| 116 | delete name; |
| 117 | } |
| 118 | |
Casey Dahlin | 59401da | 2015-10-09 18:16:45 -0700 | [diff] [blame] | 119 | AidlParcelable::AidlParcelable(const std::string& name, unsigned line, |
| 120 | const std::string& package) |
| 121 | : name_(name), |
| 122 | line_(line), |
| 123 | package_(package) { |
| 124 | item_type = USER_DATA_TYPE; |
| 125 | } |
| 126 | |
Casey Dahlin | fb7da2e | 2015-10-08 17:26:09 -0700 | [diff] [blame] | 127 | AidlInterface::AidlInterface(const std::string& name, unsigned line, |
| 128 | const std::string& comments, bool oneway, |
| 129 | std::vector<std::unique_ptr<AidlMethod>>* methods, |
| 130 | const std::string& package) |
| 131 | : name_(name), |
| 132 | comments_(comments), |
| 133 | line_(line), |
| 134 | oneway_(oneway), |
| 135 | methods_(std::move(*methods)), |
| 136 | package_(package) { |
| 137 | item_type = INTERFACE_TYPE_BINDER; |
| 138 | delete methods; |
| 139 | } |
| 140 | |
Casey Dahlin | 2b2879b | 2015-10-13 16:59:44 -0700 | [diff] [blame^] | 141 | AidlQualifiedName::AidlQualifiedName(std::string term, |
| 142 | std::string comments) |
| 143 | : terms_({term}), |
| 144 | comments_(comments) { |
| 145 | } |
| 146 | |
| 147 | void AidlQualifiedName::AddTerm(std::string term) { |
| 148 | terms_.push_back(term); |
| 149 | } |
| 150 | |
Casey Dahlin | 0edf342 | 2015-10-07 12:34:59 -0700 | [diff] [blame] | 151 | AidlImport::AidlImport(const std::string& from, |
| 152 | const std::string& needed_class, unsigned line) |
| 153 | : from_(from), |
| 154 | needed_class_(needed_class), |
| 155 | line_(line) {} |
| 156 | |
Christopher Wiley | 4a2884b | 2015-10-07 11:27:45 -0700 | [diff] [blame] | 157 | Parser::~Parser() { |
| 158 | if (raw_buffer_) { |
| 159 | yy_delete_buffer(buffer_, scanner_); |
| 160 | raw_buffer_.reset(); |
| 161 | } |
| 162 | yylex_destroy(scanner_); |
| 163 | } |
| 164 | |
| 165 | bool Parser::ParseFile(const string& filename) { |
| 166 | // Make sure we can read the file first, before trashing previous state. |
| 167 | unique_ptr<string> new_buffer = io_delegate_.GetFileContents(filename); |
| 168 | if (!new_buffer) { |
| 169 | LOG(ERROR) << "Error while opening file for parsing: '" << filename << "'"; |
| 170 | return false; |
| 171 | } |
| 172 | |
| 173 | // Throw away old parsing state if we have any. |
| 174 | if (raw_buffer_) { |
| 175 | yy_delete_buffer(buffer_, scanner_); |
| 176 | raw_buffer_.reset(); |
| 177 | } |
| 178 | |
| 179 | raw_buffer_ = std::move(new_buffer); |
| 180 | // We're going to scan this buffer in place, and yacc demands we put two |
| 181 | // nulls at the end. |
| 182 | raw_buffer_->append(2u, '\0'); |
| 183 | filename_ = filename; |
| 184 | package_.clear(); |
| 185 | error_ = 0; |
| 186 | document_ = nullptr; |
Christopher Wiley | 4a2884b | 2015-10-07 11:27:45 -0700 | [diff] [blame] | 187 | |
| 188 | buffer_ = yy_scan_buffer(&(*raw_buffer_)[0], raw_buffer_->length(), scanner_); |
| 189 | |
| 190 | int ret = yy::parser(this).parse(); |
| 191 | |
| 192 | return ret == 0 && error_ == 0; |
| 193 | } |
| 194 | |
Casey Dahlin | e250749 | 2015-09-14 17:11:20 -0700 | [diff] [blame] | 195 | void Parser::ReportError(const string& err) { |
Casey Dahlin | dd69181 | 2015-09-09 17:59:06 -0700 | [diff] [blame] | 196 | /* FIXME: We're printing out the line number as -1. We used to use yylineno |
| 197 | * (which was NEVER correct even before reentrant parsing). Now we'll need |
| 198 | * another way. |
| 199 | */ |
| 200 | cerr << filename_ << ":" << -1 << ": " << err << endl; |
| 201 | error_ = 1; |
| 202 | } |
| 203 | |
Casey Dahlin | 2b2879b | 2015-10-13 16:59:44 -0700 | [diff] [blame^] | 204 | void Parser::AddImport(AidlQualifiedName* name, unsigned line) { |
| 205 | imports_.emplace_back(new AidlImport(this->FileName(), |
| 206 | name->GetDotName(), line)); |
| 207 | delete name; |
Casey Dahlin | e250749 | 2015-09-14 17:11:20 -0700 | [diff] [blame] | 208 | } |