blob: abe1dbf10c0aab5c7b47795a593c4dd0b8e1478c [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) {
Jooyung Han397a7602021-06-05 00:06:22 +090031 auto clean_path = android::aidl::IoDelegate::CleanPath(filename);
Jooyung Hance6733e2021-05-22 02:44:17 +090032 // reuse pre-parsed document from typenames
33 for (auto& doc : typenames.AllDocuments()) {
Jooyung Han397a7602021-06-05 00:06:22 +090034 if (doc->GetLocation().GetFile() == clean_path) {
Jooyung Hance6733e2021-05-22 02:44:17 +090035 return doc.get();
36 }
37 }
Jiyong Parke5c45292020-05-26 19:06:24 +090038 // Make sure we can read the file first, before trashing previous state.
Jooyung Han397a7602021-06-05 00:06:22 +090039 unique_ptr<string> raw_buffer = io_delegate.GetFileContents(clean_path);
Jiyong Parke5c45292020-05-26 19:06:24 +090040 if (raw_buffer == nullptr) {
Jooyung Han397a7602021-06-05 00:06:22 +090041 AIDL_ERROR(clean_path) << "Error while opening file for parsing";
Jiyong Parke5c45292020-05-26 19:06:24 +090042 return nullptr;
43 }
44
45 // We're going to scan this buffer in place, and yacc demands we put two
46 // nulls at the end.
47 raw_buffer->append(2u, '\0');
48
Jooyung Han397a7602021-06-05 00:06:22 +090049 Parser parser(clean_path, *raw_buffer, typenames);
Jiyong Parke5c45292020-05-26 19:06:24 +090050
Jooyung Hance6733e2021-05-22 02:44:17 +090051 if (yy::parser(&parser).parse() != 0 || parser.HasError()) {
Jiyong Park8e79b7f2020-07-20 20:52:38 +090052 return nullptr;
53 }
Jiyong Parke5c45292020-05-26 19:06:24 +090054
Jooyung Hanc44e6e22021-05-25 11:46:18 +090055 return parser.document_;
Jiyong Parke5c45292020-05-26 19:06:24 +090056}
57
Steven Moreland6c07b832020-10-29 23:39:53 +000058void Parser::SetTypeParameters(AidlTypeSpecifier* type,
59 std::vector<std::unique_ptr<AidlTypeSpecifier>>* type_args) {
60 if (type->IsArray()) {
61 AIDL_ERROR(type) << "Must specify type parameters (<>) before array ([]).";
62 AddError();
63 }
64 if (!type->SetTypeParameters(type_args)) {
65 AIDL_ERROR(type) << "Can only specify one set of type parameters.";
66 AddError();
67 delete type_args;
68 }
69}
70
Jooyung Han289a1bc2021-05-15 15:04:05 +090071class ReferenceResolver : public AidlVisitor {
Jooyung Han29813842020-12-08 01:28:03 +090072 public:
Jooyung Hance6733e2021-05-22 02:44:17 +090073 ReferenceResolver(const AidlDefinedType* scope, TypeResolver& resolver, bool* success)
74 : scope_(scope), resolver_(resolver), success_(success) {}
Jooyung Han289a1bc2021-05-15 15:04:05 +090075
76 void Visit(const AidlTypeSpecifier& t) override {
Jooyung Hanb9d60e02021-05-21 07:54:17 +090077 // We're visiting the same node again. This can happen when two constant references
78 // point to an ancestor of this node.
79 if (t.IsResolved()) {
80 return;
81 }
82
Jooyung Han289a1bc2021-05-15 15:04:05 +090083 AidlTypeSpecifier& type = const_cast<AidlTypeSpecifier&>(t);
Jooyung Hance6733e2021-05-22 02:44:17 +090084 if (!resolver_(scope_, &type)) {
Jooyung Han289a1bc2021-05-15 15:04:05 +090085 AIDL_ERROR(type) << "Failed to resolve '" << type.GetUnresolvedName() << "'";
86 *success_ = false;
87 }
88 }
89
Jooyung Han9d3cbe22020-12-28 03:02:08 +090090 void Visit(const AidlConstantReference& v) override {
Jooyung Han29813842020-12-08 01:28:03 +090091 if (IsCircularReference(&v)) {
92 *success_ = false;
93 return;
94 }
95
Jooyung Han9d3cbe22020-12-28 03:02:08 +090096 const AidlConstantValue* resolved = v.Resolve(scope_);
Jooyung Han29813842020-12-08 01:28:03 +090097 if (!resolved) {
Jooyung Hane9f5b272021-01-07 00:18:11 +090098 AIDL_ERROR(v) << "Unknown reference '" << v.Literal() << "'";
Jooyung Han29813842020-12-08 01:28:03 +090099 *success_ = false;
100 return;
101 }
102
Jooyung Hanb9d60e02021-05-21 07:54:17 +0900103 // On error, skip recursive visiting to avoid redundant messages
104 if (!*success_) {
105 return;
106 }
Jooyung Han29813842020-12-08 01:28:03 +0900107 // resolve recursive references
108 Push(&v);
Jooyung Han289a1bc2021-05-15 15:04:05 +0900109 VisitBottomUp(*this, *resolved);
Jooyung Han29813842020-12-08 01:28:03 +0900110 Pop();
111 }
112
113 private:
114 struct StackElem {
115 const AidlDefinedType* scope;
116 const AidlConstantReference* ref;
117 };
118
119 void Push(const AidlConstantReference* ref) {
120 stack_.push_back({scope_, ref});
Jooyung Han9d3cbe22020-12-28 03:02:08 +0900121 if (ref->GetRefType()) {
122 scope_ = ref->GetRefType()->GetDefinedType();
123 }
Jooyung Han29813842020-12-08 01:28:03 +0900124 }
125
126 void Pop() {
127 scope_ = stack_.back().scope;
128 stack_.pop_back();
129 }
130
131 bool IsCircularReference(const AidlConstantReference* ref) {
132 auto it = std::find_if(stack_.begin(), stack_.end(),
133 [&](const auto& elem) { return elem.ref == ref; });
134 if (it == stack_.end()) {
135 return false;
136 }
137 std::vector<std::string> path;
138 while (it != stack_.end()) {
139 path.push_back(it->ref->Literal());
140 ++it;
141 }
142 path.push_back(ref->Literal());
143 AIDL_ERROR(ref) << "Found a circular reference: " << android::base::Join(path, " -> ");
144 return true;
145 }
146
147 const AidlDefinedType* scope_;
Jooyung Han29813842020-12-08 01:28:03 +0900148 TypeResolver& resolver_;
149 bool* success_;
150 std::vector<StackElem> stack_ = {};
151};
152
Jooyung Han289a1bc2021-05-15 15:04:05 +0900153// Resolve "unresolved" types in the "main" document.
Jooyung Hance6733e2021-05-22 02:44:17 +0900154bool ResolveReferences(const AidlDocument& document, TypeResolver& type_resolver) {
Jiyong Parke5c45292020-05-26 19:06:24 +0900155 bool success = true;
Jooyung Han690f5842020-12-04 13:02:04 +0900156
Jooyung Hance6733e2021-05-22 02:44:17 +0900157 for (const auto& type : document.DefinedTypes()) {
158 ReferenceResolver ref_resolver{type.get(), type_resolver, &success};
Jooyung Han289a1bc2021-05-15 15:04:05 +0900159 VisitBottomUp(ref_resolver, *type);
Jooyung Han690f5842020-12-04 13:02:04 +0900160 }
161
Jiyong Parke5c45292020-05-26 19:06:24 +0900162 return success;
163}
164
165Parser::Parser(const std::string& filename, std::string& raw_buffer,
166 android::aidl::AidlTypenames& typenames)
167 : filename_(filename), typenames_(typenames) {
168 yylex_init(&scanner_);
169 buffer_ = yy_scan_buffer(&raw_buffer[0], raw_buffer.length(), scanner_);
170}
171
172Parser::~Parser() {
173 yy_delete_buffer(buffer_, scanner_);
174 yylex_destroy(scanner_);
175}