Jiyong Park | e5c4529 | 2020-05-26 19:06:24 +0900 | [diff] [blame] | 1 | /* |
| 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 Moreland | 2178081 | 2020-09-11 01:29:45 +0000 | [diff] [blame] | 18 | #include "aidl_language_y.h" |
Jiyong Park | 2a7c92b | 2020-07-22 19:12:36 +0900 | [diff] [blame] | 19 | #include "logging.h" |
Jiyong Park | e5c4529 | 2020-05-26 19:06:24 +0900 | [diff] [blame] | 20 | |
| 21 | void yylex_init(void**); |
| 22 | void yylex_destroy(void*); |
| 23 | void yyset_in(FILE* f, void*); |
| 24 | int yyparse(Parser*); |
| 25 | YY_BUFFER_STATE yy_scan_buffer(char*, size_t, void*); |
| 26 | void yy_delete_buffer(YY_BUFFER_STATE, void*); |
| 27 | |
| 28 | std::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 Park | 8e79b7f | 2020-07-20 20:52:38 +0900 | [diff] [blame] | 44 | if (yy::parser(parser.get()).parse() != 0 || parser->HasError()) { |
| 45 | return nullptr; |
| 46 | } |
Jiyong Park | e5c4529 | 2020-05-26 19:06:24 +0900 | [diff] [blame] | 47 | |
| 48 | return parser; |
| 49 | } |
| 50 | |
Steven Moreland | 6c07b83 | 2020-10-29 23:39:53 +0000 | [diff] [blame] | 51 | void 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 Park | e5c4529 | 2020-05-26 19:06:24 +0900 | [diff] [blame] | 64 | bool 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 Han | 690f584 | 2020-12-04 13:02:04 +0900 | [diff] [blame^] | 73 | |
| 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 Park | e5c4529 | 2020-05-26 19:06:24 +0900 | [diff] [blame] | 118 | return success; |
| 119 | } |
| 120 | |
| 121 | Parser::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 | |
| 128 | Parser::~Parser() { |
| 129 | yy_delete_buffer(buffer_, scanner_); |
| 130 | yylex_destroy(scanner_); |
| 131 | } |