blob: 5ca18d1c5cacc07f40dfca5968f698d112fbc6a2 [file] [log] [blame]
Adam Lesinskiffa16862014-01-23 18:17:42 -08001#include "aidl_language.h"
Jiyong Park1deecc32018-07-17 01:14:41 +09002#include "aidl_typenames.h"
Christopher Wileyf690be52015-09-14 15:19:10 -07003
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>
Jiyong Park68bc77a2018-07-19 19:00:45 +09007#include <algorithm>
Jiyong Park1deecc32018-07-17 01:14:41 +09008#include <cassert>
9#include <iostream>
Jiyong Park68bc77a2018-07-19 19:00:45 +090010#include <set>
11#include <sstream>
Casey Dahlindd691812015-09-09 17:59:06 -070012#include <string>
Jiyong Park1deecc32018-07-17 01:14:41 +090013#include <utility>
Christopher Wileyf690be52015-09-14 15:19:10 -070014
Roshan Pius9d7810a2016-07-28 08:57:50 -070015#include <android-base/parseint.h>
Elliott Hughes0a620672015-12-04 13:53:18 -080016#include <android-base/strings.h>
Christopher Wileyd76067c2015-10-19 17:00:13 -070017
Ying Wang3000e752016-01-11 18:05:59 -080018#include "aidl_language_y.h"
Christopher Wiley4a2884b2015-10-07 11:27:45 -070019#include "logging.h"
Jiyong Park02da7422018-07-16 16:00:26 +090020#include "type_java.h"
21#include "type_namespace.h"
Adam Lesinskiffa16862014-01-23 18:17:42 -080022
Casey Dahlin07b9dde2015-09-10 19:13:49 -070023#ifdef _WIN32
24int isatty(int fd)
25{
26 return (fd == 0);
27}
28#endif
29
Christopher Wiley4a2884b2015-10-07 11:27:45 -070030using android::aidl::IoDelegate;
Christopher Wileyd76067c2015-10-19 17:00:13 -070031using android::base::Join;
Christopher Wiley8aa4d9f2015-11-16 19:10:45 -080032using android::base::Split;
Casey Dahlindd691812015-09-09 17:59:06 -070033using std::cerr;
34using std::endl;
Jiyong Park1deecc32018-07-17 01:14:41 +090035using std::pair;
Jiyong Park68bc77a2018-07-19 19:00:45 +090036using std::set;
Christopher Wiley4a2884b2015-10-07 11:27:45 -070037using std::string;
38using std::unique_ptr;
Jiyong Parkccf00f82018-07-17 01:39:23 +090039using std::vector;
Adam Lesinskiffa16862014-01-23 18:17:42 -080040
Casey Dahlindd691812015-09-09 17:59:06 -070041void yylex_init(void **);
42void yylex_destroy(void *);
43void yyset_in(FILE *f, void *);
Casey Dahline2507492015-09-14 17:11:20 -070044int yyparse(Parser*);
Christopher Wiley4a2884b2015-10-07 11:27:45 -070045YY_BUFFER_STATE yy_scan_buffer(char *, size_t, void *);
Casey Dahlin89d44842015-09-24 18:45:54 -070046void yy_delete_buffer(YY_BUFFER_STATE, void *);
Casey Dahlindd691812015-09-09 17:59:06 -070047
Casey Dahlincdbbc8c2015-10-14 15:31:04 -070048AidlToken::AidlToken(const std::string& text, const std::string& comments)
49 : text_(text),
50 comments_(comments) {}
Casey Dahlin98a544b2015-10-14 14:22:55 -070051
Jiyong Park68bc77a2018-07-19 19:00:45 +090052static const string kNullable("nullable");
53static const string kUtf8("utf8");
54static const string kUtf8InCpp("utf8InCpp");
55
56static const set<string> kAnnotationNames{kNullable, kUtf8, kUtf8InCpp};
57
58AidlAnnotation::AidlAnnotation(const string& name, string& error) : name_(name) {
59 if (kAnnotationNames.find(name_) == kAnnotationNames.end()) {
60 std::ostringstream stream;
61 stream << "'" << name_ << "' is not a recognized annotation. ";
62 stream << "It must be one of:";
63 for (const string& kv : kAnnotationNames) {
64 stream << " " << kv;
65 }
66 stream << ".";
67 error = stream.str();
68 }
69}
70
71static bool HasAnnotation(const set<unique_ptr<AidlAnnotation>>& annotations, const string& name) {
72 for (const auto& a : annotations) {
73 if (a->GetName() == name) {
74 return true;
75 }
76 }
77 return false;
78}
79
80bool AidlAnnotatable::IsNullable() const {
81 return HasAnnotation(annotations_, kNullable);
82}
83
84bool AidlAnnotatable::IsUtf8() const {
85 return HasAnnotation(annotations_, kUtf8);
86}
87
88bool AidlAnnotatable::IsUtf8InCpp() const {
89 return HasAnnotation(annotations_, kUtf8InCpp);
90}
91
92string AidlAnnotatable::ToString() const {
93 vector<string> ret;
94 for (const auto& a : annotations_) {
95 ret.emplace_back(a->ToString());
96 }
97 std::sort(ret.begin(), ret.end());
98 return Join(ret, " ");
99}
100
Jiyong Park1deecc32018-07-17 01:14:41 +0900101AidlTypeSpecifier::AidlTypeSpecifier(const string& unresolved_name, bool is_array,
102 vector<unique_ptr<AidlTypeSpecifier>>* type_params,
103 unsigned line, const string& comments)
104 : unresolved_name_(unresolved_name),
Casey Dahlinf7a421c2015-10-05 17:24:28 -0700105 is_array_(is_array),
Jiyong Park1deecc32018-07-17 01:14:41 +0900106 type_params_(type_params),
107 line_(line),
Casey Dahlinf2d23f72015-10-02 16:19:19 -0700108 comments_(comments) {}
109
Jiyong Park1deecc32018-07-17 01:14:41 +0900110string AidlTypeSpecifier::ToString() const {
111 string ret = GetName();
112 if (IsGeneric()) {
113 vector<string> arg_names;
114 for (const auto& ta : GetTypeParameters()) {
Jiyong Parkccf00f82018-07-17 01:39:23 +0900115 arg_names.emplace_back(ta->ToString());
116 }
Jiyong Park1deecc32018-07-17 01:14:41 +0900117 ret += "<" + Join(arg_names, ",") + ">";
Jiyong Parkccf00f82018-07-17 01:39:23 +0900118 }
Jiyong Park1deecc32018-07-17 01:14:41 +0900119 if (IsArray()) {
120 ret += "[]";
121 }
122 return ret;
Jiyong Parkccf00f82018-07-17 01:39:23 +0900123}
124
Jiyong Park02da7422018-07-16 16:00:26 +0900125string AidlTypeSpecifier::Signature() const {
126 string ret = ToString();
127 string annotations = AidlAnnotatable::ToString();
128 if (annotations != "") {
129 ret = annotations + " " + ret;
130 }
131 return ret;
132}
133
Jiyong Park1deecc32018-07-17 01:14:41 +0900134bool AidlTypeSpecifier::Resolve(android::aidl::AidlTypenames& typenames) {
135 assert(!IsResolved());
136 pair<string, bool> result = typenames.ResolveTypename(unresolved_name_);
137 if (result.second) {
138 fully_qualified_name_ = result.first;
Jiyong Parkccf00f82018-07-17 01:39:23 +0900139 }
Jiyong Park1deecc32018-07-17 01:14:41 +0900140 return result.second;
Casey Dahlin70078e62015-09-30 17:01:30 -0700141}
142
Jiyong Parkd59a10d2018-07-18 11:12:55 +0900143AidlVariableDeclaration::AidlVariableDeclaration(AidlTypeSpecifier* type, std::string name,
144 unsigned line)
Steven Moreland9ea10e32018-07-19 15:26:09 -0700145 : AidlVariableDeclaration(type, name, line, nullptr /*default_value*/) {}
146
147AidlVariableDeclaration::AidlVariableDeclaration(AidlTypeSpecifier* type, std::string name,
148 unsigned line, AidlConstantValue* default_value)
149 : type_(type), name_(name), line_(line), default_value_(default_value) {}
150
151bool AidlVariableDeclaration::CheckValid() const {
152 if (default_value_ == nullptr) return true;
153
154 const string given_type = type_->GetName();
155 const string value_type = AidlConstantValue::ToString(default_value_->GetType());
156
157 if (given_type != value_type) {
158 cerr << "Declaration " << name_ << " is of type " << given_type << " but value is of type "
159 << value_type << " on line " << line_ << endl;
160 return false;
161 }
162 return true;
163}
Steven Moreland5557f1c2018-07-02 13:50:23 -0700164
165string AidlVariableDeclaration::ToString() const {
Steven Moreland9ea10e32018-07-19 15:26:09 -0700166 string ret = type_->ToString() + " " + name_;
167 if (default_value_ != nullptr) {
168 ret += " = " + default_value_->ToString();
169 }
170 return ret;
Steven Moreland5557f1c2018-07-02 13:50:23 -0700171}
172
Jiyong Park02da7422018-07-16 16:00:26 +0900173string AidlVariableDeclaration::Signature() const {
174 return type_->Signature() + " " + name_;
175}
176
Jiyong Parkd59a10d2018-07-18 11:12:55 +0900177AidlArgument::AidlArgument(AidlArgument::Direction direction, AidlTypeSpecifier* type,
178 std::string name, unsigned line)
Steven Moreland5557f1c2018-07-02 13:50:23 -0700179 : AidlVariableDeclaration(type, name, line),
Casey Dahlinfd6fb482015-09-30 14:48:18 -0700180 direction_(direction),
Steven Moreland5557f1c2018-07-02 13:50:23 -0700181 direction_specified_(true) {}
Casey Dahlinc378c992015-09-29 16:50:40 -0700182
Jiyong Parkd59a10d2018-07-18 11:12:55 +0900183AidlArgument::AidlArgument(AidlTypeSpecifier* type, std::string name, unsigned line)
Steven Moreland5557f1c2018-07-02 13:50:23 -0700184 : AidlVariableDeclaration(type, name, line),
Casey Dahlinfd6fb482015-09-30 14:48:18 -0700185 direction_(AidlArgument::IN_DIR),
Steven Moreland5557f1c2018-07-02 13:50:23 -0700186 direction_specified_(false) {}
Casey Dahlinc378c992015-09-29 16:50:40 -0700187
Jiyong Park02da7422018-07-16 16:00:26 +0900188string AidlArgument::GetDirectionSpecifier() const {
Casey Dahlinc378c992015-09-29 16:50:40 -0700189 string ret;
Casey Dahlinc378c992015-09-29 16:50:40 -0700190 if (direction_specified_) {
191 switch(direction_) {
192 case AidlArgument::IN_DIR:
193 ret += "in ";
194 break;
195 case AidlArgument::OUT_DIR:
196 ret += "out ";
197 break;
198 case AidlArgument::INOUT_DIR:
199 ret += "inout ";
200 break;
201 }
202 }
Casey Dahlinc378c992015-09-29 16:50:40 -0700203 return ret;
204}
Casey Dahlinbc7a50a2015-09-28 19:20:50 -0700205
Jiyong Park02da7422018-07-16 16:00:26 +0900206string AidlArgument::ToString() const {
207 return GetDirectionSpecifier() + AidlVariableDeclaration::ToString();
208}
209
210std::string AidlArgument::Signature() const {
211 return GetDirectionSpecifier() + AidlVariableDeclaration::Signature();
212}
213
Steven Moreland693640b2018-07-19 13:46:27 -0700214string AidlConstantValue::ToString(Type type) {
215 switch (type) {
216 case Type::INTEGER:
217 return "int";
218 case Type::STRING:
219 return "String";
220 case Type::ERROR:
221 LOG(FATAL) << "aidl internal error: error type failed to halt program";
222 default:
223 LOG(FATAL) << "aidl internal error: unknown constant type: " << static_cast<int>(type);
224 return ""; // not reached
Roshan Pius3b2203d2016-07-22 16:13:20 -0700225 }
226}
Casey Dahlind40e2fe2015-11-24 14:06:52 -0800227
Steven Moreland693640b2018-07-19 13:46:27 -0700228AidlConstantValue::AidlConstantValue(Type type, const std::string& checked_value)
229 : type_(type), value_(checked_value) {}
230
231AidlConstantValue* AidlConstantValue::LiteralInt(int32_t value) {
232 return new AidlConstantValue(Type::INTEGER, std::to_string(value));
233}
234
235AidlConstantValue* AidlConstantValue::ParseHex(const std::string& value, unsigned line) {
236 uint32_t unsigned_value;
237 if (!android::base::ParseUint<uint32_t>(value.c_str(), &unsigned_value)) {
238 cerr << "Found invalid int value '" << value << "' on line " << line << endl;
239 return new AidlConstantValue(Type::ERROR, "");
240 }
241
242 return LiteralInt(unsigned_value);
243}
244
245AidlConstantValue* AidlConstantValue::ParseString(const std::string& value, unsigned line) {
246 for (size_t i = 0; i < value.length(); ++i) {
247 const char& c = value[i];
Christopher Wileyd6bdd8d2016-05-03 11:23:13 -0700248 if (c <= 0x1f || // control characters are < 0x20
249 c >= 0x7f || // DEL is 0x7f
250 c == '\\') { // Disallow backslashes for future proofing.
Steven Moreland693640b2018-07-19 13:46:27 -0700251 cerr << "Found invalid character at index " << i << " in string constant '" << value
252 << "' beginning on line " << line << endl;
253 return new AidlConstantValue(Type::ERROR, "");
Christopher Wileyd6bdd8d2016-05-03 11:23:13 -0700254 }
255 }
Steven Moreland693640b2018-07-19 13:46:27 -0700256
257 return new AidlConstantValue(Type::STRING, value);
258}
259
260string AidlConstantValue::ToString() const {
261 CHECK(type_ != Type::ERROR) << "aidl internal error: error should be checked " << value_;
262 return value_;
263}
264
265AidlConstantDeclaration::AidlConstantDeclaration(AidlTypeSpecifier* type, std::string name,
266 AidlConstantValue* value, unsigned line)
267 : type_(type), name_(name), value_(value), line_(line) {}
268
269bool AidlConstantDeclaration::CheckValid() const {
270 // Error message logged above
271 if (value_->GetType() == AidlConstantValue::Type::ERROR) return false;
272
273 if (type_->ToString() != AidlConstantValue::ToString(value_->GetType())) {
274 cerr << "Constant " << name_ << " is of type " << type_->ToString() << " but value is of type "
275 << AidlConstantValue::ToString(value_->GetType()) << " on line " << line_ << endl;
276 return false;
277 }
278
279 return true;
Christopher Wileyd6bdd8d2016-05-03 11:23:13 -0700280}
281
Jiyong Parkd59a10d2018-07-18 11:12:55 +0900282AidlMethod::AidlMethod(bool oneway, AidlTypeSpecifier* type, std::string name,
283 std::vector<std::unique_ptr<AidlArgument>>* args, unsigned line,
284 const std::string& comments, int id)
Casey Dahlinf4a93112015-10-05 16:58:09 -0700285 : oneway_(oneway),
286 comments_(comments),
287 type_(type),
288 name_(name),
289 line_(line),
290 arguments_(std::move(*args)),
291 id_(id) {
292 has_id_ = true;
293 delete args;
Christopher Wileyad339272015-10-05 19:11:58 -0700294 for (const unique_ptr<AidlArgument>& a : arguments_) {
295 if (a->IsIn()) { in_arguments_.push_back(a.get()); }
296 if (a->IsOut()) { out_arguments_.push_back(a.get()); }
297 }
Casey Dahlinf4a93112015-10-05 16:58:09 -0700298}
299
Jiyong Parkd59a10d2018-07-18 11:12:55 +0900300AidlMethod::AidlMethod(bool oneway, AidlTypeSpecifier* type, std::string name,
301 std::vector<std::unique_ptr<AidlArgument>>* args, unsigned line,
302 const std::string& comments)
Casey Dahlinf4a93112015-10-05 16:58:09 -0700303 : AidlMethod(oneway, type, name, args, line, comments, 0) {
304 has_id_ = false;
305}
Casey Dahlinf2d23f72015-10-02 16:19:19 -0700306
Jiyong Park02da7422018-07-16 16:00:26 +0900307string AidlMethod::Signature() const {
308 vector<string> arg_signatures;
309 for (const auto& arg : GetArguments()) {
310 arg_signatures.emplace_back(arg->Signature());
311 }
312 return GetType().Signature() + " " + GetName() + "(" + Join(arg_signatures, ", ") + ")";
313}
314
Jiyong Park1deecc32018-07-17 01:14:41 +0900315Parser::Parser(const IoDelegate& io_delegate, android::aidl::AidlTypenames* typenames)
316 : io_delegate_(io_delegate), typenames_(typenames) {
Christopher Wiley4a2884b2015-10-07 11:27:45 -0700317 yylex_init(&scanner_);
318}
319
Jiyong Parkd59a10d2018-07-18 11:12:55 +0900320AidlDefinedType::AidlDefinedType(std::string name, unsigned line, const std::string& comments,
Steven Moreland787b0432018-07-03 09:00:58 -0700321 const std::vector<std::string>& package)
Jiyong Park1deecc32018-07-17 01:14:41 +0900322 : name_(name), line_(line), comments_(comments), package_(package) {}
Steven Moreland787b0432018-07-03 09:00:58 -0700323
324std::string AidlDefinedType::GetPackage() const {
325 return Join(package_, '.');
326}
327
328std::string AidlDefinedType::GetCanonicalName() const {
329 if (package_.empty()) {
330 return GetName();
331 }
332 return GetPackage() + "." + GetName();
333}
334
Casey Dahlin2b2879b2015-10-13 16:59:44 -0700335AidlParcelable::AidlParcelable(AidlQualifiedName* name, unsigned line,
Christopher Wiley8aa4d9f2015-11-16 19:10:45 -0800336 const std::vector<std::string>& package,
337 const std::string& cpp_header)
Steven Moreland787b0432018-07-03 09:00:58 -0700338 : AidlDefinedType(name->GetDotName(), line, "" /*comments*/, package),
339 name_(name),
Christopher Wiley8aa4d9f2015-11-16 19:10:45 -0800340 cpp_header_(cpp_header) {
341 // Strip off quotation marks if we actually have a cpp header.
342 if (cpp_header_.length() >= 2) {
343 cpp_header_ = cpp_header_.substr(1, cpp_header_.length() - 2);
344 }
Casey Dahlin59401da2015-10-09 18:16:45 -0700345}
346
Jiyong Park02da7422018-07-16 16:00:26 +0900347void AidlParcelable::Write(CodeWriter* writer) const {
348 writer->Write("parcelable %s ;\n", GetName().c_str());
349}
350
Steven Moreland5557f1c2018-07-02 13:50:23 -0700351AidlStructuredParcelable::AidlStructuredParcelable(
352 AidlQualifiedName* name, unsigned line, const std::vector<std::string>& package,
353 std::vector<std::unique_ptr<AidlVariableDeclaration>>* variables)
354 : AidlParcelable(name, line, package, "" /*cpp_header*/), variables_(std::move(*variables)) {}
355
Jiyong Park02da7422018-07-16 16:00:26 +0900356void AidlStructuredParcelable::Write(CodeWriter* writer) const {
357 writer->Write("parcelable %s {\n", GetName().c_str());
358 writer->Indent();
359 for (const auto& field : GetFields()) {
360 writer->Write("%s;\n", field->Signature().c_str());
361 }
362 writer->Dedent();
363 writer->Write("}\n");
364}
365
Casey Dahlinfb7da2e2015-10-08 17:26:09 -0700366AidlInterface::AidlInterface(const std::string& name, unsigned line,
367 const std::string& comments, bool oneway,
Casey Dahlind40e2fe2015-11-24 14:06:52 -0800368 std::vector<std::unique_ptr<AidlMember>>* members,
Christopher Wileyd76067c2015-10-19 17:00:13 -0700369 const std::vector<std::string>& package)
Steven Moreland787b0432018-07-03 09:00:58 -0700370 : AidlDefinedType(name, line, comments, package),
371 oneway_(oneway) {
Casey Dahlind40e2fe2015-11-24 14:06:52 -0800372 for (auto& member : *members) {
373 AidlMember* local = member.release();
374 AidlMethod* method = local->AsMethod();
Steven Moreland693640b2018-07-19 13:46:27 -0700375 AidlConstantDeclaration* constant = local->AsConstantDeclaration();
376
377 CHECK(method == nullptr || constant == nullptr);
Casey Dahlind40e2fe2015-11-24 14:06:52 -0800378
379 if (method) {
380 methods_.emplace_back(method);
Steven Moreland693640b2018-07-19 13:46:27 -0700381 } else if (constant) {
382 constants_.emplace_back(constant);
Casey Dahlind40e2fe2015-11-24 14:06:52 -0800383 } else {
384 LOG(FATAL) << "Member is neither method nor constant!";
385 }
386 }
387
388 delete members;
Casey Dahlinfb7da2e2015-10-08 17:26:09 -0700389}
390
Jiyong Park02da7422018-07-16 16:00:26 +0900391void AidlInterface::Write(CodeWriter* writer) const {
392 writer->Write("interface %s {\n", GetName().c_str());
393 writer->Indent();
394 for (const auto& method : GetMethods()) {
395 writer->Write("%s;\n", method->Signature().c_str());
396 }
397 writer->Dedent();
398 writer->Write("}\n");
399}
400
Steven Morelandc258abc2018-07-10 14:03:38 -0700401AidlDefinedType* AidlDocument::ReleaseDefinedType() {
402 if (defined_types_.size() == 0) {
Steven Moreland5557f1c2018-07-02 13:50:23 -0700403 return nullptr;
404 }
405
Steven Morelandc258abc2018-07-10 14:03:38 -0700406 if (defined_types_.size() > 1) {
407 LOG(ERROR) << "AIDL only supports compiling one defined type per file.";
408 return nullptr;
Steven Moreland5557f1c2018-07-02 13:50:23 -0700409 }
410
Steven Morelandc258abc2018-07-10 14:03:38 -0700411 return defined_types_[0].release();
Steven Moreland5557f1c2018-07-02 13:50:23 -0700412}
413
Casey Dahlin2b2879b2015-10-13 16:59:44 -0700414AidlQualifiedName::AidlQualifiedName(std::string term,
415 std::string comments)
416 : terms_({term}),
417 comments_(comments) {
Christopher Wiley8aa4d9f2015-11-16 19:10:45 -0800418 if (term.find('.') != string::npos) {
419 terms_ = Split(term, ".");
420 for (const auto& term: terms_) {
421 if (term.empty()) {
422 LOG(FATAL) << "Malformed qualified identifier: '" << term << "'";
423 }
424 }
425 }
Casey Dahlin2b2879b2015-10-13 16:59:44 -0700426}
427
Chih-Hung Hsiehf05cc262016-07-27 11:42:51 -0700428void AidlQualifiedName::AddTerm(const std::string& term) {
Casey Dahlin2b2879b2015-10-13 16:59:44 -0700429 terms_.push_back(term);
430}
431
Casey Dahlin0edf3422015-10-07 12:34:59 -0700432AidlImport::AidlImport(const std::string& from,
433 const std::string& needed_class, unsigned line)
434 : from_(from),
435 needed_class_(needed_class),
436 line_(line) {}
437
Christopher Wiley4a2884b2015-10-07 11:27:45 -0700438Parser::~Parser() {
439 if (raw_buffer_) {
440 yy_delete_buffer(buffer_, scanner_);
441 raw_buffer_.reset();
442 }
443 yylex_destroy(scanner_);
444}
445
446bool Parser::ParseFile(const string& filename) {
447 // Make sure we can read the file first, before trashing previous state.
448 unique_ptr<string> new_buffer = io_delegate_.GetFileContents(filename);
449 if (!new_buffer) {
450 LOG(ERROR) << "Error while opening file for parsing: '" << filename << "'";
451 return false;
452 }
453
454 // Throw away old parsing state if we have any.
455 if (raw_buffer_) {
456 yy_delete_buffer(buffer_, scanner_);
457 raw_buffer_.reset();
458 }
459
460 raw_buffer_ = std::move(new_buffer);
461 // We're going to scan this buffer in place, and yacc demands we put two
462 // nulls at the end.
463 raw_buffer_->append(2u, '\0');
464 filename_ = filename;
Christopher Wileyd76067c2015-10-19 17:00:13 -0700465 package_.reset();
Christopher Wiley4a2884b2015-10-07 11:27:45 -0700466 error_ = 0;
Casey Dahlinc1f39b42015-11-24 10:34:34 -0800467 document_.reset();
Christopher Wiley4a2884b2015-10-07 11:27:45 -0700468
469 buffer_ = yy_scan_buffer(&(*raw_buffer_)[0], raw_buffer_->length(), scanner_);
470
Steven Moreland2ca4fcb2018-06-27 16:01:01 -0700471 if (yy::parser(this).parse() != 0 || error_ != 0)
472 return false;
Christopher Wiley4a2884b2015-10-07 11:27:45 -0700473
Jiyong Park1deecc32018-07-17 01:14:41 +0900474 if (document_.get() == nullptr) {
475 LOG(ERROR) << "Parser succeeded but yielded no document!";
476 return false;
477 }
478 return true;
Christopher Wiley4a2884b2015-10-07 11:27:45 -0700479}
480
Christopher Wiley90be4e32015-10-20 14:55:25 -0700481std::vector<std::string> Parser::Package() const {
482 if (!package_) {
483 return {};
484 }
485 return package_->GetTerms();
486}
487
Casey Dahlin2b2879b2015-10-13 16:59:44 -0700488void Parser::AddImport(AidlQualifiedName* name, unsigned line) {
489 imports_.emplace_back(new AidlImport(this->FileName(),
490 name->GetDotName(), line));
491 delete name;
Casey Dahline2507492015-09-14 17:11:20 -0700492}
Jiyong Park1deecc32018-07-17 01:14:41 +0900493
494bool Parser::Resolve() {
495 bool success = true;
496 for (AidlTypeSpecifier* typespec : unresolved_typespecs_) {
497 if (!typespec->Resolve(*typenames_)) {
498 LOG(ERROR) << "Failed to resolve '" << typespec->GetUnresolvedName() << "' at "
499 << this->FileName() << ":" << typespec->GetLine();
500 success = false;
501 // don't stop to show more errors if any
502 }
503 }
504 return success;
505}