blob: c7fd61ae4a50f0de3de4120af5636253bf80bc0a [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
Jooyung Hance6733e2021-05-22 02:44:17 +090028const AidlDocument* Parser::Parse(const std::string& filename,
29 const android::aidl::IoDelegate& io_delegate,
30 AidlTypenames& typenames) {
31 // reuse pre-parsed document from typenames
32 for (auto& doc : typenames.AllDocuments()) {
33 if (doc->GetLocation().GetFile() == filename) {
34 return doc.get();
35 }
36 }
Jiyong Parke5c45292020-05-26 19:06:24 +090037 // Make sure we can read the file first, before trashing previous state.
38 unique_ptr<string> raw_buffer = io_delegate.GetFileContents(filename);
39 if (raw_buffer == nullptr) {
40 AIDL_ERROR(filename) << "Error while opening file for parsing";
41 return nullptr;
42 }
43
44 // We're going to scan this buffer in place, and yacc demands we put two
45 // nulls at the end.
46 raw_buffer->append(2u, '\0');
47
Jooyung Hance6733e2021-05-22 02:44:17 +090048 Parser parser(filename, *raw_buffer, typenames);
Jiyong Parke5c45292020-05-26 19:06:24 +090049
Jooyung Hance6733e2021-05-22 02:44:17 +090050 if (yy::parser(&parser).parse() != 0 || parser.HasError()) {
Jiyong Park8e79b7f2020-07-20 20:52:38 +090051 return nullptr;
52 }
Jiyong Parke5c45292020-05-26 19:06:24 +090053
Jooyung Hance6733e2021-05-22 02:44:17 +090054 return parser.ParsedDocument();
Jiyong Parke5c45292020-05-26 19:06:24 +090055}
56
Steven Moreland6c07b832020-10-29 23:39:53 +000057void Parser::SetTypeParameters(AidlTypeSpecifier* type,
58 std::vector<std::unique_ptr<AidlTypeSpecifier>>* type_args) {
59 if (type->IsArray()) {
60 AIDL_ERROR(type) << "Must specify type parameters (<>) before array ([]).";
61 AddError();
62 }
63 if (!type->SetTypeParameters(type_args)) {
64 AIDL_ERROR(type) << "Can only specify one set of type parameters.";
65 AddError();
66 delete type_args;
67 }
68}
69
Jooyung Han289a1bc2021-05-15 15:04:05 +090070class ReferenceResolver : public AidlVisitor {
Jooyung Han29813842020-12-08 01:28:03 +090071 public:
Jooyung Hance6733e2021-05-22 02:44:17 +090072 ReferenceResolver(const AidlDefinedType* scope, TypeResolver& resolver, bool* success)
73 : scope_(scope), resolver_(resolver), success_(success) {}
Jooyung Han289a1bc2021-05-15 15:04:05 +090074
75 void Visit(const AidlTypeSpecifier& t) override {
Jooyung Hanb9d60e02021-05-21 07:54:17 +090076 // We're visiting the same node again. This can happen when two constant references
77 // point to an ancestor of this node.
78 if (t.IsResolved()) {
79 return;
80 }
81
Jooyung Han289a1bc2021-05-15 15:04:05 +090082 AidlTypeSpecifier& type = const_cast<AidlTypeSpecifier&>(t);
Jooyung Hance6733e2021-05-22 02:44:17 +090083 if (!resolver_(scope_, &type)) {
Jooyung Han289a1bc2021-05-15 15:04:05 +090084 AIDL_ERROR(type) << "Failed to resolve '" << type.GetUnresolvedName() << "'";
85 *success_ = false;
86 }
87 }
88
Jooyung Han9d3cbe22020-12-28 03:02:08 +090089 void Visit(const AidlConstantReference& v) override {
Jooyung Han29813842020-12-08 01:28:03 +090090 if (IsCircularReference(&v)) {
91 *success_ = false;
92 return;
93 }
94
Jooyung Han9d3cbe22020-12-28 03:02:08 +090095 const AidlConstantValue* resolved = v.Resolve(scope_);
Jooyung Han29813842020-12-08 01:28:03 +090096 if (!resolved) {
Jooyung Hane9f5b272021-01-07 00:18:11 +090097 AIDL_ERROR(v) << "Unknown reference '" << v.Literal() << "'";
Jooyung Han29813842020-12-08 01:28:03 +090098 *success_ = false;
99 return;
100 }
101
Jooyung Hanb9d60e02021-05-21 07:54:17 +0900102 // On error, skip recursive visiting to avoid redundant messages
103 if (!*success_) {
104 return;
105 }
Jooyung Han29813842020-12-08 01:28:03 +0900106 // resolve recursive references
107 Push(&v);
Jooyung Han289a1bc2021-05-15 15:04:05 +0900108 VisitBottomUp(*this, *resolved);
Jooyung Han29813842020-12-08 01:28:03 +0900109 Pop();
110 }
111
112 private:
113 struct StackElem {
114 const AidlDefinedType* scope;
115 const AidlConstantReference* ref;
116 };
117
118 void Push(const AidlConstantReference* ref) {
119 stack_.push_back({scope_, ref});
Jooyung Han9d3cbe22020-12-28 03:02:08 +0900120 if (ref->GetRefType()) {
121 scope_ = ref->GetRefType()->GetDefinedType();
122 }
Jooyung Han29813842020-12-08 01:28:03 +0900123 }
124
125 void Pop() {
126 scope_ = stack_.back().scope;
127 stack_.pop_back();
128 }
129
130 bool IsCircularReference(const AidlConstantReference* ref) {
131 auto it = std::find_if(stack_.begin(), stack_.end(),
132 [&](const auto& elem) { return elem.ref == ref; });
133 if (it == stack_.end()) {
134 return false;
135 }
136 std::vector<std::string> path;
137 while (it != stack_.end()) {
138 path.push_back(it->ref->Literal());
139 ++it;
140 }
141 path.push_back(ref->Literal());
142 AIDL_ERROR(ref) << "Found a circular reference: " << android::base::Join(path, " -> ");
143 return true;
144 }
145
146 const AidlDefinedType* scope_;
Jooyung Han29813842020-12-08 01:28:03 +0900147 TypeResolver& resolver_;
148 bool* success_;
149 std::vector<StackElem> stack_ = {};
150};
151
Jooyung Han289a1bc2021-05-15 15:04:05 +0900152// Resolve "unresolved" types in the "main" document.
Jooyung Hance6733e2021-05-22 02:44:17 +0900153bool ResolveReferences(const AidlDocument& document, TypeResolver& type_resolver) {
Jiyong Parke5c45292020-05-26 19:06:24 +0900154 bool success = true;
Jooyung Han690f5842020-12-04 13:02:04 +0900155
Jooyung Hance6733e2021-05-22 02:44:17 +0900156 for (const auto& type : document.DefinedTypes()) {
157 ReferenceResolver ref_resolver{type.get(), type_resolver, &success};
Jooyung Han289a1bc2021-05-15 15:04:05 +0900158 VisitBottomUp(ref_resolver, *type);
Jooyung Han690f5842020-12-04 13:02:04 +0900159 }
160
Jiyong Parke5c45292020-05-26 19:06:24 +0900161 return success;
162}
163
164Parser::Parser(const std::string& filename, std::string& raw_buffer,
165 android::aidl::AidlTypenames& typenames)
166 : filename_(filename), typenames_(typenames) {
167 yylex_init(&scanner_);
168 buffer_ = yy_scan_buffer(&raw_buffer[0], raw_buffer.length(), scanner_);
169}
170
171Parser::~Parser() {
172 yy_delete_buffer(buffer_, scanner_);
173 yylex_destroy(scanner_);
174}