blob: 7eecb6a155ffbafcb9bbe65cafd8ce3c87baeab1 [file] [log] [blame]
Adam Lesinskiffa16862014-01-23 18:17:42 -08001#include "aidl_language.h"
Christopher Wileyf690be52015-09-14 15:19:10 -07002
3#include <iostream>
Adam Lesinskiffa16862014-01-23 18:17:42 -08004#include <stdio.h>
Adam Lesinskiffa16862014-01-23 18:17:42 -08005#include <stdlib.h>
Christopher Wiley4a2884b2015-10-07 11:27:45 -07006#include <string.h>
Casey Dahlindd691812015-09-09 17:59:06 -07007#include <string>
Christopher Wileyf690be52015-09-14 15:19:10 -07008
9#include "aidl_language_y.hpp"
Christopher Wiley4a2884b2015-10-07 11:27:45 -070010#include "logging.h"
Christopher Wileyf690be52015-09-14 15:19:10 -070011#include "parse_helpers.h"
Adam Lesinskiffa16862014-01-23 18:17:42 -080012
Casey Dahlin07b9dde2015-09-10 19:13:49 -070013#ifdef _WIN32
14int isatty(int fd)
15{
16 return (fd == 0);
17}
18#endif
19
Christopher Wiley4a2884b2015-10-07 11:27:45 -070020using android::aidl::IoDelegate;
21using android::aidl::cpp_strdup;
Casey Dahlindd691812015-09-09 17:59:06 -070022using std::cerr;
23using std::endl;
Christopher Wiley4a2884b2015-10-07 11:27:45 -070024using std::string;
25using std::unique_ptr;
Adam Lesinskiffa16862014-01-23 18:17:42 -080026
Casey Dahlindd691812015-09-09 17:59:06 -070027void yylex_init(void **);
28void yylex_destroy(void *);
29void yyset_in(FILE *f, void *);
Casey Dahline2507492015-09-14 17:11:20 -070030int yyparse(Parser*);
Christopher Wiley4a2884b2015-10-07 11:27:45 -070031YY_BUFFER_STATE yy_scan_buffer(char *, size_t, void *);
Casey Dahlin89d44842015-09-24 18:45:54 -070032void yy_delete_buffer(YY_BUFFER_STATE, void *);
Casey Dahlindd691812015-09-09 17:59:06 -070033
Casey Dahlinf2d23f72015-10-02 16:19:19 -070034AidlType::AidlType(const std::string& name, unsigned line,
Casey Dahlinf7a421c2015-10-05 17:24:28 -070035 const std::string& comments, bool is_array)
Casey Dahlinf2d23f72015-10-02 16:19:19 -070036 : name_(name),
37 line_(line),
Casey Dahlinf7a421c2015-10-05 17:24:28 -070038 is_array_(is_array),
Casey Dahlinf2d23f72015-10-02 16:19:19 -070039 comments_(comments) {}
40
Christopher Wiley4a2884b2015-10-07 11:27:45 -070041string AidlType::ToString() const { return name_ + (is_array_ ? "[]" : "");
Casey Dahlin70078e62015-09-30 17:01:30 -070042}
43
Casey Dahlin0ee37582015-09-30 16:31:55 -070044AidlArgument::AidlArgument(AidlArgument::Direction direction, AidlType* type,
Casey Dahlin308f9d42015-10-05 18:48:42 -070045 std::string name, unsigned line)
Casey Dahlin0ee37582015-09-30 16:31:55 -070046 : type_(type),
Casey Dahlinfd6fb482015-09-30 14:48:18 -070047 direction_(direction),
48 direction_specified_(true),
Casey Dahlin308f9d42015-10-05 18:48:42 -070049 name_(name),
50 line_(line) {}
Casey Dahlinc378c992015-09-29 16:50:40 -070051
Casey Dahlin308f9d42015-10-05 18:48:42 -070052AidlArgument::AidlArgument(AidlType* type, std::string name, unsigned line)
Casey Dahlin0ee37582015-09-30 16:31:55 -070053 : type_(type),
Casey Dahlinfd6fb482015-09-30 14:48:18 -070054 direction_(AidlArgument::IN_DIR),
55 direction_specified_(false),
Casey Dahlin308f9d42015-10-05 18:48:42 -070056 name_(name),
57 line_(line) {}
Casey Dahlinc378c992015-09-29 16:50:40 -070058
59string 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 Dahlin70078e62015-09-30 17:01:30 -070076 ret += type_->ToString();
Casey Dahlinc378c992015-09-29 16:50:40 -070077 ret += " ";
Casey Dahlind127b502015-09-30 12:51:08 -070078 ret += name_;
Casey Dahlinc378c992015-09-29 16:50:40 -070079
80 return ret;
81}
Casey Dahlinbc7a50a2015-09-28 19:20:50 -070082
Casey Dahlinf4a93112015-10-05 16:58:09 -070083AidlMethod::AidlMethod(bool oneway, AidlType* type, std::string name,
84 std::vector<std::unique_ptr<AidlArgument>>* args,
85 unsigned line, std::string comments, int id)
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;
95}
96
97AidlMethod::AidlMethod(bool oneway, AidlType* type, std::string name,
98 std::vector<std::unique_ptr<AidlArgument>>* args,
99 unsigned line, std::string comments)
100 : AidlMethod(oneway, type, name, args, line, comments, 0) {
101 has_id_ = false;
102}
Casey Dahlinf2d23f72015-10-02 16:19:19 -0700103
Christopher Wiley4a2884b2015-10-07 11:27:45 -0700104Parser::Parser(const IoDelegate& io_delegate)
105 : io_delegate_(io_delegate) {
106 yylex_init(&scanner_);
107}
108
109Parser::~Parser() {
110 if (raw_buffer_) {
111 yy_delete_buffer(buffer_, scanner_);
112 raw_buffer_.reset();
113 }
114 yylex_destroy(scanner_);
115}
116
117bool Parser::ParseFile(const string& filename) {
118 // Make sure we can read the file first, before trashing previous state.
119 unique_ptr<string> new_buffer = io_delegate_.GetFileContents(filename);
120 if (!new_buffer) {
121 LOG(ERROR) << "Error while opening file for parsing: '" << filename << "'";
122 return false;
123 }
124
125 // Throw away old parsing state if we have any.
126 if (raw_buffer_) {
127 yy_delete_buffer(buffer_, scanner_);
128 raw_buffer_.reset();
129 }
130
131 raw_buffer_ = std::move(new_buffer);
132 // We're going to scan this buffer in place, and yacc demands we put two
133 // nulls at the end.
134 raw_buffer_->append(2u, '\0');
135 filename_ = filename;
136 package_.clear();
137 error_ = 0;
138 document_ = nullptr;
139 imports_ = nullptr;
140
141 buffer_ = yy_scan_buffer(&(*raw_buffer_)[0], raw_buffer_->length(), scanner_);
142
143 int ret = yy::parser(this).parse();
144
145 return ret == 0 && error_ == 0;
146}
147
Casey Dahline2507492015-09-14 17:11:20 -0700148void Parser::ReportError(const string& err) {
Casey Dahlindd691812015-09-09 17:59:06 -0700149 /* FIXME: We're printing out the line number as -1. We used to use yylineno
150 * (which was NEVER correct even before reentrant parsing). Now we'll need
151 * another way.
152 */
153 cerr << filename_ << ":" << -1 << ": " << err << endl;
154 error_ = 1;
155}
156
Casey Dahlin3c6df362015-10-06 15:48:35 -0700157void Parser::AddImport(std::vector<std::string>* terms, unsigned line) {
158 std::string data;
159 bool first = true;
Casey Dahline2507492015-09-14 17:11:20 -0700160
Casey Dahlin3c6df362015-10-06 15:48:35 -0700161 /* NOTE: This string building code is duplicated from below. We haven't
162 * factored it out into a function because it's hoped that when import_info
163 * becomes a class we won't need this anymore.
164 **/
165 for (const auto& term : *terms) {
166 if (first)
167 data = term;
168 else
169 data += '.' + term;
170 }
171
Casey Dahlin030977a2015-09-29 11:29:35 -0700172 import_info* import = new import_info();
Casey Dahline2507492015-09-14 17:11:20 -0700173 memset(import, 0, sizeof(import_info));
Casey Dahlin030977a2015-09-29 11:29:35 -0700174 import->from = cpp_strdup(this->FileName().c_str());
Casey Dahline2507492015-09-14 17:11:20 -0700175 import->next = imports_;
Casey Dahlin3c6df362015-10-06 15:48:35 -0700176 import->line = line;
177 import->neededClass = cpp_strdup(data.c_str());
Casey Dahline2507492015-09-14 17:11:20 -0700178 imports_ = import;
Casey Dahlin3c6df362015-10-06 15:48:35 -0700179
180 delete terms;
Casey Dahline2507492015-09-14 17:11:20 -0700181}
182
Casey Dahlin3c6df362015-10-06 15:48:35 -0700183void Parser::SetPackage(std::vector<std::string> *terms) {
184 bool first = true;
Casey Dahline2507492015-09-14 17:11:20 -0700185
Casey Dahlin3c6df362015-10-06 15:48:35 -0700186 for (const auto& term : *terms) {
187 if (first)
188 package_ = term;
189 else
190 package_ += '.' + term;
191 }
192
193 delete terms;
Casey Dahline2507492015-09-14 17:11:20 -0700194}