blob: 21a32ad20a16cd797e8cedde6e29089f925884a3 [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>
Casey Dahlindd691812015-09-09 17:59:06 -07006#include <string>
Christopher Wileyf690be52015-09-14 15:19:10 -07007
8#include "aidl_language_y.hpp"
9#include "parse_helpers.h"
Adam Lesinskiffa16862014-01-23 18:17:42 -080010
Casey Dahlin07b9dde2015-09-10 19:13:49 -070011#ifdef _WIN32
12int isatty(int fd)
13{
14 return (fd == 0);
15}
16#endif
17
Casey Dahlindd691812015-09-09 17:59:06 -070018using std::string;
19using std::cerr;
20using std::endl;
Casey Dahlin030977a2015-09-29 11:29:35 -070021using android::aidl::cpp_strdup;
Adam Lesinskiffa16862014-01-23 18:17:42 -080022
Casey Dahline2507492015-09-14 17:11:20 -070023Parser *psGlobal = NULL;
Adam Lesinskiffa16862014-01-23 18:17:42 -080024ParserCallbacks* g_callbacks = NULL; // &k_parserCallbacks;
Christopher Wileyfdeb0f42015-09-11 15:38:22 -070025char const* g_currentPackage = NULL;
26
Adam Lesinskiffa16862014-01-23 18:17:42 -080027
Casey Dahlindd691812015-09-09 17:59:06 -070028void yylex_init(void **);
29void yylex_destroy(void *);
30void yyset_in(FILE *f, void *);
Casey Dahline2507492015-09-14 17:11:20 -070031int yyparse(Parser*);
Casey Dahlin89d44842015-09-24 18:45:54 -070032YY_BUFFER_STATE yy_scan_string(const char *, void *);
33void yy_delete_buffer(YY_BUFFER_STATE, void *);
Casey Dahlindd691812015-09-09 17:59:06 -070034
Casey Dahline2507492015-09-14 17:11:20 -070035Parser::Parser(const string& filename)
Casey Dahlindd691812015-09-09 17:59:06 -070036 : filename_(filename) {
37 yylex_init(&scanner_);
38}
39
Casey Dahline2507492015-09-14 17:11:20 -070040Parser::~Parser() {
Casey Dahlin89d44842015-09-24 18:45:54 -070041 if (buffer_is_valid_)
42 yy_delete_buffer(buffer_, scanner_);
Casey Dahlindd691812015-09-09 17:59:06 -070043 yylex_destroy(scanner_);
44}
45
Casey Dahlinf2d23f72015-10-02 16:19:19 -070046AidlType::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
53AidlType::AidlType(const std::string& name, unsigned line,
54 const std::string& comments)
55 : AidlType(name, line, comments, 0) {}
56
Casey Dahlin70078e62015-09-30 17:01:30 -070057string AidlType::ToString() const {
Casey Dahlinf2d23f72015-10-02 16:19:19 -070058 return name_ + Brackets();
Casey Dahlin70078e62015-09-30 17:01:30 -070059}
60
61std::string AidlType::Brackets() const {
62 std::string result;
63
Casey Dahlinf2d23f72015-10-02 16:19:19 -070064 for (unsigned i = 0; i < dimension_; i++)
Casey Dahlin70078e62015-09-30 17:01:30 -070065 result += "[]";
66
67 return result;
68}
69
Casey Dahlin0ee37582015-09-30 16:31:55 -070070AidlArgument::AidlArgument(AidlArgument::Direction direction, AidlType* type,
Casey Dahlinfd6fb482015-09-30 14:48:18 -070071 buffer_type name)
Casey Dahlin0ee37582015-09-30 16:31:55 -070072 : type_(type),
Casey Dahlinfd6fb482015-09-30 14:48:18 -070073 direction_(direction),
74 direction_specified_(true),
Casey Dahlind127b502015-09-30 12:51:08 -070075 name_(name.data),
Casey Dahlinfd6fb482015-09-30 14:48:18 -070076 line_(name.lineno) {}
Casey Dahlinc378c992015-09-29 16:50:40 -070077
Casey Dahlin0ee37582015-09-30 16:31:55 -070078AidlArgument::AidlArgument(AidlType* type, buffer_type name)
79 : type_(type),
Casey Dahlinfd6fb482015-09-30 14:48:18 -070080 direction_(AidlArgument::IN_DIR),
81 direction_specified_(false),
82 name_(name.data),
83 line_(name.lineno) {}
Casey Dahlinc378c992015-09-29 16:50:40 -070084
85string 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 Dahlin70078e62015-09-30 17:01:30 -0700102 ret += type_->ToString();
Casey Dahlinc378c992015-09-29 16:50:40 -0700103 ret += " ";
Casey Dahlind127b502015-09-30 12:51:08 -0700104 ret += name_;
Casey Dahlinc378c992015-09-29 16:50:40 -0700105
106 return ret;
107}
Casey Dahlinbc7a50a2015-09-28 19:20:50 -0700108
Casey Dahlinf4a93112015-10-05 16:58:09 -0700109AidlMethod::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
123AidlMethod::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 Dahlinf2d23f72015-10-02 16:19:19 -0700129
Casey Dahline2507492015-09-14 17:11:20 -0700130string Parser::FileName() {
Casey Dahlindd691812015-09-09 17:59:06 -0700131 return filename_;
132}
133
Casey Dahline2507492015-09-14 17:11:20 -0700134string Parser::Package() {
Christopher Wiley5b869802015-09-15 19:40:38 -0700135 return g_currentPackage ? g_currentPackage : "";
Casey Dahlindd691812015-09-09 17:59:06 -0700136}
137
Casey Dahline2507492015-09-14 17:11:20 -0700138void Parser::ReportError(const string& err) {
Casey Dahlindd691812015-09-09 17:59:06 -0700139 /* 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 Dahline2507492015-09-14 17:11:20 -0700147bool Parser::FoundNoErrors() {
Casey Dahlindd691812015-09-09 17:59:06 -0700148 return error_ == 0;
149}
150
Casey Dahline2507492015-09-14 17:11:20 -0700151void *Parser::Scanner() {
Casey Dahlindd691812015-09-09 17:59:06 -0700152 return scanner_;
153}
154
Casey Dahline2507492015-09-14 17:11:20 -0700155bool Parser::OpenFileFromDisk() {
Casey Dahlindd691812015-09-09 17:59:06 -0700156 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 Dahlin89d44842015-09-24 18:45:54 -0700165void 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 Dahline2507492015-09-14 17:11:20 -0700173bool Parser::RunParser() {
Casey Dahlinfd2e08a2015-09-10 18:29:09 -0700174 int ret = yy::parser(this).parse();
Casey Dahlindd691812015-09-09 17:59:06 -0700175
Casey Dahlin030977a2015-09-29 11:29:35 -0700176 delete[] g_currentPackage;
Casey Dahlindd691812015-09-09 17:59:06 -0700177 g_currentPackage = NULL;
178
Casey Dahlin99801812015-09-15 18:19:25 -0700179 return ret == 0 && error_ == 0;
Casey Dahlindd691812015-09-09 17:59:06 -0700180}
Casey Dahline2507492015-09-14 17:11:20 -0700181
182void Parser::SetDocument(document_item_type *d)
183{
184 document_ = d;
185}
186
187void Parser::AddImport(const buffer_type& statement)
188{
Casey Dahlin030977a2015-09-29 11:29:35 -0700189 import_info* import = new import_info();
Casey Dahline2507492015-09-14 17:11:20 -0700190 memset(import, 0, sizeof(import_info));
Casey Dahlin030977a2015-09-29 11:29:35 -0700191 import->from = cpp_strdup(this->FileName().c_str());
Casey Dahline2507492015-09-14 17:11:20 -0700192 import->statement.lineno = statement.lineno;
Casey Dahlin030977a2015-09-29 11:29:35 -0700193 import->statement.data = cpp_strdup(statement.data);
Casey Dahline2507492015-09-14 17:11:20 -0700194 import->statement.extra = NULL;
195 import->next = imports_;
196 import->neededClass = android::aidl::parse_import_statement(statement.data);
197 imports_ = import;
198}
199
200document_item_type *Parser::GetDocument() const
201{
202 return document_;
203}
204
205import_info *Parser::GetImports() const
206{
207 return imports_;
208}