blob: 15faed5631a76bb1b80416810b698083b6ef563b [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
Jooyung Han29813842020-12-08 01:28:03 +090064class ConstantReferenceResolver : public AidlConstantValue::Visitor {
65 public:
66 ConstantReferenceResolver(const AidlDefinedType* scope, const AidlTypenames& typenames,
67 TypeResolver& resolver, bool* success)
68 : scope_(scope), typenames_(typenames), resolver_(resolver), success_(success) {}
69 void Visit(AidlConstantValue&) override {}
70 void Visit(AidlUnaryConstExpression&) override {}
71 void Visit(AidlBinaryConstExpression&) override {}
72 void Visit(AidlConstantReference& v) override {
73 if (IsCircularReference(&v)) {
74 *success_ = false;
75 return;
76 }
77
78 // when <type> is missing, we use a scope type
79 if (!v.GetRefType()) {
80 v.SetRefType(std::make_unique<AidlTypeSpecifier>(v.GetLocation(), scope_->GetCanonicalName(),
81 false, nullptr, ""));
82 }
83 if (!v.GetRefType()->IsResolved()) {
84 if (!resolver_(typenames_.GetDocumentFor(scope_), v.GetRefType().get())) {
85 AIDL_ERROR(v.GetRefType()) << "Failed to resolve '" << v.GetRefType()->GetName() << "'";
86 *success_ = false;
87 return;
88 }
89 }
90 const AidlConstantValue* resolved = v.Resolve();
91 if (!resolved) {
92 AIDL_ERROR(v.GetRefType()) << "Failed to resolve '" << v.GetRefType()->GetName() << "'";
93 *success_ = false;
94 return;
95 }
96
97 // resolve recursive references
98 Push(&v);
99 const_cast<AidlConstantValue*>(resolved)->Accept(*this);
100 Pop();
101 }
102
103 private:
104 struct StackElem {
105 const AidlDefinedType* scope;
106 const AidlConstantReference* ref;
107 };
108
109 void Push(const AidlConstantReference* ref) {
110 stack_.push_back({scope_, ref});
111 scope_ = ref->GetRefType()->GetDefinedType();
112 }
113
114 void Pop() {
115 scope_ = stack_.back().scope;
116 stack_.pop_back();
117 }
118
119 bool IsCircularReference(const AidlConstantReference* ref) {
120 auto it = std::find_if(stack_.begin(), stack_.end(),
121 [&](const auto& elem) { return elem.ref == ref; });
122 if (it == stack_.end()) {
123 return false;
124 }
125 std::vector<std::string> path;
126 while (it != stack_.end()) {
127 path.push_back(it->ref->Literal());
128 ++it;
129 }
130 path.push_back(ref->Literal());
131 AIDL_ERROR(ref) << "Found a circular reference: " << android::base::Join(path, " -> ");
132 return true;
133 }
134
135 const AidlDefinedType* scope_;
136 const AidlTypenames& typenames_;
137 TypeResolver& resolver_;
138 bool* success_;
139 std::vector<StackElem> stack_ = {};
140};
141
142bool Parser::Resolve(TypeResolver& type_resolver) {
Jiyong Parke5c45292020-05-26 19:06:24 +0900143 bool success = true;
144 for (AidlTypeSpecifier* typespec : unresolved_typespecs_) {
Jooyung Han29813842020-12-08 01:28:03 +0900145 if (!type_resolver(document_, typespec)) {
Jiyong Parke5c45292020-05-26 19:06:24 +0900146 AIDL_ERROR(typespec) << "Failed to resolve '" << typespec->GetUnresolvedName() << "'";
147 success = false;
148 // don't stop to show more errors if any
149 }
150 }
Jooyung Han690f5842020-12-04 13:02:04 +0900151
Jooyung Han690f5842020-12-04 13:02:04 +0900152 // resolve "field references" as well.
153 for (const auto& type : document_->DefinedTypes()) {
Jooyung Han29813842020-12-08 01:28:03 +0900154 ConstantReferenceResolver ref_resolver{type.get(), typenames_, type_resolver, &success};
Jooyung Han690f5842020-12-04 13:02:04 +0900155 if (auto enum_type = type->AsEnumDeclaration(); enum_type) {
156 for (const auto& enumerator : enum_type->GetEnumerators()) {
157 if (auto value = enumerator->GetValue(); value) {
Jooyung Han29813842020-12-08 01:28:03 +0900158 value->Accept(ref_resolver);
Jooyung Han690f5842020-12-04 13:02:04 +0900159 }
160 }
161 } else {
162 for (const auto& constant : type->GetConstantDeclarations()) {
Jooyung Han29813842020-12-08 01:28:03 +0900163 const_cast<AidlConstantValue&>(constant->GetValue()).Accept(ref_resolver);
Jooyung Han690f5842020-12-04 13:02:04 +0900164 }
165 for (const auto& field : type->GetFields()) {
166 if (field->IsDefaultUserSpecified()) {
Jooyung Han29813842020-12-08 01:28:03 +0900167 const_cast<AidlConstantValue*>(field->GetDefaultValue())->Accept(ref_resolver);
Jooyung Han690f5842020-12-04 13:02:04 +0900168 }
169 }
170 }
171 }
172
Jiyong Parke5c45292020-05-26 19:06:24 +0900173 return success;
174}
175
176Parser::Parser(const std::string& filename, std::string& raw_buffer,
177 android::aidl::AidlTypenames& typenames)
178 : filename_(filename), typenames_(typenames) {
179 yylex_init(&scanner_);
180 buffer_ = yy_scan_buffer(&raw_buffer[0], raw_buffer.length(), scanner_);
181}
182
183Parser::~Parser() {
184 yy_delete_buffer(buffer_, scanner_);
185 yylex_destroy(scanner_);
186}