blob: 1a84b4956d6201bcd0f33dfb1ef337973116ede7 [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,
Casey Dahlinf7a421c2015-10-05 17:24:28 -070047 const std::string& comments, bool is_array)
Casey Dahlinf2d23f72015-10-02 16:19:19 -070048 : name_(name),
49 line_(line),
Casey Dahlinf7a421c2015-10-05 17:24:28 -070050 is_array_(is_array),
Casey Dahlinf2d23f72015-10-02 16:19:19 -070051 comments_(comments) {}
52
Casey Dahlin70078e62015-09-30 17:01:30 -070053string AidlType::ToString() const {
Casey Dahlinf7a421c2015-10-05 17:24:28 -070054 return name_ + (is_array_ ? "[]" : "");
Casey Dahlin70078e62015-09-30 17:01:30 -070055}
56
Casey Dahlin0ee37582015-09-30 16:31:55 -070057AidlArgument::AidlArgument(AidlArgument::Direction direction, AidlType* type,
Casey Dahlinfd6fb482015-09-30 14:48:18 -070058 buffer_type name)
Casey Dahlin0ee37582015-09-30 16:31:55 -070059 : type_(type),
Casey Dahlinfd6fb482015-09-30 14:48:18 -070060 direction_(direction),
61 direction_specified_(true),
Casey Dahlind127b502015-09-30 12:51:08 -070062 name_(name.data),
Casey Dahlinfd6fb482015-09-30 14:48:18 -070063 line_(name.lineno) {}
Casey Dahlinc378c992015-09-29 16:50:40 -070064
Casey Dahlin0ee37582015-09-30 16:31:55 -070065AidlArgument::AidlArgument(AidlType* type, buffer_type name)
66 : type_(type),
Casey Dahlinfd6fb482015-09-30 14:48:18 -070067 direction_(AidlArgument::IN_DIR),
68 direction_specified_(false),
69 name_(name.data),
70 line_(name.lineno) {}
Casey Dahlinc378c992015-09-29 16:50:40 -070071
72string AidlArgument::ToString() const {
73 string ret;
74
75 if (direction_specified_) {
76 switch(direction_) {
77 case AidlArgument::IN_DIR:
78 ret += "in ";
79 break;
80 case AidlArgument::OUT_DIR:
81 ret += "out ";
82 break;
83 case AidlArgument::INOUT_DIR:
84 ret += "inout ";
85 break;
86 }
87 }
88
Casey Dahlin70078e62015-09-30 17:01:30 -070089 ret += type_->ToString();
Casey Dahlinc378c992015-09-29 16:50:40 -070090 ret += " ";
Casey Dahlind127b502015-09-30 12:51:08 -070091 ret += name_;
Casey Dahlinc378c992015-09-29 16:50:40 -070092
93 return ret;
94}
Casey Dahlinbc7a50a2015-09-28 19:20:50 -070095
Casey Dahlinf4a93112015-10-05 16:58:09 -070096AidlMethod::AidlMethod(bool oneway, AidlType* type, std::string name,
97 std::vector<std::unique_ptr<AidlArgument>>* args,
98 unsigned line, std::string comments, int id)
99 : oneway_(oneway),
100 comments_(comments),
101 type_(type),
102 name_(name),
103 line_(line),
104 arguments_(std::move(*args)),
105 id_(id) {
106 has_id_ = true;
107 delete args;
108}
109
110AidlMethod::AidlMethod(bool oneway, AidlType* type, std::string name,
111 std::vector<std::unique_ptr<AidlArgument>>* args,
112 unsigned line, std::string comments)
113 : AidlMethod(oneway, type, name, args, line, comments, 0) {
114 has_id_ = false;
115}
Casey Dahlinf2d23f72015-10-02 16:19:19 -0700116
Casey Dahline2507492015-09-14 17:11:20 -0700117string Parser::FileName() {
Casey Dahlindd691812015-09-09 17:59:06 -0700118 return filename_;
119}
120
Casey Dahline2507492015-09-14 17:11:20 -0700121string Parser::Package() {
Christopher Wiley5b869802015-09-15 19:40:38 -0700122 return g_currentPackage ? g_currentPackage : "";
Casey Dahlindd691812015-09-09 17:59:06 -0700123}
124
Casey Dahline2507492015-09-14 17:11:20 -0700125void Parser::ReportError(const string& err) {
Casey Dahlindd691812015-09-09 17:59:06 -0700126 /* FIXME: We're printing out the line number as -1. We used to use yylineno
127 * (which was NEVER correct even before reentrant parsing). Now we'll need
128 * another way.
129 */
130 cerr << filename_ << ":" << -1 << ": " << err << endl;
131 error_ = 1;
132}
133
Casey Dahline2507492015-09-14 17:11:20 -0700134bool Parser::FoundNoErrors() {
Casey Dahlindd691812015-09-09 17:59:06 -0700135 return error_ == 0;
136}
137
Casey Dahline2507492015-09-14 17:11:20 -0700138void *Parser::Scanner() {
Casey Dahlindd691812015-09-09 17:59:06 -0700139 return scanner_;
140}
141
Casey Dahline2507492015-09-14 17:11:20 -0700142bool Parser::OpenFileFromDisk() {
Casey Dahlindd691812015-09-09 17:59:06 -0700143 FILE *in = fopen(FileName().c_str(), "r");
144
145 if (! in)
146 return false;
147
148 yyset_in(in, Scanner());
149 return true;
150}
151
Casey Dahlin89d44842015-09-24 18:45:54 -0700152void Parser::SetFileContents(const std::string& contents) {
153 if (buffer_is_valid_)
154 yy_delete_buffer(buffer_, scanner_);
155
156 buffer_ = yy_scan_string(contents.c_str(), scanner_);
157 buffer_is_valid_ = true;
158}
159
Casey Dahline2507492015-09-14 17:11:20 -0700160bool Parser::RunParser() {
Casey Dahlinfd2e08a2015-09-10 18:29:09 -0700161 int ret = yy::parser(this).parse();
Casey Dahlindd691812015-09-09 17:59:06 -0700162
Casey Dahlin030977a2015-09-29 11:29:35 -0700163 delete[] g_currentPackage;
Casey Dahlindd691812015-09-09 17:59:06 -0700164 g_currentPackage = NULL;
165
Casey Dahlin99801812015-09-15 18:19:25 -0700166 return ret == 0 && error_ == 0;
Casey Dahlindd691812015-09-09 17:59:06 -0700167}
Casey Dahline2507492015-09-14 17:11:20 -0700168
169void Parser::SetDocument(document_item_type *d)
170{
171 document_ = d;
172}
173
174void Parser::AddImport(const buffer_type& statement)
175{
Casey Dahlin030977a2015-09-29 11:29:35 -0700176 import_info* import = new import_info();
Casey Dahline2507492015-09-14 17:11:20 -0700177 memset(import, 0, sizeof(import_info));
Casey Dahlin030977a2015-09-29 11:29:35 -0700178 import->from = cpp_strdup(this->FileName().c_str());
Casey Dahline2507492015-09-14 17:11:20 -0700179 import->statement.lineno = statement.lineno;
Casey Dahlin030977a2015-09-29 11:29:35 -0700180 import->statement.data = cpp_strdup(statement.data);
Casey Dahline2507492015-09-14 17:11:20 -0700181 import->statement.extra = NULL;
182 import->next = imports_;
183 import->neededClass = android::aidl::parse_import_statement(statement.data);
184 imports_ = import;
185}
186
187document_item_type *Parser::GetDocument() const
188{
189 return document_;
190}
191
192import_info *Parser::GetImports() const
193{
194 return imports_;
195}