blob: b5053b6b5e499ef8735e2cef5586fda18794f4a8 [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 }
Jooyung Han690f5842020-12-04 13:02:04 +090073
74 struct ConstantReferenceResolver : public AidlConstantValue::Visitor {
75 const std::string name_;
76 const AidlTypenames& typenames_;
77 bool* success_;
78 ConstantReferenceResolver(const std::string& name, const AidlTypenames& typenames,
79 bool* success)
80 : name_(name), typenames_(typenames), success_(success) {}
81 void Visit(AidlConstantValue&) override {}
82 void Visit(AidlUnaryConstExpression&) override {}
83 void Visit(AidlBinaryConstExpression&) override {}
84 void Visit(AidlConstantReference& v) override {
85 // when <type> is missing, we use
86 if (!v.GetRefType()) {
87 auto type = std::make_unique<AidlTypeSpecifier>(v.GetLocation(), name_, false, nullptr, "");
88 type->Resolve(typenames_);
89 v.SetRefType(std::move(type));
90 }
91 // check if the reference points to a valid field
92 if (!v.CheckValid()) {
93 *success_ = false;
94 }
95 }
96 };
97 // resolve "field references" as well.
98 for (const auto& type : document_->DefinedTypes()) {
99 ConstantReferenceResolver resolver{type->GetCanonicalName(), typenames_, &success};
100 if (auto enum_type = type->AsEnumDeclaration(); enum_type) {
101 for (const auto& enumerator : enum_type->GetEnumerators()) {
102 if (auto value = enumerator->GetValue(); value) {
103 value->Accept(resolver);
104 }
105 }
106 } else {
107 for (const auto& constant : type->GetConstantDeclarations()) {
108 const_cast<AidlConstantValue&>(constant->GetValue()).Accept(resolver);
109 }
110 for (const auto& field : type->GetFields()) {
111 if (field->IsDefaultUserSpecified()) {
112 const_cast<AidlConstantValue*>(field->GetDefaultValue())->Accept(resolver);
113 }
114 }
115 }
116 }
117
Jiyong Parke5c45292020-05-26 19:06:24 +0900118 return success;
119}
120
121Parser::Parser(const std::string& filename, std::string& raw_buffer,
122 android::aidl::AidlTypenames& typenames)
123 : filename_(filename), typenames_(typenames) {
124 yylex_init(&scanner_);
125 buffer_ = yy_scan_buffer(&raw_buffer[0], raw_buffer.length(), scanner_);
126}
127
128Parser::~Parser() {
129 yy_delete_buffer(buffer_, scanner_);
130 yylex_destroy(scanner_);
131}