blob: 44cee063c158512f38a4c95d12c461e684793e5d [file] [log] [blame]
// Copyright 2018 The Fuchsia Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "fidl/flat_ast.h"
#include <assert.h>
#include <stdio.h>
#include <algorithm>
#include <iostream>
#include <sstream>
#include <utility>
#include "fidl/attributes.h"
#include "fidl/diagnostic_types.h"
#include "fidl/diagnostics.h"
#include "fidl/experimental_flags.h"
#include "fidl/lexer.h"
#include "fidl/names.h"
#include "fidl/ordinals.h"
#include "fidl/parser.h"
#include "fidl/raw_ast.h"
#include "fidl/types.h"
#include "fidl/utils.h"
namespace fidl {
namespace flat {
using namespace diagnostics;
namespace {
constexpr uint32_t kHandleSameRights = 0x80000000; // ZX_HANDLE_SAME_RIGHTS
class ScopeInsertResult {
public:
explicit ScopeInsertResult(std::unique_ptr<SourceSpan> previous_occurrence)
: previous_occurrence_(std::move(previous_occurrence)) {}
static ScopeInsertResult Ok() { return ScopeInsertResult(nullptr); }
static ScopeInsertResult FailureAt(SourceSpan previous) {
return ScopeInsertResult(std::make_unique<SourceSpan>(previous));
}
bool ok() const { return previous_occurrence_ == nullptr; }
const SourceSpan& previous_occurrence() const {
assert(!ok());
return *previous_occurrence_;
}
private:
std::unique_ptr<SourceSpan> previous_occurrence_;
};
template <typename T>
class Scope {
public:
ScopeInsertResult Insert(const T& t, SourceSpan span) {
auto iter = scope_.find(t);
if (iter != scope_.end()) {
return ScopeInsertResult::FailureAt(iter->second);
} else {
scope_.emplace(t, span);
return ScopeInsertResult::Ok();
}
}
typename std::map<T, SourceSpan>::const_iterator begin() const { return scope_.begin(); }
typename std::map<T, SourceSpan>::const_iterator end() const { return scope_.end(); }
private:
std::map<T, SourceSpan> scope_;
};
using Ordinal64Scope = Scope<uint64_t>;
std::optional<std::pair<uint64_t, SourceSpan>> FindFirstNonDenseOrdinal(
const Ordinal64Scope& scope) {
uint64_t last_ordinal_seen = 0;
for (const auto& ordinal_and_loc : scope) {
uint64_t next_expected_ordinal = last_ordinal_seen + 1;
if (ordinal_and_loc.first != next_expected_ordinal) {
return std::optional{std::make_pair(next_expected_ordinal, ordinal_and_loc.second)};
}
last_ordinal_seen = ordinal_and_loc.first;
}
return std::nullopt;
}
struct MethodScope {
Ordinal64Scope ordinals;
Scope<std::string> canonical_names;
Scope<const Protocol*> protocols;
};
// A helper class to derive the resourceness of synthesized decls based on their
// members. If the given std::optional<types::Resourceness> is already set
// (meaning the decl is user-defined, not synthesized), this does nothing.
//
// Types added via AddType must already be compiled. In other words, there must
// not be cycles among the synthesized decls.
class DeriveResourceness {
public:
explicit DeriveResourceness(std::optional<types::Resourceness>* target)
: target_(target), derive_(!target->has_value()), result_(types::Resourceness::kValue) {}
~DeriveResourceness() {
if (derive_) {
*target_ = result_;
}
}
void AddType(const Type* type) {
if (derive_ && result_ == types::Resourceness::kValue &&
type->Resourceness() == types::Resourceness::kResource) {
result_ = types::Resourceness::kResource;
}
}
private:
std::optional<types::Resourceness>* const target_;
const bool derive_;
types::Resourceness result_;
};
// A helper class to track when a Decl is compiling and compiled.
class Compiling {
public:
explicit Compiling(Decl* decl) : decl_(decl) { decl_->compiling = true; }
~Compiling() {
decl_->compiling = false;
decl_->compiled = true;
}
private:
Decl* decl_;
};
template <typename T>
std::unique_ptr<Diagnostic> ValidateUnknownConstraints(const Decl& decl,
types::Strictness decl_strictness,
const std::vector<const T*>* members) {
if (!members)
return nullptr;
const bool is_transitional = decl.HasAttribute("Transitional");
const bool is_strict = [&] {
switch (decl_strictness) {
case types::Strictness::kStrict:
return true;
case types::Strictness::kFlexible:
return false;
}
}();
bool found_member = false;
for (const auto* member : *members) {
const bool has_unknown = member->attributes && member->attributes->HasAttribute("Unknown");
if (!has_unknown)
continue;
if (is_strict && !is_transitional) {
return Reporter::MakeError(ErrUnknownAttributeOnInvalidType, member->name);
}
if (found_member) {
return Reporter::MakeError(ErrUnknownAttributeOnMultipleMembers, member->name);
}
found_member = true;
}
return nullptr;
}
} // namespace
uint32_t PrimitiveType::SubtypeSize(types::PrimitiveSubtype subtype) {
switch (subtype) {
case types::PrimitiveSubtype::kBool:
case types::PrimitiveSubtype::kInt8:
case types::PrimitiveSubtype::kUint8:
return 1u;
case types::PrimitiveSubtype::kInt16:
case types::PrimitiveSubtype::kUint16:
return 2u;
case types::PrimitiveSubtype::kFloat32:
case types::PrimitiveSubtype::kInt32:
case types::PrimitiveSubtype::kUint32:
return 4u;
case types::PrimitiveSubtype::kFloat64:
case types::PrimitiveSubtype::kInt64:
case types::PrimitiveSubtype::kUint64:
return 8u;
}
}
bool Decl::HasAttribute(std::string_view name) const {
if (!attributes)
return false;
return attributes->HasAttribute(std::string(name));
}
std::string_view Decl::GetAttribute(std::string_view name) const {
if (!attributes)
return std::string_view();
for (const auto& attribute : attributes->attributes) {
if (attribute.name == name) {
if (attribute.value != "") {
const auto& value = attribute.value;
return std::string_view(value.data(), value.size());
}
// Don't search for another attribute with the same name.
break;
}
}
return std::string_view();
}
std::string Decl::GetName() const { return std::string(name.decl_name()); }
const std::set<std::pair<std::string, std::string_view>> allowed_simple_unions{{
{"fuchsia.io", "NodeInfo"},
}};
bool IsSimple(const Type* type, Reporter* reporter) {
auto depth = fidl::OldWireFormatDepth(type);
switch (type->kind) {
case Type::Kind::kVector: {
auto vector_type = static_cast<const VectorType*>(type);
if (*vector_type->element_count == Size::Max())
return false;
switch (vector_type->element_type->kind) {
case Type::Kind::kHandle:
case Type::Kind::kRequestHandle:
case Type::Kind::kPrimitive:
return true;
case Type::Kind::kArray:
case Type::Kind::kVector:
case Type::Kind::kString:
case Type::Kind::kIdentifier:
return false;
}
}
case Type::Kind::kString: {
auto string_type = static_cast<const StringType*>(type);
return *string_type->max_size < Size::Max();
}
case Type::Kind::kArray:
case Type::Kind::kHandle:
case Type::Kind::kRequestHandle:
case Type::Kind::kPrimitive:
return depth == 0u;
case Type::Kind::kIdentifier: {
auto identifier_type = static_cast<const IdentifierType*>(type);
if (identifier_type->type_decl->kind == Decl::Kind::kUnion) {
auto union_name = std::make_pair<const std::string&, const std::string_view&>(
LibraryName(identifier_type->name.library(), "."), identifier_type->name.decl_name());
if (allowed_simple_unions.find(union_name) == allowed_simple_unions.end()) {
// Any unions not in the allow-list are treated as non-simple.
reporter->Report(ErrUnionCannotBeSimple, identifier_type->name.span(),
identifier_type->name);
return false;
}
}
switch (identifier_type->nullability) {
case types::Nullability::kNullable:
// If the identifier is nullable, then we can handle a depth of 1
// because the secondary object is directly accessible.
return depth <= 1u;
case types::Nullability::kNonnullable:
return depth == 0u;
}
}
}
}
FieldShape Struct::Member::fieldshape(WireFormat wire_format) const {
return FieldShape(*this, wire_format);
}
FieldShape Table::Member::Used::fieldshape(WireFormat wire_format) const {
return FieldShape(*this, wire_format);
}
FieldShape Union::Member::Used::fieldshape(WireFormat wire_format) const {
return FieldShape(*this, wire_format);
}
std::vector<std::reference_wrapper<const Union::Member>> Union::MembersSortedByXUnionOrdinal()
const {
std::vector<std::reference_wrapper<const Member>> sorted_members(members.cbegin(),
members.cend());
std::sort(sorted_members.begin(), sorted_members.end(),
[](const auto& member1, const auto& member2) {
return member1.get().ordinal->value < member2.get().ordinal->value;
});
return sorted_members;
}
bool Typespace::Create(const flat::Name& name, const Type* arg_type,
const std::optional<uint32_t>& obj_type,
const std::optional<types::HandleSubtype>& handle_subtype,
const Constant* handle_rights, const Size* size,
types::Nullability nullability, const Type** out_type,
std::optional<TypeConstructor::FromTypeAlias>* out_from_type_alias) {
std::unique_ptr<Type> type;
if (!CreateNotOwned(name, arg_type, obj_type, handle_subtype, handle_rights, size, nullability,
&type, out_from_type_alias))
return false;
types_.push_back(std::move(type));
*out_type = types_.back().get();
return true;
}
bool Typespace::CreateNotOwned(const flat::Name& name, const Type* arg_type,
const std::optional<uint32_t>& obj_type,
const std::optional<types::HandleSubtype>& handle_subtype,
const Constant* handle_rights, const Size* size,
types::Nullability nullability, std::unique_ptr<Type>* out_type,
std::optional<TypeConstructor::FromTypeAlias>* out_from_type_alias) {
// TODO(pascallouis): lookup whether we've already created the type, and
// return it rather than create a new one. Lookup must be by name,
// arg_type, size, and nullability.
auto type_template = LookupTemplate(name);
if (type_template == nullptr) {
reporter_->Report(ErrUnknownType, name.span(), name);
return false;
}
return type_template->Create({.span = name.span(),
.arg_type = arg_type,
.obj_type = obj_type,
.handle_subtype = handle_subtype,
.handle_rights = handle_rights,
.size = size,
.nullability = nullability},
out_type, out_from_type_alias);
}
void Typespace::AddTemplate(std::unique_ptr<TypeTemplate> type_template) {
templates_.emplace(type_template->name(), std::move(type_template));
}
const TypeTemplate* Typespace::LookupTemplate(const flat::Name& name) const {
auto global_name = Name::Key(nullptr, name.decl_name());
if (auto iter = templates_.find(global_name); iter != templates_.end()) {
return iter->second.get();
}
if (auto iter = templates_.find(name); iter != templates_.end()) {
return iter->second.get();
}
return nullptr;
}
bool TypeTemplate::Fail(const ErrorDef<const TypeTemplate*>& err,
const std::optional<SourceSpan>& span) const {
reporter_->Report(err, span, this);
return false;
}
class PrimitiveTypeTemplate : public TypeTemplate {
public:
PrimitiveTypeTemplate(Typespace* typespace, Reporter* reporter, const std::string& name,
types::PrimitiveSubtype subtype)
: TypeTemplate(Name::CreateIntrinsic(name), typespace, reporter), subtype_(subtype) {}
bool Create(const CreateInvocation& args, std::unique_ptr<Type>* out_type,
std::optional<TypeConstructor::FromTypeAlias>* out_from_type_alias) const {
assert(!args.handle_subtype);
assert(!args.handle_rights);
if (args.arg_type != nullptr)
return Fail(ErrCannotBeParameterized, args.span);
if (args.size != nullptr)
return Fail(ErrCannotHaveSize, args.span);
if (args.nullability == types::Nullability::kNullable)
return Fail(ErrCannotBeNullable, args.span);
*out_type = std::make_unique<PrimitiveType>(name_, subtype_);
return true;
}
private:
const types::PrimitiveSubtype subtype_;
};
class BytesTypeTemplate final : public TypeTemplate {
public:
BytesTypeTemplate(Typespace* typespace, Reporter* reporter)
: TypeTemplate(Name::CreateIntrinsic("vector"), typespace, reporter),
uint8_type_(kUint8Type) {}
bool Create(const CreateInvocation& args, std::unique_ptr<Type>* out_type,
std::optional<TypeConstructor::FromTypeAlias>* out_from_type_alias) const {
assert(!args.handle_subtype);
assert(!args.handle_rights);
if (args.arg_type != nullptr)
return Fail(ErrCannotBeParameterized, args.span);
const Size* size = args.size;
if (size == nullptr)
size = &max_size;
*out_type = std::make_unique<VectorType>(name_, &uint8_type_, size, args.nullability);
return true;
}
private:
// TODO(fxbug.dev/7724): Remove when canonicalizing types.
const Name kUint8TypeName = Name::CreateIntrinsic("uint8");
const PrimitiveType kUint8Type = PrimitiveType(kUint8TypeName, types::PrimitiveSubtype::kUint8);
const PrimitiveType uint8_type_;
Size max_size = Size::Max();
};
class ArrayTypeTemplate final : public TypeTemplate {
public:
ArrayTypeTemplate(Typespace* typespace, Reporter* reporter)
: TypeTemplate(Name::CreateIntrinsic("array"), typespace, reporter) {}
bool Create(const CreateInvocation& args, std::unique_ptr<Type>* out_type,
std::optional<TypeConstructor::FromTypeAlias>* out_from_type_alias) const {
assert(!args.handle_subtype);
assert(!args.handle_rights);
if (args.arg_type == nullptr)
return Fail(ErrMustBeParameterized, args.span);
if (args.size == nullptr)
return Fail(ErrMustHaveSize, args.span);
if (args.size->value == 0)
return Fail(ErrMustHaveNonZeroSize, args.span);
if (args.nullability == types::Nullability::kNullable)
return Fail(ErrCannotBeNullable, args.span);
*out_type = std::make_unique<ArrayType>(name_, args.arg_type, args.size);
return true;
}
};
class VectorTypeTemplate final : public TypeTemplate {
public:
VectorTypeTemplate(Typespace* typespace, Reporter* reporter)
: TypeTemplate(Name::CreateIntrinsic("vector"), typespace, reporter) {}
bool Create(const CreateInvocation& args, std::unique_ptr<Type>* out_type,
std::optional<TypeConstructor::FromTypeAlias>* out_from_type_alias) const {
assert(!args.handle_subtype);
assert(!args.handle_rights);
if (args.arg_type == nullptr)
return Fail(ErrMustBeParameterized, args.span);
const Size* size = args.size;
if (size == nullptr)
size = &max_size;
*out_type = std::make_unique<VectorType>(name_, args.arg_type, size, args.nullability);
return true;
}
private:
Size max_size = Size::Max();
};
class StringTypeTemplate final : public TypeTemplate {
public:
StringTypeTemplate(Typespace* typespace, Reporter* reporter)
: TypeTemplate(Name::CreateIntrinsic("string"), typespace, reporter) {}
bool Create(const CreateInvocation& args, std::unique_ptr<Type>* out_type,
std::optional<TypeConstructor::FromTypeAlias>* out_from_type_alias) const {
assert(!args.handle_subtype);
assert(!args.handle_rights);
if (args.arg_type != nullptr)
return Fail(ErrCannotBeParameterized, args.span);
const Size* size = args.size;
if (size == nullptr)
size = &max_size;
*out_type = std::make_unique<StringType>(name_, size, args.nullability);
return true;
}
private:
Size max_size = Size::Max();
};
class HandleTypeTemplate final : public TypeTemplate {
public:
HandleTypeTemplate(Typespace* typespace, Reporter* reporter)
: TypeTemplate(Name::CreateIntrinsic("handle"), typespace, reporter) {
same_rights = std::make_unique<Constant>(Constant::Kind::kSynthesized, SourceSpan());
same_rights->ResolveTo(std::make_unique<NumericConstantValue<uint32_t>>(kHandleSameRights));
}
bool Create(const CreateInvocation& args, std::unique_ptr<Type>* out_type,
std::optional<TypeConstructor::FromTypeAlias>* out_from_type_alias) const {
assert(args.arg_type == nullptr);
if (args.size != nullptr)
return Fail(ErrCannotHaveSize, args.span);
// TODO(fxbug.dev/64629): When we are ready to create handle types, we
// should have an object type (and/or subtype) determined and not require
// these hardcoded defaults.
auto obj_type = args.obj_type.value_or(static_cast<uint32_t>(types::HandleSubtype::kHandle));
auto handle_subtype = args.handle_subtype.value_or(types::HandleSubtype::kHandle);
const Constant* handle_rights = args.handle_rights;
if (handle_rights == nullptr)
handle_rights = same_rights.get();
*out_type = std::make_unique<HandleType>(name_, obj_type, handle_subtype, handle_rights,
args.nullability);
return true;
}
private:
std::unique_ptr<Constant> same_rights;
};
class RequestTypeTemplate final : public TypeTemplate {
public:
RequestTypeTemplate(Typespace* typespace, Reporter* reporter)
: TypeTemplate(Name::CreateIntrinsic("request"), typespace, reporter) {}
bool Create(const CreateInvocation& args, std::unique_ptr<Type>* out_type,
std::optional<TypeConstructor::FromTypeAlias>* out_from_type_alias) const {
assert(!args.handle_subtype);
assert(!args.handle_rights);
if (args.arg_type == nullptr)
return Fail(ErrMustBeParameterized, args.span);
if (args.arg_type->kind != Type::Kind::kIdentifier)
return Fail(ErrMustBeAProtocol, args.span);
auto protocol_type = static_cast<const IdentifierType*>(args.arg_type);
if (protocol_type->type_decl->kind != Decl::Kind::kProtocol)
return Fail(ErrMustBeAProtocol, args.span);
if (args.size != nullptr)
return Fail(ErrCannotHaveSize, args.span);
*out_type = std::make_unique<RequestHandleType>(name_, protocol_type, args.nullability);
return true;
}
private:
// TODO(pascallouis): Make Min/Max an actual value on NumericConstantValue
// class, to simply write &Size::Max() above.
Size max_size = Size::Max();
};
class TypeDeclTypeTemplate final : public TypeTemplate {
public:
TypeDeclTypeTemplate(Name name, Typespace* typespace, Reporter* reporter, Library* library,
TypeDecl* type_decl)
: TypeTemplate(std::move(name), typespace, reporter),
library_(library),
type_decl_(type_decl) {}
bool Create(const CreateInvocation& args, std::unique_ptr<Type>* out_type,
std::optional<TypeConstructor::FromTypeAlias>* out_from_type_alias) const {
assert(!args.handle_subtype);
if (!type_decl_->compiled && type_decl_->kind != Decl::Kind::kProtocol) {
if (type_decl_->compiling) {
type_decl_->recursive = true;
} else {
if (!library_->CompileDecl(type_decl_)) {
return false;
}
}
}
switch (type_decl_->kind) {
case Decl::Kind::kService:
case Decl::Kind::kProtocol:
break;
case Decl::Kind::kUnion:
// Do nothing here: nullable Unions have the same encoding
// representation as non-optional Unions (i.e. nullable Unions are
// inlined).
break;
case Decl::Kind::kBits:
case Decl::Kind::kEnum:
case Decl::Kind::kTable:
if (args.nullability == types::Nullability::kNullable)
return Fail(ErrCannotBeNullable, args.span);
break;
case Decl::Kind::kResource: {
// Currently the only resource types are new-style handles,
// and they should be resolved to concrete subtypes and
// dispatched to the handle template earlier.
assert(false);
break;
}
default:
if (args.nullability == types::Nullability::kNullable)
break;
}
*out_type = std::make_unique<IdentifierType>(name_, args.nullability, type_decl_);
return true;
}
private:
Library* library_;
TypeDecl* type_decl_;
};
class TypeAliasTypeTemplate final : public TypeTemplate {
public:
TypeAliasTypeTemplate(Name name, Typespace* typespace, Reporter* reporter, Library* library,
TypeAlias* decl)
: TypeTemplate(std::move(name), typespace, reporter), library_(library), decl_(decl) {}
bool Create(const CreateInvocation& args, std::unique_ptr<Type>* out_type,
std::optional<TypeConstructor::FromTypeAlias>* out_from_type_alias) const {
assert(!args.handle_subtype);
assert(!args.handle_rights);
if (!decl_->compiled) {
assert(!decl_->compiling && "TODO(fxbug.dev/35218): Improve support for recursive types.");
if (!library_->CompileDecl(decl_)) {
return Fail(ErrMustBeParameterized, args.span);
}
}
const Type* arg_type = nullptr;
if (decl_->partial_type_ctor->maybe_arg_type_ctor) {
if (args.arg_type) {
return Fail(ErrCannotParametrizeTwice, args.span);
}
arg_type = decl_->partial_type_ctor->maybe_arg_type_ctor->type;
} else {
arg_type = args.arg_type;
}
const Size* size = nullptr;
if (decl_->partial_type_ctor->maybe_size) {
if (args.size) {
return Fail(ErrCannotBoundTwice, args.span);
}
size = static_cast<const Size*>(&decl_->partial_type_ctor->maybe_size->Value());
} else {
size = args.size;
}
std::optional<uint32_t> obj_type;
std::optional<types::HandleSubtype> handle_subtype;
if (decl_->partial_type_ctor->handle_subtype_identifier) {
if (args.handle_subtype) {
return Fail(ErrCannotParametrizeTwice, args.span);
}
obj_type = decl_->partial_type_ctor->handle_obj_type_resolved;
handle_subtype = decl_->partial_type_ctor->handle_subtype_identifier_resolved;
} else {
obj_type = args.obj_type;
handle_subtype = args.handle_subtype;
}
types::Nullability nullability;
if (decl_->partial_type_ctor->nullability == types::Nullability::kNullable) {
if (args.nullability == types::Nullability::kNullable) {
return Fail(ErrCannotIndicateNullabilityTwice, args.span);
}
nullability = types::Nullability::kNullable;
} else {
nullability = args.nullability;
}
if (!typespace_->CreateNotOwned(decl_->partial_type_ctor->name, arg_type, obj_type,
handle_subtype, decl_->partial_type_ctor->handle_rights.get(),
size, nullability, out_type, nullptr))
return false;
if (out_from_type_alias)
*out_from_type_alias = TypeConstructor::FromTypeAlias(decl_, args.arg_type, args.size,
args.handle_subtype, args.nullability);
return true;
}
private:
Library* library_;
TypeAlias* decl_;
};
Typespace Typespace::RootTypes(Reporter* reporter) {
Typespace root_typespace(reporter);
auto add_template = [&](std::unique_ptr<TypeTemplate> type_template) {
const Name& name = type_template->name();
root_typespace.templates_.emplace(name, std::move(type_template));
};
auto add_primitive = [&](const std::string& name, types::PrimitiveSubtype subtype) {
add_template(std::make_unique<PrimitiveTypeTemplate>(&root_typespace, reporter, name, subtype));
};
add_primitive("bool", types::PrimitiveSubtype::kBool);
add_primitive("int8", types::PrimitiveSubtype::kInt8);
add_primitive("int16", types::PrimitiveSubtype::kInt16);
add_primitive("int32", types::PrimitiveSubtype::kInt32);
add_primitive("int64", types::PrimitiveSubtype::kInt64);
add_primitive("uint8", types::PrimitiveSubtype::kUint8);
add_primitive("uint16", types::PrimitiveSubtype::kUint16);
add_primitive("uint32", types::PrimitiveSubtype::kUint32);
add_primitive("uint64", types::PrimitiveSubtype::kUint64);
add_primitive("float32", types::PrimitiveSubtype::kFloat32);
add_primitive("float64", types::PrimitiveSubtype::kFloat64);
// TODO(fxbug.dev/7807): Remove when there is generalized support.
const static auto kByteName = Name::CreateIntrinsic("byte");
const static auto kBytesName = Name::CreateIntrinsic("bytes");
root_typespace.templates_.emplace(
kByteName, std::make_unique<PrimitiveTypeTemplate>(&root_typespace, reporter, "uint8",
types::PrimitiveSubtype::kUint8));
root_typespace.templates_.emplace(kBytesName,
std::make_unique<BytesTypeTemplate>(&root_typespace, reporter));
add_template(std::make_unique<ArrayTypeTemplate>(&root_typespace, reporter));
add_template(std::make_unique<VectorTypeTemplate>(&root_typespace, reporter));
add_template(std::make_unique<StringTypeTemplate>(&root_typespace, reporter));
add_template(std::make_unique<HandleTypeTemplate>(&root_typespace, reporter));
add_template(std::make_unique<RequestTypeTemplate>(&root_typespace, reporter));
return root_typespace;
}
AttributeSchema::AttributeSchema(const std::set<Placement>& allowed_placements,
const std::set<std::string> allowed_values, Constraint constraint)
: allowed_placements_(allowed_placements),
allowed_values_(allowed_values),
constraint_(std::move(constraint)) {}
AttributeSchema AttributeSchema::Deprecated() {
return AttributeSchema({Placement::kDeprecated}, {});
}
void AttributeSchema::ValidatePlacement(Reporter* reporter, const raw::Attribute& attribute,
Placement placement) const {
if (allowed_placements_.size() == 0)
return;
if (allowed_placements_.size() == 1 && *allowed_placements_.cbegin() == Placement::kDeprecated) {
reporter->Report(ErrDeprecatedAttribute, attribute.span(), attribute);
return;
}
auto iter = allowed_placements_.find(placement);
if (iter != allowed_placements_.end())
return;
reporter->Report(ErrInvalidAttributePlacement, attribute.span(), attribute);
}
void AttributeSchema::ValidateValue(Reporter* reporter, const raw::Attribute& attribute) const {
if (allowed_values_.size() == 0)
return;
auto iter = allowed_values_.find(attribute.value);
if (iter != allowed_values_.end())
return;
reporter->Report(ErrInvalidAttributeValue, attribute.span(), attribute, attribute.value,
allowed_values_);
}
void AttributeSchema::ValidateConstraint(Reporter* reporter, const raw::Attribute& attribute,
const Decl* decl) const {
auto check = reporter->Checkpoint();
auto passed = constraint_(reporter, attribute, decl);
if (passed) {
assert(check.NoNewErrors() && "cannot add errors and pass");
} else if (check.NoNewErrors()) {
// TODO(pascallouis): It would be nicer to use the span of
// the declaration, however we do not keep it around today.
reporter->Report(ErrAttributeConstraintNotSatisfied, attribute.span(), attribute,
attribute.value);
}
}
bool SimpleLayoutConstraint(Reporter* reporter, const raw::Attribute& attribute, const Decl* decl) {
assert(decl->kind == Decl::Kind::kStruct);
auto struct_decl = static_cast<const Struct*>(decl);
bool ok = true;
for (const auto& member : struct_decl->members) {
if (!IsSimple(member.type_ctor.get()->type, reporter)) {
reporter->Report(ErrMemberMustBeSimple, member.name, member.name.data());
ok = false;
}
}
return ok;
}
bool ParseBound(Reporter* reporter, const SourceSpan& span, const std::string& input,
uint32_t* out_value) {
auto result = utils::ParseNumeric(input, out_value, 10);
switch (result) {
case utils::ParseNumericResult::kOutOfBounds:
reporter->Report(ErrBoundIsTooBig, span);
return false;
case utils::ParseNumericResult::kMalformed: {
reporter->Report(ErrUnableToParseBound, span, input);
return false;
}
case utils::ParseNumericResult::kSuccess:
return true;
}
}
bool Library::VerifyInlineSize(const Struct* struct_decl) {
if (struct_decl->typeshape(WireFormat::kV1NoEe).InlineSize() >= 65536) {
return Library::Fail(ErrInlineSizeExceeds64k, *struct_decl);
}
return true;
}
bool MaxBytesConstraint(Reporter* reporter, const raw::Attribute& attribute, const Decl* decl) {
uint32_t bound;
if (!ParseBound(reporter, attribute.span(), attribute.value, &bound))
return false;
uint32_t max_bytes = std::numeric_limits<uint32_t>::max();
switch (decl->kind) {
case Decl::Kind::kStruct: {
auto struct_decl = static_cast<const Struct*>(decl);
max_bytes = struct_decl->typeshape(WireFormat::kV1NoEe).InlineSize() +
struct_decl->typeshape(WireFormat::kV1NoEe).MaxOutOfLine();
break;
}
case Decl::Kind::kTable: {
auto table_decl = static_cast<const Table*>(decl);
max_bytes = table_decl->typeshape(WireFormat::kV1NoEe).InlineSize() +
table_decl->typeshape(WireFormat::kV1NoEe).MaxOutOfLine();
break;
}
case Decl::Kind::kUnion: {
auto union_decl = static_cast<const Union*>(decl);
max_bytes = union_decl->typeshape(WireFormat::kV1NoEe).InlineSize() +
union_decl->typeshape(WireFormat::kV1NoEe).MaxOutOfLine();
break;
}
default:
assert(false && "unexpected kind");
return false;
}
if (max_bytes > bound) {
reporter->Report(ErrTooManyBytes, attribute.span(), bound, max_bytes);
return false;
}
return true;
}
bool MaxHandlesConstraint(Reporter* reporter, const raw::Attribute& attribute, const Decl* decl) {
uint32_t bound;
if (!ParseBound(reporter, attribute.span(), attribute.value, &bound))
return false;
uint32_t max_handles = std::numeric_limits<uint32_t>::max();
switch (decl->kind) {
case Decl::Kind::kStruct: {
auto struct_decl = static_cast<const Struct*>(decl);
max_handles = struct_decl->typeshape(WireFormat::kV1NoEe).MaxHandles();
break;
}
case Decl::Kind::kTable: {
auto table_decl = static_cast<const Table*>(decl);
max_handles = table_decl->typeshape(WireFormat::kV1NoEe).MaxHandles();
break;
}
case Decl::Kind::kUnion: {
auto union_decl = static_cast<const Union*>(decl);
max_handles = union_decl->typeshape(WireFormat::kV1NoEe).MaxHandles();
break;
}
default:
assert(false && "unexpected kind");
return false;
}
if (max_handles > bound) {
reporter->Report(ErrTooManyHandles, attribute.span(), bound, max_handles);
return false;
}
return true;
}
bool ResultShapeConstraint(Reporter* reporter, const raw::Attribute& attribute, const Decl* decl) {
assert(decl->kind == Decl::Kind::kUnion);
auto union_decl = static_cast<const Union*>(decl);
assert(union_decl->members.size() == 2);
auto& error_member = union_decl->members.at(1);
assert(error_member.maybe_used && "must have an error member");
auto error_type = error_member.maybe_used->type_ctor->type;
const PrimitiveType* error_primitive = nullptr;
if (error_type->kind == Type::Kind::kPrimitive) {
error_primitive = static_cast<const PrimitiveType*>(error_type);
} else if (error_type->kind == Type::Kind::kIdentifier) {
auto identifier_type = static_cast<const IdentifierType*>(error_type);
if (identifier_type->type_decl->kind == Decl::Kind::kEnum) {
auto error_enum = static_cast<const Enum*>(identifier_type->type_decl);
assert(error_enum->subtype_ctor->type->kind == Type::Kind::kPrimitive);
error_primitive = static_cast<const PrimitiveType*>(error_enum->subtype_ctor->type);
}
}
if (!error_primitive || (error_primitive->subtype != types::PrimitiveSubtype::kInt32 &&
error_primitive->subtype != types::PrimitiveSubtype::kUint32)) {
reporter->Report(ErrInvalidErrorType, decl->name.span());
return false;
}
return true;
}
static std::string Trim(std::string s) {
s.erase(s.begin(), std::find_if(s.begin(), s.end(), [](int ch) {
return !utils::IsWhitespace(static_cast<char>(ch));
}));
s.erase(std::find_if(s.rbegin(), s.rend(),
[](int ch) { return !utils::IsWhitespace(static_cast<char>(ch)); })
.base(),
s.end());
return s;
}
bool TransportConstraint(Reporter* reporter, const raw::Attribute& attribute, const Decl* decl) {
// Parse comma separated transports
const std::string& value = attribute.value;
std::string::size_type prev_pos = 0;
std::string::size_type pos;
std::vector<std::string> transports;
while ((pos = value.find(',', prev_pos)) != std::string::npos) {
transports.emplace_back(Trim(value.substr(prev_pos, pos - prev_pos)));
prev_pos = pos + 1;
}
transports.emplace_back(Trim(value.substr(prev_pos)));
// Validate that they're ok
// function-local static pointer to non-trivially-destructible type
// is allowed by styleguide
static const auto kValidTransports = new std::set<std::string>{
"Banjo",
"Channel",
"Syscall",
};
for (auto transport : transports) {
if (kValidTransports->count(transport) == 0) {
reporter->Report(ErrInvalidTransportType, decl->name.span(), transport, *kValidTransports);
return false;
}
}
return true;
}
const Resource::Property* Resource::LookupProperty(std::string_view name) {
for (const Property& property : properties) {
if (property.name.data() == name.data()) {
return &property;
}
}
return nullptr;
}
Libraries::Libraries() {
// clang-format off
AddAttributeSchema("Discoverable", AttributeSchema({
AttributeSchema::Placement::kProtocolDecl,
}, {
"",
}));
AddAttributeSchema("Doc", AttributeSchema({
/* any placement */
}, {
/* any value */
}));
AddAttributeSchema("Layout", AttributeSchema::Deprecated()),
AddAttributeSchema("ForDeprecatedCBindings", AttributeSchema({
AttributeSchema::Placement::kProtocolDecl,
AttributeSchema::Placement::kStructDecl,
}, {
"",
},
SimpleLayoutConstraint));
AddAttributeSchema("MaxBytes", AttributeSchema({
AttributeSchema::Placement::kProtocolDecl,
AttributeSchema::Placement::kMethod,
AttributeSchema::Placement::kStructDecl,
AttributeSchema::Placement::kTableDecl,
AttributeSchema::Placement::kUnionDecl,
}, {
/* any value */
},
MaxBytesConstraint));
AddAttributeSchema("MaxHandles", AttributeSchema({
AttributeSchema::Placement::kProtocolDecl,
AttributeSchema::Placement::kMethod,
AttributeSchema::Placement::kStructDecl,
AttributeSchema::Placement::kTableDecl,
AttributeSchema::Placement::kUnionDecl,
}, {
/* any value */
},
MaxHandlesConstraint));
AddAttributeSchema("Result", AttributeSchema({
AttributeSchema::Placement::kUnionDecl,
}, {
"",
},
ResultShapeConstraint));
AddAttributeSchema("Selector", AttributeSchema({
AttributeSchema::Placement::kMethod,
}, {
/* any value */
}));
AddAttributeSchema("Transitional", AttributeSchema({
AttributeSchema::Placement::kMethod,
AttributeSchema::Placement::kBitsDecl,
AttributeSchema::Placement::kEnumDecl,
AttributeSchema::Placement::kUnionDecl,
}, {
/* any value */
}));
AddAttributeSchema("Transport", AttributeSchema({
AttributeSchema::Placement::kProtocolDecl,
}, {
/* any value */
}, TransportConstraint));
AddAttributeSchema("Unknown", AttributeSchema({
AttributeSchema::Placement::kEnumMember,
AttributeSchema::Placement::kUnionMember,
}, {
""
}));
// clang-format on
}
bool Libraries::Insert(std::unique_ptr<Library> library) {
std::vector<std::string_view> library_name = library->name();
auto iter = all_libraries_.emplace(library_name, std::move(library));
return iter.second;
}
bool Libraries::Lookup(const std::vector<std::string_view>& library_name,
Library** out_library) const {
auto iter = all_libraries_.find(library_name);
if (iter == all_libraries_.end()) {
return false;
}
*out_library = iter->second.get();
return true;
}
std::set<std::vector<std::string_view>> Libraries::Unused(const Library* target_library) const {
std::set<std::vector<std::string_view>> unused;
for (auto& name_library : all_libraries_)
unused.insert(name_library.first);
unused.erase(target_library->name());
std::set<const Library*> worklist = {target_library};
while (worklist.size() != 0) {
auto it = worklist.begin();
auto next = *it;
worklist.erase(it);
for (const auto dependency : next->dependencies()) {
unused.erase(dependency->name());
worklist.insert(dependency);
}
}
return unused;
}
size_t EditDistance(const std::string& sequence1, const std::string& sequence2) {
size_t s1_length = sequence1.length();
size_t s2_length = sequence2.length();
size_t row1[s1_length + 1];
size_t row2[s1_length + 1];
size_t* last_row = row1;
size_t* this_row = row2;
for (size_t i = 0; i <= s1_length; i++)
last_row[i] = i;
for (size_t j = 0; j < s2_length; j++) {
this_row[0] = j + 1;
auto s2c = sequence2[j];
for (size_t i = 1; i <= s1_length; i++) {
auto s1c = sequence1[i - 1];
this_row[i] = std::min(std::min(last_row[i] + 1, this_row[i - 1] + 1),
last_row[i - 1] + (s1c == s2c ? 0 : 1));
}
std::swap(last_row, this_row);
}
return last_row[s1_length];
}
const AttributeSchema* Libraries::RetrieveAttributeSchema(Reporter* reporter,
const raw::Attribute& attribute) const {
const auto& attribute_name = attribute.name;
auto iter = attribute_schemas_.find(attribute_name);
if (iter != attribute_schemas_.end()) {
const auto& schema = iter->second;
return &schema;
}
// Skip typo check?
if (reporter == nullptr)
return nullptr;
// Match against all known attributes.
for (const auto& name_and_schema : attribute_schemas_) {
auto edit_distance = EditDistance(name_and_schema.first, attribute_name);
if (0 < edit_distance && edit_distance < 2) {
reporter->Report(WarnAttributeTypo, attribute.span(), attribute_name, name_and_schema.first);
return nullptr;
}
}
return nullptr;
}
bool Dependencies::Register(const SourceSpan& span, std::string_view filename, Library* dep_library,
const std::unique_ptr<raw::Identifier>& maybe_alias) {
refs_.push_back(std::make_unique<LibraryRef>(span, dep_library));
auto ref = refs_.back().get();
auto library_name = dep_library->name();
if (!InsertByName(filename, library_name, ref)) {
return false;
}
if (maybe_alias) {
std::vector<std::string_view> alias_name = {maybe_alias->span().data()};
if (!InsertByName(filename, alias_name, ref)) {
return false;
}
}
dependencies_aggregate_.insert(dep_library);
return true;
}
bool Dependencies::InsertByName(std::string_view filename,
const std::vector<std::string_view>& name, LibraryRef* ref) {
auto iter = dependencies_.find(std::string(filename));
if (iter == dependencies_.end()) {
dependencies_.emplace(filename, std::make_unique<ByName>());
}
iter = dependencies_.find(std::string(filename));
assert(iter != dependencies_.end());
auto insert = iter->second->emplace(name, ref);
return insert.second;
}
bool Dependencies::Contains(std::string_view filename, const std::vector<std::string_view>& name) {
auto iter1 = dependencies_.find(std::string(filename));
if (iter1 == dependencies_.end()) {
return false;
}
auto iter2 = iter1->second->find(name);
return iter2 != iter1->second->end();
}
bool Dependencies::Lookup(std::string_view filename, const std::vector<std::string_view>& name,
Dependencies::LookupMode mode, Library** out_library) const {
auto iter1 = dependencies_.find(std::string(filename));
if (iter1 == dependencies_.end()) {
return false;
}
auto iter2 = iter1->second->find(name);
if (iter2 == iter1->second->end()) {
return false;
}
auto ref = iter2->second;
if (mode == Dependencies::LookupMode::kUse) {
ref->used_ = true;
}
*out_library = ref->library_;
return true;
}
bool Dependencies::VerifyAllDependenciesWereUsed(const Library& for_library, Reporter* reporter) {
auto checkpoint = reporter->Checkpoint();
for (auto by_name_iter = dependencies_.begin(); by_name_iter != dependencies_.end();
by_name_iter++) {
const auto& by_name = *by_name_iter->second;
for (const auto& name_to_ref : by_name) {
const auto& ref = name_to_ref.second;
if (ref->used_)
continue;
reporter->Report(ErrUnusedImport, ref->span_, for_library.name(), ref->library_->name(),
ref->library_->name());
}
}
return checkpoint.NoNewErrors();
}
// Consuming the AST is primarily concerned with walking the tree and
// flattening the representation. The AST's declaration nodes are
// converted into the Library's foo_declaration structures. This means pulling
// a struct declaration inside a protocol out to the top level and
// so on.
std::string LibraryName(const Library* library, std::string_view separator) {
if (library != nullptr) {
return StringJoin(library->name(), separator);
}
return std::string();
}
bool Library::Fail(std::unique_ptr<Diagnostic> err) {
assert(err && "should not report nullptr error");
reporter_->Report(std::move(err));
return false;
}
template <typename... Args>
bool Library::Fail(const ErrorDef<Args...>& err, const Args&... args) {
reporter_->Report(err, args...);
return false;
}
template <typename... Args>
bool Library::Fail(const ErrorDef<Args...>& err, const std::optional<SourceSpan>& span,
const Args&... args) {
reporter_->Report(err, span, args...);
return false;
}
void Library::ValidateAttributesPlacement(AttributeSchema::Placement placement,
const raw::AttributeList* attributes) {
if (attributes == nullptr)
return;
for (const auto& attribute : attributes->attributes) {
auto schema = all_libraries_->RetrieveAttributeSchema(reporter_, attribute);
if (schema != nullptr) {
schema->ValidatePlacement(reporter_, attribute, placement);
schema->ValidateValue(reporter_, attribute);
}
}
}
void Library::ValidateAttributesConstraints(const Decl* decl,
const raw::AttributeList* attributes) {
if (attributes == nullptr)
return;
for (const auto& attribute : attributes->attributes) {
auto schema = all_libraries_->RetrieveAttributeSchema(nullptr, attribute);
if (schema != nullptr)
schema->ValidateConstraint(reporter_, attribute, decl);
}
}
bool Library::LookupDependency(std::string_view filename, const std::vector<std::string_view>& name,
Library** out_library) const {
return dependencies_.Lookup(filename, name, Dependencies::LookupMode::kSilent, out_library);
}
SourceSpan Library::GeneratedSimpleName(const std::string& name) {
return generated_source_file_.AddLine(name);
}
std::string Library::NextAnonymousName() {
// TODO(fxbug.dev/7920): Improve anonymous name generation. We want to be
// specific about how these names are generated once they appear in the
// JSON IR, and are exposed to the backends.
std::ostringstream data;
data << "SomeLongAnonymousPrefix";
data << anon_counter_++;
return data.str();
}
std::optional<Name> Library::CompileCompoundIdentifier(
const raw::CompoundIdentifier* compound_identifier) {
const auto& components = compound_identifier->components;
assert(components.size() >= 1);
SourceSpan decl_name = components.back()->span();
// First try resolving the identifier in the library.
if (components.size() == 1) {
return Name::CreateSourced(this, decl_name);
}
std::vector<std::string_view> library_name;
for (auto iter = components.begin(); iter != components.end() - 1; ++iter) {
library_name.push_back((*iter)->span().data());
}
auto filename = compound_identifier->span().source_file().filename();
Library* dep_library = nullptr;
if (dependencies_.Lookup(filename, library_name, Dependencies::LookupMode::kUse, &dep_library)) {
return Name::CreateSourced(dep_library, decl_name);
}
// If the identifier is not found in the library it might refer to a
// declaration with a member (e.g. library.EnumX.val or BitsY.val).
SourceSpan member_name = decl_name;
SourceSpan member_decl_name = components.rbegin()[1]->span();
if (components.size() == 2) {
return Name::CreateSourced(this, member_decl_name, std::string(member_name.data()));
}
std::vector<std::string_view> member_library_name(library_name);
member_library_name.pop_back();
Library* member_dep_library = nullptr;
if (dependencies_.Lookup(filename, member_library_name, Dependencies::LookupMode::kUse,
&member_dep_library)) {
return Name::CreateSourced(member_dep_library, member_decl_name,
std::string(member_name.data()));
}
Fail(ErrUnknownDependentLibrary, components[0]->span(), library_name, member_library_name);
return std::nullopt;
}
namespace {
template <typename T>
void StoreDecl(Decl* decl_ptr, std::vector<std::unique_ptr<T>>* declarations) {
std::unique_ptr<T> t_decl;
t_decl.reset(static_cast<T*>(decl_ptr));
declarations->push_back(std::move(t_decl));
}
} // namespace
bool Library::RegisterDecl(std::unique_ptr<Decl> decl) {
assert(decl);
auto decl_ptr = decl.release();
auto kind = decl_ptr->kind;
switch (kind) {
case Decl::Kind::kBits:
StoreDecl(decl_ptr, &bits_declarations_);
break;
case Decl::Kind::kConst:
StoreDecl(decl_ptr, &const_declarations_);
break;
case Decl::Kind::kEnum:
StoreDecl(decl_ptr, &enum_declarations_);
break;
case Decl::Kind::kProtocol:
StoreDecl(decl_ptr, &protocol_declarations_);
break;
case Decl::Kind::kResource:
StoreDecl(decl_ptr, &resource_declarations_);
break;
case Decl::Kind::kService:
StoreDecl(decl_ptr, &service_declarations_);
break;
case Decl::Kind::kStruct:
StoreDecl(decl_ptr, &struct_declarations_);
break;
case Decl::Kind::kTable:
StoreDecl(decl_ptr, &table_declarations_);
break;
case Decl::Kind::kTypeAlias:
StoreDecl(decl_ptr, &type_alias_declarations_);
break;
case Decl::Kind::kUnion:
StoreDecl(decl_ptr, &union_declarations_);
break;
} // switch
const Name& name = decl_ptr->name;
{
const auto it = declarations_.emplace(name, decl_ptr);
if (!it.second) {
const auto previous_name = it.first->second->name;
assert(previous_name.span() && "declarations_ has a name with no span");
return Fail(ErrNameCollision, name.span(), name, *previous_name.span());
}
}
const auto canonical_decl_name = utils::canonicalize(name.decl_name());
{
const auto it = declarations_by_canonical_name_.emplace(canonical_decl_name, decl_ptr);
if (!it.second) {
const auto previous_name = it.first->second->name;
assert(previous_name.span() && "declarations_by_canonical_name_ has a name with no span");
return Fail(ErrNameCollisionCanonical, name.span(), name, previous_name,
*previous_name.span(), canonical_decl_name);
}
}
if (name.span()) {
if (dependencies_.Contains(name.span()->source_file().filename(), {name.span()->data()})) {
return Fail(ErrDeclNameConflictsWithLibraryImport, name, name);
}
if (dependencies_.Contains(name.span()->source_file().filename(), {canonical_decl_name})) {
return Fail(ErrDeclNameConflictsWithLibraryImportCanonical, name, name, canonical_decl_name);
}
}
switch (kind) {
case Decl::Kind::kBits:
case Decl::Kind::kEnum:
case Decl::Kind::kService:
case Decl::Kind::kStruct:
case Decl::Kind::kTable:
case Decl::Kind::kUnion:
case Decl::Kind::kProtocol: {
auto type_decl = static_cast<TypeDecl*>(decl_ptr);
auto type_template =
std::make_unique<TypeDeclTypeTemplate>(name, typespace_, reporter_, this, type_decl);
typespace_->AddTemplate(std::move(type_template));
break;
}
case Decl::Kind::kTypeAlias: {
auto type_alias_decl = static_cast<TypeAlias*>(decl_ptr);
auto type_alias_template = std::make_unique<TypeAliasTypeTemplate>(
name, typespace_, reporter_, this, type_alias_decl);
typespace_->AddTemplate(std::move(type_alias_template));
break;
}
case Decl::Kind::kConst:
case Decl::Kind::kResource:
break;
} // switch
return true;
}
ConsumeStep Library::StartConsumeStep(fidl::utils::Syntax syntax) {
return ConsumeStep(this, syntax);
}
CompileStep Library::StartCompileStep() { return CompileStep(this); }
VerifyResourcenessStep Library::StartVerifyResourcenessStep() {
return VerifyResourcenessStep(this);
}
VerifyAttributesStep Library::StartVerifyAttributesStep() { return VerifyAttributesStep(this); }
bool Library::ConsumeConstant(std::unique_ptr<raw::Constant> raw_constant,
std::unique_ptr<Constant>* out_constant) {
switch (raw_constant->kind) {
case raw::Constant::Kind::kIdentifier: {
auto identifier = static_cast<raw::IdentifierConstant*>(raw_constant.get());
auto name = CompileCompoundIdentifier(identifier->identifier.get());
if (!name)
return false;
*out_constant =
std::make_unique<IdentifierConstant>(std::move(name.value()), identifier->span());
break;
}
case raw::Constant::Kind::kLiteral: {
auto literal = static_cast<raw::LiteralConstant*>(raw_constant.get());
*out_constant = std::make_unique<LiteralConstant>(std::move(literal->literal));
break;
}
case raw::Constant::Kind::kBinaryOperator: {
auto binary_operator_constant = static_cast<raw::BinaryOperatorConstant*>(raw_constant.get());
BinaryOperatorConstant::Operator op;
switch (binary_operator_constant->op) {
case raw::BinaryOperatorConstant::Operator::kOr:
op = BinaryOperatorConstant::Operator::kOr;
break;
}
std::unique_ptr<Constant> left_operand;
if (!ConsumeConstant(std::move(binary_operator_constant->left_operand), &left_operand)) {
return false;
}
std::unique_ptr<Constant> right_operand;
if (!ConsumeConstant(std::move(binary_operator_constant->right_operand), &right_operand)) {
return false;
}
*out_constant = std::make_unique<BinaryOperatorConstant>(
std::move(left_operand), std::move(right_operand), op, binary_operator_constant->span());
break;
}
}
return true;
}
bool Library::ConsumeTypeConstructorOld(std::unique_ptr<raw::TypeConstructorOld> raw_type_ctor,
std::unique_ptr<TypeConstructor>* out_type_ctor) {
auto name = CompileCompoundIdentifier(raw_type_ctor->identifier.get());
if (!name)
return false;
std::unique_ptr<TypeConstructor> maybe_arg_type_ctor;
if (raw_type_ctor->maybe_arg_type_ctor != nullptr) {
if (!ConsumeTypeConstructorOld(std::move(raw_type_ctor->maybe_arg_type_ctor),
&maybe_arg_type_ctor))
return false;
}
std::unique_ptr<Constant> maybe_size;
if (raw_type_ctor->maybe_size != nullptr) {
if (!ConsumeConstant(std::move(raw_type_ctor->maybe_size), &maybe_size))
return false;
}
std::unique_ptr<Constant> handle_rights;
if (raw_type_ctor->handle_rights != nullptr) {
if (!ConsumeConstant(std::move(raw_type_ctor->handle_rights), &handle_rights))
return false;
}
std::optional<Name> handle_subtype_identifier;
if (raw_type_ctor->handle_subtype_identifier) {
handle_subtype_identifier =
Name::CreateSourced(this, raw_type_ctor->handle_subtype_identifier->span());
}
*out_type_ctor = std::make_unique<TypeConstructor>(
std::move(name.value()), std::move(maybe_arg_type_ctor), std::move(handle_subtype_identifier),
std::move(handle_rights), std::move(maybe_size), raw_type_ctor->nullability,
fidl::utils::Syntax::kOld);
return true;
}
void Library::ConsumeUsing(std::unique_ptr<raw::Using> using_directive,
fidl::utils::Syntax syntax) {
if (using_directive->maybe_type_ctor) {
ConsumeTypeAlias(std::move(using_directive), syntax);
return;
}
if (using_directive->attributes && using_directive->attributes->attributes.size() != 0) {
Fail(ErrAttributesNotAllowedOnLibraryImport, using_directive->span(),
*(using_directive->attributes));
return;
}
std::vector<std::string_view> library_name;
for (const auto& component : using_directive->using_path->components) {
library_name.push_back(component->span().data());
}
Library* dep_library = nullptr;
if (!all_libraries_->Lookup(library_name, &dep_library)) {
Fail(ErrUnknownLibrary, using_directive->using_path->components[0]->span(), library_name);
return;
}
auto filename = using_directive->span().source_file().filename();
if (!dependencies_.Register(using_directive->span(), filename, dep_library,
using_directive->maybe_alias)) {
Fail(ErrDuplicateLibraryImport, library_name);
return;
}
// Import declarations, and type aliases of dependent library.
const auto& declarations = dep_library->declarations_;
declarations_.insert(declarations.begin(), declarations.end());
}
bool Library::ConsumeTypeAlias(std::unique_ptr<raw::AliasDeclaration> alias_declaration,
fidl::utils::Syntax syntax) {
assert(alias_declaration->alias && IsTypeConstructorDefined(alias_declaration->type_ctor));
auto alias_name = Name::CreateSourced(this, alias_declaration->alias->span());
std::unique_ptr<TypeConstructor> type_ctor_;
if (!ConsumeTypeConstructor(std::move(alias_declaration->type_ctor), alias_name, &type_ctor_))
return false;
return RegisterDecl(std::make_unique<TypeAlias>(std::move(alias_declaration->attributes),
std::move(alias_name), std::move(type_ctor_),
false /* allow_partial_type_ctor */));
}
bool Library::ConsumeTypeAlias(std::unique_ptr<raw::Using> using_directive,
fidl::utils::Syntax syntax) {
assert(using_directive->maybe_type_ctor);
assert(syntax == fidl::utils::Syntax::kOld && "new syntax shouldn't use using aliases");
auto span = using_directive->using_path->components[0]->span();
auto alias_name = Name::CreateSourced(this, span);
std::unique_ptr<TypeConstructor> partial_type_ctor_;
if (!ConsumeTypeConstructorOld(std::move(using_directive->maybe_type_ctor), &partial_type_ctor_))
return false;
return RegisterDecl(std::make_unique<TypeAlias>(
std::move(using_directive->attributes), std::move(alias_name), std::move(partial_type_ctor_),
true /* allow_partial_type_ctor */));
}
void Library::ConsumeBitsDeclaration(std::unique_ptr<raw::BitsDeclaration> bits_declaration) {
std::vector<Bits::Member> members;
for (auto& member : bits_declaration->members) {
auto span = member->identifier->span();
std::unique_ptr<Constant> value;
if (!ConsumeConstant(std::move(member->value), &value))
return;
members.emplace_back(span, std::move(value), std::move(member->attributes));
// TODO(pascallouis): right now, members are not registered. Look into
// registering them, potentially under the bits name qualifier such as
// <name_of_bits>.<name_of_member>.
}
std::unique_ptr<TypeConstructor> type_ctor;
if (bits_declaration->maybe_type_ctor) {
if (!ConsumeTypeConstructorOld(std::move(bits_declaration->maybe_type_ctor), &type_ctor))
return;
} else {
type_ctor = TypeConstructor::CreateSizeType();
}
RegisterDecl(std::make_unique<Bits>(
std::move(bits_declaration->attributes),
Name::CreateSourced(this, bits_declaration->identifier->span()), std::move(type_ctor),
std::move(members), bits_declaration->strictness));
}
void Library::ConsumeConstDeclaration(std::unique_ptr<raw::ConstDeclaration> const_declaration) {
auto attributes = std::move(const_declaration->attributes);
auto span = const_declaration->identifier->span();
auto name = Name::CreateSourced(this, span);
std::unique_ptr<TypeConstructor> type_ctor;
// TODO(fxbug.dev/73285): shouldn't need to care about context here
if (!ConsumeTypeConstructor(std::move(const_declaration->type_ctor), name, &type_ctor))
return;
std::unique_ptr<Constant> constant;
if (!ConsumeConstant(std::move(const_declaration->constant), &constant))
return;
RegisterDecl(std::make_unique<Const>(std::move(attributes), std::move(name), std::move(type_ctor),
std::move(constant)));
}
void Library::ConsumeEnumDeclaration(std::unique_ptr<raw::EnumDeclaration> enum_declaration) {
std::vector<Enum::Member> members;
for (auto& member : enum_declaration->members) {
auto span = member->identifier->span();
std::unique_ptr<Constant> value;
if (!ConsumeConstant(std::move(member->value), &value))
return;
members.emplace_back(span, std::move(value), std::move(member->attributes));
// TODO(pascallouis): right now, members are not registered. Look into
// registering them, potentially under the enum name qualifier such as
// <name_of_enum>.<name_of_member>.
}
std::unique_ptr<TypeConstructor> type_ctor;
if (enum_declaration->maybe_type_ctor) {
if (!ConsumeTypeConstructorOld(std::move(enum_declaration->maybe_type_ctor), &type_ctor))
return;
} else {
type_ctor = TypeConstructor::CreateSizeType();
}
RegisterDecl(std::make_unique<Enum>(
std::move(enum_declaration->attributes),
Name::CreateSourced(this, enum_declaration->identifier->span()), std::move(type_ctor),
std::move(members), enum_declaration->strictness));
}
bool Library::CreateMethodResult(const Name& protocol_name, SourceSpan response_span,
raw::ProtocolMethod* method, Struct* in_response,
fidl::utils::Syntax syntax, Struct** out_response) {
// Compile the error type.
std::unique_ptr<TypeConstructor> error_type_ctor;
if (!ConsumeTypeConstructorOld(std::move(method->maybe_error_ctor), &error_type_ctor))
return false;
// Make the Result union containing the response struct and the
// error type.
SourceSpan method_name_span = method->identifier->span();
// TODO(fxbug.dev/8027): Join spans of response and error constructor for `result_name`.
auto result_name = Name::CreateDerived(
this, response_span,
StringJoin({protocol_name.decl_name(), method_name_span.data(), "Result"}, "_"));
raw::SourceElement sourceElement = raw::SourceElement(fidl::Token(), fidl::Token());
Union::Member response_member{
std::make_unique<raw::Ordinal64>(sourceElement, 1), // success case explicitly has ordinal 1
IdentifierTypeForDecl(in_response, types::Nullability::kNonnullable, syntax),
GeneratedSimpleName("response"), nullptr};
Union::Member error_member{
std::make_unique<raw::Ordinal64>(sourceElement, 2), // error case explicitly has ordinal 2
std::move(error_type_ctor), GeneratedSimpleName("err"), nullptr};
std::vector<Union::Member> result_members;
result_members.push_back(std::move(response_member));
result_members.push_back(std::move(error_member));
std::vector<raw::Attribute> result_attributes;
result_attributes.emplace_back(*method, raw::Attribute::Provenance::kDefault, "Result", "");
auto result_attributelist =
std::make_unique<raw::AttributeList>(*method, std::move(result_attributes));
auto union_decl = std::make_unique<Union>(std::move(result_attributelist), std::move(result_name),
std::move(result_members), types::Strictness::kStrict,
std::nullopt /* resourceness */);
auto result_decl = union_decl.get();
if (!RegisterDecl(std::move(union_decl)))
return false;
// Make a new response struct for the method containing just the
// result union.
std::vector<Struct::Member> response_members;
response_members.push_back(
Struct::Member(IdentifierTypeForDecl(result_decl, types::Nullability::kNonnullable, syntax),
GeneratedSimpleName("result"), nullptr, nullptr));
auto struct_decl = std::make_unique<Struct>(
nullptr /* attributes */, Name::CreateDerived(this, response_span, NextAnonymousName()),
std::move(response_members), std::nullopt /* resourceness */,
true /* is_request_or_response */);
auto struct_decl_ptr = struct_decl.get();
if (!RegisterDecl(std::move(struct_decl)))
return false;
*out_response = struct_decl_ptr;
return true;
}
void Library::ConsumeProtocolDeclaration(
std::unique_ptr<raw::ProtocolDeclaration> protocol_declaration, fidl::utils::Syntax syntax) {
auto attributes = std::move(protocol_declaration->attributes);
auto name = Name::CreateSourced(this, protocol_declaration->identifier->span());
std::set<Name> composed_protocols;
for (auto& composed_protocol : protocol_declaration->composed_protocols) {
auto& protocol_name = composed_protocol->protocol_name;
auto composed_protocol_name = CompileCompoundIdentifier(protocol_name.get());
if (!composed_protocol_name)
return;
if (!composed_protocols.insert(std::move(composed_protocol_name.value())).second) {
Fail(ErrProtocolComposedMultipleTimes, composed_protocol_name->span());
return;
}
}
std::vector<Protocol::Method> methods;
for (auto& method : protocol_declaration->methods) {
auto selector =
fidl::ordinals::GetSelector(method->attributes.get(), method->identifier->span());
if (!utils::IsValidIdentifierComponent(selector) &&
!utils::IsValidFullyQualifiedMethodIdentifier(selector)) {
Fail(ErrInvalidSelectorValue, method->identifier->span());
}
auto generated_ordinal64 = std::make_unique<raw::Ordinal64>(
method_hasher_(library_name_, name.decl_name(), selector, *method->identifier));
auto attributes = std::move(method->attributes);
SourceSpan method_name = method->identifier->span();
Struct* maybe_request = nullptr;
if (method->maybe_request != nullptr) {
auto request_span = method->maybe_request->span();
auto request_name = Name::CreateDerived(this, request_span, NextAnonymousName());
if (!ConsumeParameterList(std::move(request_name), std::move(method->maybe_request), true,
syntax, &maybe_request))
return;
}
Struct* maybe_response = nullptr;
if (method->maybe_response != nullptr) {
const bool has_error = (method->maybe_error_ctor != nullptr);
SourceSpan response_span = method->maybe_response->span();
Name response_name = Name::CreateDerived(
this, response_span,
has_error ? StringJoin({name.decl_name(), method_name.data(), "Response"}, "_")
: NextAnonymousName());
if (!ConsumeParameterList(std::move(response_name), std::move(method->maybe_response),
!has_error, syntax, &maybe_response))
return;
if (has_error) {
if (!CreateMethodResult(name, response_span, method.get(), maybe_response, syntax,
&maybe_response))
return;
}
}
assert(maybe_request != nullptr || maybe_response != nullptr);
methods.emplace_back(std::move(attributes), std::move(generated_ordinal64),
std::move(method_name), std::move(maybe_request),
std::move(maybe_response));
}
RegisterDecl(std::make_unique<Protocol>(std::move(attributes), std::move(name),
std::move(composed_protocols), std::move(methods)));
}
bool Library::ConsumeResourceDeclaration(
std::unique_ptr<raw::ResourceDeclaration> resource_declaration, fidl::utils::Syntax syntax) {
auto name = Name::CreateSourced(this, resource_declaration->identifier->span());
std::vector<Resource::Property> properties;
for (auto& property : resource_declaration->properties) {
std::unique_ptr<TypeConstructor> type_ctor;
auto anonymous_resource_name = Name::CreateDerived(
this, property->span(),
std::string(name.decl_name()) + std::string(property->identifier->span().data()));
if (!ConsumeTypeConstructor(std::move(property->type_ctor), anonymous_resource_name,
&type_ctor))
return false;
auto attributes = std::move(property->attributes);
properties.emplace_back(std::move(type_ctor), property->identifier->span(),
std::move(attributes));
}
std::unique_ptr<TypeConstructor> type_ctor;
if (raw::IsTypeConstructorDefined(resource_declaration->maybe_type_ctor)) {
// TODO(fxbug.dev/73285): shouldn't need to care about the naming context here.
if (!ConsumeTypeConstructor(std::move(resource_declaration->maybe_type_ctor), name, &type_ctor))
return false;
} else {
type_ctor = TypeConstructor::CreateSizeType();
}
return RegisterDecl(std::make_unique<Resource>(std::move(resource_declaration->attributes),
std::move(name), std::move(type_ctor),
std::move(properties)));
}
std::unique_ptr<TypeConstructor> Library::IdentifierTypeForDecl(const Decl* decl,
types::Nullability nullability,
fidl::utils::Syntax syntax) {
return std::make_unique<TypeConstructor>(decl->name, nullptr /* maybe_arg_type */,
std::optional<Name>() /* handle_subtype_identifier */,
nullptr /* handle_rights */, nullptr /* maybe_size */,
nullability, syntax);
}
bool Library::ConsumeParameterList(Name name, std::unique_ptr<raw::ParameterList> parameter_list,
bool is_request_or_response, fidl::utils::Syntax syntax,
Struct** out_struct_decl) {
std::vector<Struct::Member> members;
for (auto& parameter : parameter_list->parameter_list) {
std::unique_ptr<TypeConstructor> type_ctor;
// TODO(fxbug.dev/73285): finalize layout naming
auto param_name = Name::CreateDerived(
this, parameter->span(),
std::string(name.decl_name()) +
utils::to_upper_camel_case(std::string(parameter->identifier->span().data())));
if (!ConsumeTypeConstructor(std::move(parameter->type_ctor), param_name, &type_ctor))
return false;
ValidateAttributesPlacement(AttributeSchema::Placement::kStructMember,
parameter->attributes.get());
members.emplace_back(std::move(type_ctor), parameter->identifier->span(),
nullptr /* maybe_default_value */, std::move(parameter->attributes));
}
if (!RegisterDecl(std::make_unique<Struct>(nullptr /* attributes */, std::move(name),
std::move(members), std::nullopt /* resourceness */,
is_request_or_response)))
return false;
*out_struct_decl = struct_declarations_.back().get();
return true;
}
void Library::ConsumeServiceDeclaration(std::unique_ptr<raw::ServiceDeclaration> service_decl,
fidl::utils::Syntax syntax) {
auto attributes = std::move(service_decl->attributes);
auto name = Name::CreateSourced(this, service_decl->identifier->span());
std::vector<Service::Member> members;
for (auto& member : service_decl->members) {
std::unique_ptr<TypeConstructor> type_ctor;
// TODO(fxbug.dev/73285): shouldn't need to care about naming context here.
if (!ConsumeTypeConstructor(std::move(member->type_ctor), name, &type_ctor))
return;
members.emplace_back(std::move(type_ctor), member->identifier->span(),
std::move(member->attributes));
}
RegisterDecl(
std::make_unique<Service>(std::move(attributes), std::move(name), std::move(members)));
}
void Library::ConsumeStructDeclaration(std::unique_ptr<raw::StructDeclaration> struct_declaration) {
auto attributes = std::move(struct_declaration->attributes);
auto name = Name::CreateSourced(this, struct_declaration->identifier->span());
std::vector<Struct::Member> members;
for (auto& member : struct_declaration->members) {
std::unique_ptr<TypeConstructor> type_ctor;
if (!ConsumeTypeConstructorOld(std::move(member->type_ctor), &type_ctor))
return;
std::unique_ptr<Constant> maybe_default_value;
if (member->maybe_default_value != nullptr) {
if (!ConsumeConstant(std::move(member->maybe_default_value), &maybe_default_value))
return;
}
auto attributes = std::move(member->attributes);
members.emplace_back(std::move(type_ctor), member->identifier->span(),
std::move(maybe_default_value), std::move(attributes));
}
RegisterDecl(std::make_unique<Struct>(std::move(attributes), std::move(name), std::move(members),
struct_declaration->resourceness));
}
void Library::ConsumeTableDeclaration(std::unique_ptr<raw::TableDeclaration> table_declaration) {
auto attributes = std::move(table_declaration->attributes);
auto name = Name::CreateSourced(this, table_declaration->identifier->span());
std::vector<Table::Member> members;
for (auto& member : table_declaration->members) {
auto ordinal_literal = std::move(member->ordinal);
if (member->maybe_used) {
std::unique_ptr<TypeConstructor> type_ctor;
if (!ConsumeTypeConstructorOld(std::move(member->maybe_used->type_ctor), &type_ctor))
return;
std::unique_ptr<Constant> maybe_default_value;
if (member->maybe_used->maybe_default_value) {
// TODO(fxbug.dev/7932): Support defaults on tables.
const auto default_value = member->maybe_used->maybe_default_value.get();
reporter_->Report(ErrDefaultsOnTablesNotSupported, default_value->span());
}
if (type_ctor->nullability != types::Nullability::kNonnullable) {
Fail(ErrNullableTableMember, member->span());
return;
}
auto attributes = std::move(member->maybe_used->attributes);
members.emplace_back(std::move(ordinal_literal), std::move(type_ctor),
member->maybe_used->identifier->span(), std::move(maybe_default_value),
std::move(attributes));
} else {
members.emplace_back(std::move(ordinal_literal), member->span());
}
}
RegisterDecl(std::make_unique<Table>(std::move(attributes), std::move(name), std::move(members),
table_declaration->strictness,
table_declaration->resourceness));
}
void Library::ConsumeUnionDeclaration(std::unique_ptr<raw::UnionDeclaration> union_declaration) {
auto name = Name::CreateSourced(this, union_declaration->identifier->span());
assert(!union_declaration->members.empty() && "unions must have at least one member");
auto union_name =
std::pair<std::string, std::string_view>(LibraryName(this, "."), name.decl_name());
std::vector<Union::Member> members;
for (auto& member : union_declaration->members) {
auto explicit_ordinal = std::move(member->ordinal);
if (member->maybe_used) {
std::unique_ptr<TypeConstructor> type_ctor;
if (!ConsumeTypeConstructorOld(std::move(member->maybe_used->type_ctor), &type_ctor))
return;
if (member->maybe_used->maybe_default_value) {
const auto default_value = member->maybe_used->maybe_default_value.get();
reporter_->Report(ErrDefaultsOnUnionsNotSupported, default_value->span());
}
if (type_ctor->nullability != types::Nullability::kNonnullable) {
Fail(ErrNullableUnionMember, member->span());
return;
}
members.emplace_back(std::move(explicit_ordinal), std::move(type_ctor),
member->maybe_used->identifier->span(),
std::move(member->maybe_used->attributes));
} else {
members.emplace_back(std::move(explicit_ordinal), member->span());
}
}
RegisterDecl(std::make_unique<Union>(std::move(union_declaration->attributes), std::move(name),
std::move(members), union_declaration->strictness,
union_declaration->resourceness));
}
// TODO(fxbug.dev/71536): these conversion methods may need to be refactored
// once the new flat AST lands, and such coercion is no longer needed.
template <typename T, typename M>
bool Library::ConsumeValueLayout(std::unique_ptr<raw::Layout> layout, const Name& context) {
std::vector<M> members;
size_t index = 0;
for (auto& mem : layout->members) {
auto member = static_cast<raw::ValueLayoutMember*>(mem.get());
auto span = member->identifier->span();
std::unique_ptr<Constant> value;
if (!ConsumeConstant(std::move(member->value), &value))
return false;
members.emplace_back(span, std::move(value), /*attributes=*/nullptr);
index++;
}
std::unique_ptr<TypeConstructor> subtype_ctor;
if (layout->subtype_ctor != nullptr) {
if (!ConsumeTypeConstructorNew(std::move(layout->subtype_ctor), context, &subtype_ctor))
return false;
} else {
subtype_ctor = TypeConstructor::CreateSizeType();
}
RegisterDecl(std::make_unique<T>(
/*attributes=*/nullptr, context, std::move(subtype_ctor), std::move(members),
layout->strictness.value_or(types::Strictness::kFlexible)));
return true;
}
template <typename T, typename M>
bool Library::ConsumeOrdinaledLayout(std::unique_ptr<raw::Layout> layout, const Name& context) {
std::vector<M> members;
for (auto& mem : layout->members) {
auto member = static_cast<raw::OrdinaledLayoutMember*>(mem.get());
if (member->reserved) {
members.emplace_back(std::move(member->ordinal), member->span());
continue;
}
auto name_of_anonymous_layout = Name::CreateDerived(
this, member->span(),
std::string(context.decl_name()) +
utils::to_upper_camel_case(std::string(member->identifier->span().data())));
std::unique_ptr<TypeConstructor> type_ctor;
if (!ConsumeTypeConstructorNew(std::move(member->type_ctor), name_of_anonymous_layout,
&type_ctor))
return false;
if (type_ctor->nullability != types::Nullability::kNonnullable) {
// TODO(fxbug.dev/65978): This error message could be more specific. We
// may want to just pass the specific error message as a template arg, or
// otherwise handle this sort of validation when compiling.
Fail(ErrNullableOrdinaledMember, member->span());
return false;
}
members.emplace_back(std::move(member->ordinal), std::move(type_ctor),
member->identifier->span(), /*attributes=*/nullptr);
}
RegisterDecl(std::make_unique<T>(
/*attributes=*/nullptr, context, std::move(members),
layout->strictness.value_or(types::Strictness::kFlexible), layout->resourceness));
return true;
}
bool Library::ConsumeStructLayout(std::unique_ptr<raw::Layout> layout, const Name& context) {
std::vector<Struct::Member> members;
for (auto& mem : layout->members) {
auto member = static_cast<raw::StructLayoutMember*>(mem.get());
auto name_of_anonymous_layout = Name::CreateDerived(
this, member->span(),
std::string(context.decl_name()) +
utils::to_upper_camel_case(std::string(member->identifier->span().data())));
std::unique_ptr<TypeConstructor> type_ctor;
if (!ConsumeTypeConstructorNew(std::move(member->type_ctor), name_of_anonymous_layout,
&type_ctor))
return false;
std::unique_ptr<Constant> default_value;
if (member->default_value != nullptr) {
ConsumeConstant(std::move(member->default_value), &default_value);
}
members.emplace_back(std::move(type_ctor), member->identifier->span(), std::move(default_value),
/*attributes=*/nullptr);
}
RegisterDecl(std::make_unique<Struct>(/*attributes=*/nullptr, context, std::move(members),
layout->resourceness));
return true;
}
bool Library::ConsumeLayout(std::unique_ptr<raw::Layout> layout, const Name& context) {
switch (layout->kind) {
case raw::Layout::Kind::kBits: {
return ConsumeValueLayout<Bits, Bits::Member>(std::move(layout), context);
}
case raw::Layout::Kind::kEnum: {
return ConsumeValueLayout<Enum, Enum::Member>(std::move(layout), context);
}
case raw::Layout::Kind::kStruct: {
return ConsumeStructLayout(std::move(layout), context);
}
case raw::Layout::Kind::kTable: {
return ConsumeOrdinaledLayout<Table, Table::Member>(std::move(layout), context);
}
case raw::Layout::Kind::kUnion: {
return ConsumeOrdinaledLayout<Union, Union::Member>(std::move(layout), context);
}
}
assert(false && "layouts must be of type bits, enum, struct, table, or union");
return true;
}
bool Library::IsOptionalConstraint(std::unique_ptr<TypeConstructor>& type_ctor,
const std::unique_ptr<raw::Constant>& constant) {
if (constant->span().data() == "optional") {
return true;
}
return false;
}
bool Library::ConsumeTypeConstructorNew(std::unique_ptr<raw::TypeConstructorNew> raw_type_ctor,
const Name& context,
std::unique_ptr<TypeConstructor>* out_type) {
auto params = std::move(raw_type_ctor->parameters);
auto constraints = std::move(raw_type_ctor->constraints);
size_t num_constraints = constraints == nullptr ? 0 : constraints->items.size();
if (raw_type_ctor->layout_ref->kind == raw::LayoutReference::Kind::kInline) {
assert((params == nullptr || params->items.empty()) &&
"anonymous layouts cannot have type parameters");
auto inline_ref = static_cast<raw::InlineLayoutReference*>(raw_type_ctor->layout_ref.get());
if (!ConsumeLayout(std::move(inline_ref->layout), context)) {
return false;
}
auto type_ctor = std::make_unique<TypeConstructor>(
context,
/*maybe_arg_type_ctor=*/nullptr,
/*handle_subtype_identifier=*/std::nullopt,
/*handle_rights=*/nullptr,
/*maybe_size=*/nullptr, types::Nullability::kNonnullable, fidl::utils::Syntax::kNew);
// Inline layouts may only plausibly have one constraints ("optional"), so
// check it here.
if (num_constraints > 1) {
Fail(ErrConstraintsOverflow, type_ctor->name, size_t(1), num_constraints);
}
if (num_constraints == 1) {
if (IsOptionalConstraint(type_ctor, constraints->items[0])) {
type_ctor->nullability = types::Nullability::kNullable;
} else {
Fail(ErrConstraintOptionalMisspelled, type_ctor->name,
constraints->items[0]->span().data());
return false;
}
}
if (out_type)
*out_type = std::move(type_ctor);
return true;
}
auto named_ref = static_cast<raw::NamedLayoutReference*>(raw_type_ctor->layout_ref.get());
SourceSpan span = named_ref->identifier->span();
auto last_name_component = named_ref->identifier->components.back()->span().data();
auto name = CompileCompoundIdentifier(named_ref->identifier.get());
if (!name)
return false;
// Initialize the typector with just a name. The other parameters are filled in below.
auto type_ctor = std::make_unique<TypeConstructor>(
std::move(name.value()), nullptr, std::nullopt, nullptr, nullptr,
types::Nullability::kNonnullable, fidl::utils::Syntax::kNew);
// TODO(fxbug.dev/71536): this mess will get fixed once the new flat AST lands.
// Are there type parameters?
if (params != nullptr && !params->items.empty()) {
// There are currently only 3 generic types: box<TYPE>, vector<TYPE> and array<TYPE,SIZE>.
// That means the logic here can be very simple (for now): assume the first type parameter is
// always a maybe_type_ctor, and the second (if it exists) is a maybe_size.
auto builtin = span.data();
std::unique_ptr<raw::LayoutParameter> param;
if (params->items.size() >= 1) {
// Because all of the generic types we have today only take at most one nested type, it is
// okay to pass the name context through like this. If we ever introduce generic types
// that take two or more types as parameters (ex: map<K,V>, or allowing things like
// struct{}<T>), we'll need extend the name contexts we pass down to their respective
// constructors.
param = std::move(params->items[0]);
auto name = Name::CreateDerived(this, span, context.full_name());
switch (param->kind) {
case raw::LayoutParameter::Kind::kType: {
auto type_param = static_cast<raw::TypeLayoutParameter*>(param.get());
if (!ConsumeTypeConstructorNew(std::move(type_param->type_ctor), name,
&type_ctor->maybe_arg_type_ctor))
return false;
break;
}
case raw::LayoutParameter::Kind::kAmbiguous: {
auto type_param = static_cast<raw::AmbiguousLayoutParameter*>(param.get());
auto inner_name = Name::CreateSourced(this, type_param->identifier->span());
type_ctor->maybe_arg_type_ctor = std::make_unique<TypeConstructor>(
inner_name,
/*maybe_arg_type_ctor=*/nullptr,
/*handle_subtype_identifier=*/std::nullopt,
/*handle_rights=*/nullptr,
/*maybe_size=*/nullptr, types::Nullability::kNonnullable, fidl::utils::Syntax::kNew);
break;
}
case raw::LayoutParameter::Kind::kLiteral: {
assert(false && "the first type parameter must be a type, not a value");
break;
}
}
}
if (builtin == "array" && params->items.size() >= 2) {
param = std::move(params->items[1]);
switch (param->kind) {
case raw::LayoutParameter::Kind::kAmbiguous: {
auto type_param = static_cast<raw::AmbiguousLayoutParameter*>(param.get());
type_ctor->maybe_size = std::make_unique<Constant>(Constant::Kind::kIdentifier,
type_param->identifier->span());
break;
}
case raw::LayoutParameter::Kind::kLiteral: {
auto type_param = static_cast<raw::LiteralLayoutParameter*>(param.get());
ConsumeConstant(std::move(type_param->literal), &type_ctor->maybe_size);
break;
}
default: {
assert(false && "the second type parameter must be a numeric value");
}
}
}
}
// The rest of this function deals with properly applying constraints in two
// contexts: the special "handle" case where constraints must be of the form
// [HANDLE_SUBTYPE, HANDLE_RIGHTS, OPTIONAL], and the "default" context, which
// accepts constraints of [SIZE, OPTIONAL].
// TODO(fxbug.dev/68667): handle client and server end types when they exist.
// TODO(fxbug.dev/71536): The following code makes one assumption that we may
// not want to keep: that the constraint optional cannot be overwritten. In
// other words, even if in a FIDL file "optional" is defined to have some
// meaining, we still assume the word "optional" does not inherit that
// meaning when used in constraints. For example, consider this FIDL:
//
// const optional uint8 = 3;
// alias foo = vector<bool>:optional; // <- What does this "optional" mean?
//
// While in the future we may like for optional to resolve to "3" in such
// cases, for now this override is ignored, and "optional" always means
// "optional" in the context of constraints.
if (last_name_component == "handle") {
// The parser currently only applies handle-like constraints onto types that
// specifically end in the word "handle" ("handle" or "zx.handle"), and does
// not do so to their aliases. We replicate that behavior here.
if (num_constraints > 3) {
Fail(ErrConstraintsOverflow, type_ctor->name, size_t(3), num_constraints);
return false;
}
if (num_constraints == 1) {
// The lone constraint MUST be either one of a subtype OR an "optional."
if (IsOptionalConstraint(type_ctor, constraints->items[0])) {
type_ctor->nullability = types::Nullability::kNullable;
} else {
type_ctor->handle_subtype_identifier =
Name::CreateSourced(this, constraints->items[0]->span());
}
} else if (num_constraints == 2) {
// The first constraint MUST be a subtype, the second MUST be a handle
// rights OR an "optional."
type_ctor->handle_subtype_identifier =
Name::CreateSourced(this, constraints->items[0]->span());
if (IsOptionalConstraint(type_ctor, constraints->items[1])) {
type_ctor->nullability = types::Nullability::kNullable;
} else if (!ConsumeConstant(std::move(constraints->items[1]), &type_ctor->handle_rights)) {
return false;
}
} else if (num_constraints == 3) {
// The first constraint MUST be a subtype, the second MUST be a handle
// rights, and the third MUST be an "optional."
type_ctor->handle_subtype_identifier =
Name::CreateSourced(this, constraints->items[0]->span());
if (!ConsumeConstant(std::move(constraints->items[1]), &type_ctor->handle_rights)) {
return false;
}
if (IsOptionalConstraint(type_ctor, constraints->items[2])) {
type_ctor->nullability = types::Nullability::kNullable;
} else {
Fail(ErrConstraintOptionalMisspelled, type_ctor->name,
constraints->items[2]->span().data());
return false;
}
}
} else {
// In the non-handle case, constraints are interpreted to be of the
// [SIZE, OPTIONAL] format.
if (num_constraints > 2) {
Fail(ErrConstraintsOverflow, type_ctor->name, size_t(2), num_constraints);
return false;
}
if (num_constraints == 1) {
// The lone constraint MUST be either one of a size OR an "optional."
if (IsOptionalConstraint(type_ctor, constraints->items[0])) {
type_ctor->nullability = types::Nullability::kNullable;
} else if (!ConsumeConstant(std::move(constraints->items[0]), &type_ctor->maybe_size)) {
return false;
}
} else if (num_constraints == 2) {
// The first constraint MUST be a size, the second MUST be "optional."
if (!ConsumeConstant(std::move(constraints->items[0]), &type_ctor->maybe_size)) {
return false;
}
if (IsOptionalConstraint(type_ctor, constraints->items[1])) {
type_ctor->nullability = types::Nullability::kNullable;
} else {
Fail(ErrConstraintOptionalMisspelled, type_ctor->name,
constraints->items[1]->span().data());
return false;
}
type_ctor->nullability = types::Nullability::kNullable;
}
}
if (out_type)
*out_type = std::move(type_ctor);
return true;
}
bool Library::ConsumeTypeConstructor(raw::TypeConstructor raw_type_ctor, const Name& context,
std::unique_ptr<TypeConstructor>* out_type) {
return std::visit(fidl::utils::matchers{
[&, this](std::unique_ptr<raw::TypeConstructorOld> e) -> bool {
return ConsumeTypeConstructorOld(std::move(e), out_type);
},
[&, this](std::unique_ptr<raw::TypeConstructorNew> e) -> bool {
return ConsumeTypeConstructorNew(std::move(e), context, out_type);
},
},
std::move(raw_type_ctor));
}
void Library::ConsumeTypeDecl(std::unique_ptr<raw::TypeDecl> type_decl) {
auto name = Name::CreateSourced(this, type_decl->identifier->span());
auto& layout_ref = type_decl->type_ctor->layout_ref;
if (layout_ref->kind == raw::LayoutReference::Kind::kNamed) {
auto named_ref = static_cast<raw::NamedLayoutReference*>(layout_ref.get());
Fail(ErrNewTypesNotAllowed, name, named_ref->span().data());
return;
}
ConsumeTypeConstructorNew(std::move(type_decl->type_ctor), name, nullptr);
}
bool Library::ConsumeFile(std::unique_ptr<raw::File> file) {
if (file->attributes) {
ValidateAttributesPlacement(AttributeSchema::Placement::kLibrary, file->attributes.get());
if (!attributes_) {
attributes_ = std::move(file->attributes);
} else {
AttributesBuilder attributes_builder(reporter_, std::move(attributes_->attributes));
for (auto& attribute : file->attributes->attributes) {
if (!attributes_builder.Insert(std::move(attribute)))
return false;
}
attributes_ = std::make_unique<raw::AttributeList>(
raw::SourceElement(file->attributes->start_, file->attributes->end_),
attributes_builder.Done());
}
}
// All fidl files in a library should agree on the library name.
std::vector<std::string_view> new_name;
for (const auto& part : file->library_name->components) {
new_name.push_back(part->span().data());
}
if (!library_name_.empty()) {
if (new_name != library_name_) {
return Fail(ErrFilesDisagreeOnLibraryName, file->library_name->components[0]->span());
}
} else {
library_name_ = new_name;
}
auto step = StartConsumeStep(file->syntax);
auto using_list = std::move(file->using_list);
for (auto& using_directive : using_list) {
step.ForUsing(std::move(using_directive));
}
auto alias_list = std::move(file->alias_list);
for (auto& alias_declaration : alias_list) {
step.ForAliasDeclaration(std::move(alias_declaration));
}
auto bits_declaration_list = std::move(file->bits_declaration_list);
for (auto& bits_declaration : bits_declaration_list) {
step.ForBitsDeclaration(std::move(bits_declaration));
}
auto const_declaration_list = std::move(file->const_declaration_list);
for (auto& const_declaration : const_declaration_list) {
step.ForConstDeclaration(std::move(const_declaration));
}
auto enum_declaration_list = std::move(file->enum_declaration_list);
for (auto& enum_declaration : enum_declaration_list) {
step.ForEnumDeclaration(std::move(enum_declaration));
}
auto protocol_declaration_list = std::move(file->protocol_declaration_list);
for (auto& protocol_declaration : protocol_declaration_list) {
step.ForProtocolDeclaration(std::move(protocol_declaration));
}
auto resource_declaration_list = std::move(file->resource_declaration_list);
for (auto& resource_declaration : resource_declaration_list) {
step.ForResourceDeclaration(std::move(resource_declaration));
}
auto service_declaration_list = std::move(file->service_declaration_list);
for (auto& service_declaration : service_declaration_list) {
step.ForServiceDeclaration(std::move(service_declaration));
}
auto struct_declaration_list = std::move(file->struct_declaration_list);
for (auto& struct_declaration : struct_declaration_list) {
step.ForStructDeclaration(std::move(struct_declaration));
}
auto table_declaration_list = std::move(file->table_declaration_list);
for (auto& table_declaration : table_declaration_list) {
step.ForTableDeclaration(std::move(table_declaration));
}
auto union_declaration_list = std::move(file->union_declaration_list);
for (auto& union_declaration : union_declaration_list) {
step.ForUnionDeclaration(std::move(union_declaration));
}
auto type_decls = std::move(file->type_decls);
for (auto& type_decl : type_decls) {
step.ForTypeDecl(std::move(type_decl));
}
return step.Done();
}
bool Library::ResolveOrOperatorConstant(Constant* constant, const Type* type,
const ConstantValue& left_operand,
const ConstantValue& right_operand) {
assert(left_operand.kind == right_operand.kind &&
"left and right operands of or operator must be of the same kind");
type = TypeResolve(type);
if (type == nullptr)
return false;
if (type->kind != Type::Kind::kPrimitive) {
return Fail(ErrOrOperatorOnNonPrimitiveValue);
}
std::unique_ptr<ConstantValue> left_operand_u64;
std::unique_ptr<ConstantValue> right_operand_u64;
if (!left_operand.Convert(ConstantValue::Kind::kUint64, &left_operand_u64))
return false;
if (!right_operand.Convert(ConstantValue::Kind::kUint64, &right_operand_u64))
return false;
NumericConstantValue<uint64_t> result =
*static_cast<NumericConstantValue<uint64_t>*>(left_operand_u64.get()) |
*static_cast<NumericConstantValue<uint64_t>*>(right_operand_u64.get());
std::unique_ptr<ConstantValue> converted_result;
if (!result.Convert(ConstantValuePrimitiveKind(static_cast<const PrimitiveType*>(type)->subtype),
&converted_result))
return false;
constant->ResolveTo(std::move(converted_result));
return true;
}
bool Library::ResolveConstant(Constant* constant, const Type* type) {
assert(constant != nullptr);
// Prevent re-entry.
if (constant->compiled)
return constant->IsResolved();
constant->compiled = true;
switch (constant->kind) {
case Constant::Kind::kIdentifier: {
auto identifier_constant = static_cast<IdentifierConstant*>(constant);
return ResolveIdentifierConstant(identifier_constant, type);
}
case Constant::Kind::kLiteral: {
auto literal_constant = static_cast<LiteralConstant*>(constant);
return ResolveLiteralConstant(literal_constant, type);
}
case Constant::Kind::kSynthesized: {
assert(false && "Compiler bug: synthesized constant does not have a resolved value!");
break;
}
case Constant::Kind::kBinaryOperator: {
auto binary_operator_constant = static_cast<BinaryOperatorConstant*>(constant);
if (!ResolveConstant(binary_operator_constant->left_operand.get(), type)) {
return false;
}
if (!ResolveConstant(binary_operator_constant->right_operand.get(), type)) {
return false;
}
switch (binary_operator_constant->op) {
case BinaryOperatorConstant::Operator::kOr: {
return ResolveOrOperatorConstant(constant, type,
binary_operator_constant->left_operand->Value(),
binary_operator_constant->right_operand->Value());
}
}
assert(false && "Compiler bug: unhandled binary operator");
break;
}
}
__builtin_unreachable();
}
ConstantValue::Kind Library::ConstantValuePrimitiveKind(
const types::PrimitiveSubtype primitive_subtype) {
switch (primitive_subtype) {
case types::PrimitiveSubtype::kBool:
return ConstantValue::Kind::kBool;
case types::PrimitiveSubtype::kInt8:
return ConstantValue::Kind::kInt8;
case types::PrimitiveSubtype::kInt16:
return ConstantValue::Kind::kInt16;
case types::PrimitiveSubtype::kInt32:
return ConstantValue::Kind::kInt32;
case types::PrimitiveSubtype::kInt64: