blob: b96ba3ae4ea496b4886b9e02cd65ed75a7c95c44 [file] [log] [blame]
Jiyong Parke5c45292020-05-26 19:06:24 +09001/*
2 * Copyright (C) 2019, The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "parser.h"
Steven Moreland21780812020-09-11 01:29:45 +000018#include "aidl_language_y.h"
Jiyong Park2a7c92b2020-07-22 19:12:36 +090019#include "logging.h"
Jiyong Parke5c45292020-05-26 19:06:24 +090020
21void yylex_init(void**);
22void yylex_destroy(void*);
23void yyset_in(FILE* f, void*);
24int yyparse(Parser*);
25YY_BUFFER_STATE yy_scan_buffer(char*, size_t, void*);
26void yy_delete_buffer(YY_BUFFER_STATE, void*);
27
28std::unique_ptr<Parser> Parser::Parse(const std::string& filename,
29 const android::aidl::IoDelegate& io_delegate,
30 AidlTypenames& typenames) {
31 // Make sure we can read the file first, before trashing previous state.
32 unique_ptr<string> raw_buffer = io_delegate.GetFileContents(filename);
33 if (raw_buffer == nullptr) {
34 AIDL_ERROR(filename) << "Error while opening file for parsing";
35 return nullptr;
36 }
37
38 // We're going to scan this buffer in place, and yacc demands we put two
39 // nulls at the end.
40 raw_buffer->append(2u, '\0');
41
42 std::unique_ptr<Parser> parser(new Parser(filename, *raw_buffer, typenames));
43
Jiyong Park8e79b7f2020-07-20 20:52:38 +090044 if (yy::parser(parser.get()).parse() != 0 || parser->HasError()) {
45 return nullptr;
46 }
Jiyong Parke5c45292020-05-26 19:06:24 +090047
48 return parser;
49}
50
Steven Moreland6c07b832020-10-29 23:39:53 +000051void Parser::SetTypeParameters(AidlTypeSpecifier* type,
52 std::vector<std::unique_ptr<AidlTypeSpecifier>>* type_args) {
53 if (type->IsArray()) {
54 AIDL_ERROR(type) << "Must specify type parameters (<>) before array ([]).";
55 AddError();
56 }
57 if (!type->SetTypeParameters(type_args)) {
58 AIDL_ERROR(type) << "Can only specify one set of type parameters.";
59 AddError();
60 delete type_args;
61 }
62}
63
Jiyong Parke5c45292020-05-26 19:06:24 +090064bool Parser::Resolve() {
65 bool success = true;
66 for (AidlTypeSpecifier* typespec : unresolved_typespecs_) {
67 if (!typespec->Resolve(typenames_)) {
68 AIDL_ERROR(typespec) << "Failed to resolve '" << typespec->GetUnresolvedName() << "'";
69 success = false;
70 // don't stop to show more errors if any
71 }
72 }
73 return success;
74}
75
76Parser::Parser(const std::string& filename, std::string& raw_buffer,
77 android::aidl::AidlTypenames& typenames)
78 : filename_(filename), typenames_(typenames) {
79 yylex_init(&scanner_);
80 buffer_ = yy_scan_buffer(&raw_buffer[0], raw_buffer.length(), scanner_);
81}
82
83Parser::~Parser() {
84 yy_delete_buffer(buffer_, scanner_);
85 yylex_destroy(scanner_);
86}