blob: b5053b6b5e499ef8735e2cef5586fda18794f4a8 [file] [log] [blame]
/*
* Copyright (C) 2019, The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "parser.h"
#include "aidl_language_y.h"
#include "logging.h"
void yylex_init(void**);
void yylex_destroy(void*);
void yyset_in(FILE* f, void*);
int yyparse(Parser*);
YY_BUFFER_STATE yy_scan_buffer(char*, size_t, void*);
void yy_delete_buffer(YY_BUFFER_STATE, void*);
std::unique_ptr<Parser> Parser::Parse(const std::string& filename,
const android::aidl::IoDelegate& io_delegate,
AidlTypenames& typenames) {
// Make sure we can read the file first, before trashing previous state.
unique_ptr<string> raw_buffer = io_delegate.GetFileContents(filename);
if (raw_buffer == nullptr) {
AIDL_ERROR(filename) << "Error while opening file for parsing";
return nullptr;
}
// We're going to scan this buffer in place, and yacc demands we put two
// nulls at the end.
raw_buffer->append(2u, '\0');
std::unique_ptr<Parser> parser(new Parser(filename, *raw_buffer, typenames));
if (yy::parser(parser.get()).parse() != 0 || parser->HasError()) {
return nullptr;
}
return parser;
}
void Parser::SetTypeParameters(AidlTypeSpecifier* type,
std::vector<std::unique_ptr<AidlTypeSpecifier>>* type_args) {
if (type->IsArray()) {
AIDL_ERROR(type) << "Must specify type parameters (<>) before array ([]).";
AddError();
}
if (!type->SetTypeParameters(type_args)) {
AIDL_ERROR(type) << "Can only specify one set of type parameters.";
AddError();
delete type_args;
}
}
bool Parser::Resolve() {
bool success = true;
for (AidlTypeSpecifier* typespec : unresolved_typespecs_) {
if (!typespec->Resolve(typenames_)) {
AIDL_ERROR(typespec) << "Failed to resolve '" << typespec->GetUnresolvedName() << "'";
success = false;
// don't stop to show more errors if any
}
}
struct ConstantReferenceResolver : public AidlConstantValue::Visitor {
const std::string name_;
const AidlTypenames& typenames_;
bool* success_;
ConstantReferenceResolver(const std::string& name, const AidlTypenames& typenames,
bool* success)
: name_(name), typenames_(typenames), success_(success) {}
void Visit(AidlConstantValue&) override {}
void Visit(AidlUnaryConstExpression&) override {}
void Visit(AidlBinaryConstExpression&) override {}
void Visit(AidlConstantReference& v) override {
// when <type> is missing, we use
if (!v.GetRefType()) {
auto type = std::make_unique<AidlTypeSpecifier>(v.GetLocation(), name_, false, nullptr, "");
type->Resolve(typenames_);
v.SetRefType(std::move(type));
}
// check if the reference points to a valid field
if (!v.CheckValid()) {
*success_ = false;
}
}
};
// resolve "field references" as well.
for (const auto& type : document_->DefinedTypes()) {
ConstantReferenceResolver resolver{type->GetCanonicalName(), typenames_, &success};
if (auto enum_type = type->AsEnumDeclaration(); enum_type) {
for (const auto& enumerator : enum_type->GetEnumerators()) {
if (auto value = enumerator->GetValue(); value) {
value->Accept(resolver);
}
}
} else {
for (const auto& constant : type->GetConstantDeclarations()) {
const_cast<AidlConstantValue&>(constant->GetValue()).Accept(resolver);
}
for (const auto& field : type->GetFields()) {
if (field->IsDefaultUserSpecified()) {
const_cast<AidlConstantValue*>(field->GetDefaultValue())->Accept(resolver);
}
}
}
}
return success;
}
Parser::Parser(const std::string& filename, std::string& raw_buffer,
android::aidl::AidlTypenames& typenames)
: filename_(filename), typenames_(typenames) {
yylex_init(&scanner_);
buffer_ = yy_scan_buffer(&raw_buffer[0], raw_buffer.length(), scanner_);
}
Parser::~Parser() {
yy_delete_buffer(buffer_, scanner_);
yylex_destroy(scanner_);
}