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> |
Casey Dahlin | dd69181 | 2015-09-09 17:59:06 -0700 | [diff] [blame] | 6 | #include <string> |
Christopher Wiley | f690be5 | 2015-09-14 15:19:10 -0700 | [diff] [blame] | 7 | |
| 8 | #include "aidl_language_y.hpp" |
| 9 | #include "parse_helpers.h" |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 10 | |
Casey Dahlin | 07b9dde | 2015-09-10 19:13:49 -0700 | [diff] [blame] | 11 | #ifdef _WIN32 |
| 12 | int isatty(int fd) |
| 13 | { |
| 14 | return (fd == 0); |
| 15 | } |
| 16 | #endif |
| 17 | |
Casey Dahlin | dd69181 | 2015-09-09 17:59:06 -0700 | [diff] [blame] | 18 | using std::string; |
| 19 | using std::cerr; |
| 20 | using std::endl; |
Casey Dahlin | 030977a | 2015-09-29 11:29:35 -0700 | [diff] [blame] | 21 | using android::aidl::cpp_strdup; |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 22 | |
Casey Dahlin | e250749 | 2015-09-14 17:11:20 -0700 | [diff] [blame] | 23 | Parser *psGlobal = NULL; |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 24 | ParserCallbacks* g_callbacks = NULL; // &k_parserCallbacks; |
Christopher Wiley | fdeb0f4 | 2015-09-11 15:38:22 -0700 | [diff] [blame] | 25 | char const* g_currentPackage = NULL; |
| 26 | |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 27 | |
Casey Dahlin | dd69181 | 2015-09-09 17:59:06 -0700 | [diff] [blame] | 28 | void yylex_init(void **); |
| 29 | void yylex_destroy(void *); |
| 30 | void yyset_in(FILE *f, void *); |
Casey Dahlin | e250749 | 2015-09-14 17:11:20 -0700 | [diff] [blame] | 31 | int yyparse(Parser*); |
Casey Dahlin | 89d4484 | 2015-09-24 18:45:54 -0700 | [diff] [blame] | 32 | YY_BUFFER_STATE yy_scan_string(const char *, void *); |
| 33 | void yy_delete_buffer(YY_BUFFER_STATE, void *); |
Casey Dahlin | dd69181 | 2015-09-09 17:59:06 -0700 | [diff] [blame] | 34 | |
Casey Dahlin | e250749 | 2015-09-14 17:11:20 -0700 | [diff] [blame] | 35 | Parser::Parser(const string& filename) |
Casey Dahlin | dd69181 | 2015-09-09 17:59:06 -0700 | [diff] [blame] | 36 | : filename_(filename) { |
| 37 | yylex_init(&scanner_); |
| 38 | } |
| 39 | |
Casey Dahlin | e250749 | 2015-09-14 17:11:20 -0700 | [diff] [blame] | 40 | Parser::~Parser() { |
Casey Dahlin | 89d4484 | 2015-09-24 18:45:54 -0700 | [diff] [blame] | 41 | if (buffer_is_valid_) |
| 42 | yy_delete_buffer(buffer_, scanner_); |
Casey Dahlin | dd69181 | 2015-09-09 17:59:06 -0700 | [diff] [blame] | 43 | yylex_destroy(scanner_); |
| 44 | } |
| 45 | |
Casey Dahlin | f2d23f7 | 2015-10-02 16:19:19 -0700 | [diff] [blame] | 46 | AidlType::AidlType(const std::string& name, unsigned line, |
| 47 | const std::string& comments, unsigned dimension) |
| 48 | : name_(name), |
| 49 | line_(line), |
| 50 | dimension_(dimension), |
| 51 | comments_(comments) {} |
| 52 | |
| 53 | AidlType::AidlType(const std::string& name, unsigned line, |
| 54 | const std::string& comments) |
| 55 | : AidlType(name, line, comments, 0) {} |
| 56 | |
Casey Dahlin | 70078e6 | 2015-09-30 17:01:30 -0700 | [diff] [blame] | 57 | string AidlType::ToString() const { |
Casey Dahlin | f2d23f7 | 2015-10-02 16:19:19 -0700 | [diff] [blame] | 58 | return name_ + Brackets(); |
Casey Dahlin | 70078e6 | 2015-09-30 17:01:30 -0700 | [diff] [blame] | 59 | } |
| 60 | |
| 61 | std::string AidlType::Brackets() const { |
| 62 | std::string result; |
| 63 | |
Casey Dahlin | f2d23f7 | 2015-10-02 16:19:19 -0700 | [diff] [blame] | 64 | for (unsigned i = 0; i < dimension_; i++) |
Casey Dahlin | 70078e6 | 2015-09-30 17:01:30 -0700 | [diff] [blame] | 65 | result += "[]"; |
| 66 | |
| 67 | return result; |
| 68 | } |
| 69 | |
Casey Dahlin | 0ee3758 | 2015-09-30 16:31:55 -0700 | [diff] [blame] | 70 | AidlArgument::AidlArgument(AidlArgument::Direction direction, AidlType* type, |
Casey Dahlin | fd6fb48 | 2015-09-30 14:48:18 -0700 | [diff] [blame] | 71 | buffer_type name) |
Casey Dahlin | 0ee3758 | 2015-09-30 16:31:55 -0700 | [diff] [blame] | 72 | : type_(type), |
Casey Dahlin | fd6fb48 | 2015-09-30 14:48:18 -0700 | [diff] [blame] | 73 | direction_(direction), |
| 74 | direction_specified_(true), |
Casey Dahlin | d127b50 | 2015-09-30 12:51:08 -0700 | [diff] [blame] | 75 | name_(name.data), |
Casey Dahlin | fd6fb48 | 2015-09-30 14:48:18 -0700 | [diff] [blame] | 76 | line_(name.lineno) {} |
Casey Dahlin | c378c99 | 2015-09-29 16:50:40 -0700 | [diff] [blame] | 77 | |
Casey Dahlin | 0ee3758 | 2015-09-30 16:31:55 -0700 | [diff] [blame] | 78 | AidlArgument::AidlArgument(AidlType* type, buffer_type name) |
| 79 | : type_(type), |
Casey Dahlin | fd6fb48 | 2015-09-30 14:48:18 -0700 | [diff] [blame] | 80 | direction_(AidlArgument::IN_DIR), |
| 81 | direction_specified_(false), |
| 82 | name_(name.data), |
| 83 | line_(name.lineno) {} |
Casey Dahlin | c378c99 | 2015-09-29 16:50:40 -0700 | [diff] [blame] | 84 | |
| 85 | string AidlArgument::ToString() const { |
| 86 | string ret; |
| 87 | |
| 88 | if (direction_specified_) { |
| 89 | switch(direction_) { |
| 90 | case AidlArgument::IN_DIR: |
| 91 | ret += "in "; |
| 92 | break; |
| 93 | case AidlArgument::OUT_DIR: |
| 94 | ret += "out "; |
| 95 | break; |
| 96 | case AidlArgument::INOUT_DIR: |
| 97 | ret += "inout "; |
| 98 | break; |
| 99 | } |
| 100 | } |
| 101 | |
Casey Dahlin | 70078e6 | 2015-09-30 17:01:30 -0700 | [diff] [blame] | 102 | ret += type_->ToString(); |
Casey Dahlin | c378c99 | 2015-09-29 16:50:40 -0700 | [diff] [blame] | 103 | ret += " "; |
Casey Dahlin | d127b50 | 2015-09-30 12:51:08 -0700 | [diff] [blame] | 104 | ret += name_; |
Casey Dahlin | c378c99 | 2015-09-29 16:50:40 -0700 | [diff] [blame] | 105 | |
| 106 | return ret; |
| 107 | } |
Casey Dahlin | bc7a50a | 2015-09-28 19:20:50 -0700 | [diff] [blame] | 108 | |
Casey Dahlin | f4a9311 | 2015-10-05 16:58:09 -0700 | [diff] [blame^] | 109 | AidlMethod::AidlMethod(bool oneway, AidlType* type, std::string name, |
| 110 | std::vector<std::unique_ptr<AidlArgument>>* args, |
| 111 | unsigned line, std::string comments, int id) |
| 112 | : oneway_(oneway), |
| 113 | comments_(comments), |
| 114 | type_(type), |
| 115 | name_(name), |
| 116 | line_(line), |
| 117 | arguments_(std::move(*args)), |
| 118 | id_(id) { |
| 119 | has_id_ = true; |
| 120 | delete args; |
| 121 | } |
| 122 | |
| 123 | AidlMethod::AidlMethod(bool oneway, AidlType* type, std::string name, |
| 124 | std::vector<std::unique_ptr<AidlArgument>>* args, |
| 125 | unsigned line, std::string comments) |
| 126 | : AidlMethod(oneway, type, name, args, line, comments, 0) { |
| 127 | has_id_ = false; |
| 128 | } |
Casey Dahlin | f2d23f7 | 2015-10-02 16:19:19 -0700 | [diff] [blame] | 129 | |
Casey Dahlin | e250749 | 2015-09-14 17:11:20 -0700 | [diff] [blame] | 130 | string Parser::FileName() { |
Casey Dahlin | dd69181 | 2015-09-09 17:59:06 -0700 | [diff] [blame] | 131 | return filename_; |
| 132 | } |
| 133 | |
Casey Dahlin | e250749 | 2015-09-14 17:11:20 -0700 | [diff] [blame] | 134 | string Parser::Package() { |
Christopher Wiley | 5b86980 | 2015-09-15 19:40:38 -0700 | [diff] [blame] | 135 | return g_currentPackage ? g_currentPackage : ""; |
Casey Dahlin | dd69181 | 2015-09-09 17:59:06 -0700 | [diff] [blame] | 136 | } |
| 137 | |
Casey Dahlin | e250749 | 2015-09-14 17:11:20 -0700 | [diff] [blame] | 138 | void Parser::ReportError(const string& err) { |
Casey Dahlin | dd69181 | 2015-09-09 17:59:06 -0700 | [diff] [blame] | 139 | /* FIXME: We're printing out the line number as -1. We used to use yylineno |
| 140 | * (which was NEVER correct even before reentrant parsing). Now we'll need |
| 141 | * another way. |
| 142 | */ |
| 143 | cerr << filename_ << ":" << -1 << ": " << err << endl; |
| 144 | error_ = 1; |
| 145 | } |
| 146 | |
Casey Dahlin | e250749 | 2015-09-14 17:11:20 -0700 | [diff] [blame] | 147 | bool Parser::FoundNoErrors() { |
Casey Dahlin | dd69181 | 2015-09-09 17:59:06 -0700 | [diff] [blame] | 148 | return error_ == 0; |
| 149 | } |
| 150 | |
Casey Dahlin | e250749 | 2015-09-14 17:11:20 -0700 | [diff] [blame] | 151 | void *Parser::Scanner() { |
Casey Dahlin | dd69181 | 2015-09-09 17:59:06 -0700 | [diff] [blame] | 152 | return scanner_; |
| 153 | } |
| 154 | |
Casey Dahlin | e250749 | 2015-09-14 17:11:20 -0700 | [diff] [blame] | 155 | bool Parser::OpenFileFromDisk() { |
Casey Dahlin | dd69181 | 2015-09-09 17:59:06 -0700 | [diff] [blame] | 156 | FILE *in = fopen(FileName().c_str(), "r"); |
| 157 | |
| 158 | if (! in) |
| 159 | return false; |
| 160 | |
| 161 | yyset_in(in, Scanner()); |
| 162 | return true; |
| 163 | } |
| 164 | |
Casey Dahlin | 89d4484 | 2015-09-24 18:45:54 -0700 | [diff] [blame] | 165 | void Parser::SetFileContents(const std::string& contents) { |
| 166 | if (buffer_is_valid_) |
| 167 | yy_delete_buffer(buffer_, scanner_); |
| 168 | |
| 169 | buffer_ = yy_scan_string(contents.c_str(), scanner_); |
| 170 | buffer_is_valid_ = true; |
| 171 | } |
| 172 | |
Casey Dahlin | e250749 | 2015-09-14 17:11:20 -0700 | [diff] [blame] | 173 | bool Parser::RunParser() { |
Casey Dahlin | fd2e08a | 2015-09-10 18:29:09 -0700 | [diff] [blame] | 174 | int ret = yy::parser(this).parse(); |
Casey Dahlin | dd69181 | 2015-09-09 17:59:06 -0700 | [diff] [blame] | 175 | |
Casey Dahlin | 030977a | 2015-09-29 11:29:35 -0700 | [diff] [blame] | 176 | delete[] g_currentPackage; |
Casey Dahlin | dd69181 | 2015-09-09 17:59:06 -0700 | [diff] [blame] | 177 | g_currentPackage = NULL; |
| 178 | |
Casey Dahlin | 9980181 | 2015-09-15 18:19:25 -0700 | [diff] [blame] | 179 | return ret == 0 && error_ == 0; |
Casey Dahlin | dd69181 | 2015-09-09 17:59:06 -0700 | [diff] [blame] | 180 | } |
Casey Dahlin | e250749 | 2015-09-14 17:11:20 -0700 | [diff] [blame] | 181 | |
| 182 | void Parser::SetDocument(document_item_type *d) |
| 183 | { |
| 184 | document_ = d; |
| 185 | } |
| 186 | |
| 187 | void Parser::AddImport(const buffer_type& statement) |
| 188 | { |
Casey Dahlin | 030977a | 2015-09-29 11:29:35 -0700 | [diff] [blame] | 189 | import_info* import = new import_info(); |
Casey Dahlin | e250749 | 2015-09-14 17:11:20 -0700 | [diff] [blame] | 190 | memset(import, 0, sizeof(import_info)); |
Casey Dahlin | 030977a | 2015-09-29 11:29:35 -0700 | [diff] [blame] | 191 | import->from = cpp_strdup(this->FileName().c_str()); |
Casey Dahlin | e250749 | 2015-09-14 17:11:20 -0700 | [diff] [blame] | 192 | import->statement.lineno = statement.lineno; |
Casey Dahlin | 030977a | 2015-09-29 11:29:35 -0700 | [diff] [blame] | 193 | import->statement.data = cpp_strdup(statement.data); |
Casey Dahlin | e250749 | 2015-09-14 17:11:20 -0700 | [diff] [blame] | 194 | import->statement.extra = NULL; |
| 195 | import->next = imports_; |
| 196 | import->neededClass = android::aidl::parse_import_statement(statement.data); |
| 197 | imports_ = import; |
| 198 | } |
| 199 | |
| 200 | document_item_type *Parser::GetDocument() const |
| 201 | { |
| 202 | return document_; |
| 203 | } |
| 204 | |
| 205 | import_info *Parser::GetImports() const |
| 206 | { |
| 207 | return imports_; |
| 208 | } |